From 972924df80c60cf47310ab566c3614120ac24c0c Mon Sep 17 00:00:00 2001 From: Nicolas Perriault Date: Fri, 17 Apr 2015 19:37:31 +0200 Subject: [PATCH] Added support for getElementsByClassName to JSDOMParser. --- JSDOMParser.js | 14 ++++++++++++++ test/test-jsdomparser.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/JSDOMParser.js b/JSDOMParser.js index e2fef10..a388950 100644 --- a/JSDOMParser.js +++ b/JSDOMParser.js @@ -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; iplop' + + '
plap
' + + '

baz

' + + ''; + 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); + }); +});