1
0
Fork 0

FieldStyler: Remove classes not added by #addClass

This code assumed that no classes would be removed that were _not_
added by #addClass.  Well, that's false.

* src/ui/styler/FieldStyler.js (removeClass): Consider both spaces and
  boundary preceding class name.

* test/ui/styler/FieldStylerTest.js: Add test case.
master
Mike Gerwitz 2017-03-17 14:11:16 -04:00
parent 6bba4322bf
commit 7567306123
2 changed files with 103 additions and 4 deletions

View File

@ -1,7 +1,7 @@
/**
* Style fields using CSS
*
* Copyright (C) 2016 LoVullo Associates, Inc.
* Copyright (C) 2016, 2017 LoVullo Associates, Inc.
*
* This file is part of liza.
*
@ -140,10 +140,11 @@ module.exports = AbstractClass( 'FieldStyler',
}
else if ( typeof element.className === 'string' )
{
// note that we use a space instead of a boundary for the character
// preceding the match due to the implementation of addClass()
// note that the implementation of #addClass adds a space,
// but we also need to cater to classes that might have been
// added outside of this system
element.className = element.className.replace(
new RegExp( ( ' ' + cls + '\\b' ), 'g' ), ''
new RegExp( ( '( +|\\b)' + cls + '\\b' ), 'g' ), ''
);
}

View File

@ -0,0 +1,98 @@
/**
* Test case for FieldStyler
*
* Copyright (C) 2017 LoVullo Associates, Inc.
*
* This file is part of the Liza Data Collection Framework
*
* Liza is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const { expect } = require( 'chai' );
const { Class } = require( 'easejs' );
const styler = require( '../../../' ).ui.styler;
const Sut = styler.FieldStyler.extend(
{
getId() {},
isApplied( field, element ) {},
applyStyle( field, element, row ) {},
revokeStyle( field, element, row ) {},
add( element, cls ) { this.addClass( element, cls ); },
remove( element, cls ) { this.removeClass( element, cls ); },
} );
describe( 'ui/styler/FieldStyler', () =>
{
[
{
label: 'adds class using #addClass on empty',
given: '',
add: 'foo',
expected: ' foo',
},
{
label: 'adds class using #addClass with existing',
given: 'foo bar',
add: 'baz',
expected: 'foo bar baz',
},
{
label: 'removes class added by #addClass',
given: 'cow',
add: 'moo',
remove: 'moo',
expected: 'cow',
},
{
label: 'removes class added by #addClass on empty',
given: '',
add: 'moo',
remove: 'moo',
expected: '',
},
{
label: 'removes classes added externally',
given: 'cow moo',
remove: 'cow',
expected: ' moo',
},
{
label: 'removes duplicate classes',
given: 'moo cow cow and',
remove: 'cow',
expected: 'moo and',
},
].forEach( ( { label, given, add, remove, expected } ) =>
{
it( label, () =>
{
const element = { className: given };
const sut = Sut();
// #addClass and #removeClass respectively
add && sut.add( element, add )
remove && sut.remove( element, remove );
expect( element.className )
.to.equal( expected );
} );
} );
} );