Skip to content

Commit

Permalink
fix(virtual-node): fix hasClass to work with svg elements (#1603)
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed May 30, 2019
1 parent 2a0c5ce commit 9d83662
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/core/base/virtual-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ class VirtualNode {
* @return {Boolean} True if the actualNode has the given class, false otherwise.
*/
hasClass(className) {
if (typeof this.actualNode.className !== 'string') {
// get the value of the class attribute as svgs return a SVGAnimatedString
// if you access the className property
let classAttr = this.attr('class');
if (!classAttr) {
return false;
}

let selector = ' ' + className + ' ';
return (
(' ' + this.actualNode.className + ' ')
.replace(whitespaceRegex, ' ')
.indexOf(selector) >= 0
(' ' + classAttr + ' ').replace(whitespaceRegex, ' ').indexOf(selector) >=
0
);
}

Expand Down
8 changes: 8 additions & 0 deletions test/core/base/virtual-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ describe('VirtualNode', function() {
assert.isTrue(vNode.hasClass('visually-hidden'));
});

it('should return true for svg elements', function() {
node = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
node.setAttribute('class', 'my-class');
var vNode = new VirtualNode(node);

assert.isTrue(vNode.hasClass('my-class'));
});

it('should return false when the element does not contain the class', function() {
var vNode = new VirtualNode(node);

Expand Down

0 comments on commit 9d83662

Please sign in to comment.