From 9cf0a97ff9f6967315fcda68e6b27d337477b89a Mon Sep 17 00:00:00 2001 From: Pablo Molina Date: Sun, 30 Apr 2017 13:35:00 +0200 Subject: [PATCH 1/2] Added test for classnames with namespaces, it fails --- packages/metal-dom/test/dom.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/metal-dom/test/dom.js b/packages/metal-dom/test/dom.js index aa80b4e0..9eb9eb3e 100644 --- a/packages/metal-dom/test/dom.js +++ b/packages/metal-dom/test/dom.js @@ -150,6 +150,16 @@ describe('dom', function() { dom.toggleClasses(element, 'adipiscing elit lorem sit consectetur'); assert.strictEqual('ipsum dolor amet adipiscing elit consectetur', element.className); }); + + it('should return false when checking if a classname with spaces exists', function() { + let el = document.createElement('div'); + dom.addClasses(el, 'foo'); + dom.addClasses(el, 'bar'); + assert.strictEqual(dom.hasClass(el, 'foo'), true); + assert.strictEqual(dom.hasClass(el, 'bar'), true); + assert.strictEqual(dom.hasClass(el, 'foo bar'), false); + assert.strictEqual(dom.hasClass(el, 'bar foo'), false); + }); }); describe('contains', function() { From 208a5992fda1e29c0ac2e4a4f3b393b6c3574954 Mon Sep 17 00:00:00 2001 From: Pablo Molina Date: Sun, 30 Apr 2017 13:55:33 +0200 Subject: [PATCH 2/2] Updated hasClassWithNative_ and hasClassWithoutNative_ Now the pass the given test. For the case of hasClassWithNative_, it tries to use the native methods, but catches a dom exception with the text "contains HTML space characters", other exceptions are thrown. --- packages/metal-dom/src/domNamed.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/metal-dom/src/domNamed.js b/packages/metal-dom/src/domNamed.js index 66254b71..9e831db7 100644 --- a/packages/metal-dom/src/domNamed.js +++ b/packages/metal-dom/src/domNamed.js @@ -340,7 +340,8 @@ export function hasClass(element, className) { * @private */ function hasClassWithNative_(element, className) { - return element.classList.contains(className); + return className.indexOf(' ') === -1 && + element.classList.contains(className); } /** @@ -351,7 +352,8 @@ function hasClassWithNative_(element, className) { * @private */ function hasClassWithoutNative_(element, className) { - return (` ${element.className} `).indexOf(` ${className} `) >= 0; + return (` ${element.className} `).indexOf(` ${className} `) >= 0 && + className.split(' ').length === 1; } /**