Skip to content

Commit

Permalink
Add firstElementChild, lastElementChild, nextElementSibling, previous…
Browse files Browse the repository at this point in the history
…ElementSibling
  • Loading branch information
arv committed Mar 16, 2012
1 parent e6b9d28 commit 76d13c8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Source/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var Document = (function() {
createElement: util.method(function(tagName) {
// TODO(arv): Only lowercase for HTMLDocument.
var constr = tagNameToConstructor.get(tagName.toLowerCase());
return new constr(this);
return new constr(this, tagName);
}),
createTextNode: util.method(function(text) {
return new Text(this, text);
Expand Down
25 changes: 24 additions & 1 deletion Source/Element.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
var Element = (function() {
'use strict';

function nextElement(node) {
for (; node && node.nodeType !== Node.ELEMENT_NODE; node = node._nextSibling) {}
return node;
}

function previousElement(node) {
for (; node && node.nodeType !== Node.ELEMENT_NODE; node = node._previousSibling) {}
return node;
}

function normalizeAttributeName(element, name) {
return element._isHTML ? name.toLowerCase() : name;
}
Expand Down Expand Up @@ -40,7 +50,20 @@ var Element = (function() {
return this.tagName;
}),

nodeType: util.readOnlyValue(Node.ELEMENT_NODE)
nodeType: util.readOnlyValue(Node.ELEMENT_NODE),

firstElementChild: util.readOnly(function() {
return nextElement(this._firstChild);
}),
lastElementChild: util.readOnly(function() {
return previousElement(this._lastChild);
}),
previousElementSibling: util.readOnly(function() {
return previousElement(this._previousSibling);
}),
nextElementSibling: util.readOnly(function() {
return nextElement(this._nextSibling);
}),
});

return Element;
Expand Down
5 changes: 5 additions & 0 deletions Source/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ var Node = (function() {
nextSibling: util.readOnly(function() { return this._nextSibling; }),
previousSibling: util.readOnly(function() { return this._previousSibling; }),

parentElement: util.readOnly(function() {
var p = this._parentNode;
return p && (p.nodeType === Node.ELEMENT_NODE ? p : null);
}),

appendChild: util.method(appendChild),
removeChild: util.method(removeChild),
insertBefore: util.method(insertBefore),
Expand Down
2 changes: 2 additions & 0 deletions Source/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
console.log(document.childNodes.length);
document.removeChild(document.childNodes.item(1));
console.log(document.childNodes.length);

console.log(html.firstElementChild, html.firstElementChild.tagName);
})();

</script>

0 comments on commit 76d13c8

Please sign in to comment.