Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(virtual-node): fix hasClass to work with svg elements #1603

Merged
merged 1 commit into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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