Compare commits

...

1 Commits

Author SHA1 Message Date
Nicolas Perriault 972924df80 Added support for getElementsByClassName to JSDOMParser. 9 years ago

@ -313,6 +313,16 @@
return elems;
}
function getElementsByClassName(classname) {
var elems = [];
var re = new RegExp('(^| )' + classname + '( |$)');
var nodes = this.getElementsByTagName("*");
for (var i=0, j=nodes.length; i<j; i++)
if (re.test(nodes[i].className))
elems.push(nodes[i]);
return elems;
}
var Node = function () {};
Node.prototype = {
@ -571,6 +581,8 @@
getElementsByTagName: getElementsByTagName,
getElementsByClassName: getElementsByClassName,
getElementById: function (id) {
function getElem(node) {
var length = node.children.length;
@ -615,6 +627,8 @@
getElementsByTagName: getElementsByTagName,
getElementsByClassName: getElementsByClassName,
get className() {
return this.getAttribute("class") || "";
},

@ -282,3 +282,36 @@ describe("Tag local name case handling", function() {
expect(doc.firstChild.firstChild.firstChild.localName).eql("clippath");
});
});
describe("#getElementsByClassName", function() {
var doc;
beforeEach(function() {
var html = '<div>' +
' <div class="foo bar">plop</div>' +
' <div class="bar">plap</div>' +
' <p>baz</p>' +
'</div>';
doc = new JSDOMParser().parse(html);
});
it("should find elements having provided class name", function() {
expect(doc.getElementsByClassName("foo")).to.have.length.of(1);
});
it("shouldn't find unmacthing elements", function() {
expect(doc.getElementsByClassName("baz")).to.have.length.of(0);
});
it("should retrieve multiple elements matching a single class name", function() {
expect(doc.getElementsByClassName("bar")).to.have.length.of(2);
});
it("should search combined class names", function() {
expect(doc.getElementsByClassName("foo bar")).to.have.length.of(1);
});
it("should perform a case-sensitive search", function() {
expect(doc.getElementsByClassName("FOO")).to.have.length.of(0);
});
});

Loading…
Cancel
Save