Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <gerwitzm@lovullo.com>2017-03-17 14:11:16 -0400
committerMike Gerwitz <gerwitzm@lovullo.com>2017-03-17 14:11:16 -0400
commit7567306123c4bc8448b4003f3fd77f3caaf42570 (patch)
tree5e437939d69ac261ccbafbc9a5da4fd3b900f52f
parent6bba4322bf9dc1a589bf1ff634c606853bde51bf (diff)
downloadliza-7567306123c4bc8448b4003f3fd77f3caaf42570.tar.gz
liza-7567306123c4bc8448b4003f3fd77f3caaf42570.tar.bz2
liza-7567306123c4bc8448b4003f3fd77f3caaf42570.zip
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.
-rw-r--r--src/ui/styler/FieldStyler.js9
-rw-r--r--test/ui/styler/FieldStylerTest.js98
2 files changed, 103 insertions, 4 deletions
diff --git a/src/ui/styler/FieldStyler.js b/src/ui/styler/FieldStyler.js
index 3d62cd9..07d5ee9 100644
--- a/src/ui/styler/FieldStyler.js
+++ b/src/ui/styler/FieldStyler.js
@@ -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' ), ''
);
}
diff --git a/test/ui/styler/FieldStylerTest.js b/test/ui/styler/FieldStylerTest.js
new file mode 100644
index 0000000..0a4fc09
--- /dev/null
+++ b/test/ui/styler/FieldStylerTest.js
@@ -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 );
+ } );
+ } );
+} );