Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support multi things with getElementsByClassName

  • Loading branch information...
commit 6b0f9b956b994add1d6947986ff0d4f2d48c9f3a 1 parent 0117257
@Raynos authored
Showing with 29 additions and 14 deletions.
  1. +10 −13 document.js
  2. +3 −1 package.json
  3. +16 −0 test/test-document.js
View
23 document.js
@@ -1,3 +1,5 @@
+var domWalk = require("dom-walk")
+
var DOMText = require("./dom-text.js")
var DOMElement = require("./dom-element.js")
var DocumentFragment = require("./dom-fragment.js")
@@ -69,20 +71,15 @@ proto.getElementsByClassName = function getElementsByClassName(classNames, paren
parent = this.body
}
- if (parent.className === classNames) {
- return [parent]
- }
+ var elems = []
- var arr = parent.childNodes
- var result = null
+ domWalk(parent, function (node) {
+ var classes = node.className.split(" ")
- if (!arr) {
- return result
- }
+ if (classes.indexOf(classNames) !== -1) {
+ elems.push(node)
+ }
+ })
- for (var i = 0, len = arr.length; !result && i < len; i++) {
- result = getElementsByClassName(classNames, arr[i])
- }
-
- return result
+ return elems
}
View
4 package.json
@@ -16,7 +16,9 @@
"url": "https://github.com/Raynos/min-document/issues",
"email": "raynos2@gmail.com"
},
- "dependencies": {},
+ "dependencies": {
+ "dom-walk": "^0.1.0"
+ },
"devDependencies": {
"run-browser": "git://github.com/Raynos/run-browser",
"tap-dot": "^0.2.1",
View
16 test/test-document.js
@@ -125,6 +125,22 @@ function testDocument(document) {
assert.end()
})
+ test("can getElementsByClassName for many elements", function (assert) {
+ function h(className) {
+ var div = document.createElement("div")
+ div.className = className
+ return div
+ }
+
+ document.body.appendChild(h("multi-class-bar"))
+ document.body.appendChild(h("multi-class-bar"))
+
+ var elems = document.getElementsByClassName("multi-class-bar")
+ assert.equal(elems.length, 2)
+
+ assert.end()
+ })
+
test("can create/manipulate textnodes", function (assert) {
var textnode = document.createTextNode("hello")

0 comments on commit 6b0f9b9

Please sign in to comment.
Something went wrong with that request. Please try again.