JavaScript ElementTraversal proposal
27 July W3 announced a new Working Draft, called: ElementTraversal
This is a "new" feature but today it's not cross browser so this is my proposal to have Element Trasversal with every recent browser:
And this is a basic test:
My proposal is based on extend Function prototype, You can find them here.
Just a note about update extra method: not every browser support a defineGetter / setter method so update is a quick way to have correct properties.
If You create an instance and You modify some node, You should use:
This method returns instance itself, so You can pass them directly on other functions.
This is a "new" feature but today it's not cross browser so this is my proposal to have Element Trasversal with every recent browser:
(ElementTraversal = function(node){
this.node = node;
this.firstElementChild = this.getFirstElementChild();
this.lastElementChild = this.getLastElementChild();
this.previousElementSibling = this.getPreviousElementSibling();
this.nextElementSibling = this.getNextElementSibling();
this.childElementCount = this.getChildElementCount();
}).extend(null, {
getFirstElementChild:function(){
for(var c = this.node.childNodes,
i = 0,
j = (c && c.length) || 0,
e;
i < j;
i++
){
if(c[i].nodeType === 1)
e = c[j = i];
};
return e;
},
getLastElementChild:function(){
for(var c = this.node.childNodes,
i = ((c && c.length) || 0) - 1,
j = -1,
e;
i > j;
i--
){
if(c[i].nodeType === 1)
e = c[j = i];
};
return e;
},
getPreviousElementSibling:function(){
var e = this.node.previousSibling;
while(e && e.nodeType !== 1)
e = e.previousSibling;
return e;
},
getNextElementSibling:function(){
var e = this.node.nextSibling;
while(e && e.nodeType !== 1)
e = e.nextSibling;
return e;
},
getChildElementCount:function(){
for(var c = this.node.childNodes,
i = 0,
j = (c && c.length) || 0,
n = 0;
i < j;
i++
){
if(c[i].nodeType === 1)
++n;
};
return n;
},
update:function(){
this.constructor.call(this, this.node);
return this;
}
});
And this is a basic test:
test
test
test
testtest
My proposal is based on extend Function prototype, You can find them here.
Just a note about update extra method: not every browser support a defineGetter / setter method so update is a quick way to have correct properties.
If You create an instance and You modify some node, You should use:
ET.update();
This method returns instance itself, so You can pass them directly on other functions.
Comments
Post a Comment