
document.createTreeWalker=function(root,whatToShow,filter,expandEntityReferences){
return new TreeWalker(root,whatToShow,filter,expandEntityReferences);};
function TreeWalker(root,whatToShow,filter,expandEntityReferences){
this.root=root;
this.whatToShow=whatToShow;
this.filter=filter;
this.expandEntityReferences=expandEntityReferences;
this.currentNode=root}
TreeWalker.prototype.parentNode=function(){
var n=this.currentNode.parentNode;
while(n!=this.root&&n!=null&&!this._isAccepted(n))
n=n.parentNode;
if(n!=null)
this.currentNode=n;
return n;};
TreeWalker.prototype.firstChild=function(){
var n=this.currentNode.firstChild;
while(n!=null&&!this._isAccepted(n))
n=n.nextSibling;
if(n!=null)
this.currentNode=n;
return n;};
TreeWalker.prototype.lastChild=function(){
var n=this.currentNode.lastChild;
while(n!=null&&!this._isAccepted(n))
n=n.previousSibling;
if(n!=null)
this.currentNode=n;
return n;};
TreeWalker.prototype.nextSibling=function(){
var n=this.currentNode.nextSibling;
while(n!=null&&!this._isAccepted(n))
n=n.nextSibling;
if(n!=null)
this.currentNode=n;
return n;};
TreeWalker.prototype.previosuSibling=function(){
var n=this.currentNode.previousSibling;
while(n!=null&&!this._isAccepted(n))
n=n.previousSibling;
if(n!=null)
this.currentNode=n;
return n;};
TreeWalker.prototype.nextNode=function(){
var n=this.currentNode;
var isPartOfSubTree=this._getNodeContains(this.root,this.currentNode);
var insideRoot,nodeFilterConstant=NodeFilter.FILTER_ACCEPT;
do{
n=this._getNodeAfter(n,nodeFilterConstant);
nodeFilterConstant=this._acceptNode(n);
insideRoot=!isPartOfSubTree||this._getNodeContains(this.root,n);}while(n!=null&&insideRoot&&nodeFilterConstant!=NodeFilter.FILTER_ACCEPT);
if(n!=null&&insideRoot)
this.currentNode=n;
else if(!insideRoot)
return null;
return n;};
TreeWalker.prototype.previousNode=function(){
var n=this.currentNode;
var isPartOfSubTree=this._getNodeContains(this.root,this.currentNode);
var insideRoot,nodeFilterConstant=NodeFilter.FILTER_ACCEPT;
do{
n=this._getNodeBefore(n,nodeFilterConstant);
nodeFilterConstant=this._acceptNode(n);
insideRoot=!isPartOfSubTree||this._getNodeContains(this.root,n);}while(n!=null&&insideRoot&&nodeFilterConstant!=NodeFilter.FILTER_ACCEPT);
if(n!=null&&insideRoot)
this.currentNode=n;
else if(!insideRoot)
return null;
return n;};
TreeWalker.prototype._isAccepted=function(n){
return this._acceptNode(n)==NodeFilter.FILTER_ACCEPT;};
TreeWalker.prototype._acceptNode=function(n){
if(n==null)
return NodeFilter.FILTER_REJECT;
var whatToShowAccepted=NodeFilter._acceptNode(n,this.whatToShow);
if(whatToShowAccepted!=NodeFilter.FILTER_ACCEPT)
return whatToShowAccepted;
if(this.filter!=null)
return this.filter.acceptNode(n);
return NodeFilter.FILTER_ACCEPT;};
TreeWalker.prototype._getNodeAfter=function(n,nodeFilterConstant){
if(n==null)
return null;
else if(n.hasChildNodes()&&nodeFilterConstant!=NodeFilter.FILTER_REJECT)
return n.firstChild;
else if(n.nextSibling!=null)
return n.nextSibling;
else{
var tmp=n;
while(true){
if(tmp==null)
return null;
else if(tmp.nextSibling!=null)
return tmp.nextSibling;
else
tmp=tmp.parentNode;}}};
TreeWalker.prototype._getNodeBefore=function(n,nodeFilterConstant){
if(n==null)
return null;
if(n.previousSibling!=null)
return this._getLastDescendant(n.previousSibling,nodeFilterConstant);
else
return n.parentNode;};
TreeWalker.prototype._getLastDescendant=function(n,nodeFilterConstant){
if(n.hasChildNodes()&&!nodeFilterConstant!=NodeFilter.FITLER_REJECT)
return this._getLastDescendant(n.lastChild);
else
return n;};
TreeWalker.prototype._getNodeContains=function(p,c){
if(c==null)
return false;
else if(p==c)
return true;
else
return this._getNodeContains(p,c.parentNode);}
function NodeFilter(){}
NodeFilter.FILTER_ACCEPT=1;
NodeFilter.FILTER_REJECT=2;
NodeFilter.FILTER_SKIP=3;
NodeFilter.SHOW_ALL=0xFFFFFFFF;
NodeFilter.SHOW_ELEMENT=0x00000001;
NodeFilter.SHOW_ATTRIBUTE=0x00000002;
NodeFilter.SHOW_TEXT=0x00000004;
NodeFilter.SHOW_CDATA_SECTION=0x00000008;
NodeFilter.SHOW_ENTITY_REFERENCE=0x00000010;
NodeFilter.SHOW_ENTITY=0x00000020;
NodeFilter.SHOW_PROCESSING_INSTRUCTION=0x00000040;
NodeFilter.SHOW_COMMENT=0x00000080;
NodeFilter.SHOW_DOCUMENT=0x00000100;
NodeFilter.SHOW_DOCUMENT_TYPE=0x00000200;
NodeFilter.SHOW_DOCUMENT_FRAGMENT=0x00000400;
NodeFilter.SHOW_NOTATION=0x00000800;
NodeFilter.prototype.acceptNode=function(n){
return NodeFilter.FILTER_ACCEPT;};
NodeFilter._acceptNode=function(n,whatToShow){
if(whatToShow==NodeFilter.SHOW_ALL)
return NodeFilter.FILTER_ACCEPT;
var bitMask;
switch(n.nodeType){
case 1:
bitMask=NodeFilter.SHOW_ELEMENT;
break;
case 2:
bitMask=NodeFilter.SHOW_ATTRIBUTE
break;
case 3:
bitMask=NodeFilter.SHOW_TEXT;
break;
case 4:
bitMask=NodeFilter.SHOW_CDATA_SECTION;
break;
case 5:
bitMask=NodeFilter.SHOW_ENTITY_REFERENCE;
break;
case 6:
bitMask=NodeFilter.SHOW_ENTITY;
break;
case 7:
bitMask=NodeFilter.SHOW_PROCESSING_INSTRUCTION;
break;
case 8:
bitMask=NodeFilter.SHOW_COMMENT;
break;
case 9:
bitMask=NodeFilter.SHOW_DOCUMENT;
break;
case 10:
bitMask=NodeFilter.SHOW_DOCUMENT_TYPE;
break;
case 11:
bitMask=NodeFilter.SHOW_DOCUMENT_FRAGMENT;
break;
case 12:
bitMask=NodeFilter.SHOW_NOTATION;
break;}
return(bitMask&whatToShow)!=0?
NodeFilter.FILTER_ACCEPT:
NodeFilter.FILTER_SKIP;};

