Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
SVGElement does not have classList in IE
Browse files Browse the repository at this point in the history
We therefore move it to HTMLElement so that code that does feature
detection of classList will work correctly in IE.

Fixes #454
  • Loading branch information
arv committed Jun 9, 2014
1 parent 6c4490e commit 52760a3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/wrappers/SVGElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
(function(scope) {
'use strict';

var Element = scope.wrappers.Element;
var HTMLElement = scope.wrappers.HTMLElement;
var registerObject = scope.registerObject;

var SVG_NS = 'http://www.w3.org/2000/svg';
var svgTitleElement = document.createElementNS(SVG_NS, 'title');
var SVGTitleElement = registerObject(svgTitleElement);
var SVGElement = Object.getPrototypeOf(SVGTitleElement.prototype).constructor;

// IE11 does not have classList for SVG elements. The spec says that classList
// is an accessor on Element, but IE11 puts classList on HTMLElement, leaving
// SVGElement without a classList property. We therefore move the accessor for
// IE11.
if (!('classList' in window.SVGElement)) {
var descr = Object.getOwnPropertyDescriptor(Element.prototype, 'classList');
Object.defineProperty(HTMLElement.prototype, 'classList', descr);
delete Element.prototype.classList;
}

scope.wrappers.SVGElement = SVGElement;
})(window.ShadowDOMPolyfill);
14 changes: 14 additions & 0 deletions test/js/SVGElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ suite('SVGElement', function() {
assert.notInstanceOf(el, HTMLElement);
});

test('classList', function() {
var el = document.createElementNS(SVG_NS, 'svg');
el.setAttribute('class', 'a b');

assert.isTrue(el.classList === undefined ||
(el.classList instanceof DOMTokenList));

if (el.classList !== undefined) {
assert.equal(el.classList.length, 2);
assert.isTrue(el.classList.contains('a'));
assert.isTrue(el.classList.contains('b'));
}
});

});

0 comments on commit 52760a3

Please sign in to comment.