"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/xmldom/dom.js" (7 Feb 2017, 34247 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * DOM Level 2
    3  * Object DOMException
    4  * @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html
    5  * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html
    6  */
    7 
    8 function copy(src,dest){
    9     for(var p in src){
   10         dest[p] = src[p];
   11     }
   12 }
   13 /**
   14 ^\w+\.prototype\.([_\w]+)\s*=\s*((?:.*\{\s*?[\r\n][\s\S]*?^})|\S.*?(?=[;\r\n]));?
   15 ^\w+\.prototype\.([_\w]+)\s*=\s*(\S.*?(?=[;\r\n]));?
   16  */
   17 function _extends(Class,Super){
   18     var pt = Class.prototype;
   19     if(Object.create){
   20         var ppt = Object.create(Super.prototype)
   21         pt.__proto__ = ppt;
   22     }
   23     if(!(pt instanceof Super)){
   24         function t(){};
   25         t.prototype = Super.prototype;
   26         t = new t();
   27         copy(pt,t);
   28         Class.prototype = pt = t;
   29     }
   30     if(pt.constructor != Class){
   31         if(typeof Class != 'function'){
   32             console.error("unknow Class:"+Class)
   33         }
   34         pt.constructor = Class
   35     }
   36 }
   37 var htmlns = 'http://www.w3.org/1999/xhtml' ;
   38 // Node Types
   39 var NodeType = {}
   40 var ELEMENT_NODE                = NodeType.ELEMENT_NODE                = 1;
   41 var ATTRIBUTE_NODE              = NodeType.ATTRIBUTE_NODE              = 2;
   42 var TEXT_NODE                   = NodeType.TEXT_NODE                   = 3;
   43 var CDATA_SECTION_NODE          = NodeType.CDATA_SECTION_NODE          = 4;
   44 var ENTITY_REFERENCE_NODE       = NodeType.ENTITY_REFERENCE_NODE       = 5;
   45 var ENTITY_NODE                 = NodeType.ENTITY_NODE                 = 6;
   46 var PROCESSING_INSTRUCTION_NODE = NodeType.PROCESSING_INSTRUCTION_NODE = 7;
   47 var COMMENT_NODE                = NodeType.COMMENT_NODE                = 8;
   48 var DOCUMENT_NODE               = NodeType.DOCUMENT_NODE               = 9;
   49 var DOCUMENT_TYPE_NODE          = NodeType.DOCUMENT_TYPE_NODE          = 10;
   50 var DOCUMENT_FRAGMENT_NODE      = NodeType.DOCUMENT_FRAGMENT_NODE      = 11;
   51 var NOTATION_NODE               = NodeType.NOTATION_NODE               = 12;
   52 
   53 // ExceptionCode
   54 var ExceptionCode = {}
   55 var ExceptionMessage = {};
   56 var INDEX_SIZE_ERR              = ExceptionCode.INDEX_SIZE_ERR              = ((ExceptionMessage[1]="Index size error"),1);
   57 var DOMSTRING_SIZE_ERR          = ExceptionCode.DOMSTRING_SIZE_ERR          = ((ExceptionMessage[2]="DOMString size error"),2);
   58 var HIERARCHY_REQUEST_ERR       = ExceptionCode.HIERARCHY_REQUEST_ERR       = ((ExceptionMessage[3]="Hierarchy request error"),3);
   59 var WRONG_DOCUMENT_ERR          = ExceptionCode.WRONG_DOCUMENT_ERR          = ((ExceptionMessage[4]="Wrong document"),4);
   60 var INVALID_CHARACTER_ERR       = ExceptionCode.INVALID_CHARACTER_ERR       = ((ExceptionMessage[5]="Invalid character"),5);
   61 var NO_DATA_ALLOWED_ERR         = ExceptionCode.NO_DATA_ALLOWED_ERR         = ((ExceptionMessage[6]="No data allowed"),6);
   62 var NO_MODIFICATION_ALLOWED_ERR = ExceptionCode.NO_MODIFICATION_ALLOWED_ERR = ((ExceptionMessage[7]="No modification allowed"),7);
   63 var NOT_FOUND_ERR               = ExceptionCode.NOT_FOUND_ERR               = ((ExceptionMessage[8]="Not found"),8);
   64 var NOT_SUPPORTED_ERR           = ExceptionCode.NOT_SUPPORTED_ERR           = ((ExceptionMessage[9]="Not supported"),9);
   65 var INUSE_ATTRIBUTE_ERR         = ExceptionCode.INUSE_ATTRIBUTE_ERR         = ((ExceptionMessage[10]="Attribute in use"),10);
   66 //level2
   67 var INVALID_STATE_ERR           = ExceptionCode.INVALID_STATE_ERR           = ((ExceptionMessage[11]="Invalid state"),11);
   68 var SYNTAX_ERR                  = ExceptionCode.SYNTAX_ERR                  = ((ExceptionMessage[12]="Syntax error"),12);
   69 var INVALID_MODIFICATION_ERR    = ExceptionCode.INVALID_MODIFICATION_ERR    = ((ExceptionMessage[13]="Invalid modification"),13);
   70 var NAMESPACE_ERR               = ExceptionCode.NAMESPACE_ERR               = ((ExceptionMessage[14]="Invalid namespace"),14);
   71 var INVALID_ACCESS_ERR          = ExceptionCode.INVALID_ACCESS_ERR          = ((ExceptionMessage[15]="Invalid access"),15);
   72 
   73 
   74 function DOMException(code, message) {
   75     if(message instanceof Error){
   76         var error = message;
   77     }else{
   78         error = this;
   79         Error.call(this, ExceptionMessage[code]);
   80         this.message = ExceptionMessage[code];
   81         if(Error.captureStackTrace) Error.captureStackTrace(this, DOMException);
   82     }
   83     error.code = code;
   84     if(message) this.message = this.message + ": " + message;
   85     return error;
   86 };
   87 DOMException.prototype = Error.prototype;
   88 copy(ExceptionCode,DOMException)
   89 /**
   90  * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-536297177
   91  * The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. NodeList objects in the DOM are live.
   92  * The items in the NodeList are accessible via an integral index, starting from 0.
   93  */
   94 function NodeList() {
   95 };
   96 NodeList.prototype = {
   97     /**
   98      * The number of nodes in the list. The range of valid child node indices is 0 to length-1 inclusive.
   99      * @standard level1
  100      */
  101     length:0, 
  102     /**
  103      * Returns the indexth item in the collection. If index is greater than or equal to the number of nodes in the list, this returns null.
  104      * @standard level1
  105      * @param index  unsigned long 
  106      *   Index into the collection.
  107      * @return Node
  108      *  The node at the indexth position in the NodeList, or null if that is not a valid index. 
  109      */
  110     item: function(index) {
  111         return this[index] || null;
  112     },
  113     toString:function(isHTML,nodeFilter){
  114         for(var buf = [], i = 0;i<this.length;i++){
  115             serializeToString(this[i],buf,isHTML,nodeFilter);
  116         }
  117         return buf.join('');
  118     }
  119 };
  120 function LiveNodeList(node,refresh){
  121     this._node = node;
  122     this._refresh = refresh
  123     _updateLiveList(this);
  124 }
  125 function _updateLiveList(list){
  126     var inc = list._node._inc || list._node.ownerDocument._inc;
  127     if(list._inc != inc){
  128         var ls = list._refresh(list._node);
  129         //console.log(ls.length)
  130         __set__(list,'length',ls.length);
  131         copy(ls,list);
  132         list._inc = inc;
  133     }
  134 }
  135 LiveNodeList.prototype.item = function(i){
  136     _updateLiveList(this);
  137     return this[i];
  138 }
  139 
  140 _extends(LiveNodeList,NodeList);
  141 /**
  142  * 
  143  * Objects implementing the NamedNodeMap interface are used to represent collections of nodes that can be accessed by name. Note that NamedNodeMap does not inherit from NodeList; NamedNodeMaps are not maintained in any particular order. Objects contained in an object implementing NamedNodeMap may also be accessed by an ordinal index, but this is simply to allow convenient enumeration of the contents of a NamedNodeMap, and does not imply that the DOM specifies an order to these Nodes.
  144  * NamedNodeMap objects in the DOM are live.
  145  * used for attributes or DocumentType entities 
  146  */
  147 function NamedNodeMap() {
  148 };
  149 
  150 function _findNodeIndex(list,node){
  151     var i = list.length;
  152     while(i--){
  153         if(list[i] === node){return i}
  154     }
  155 }
  156 
  157 function _addNamedNode(el,list,newAttr,oldAttr){
  158     if(oldAttr){
  159         list[_findNodeIndex(list,oldAttr)] = newAttr;
  160     }else{
  161         list[list.length++] = newAttr;
  162     }
  163     if(el){
  164         newAttr.ownerElement = el;
  165         var doc = el.ownerDocument;
  166         if(doc){
  167             oldAttr && _onRemoveAttribute(doc,el,oldAttr);
  168             _onAddAttribute(doc,el,newAttr);
  169         }
  170     }
  171 }
  172 function _removeNamedNode(el,list,attr){
  173     //console.log('remove attr:'+attr)
  174     var i = _findNodeIndex(list,attr);
  175     if(i>=0){
  176         var lastIndex = list.length-1
  177         while(i<lastIndex){
  178             list[i] = list[++i]
  179         }
  180         list.length = lastIndex;
  181         if(el){
  182             var doc = el.ownerDocument;
  183             if(doc){
  184                 _onRemoveAttribute(doc,el,attr);
  185                 attr.ownerElement = null;
  186             }
  187         }
  188     }else{
  189         throw DOMException(NOT_FOUND_ERR,new Error(el.tagName+'@'+attr))
  190     }
  191 }
  192 NamedNodeMap.prototype = {
  193     length:0,
  194     item:NodeList.prototype.item,
  195     getNamedItem: function(key) {
  196 //      if(key.indexOf(':')>0 || key == 'xmlns'){
  197 //          return null;
  198 //      }
  199         //console.log()
  200         var i = this.length;
  201         while(i--){
  202             var attr = this[i];
  203             //console.log(attr.nodeName,key)
  204             if(attr.nodeName == key){
  205                 return attr;
  206             }
  207         }
  208     },
  209     setNamedItem: function(attr) {
  210         var el = attr.ownerElement;
  211         if(el && el!=this._ownerElement){
  212             throw new DOMException(INUSE_ATTRIBUTE_ERR);
  213         }
  214         var oldAttr = this.getNamedItem(attr.nodeName);
  215         _addNamedNode(this._ownerElement,this,attr,oldAttr);
  216         return oldAttr;
  217     },
  218     /* returns Node */
  219     setNamedItemNS: function(attr) {// raises: WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR
  220         var el = attr.ownerElement, oldAttr;
  221         if(el && el!=this._ownerElement){
  222             throw new DOMException(INUSE_ATTRIBUTE_ERR);
  223         }
  224         oldAttr = this.getNamedItemNS(attr.namespaceURI,attr.localName);
  225         _addNamedNode(this._ownerElement,this,attr,oldAttr);
  226         return oldAttr;
  227     },
  228 
  229     /* returns Node */
  230     removeNamedItem: function(key) {
  231         var attr = this.getNamedItem(key);
  232         _removeNamedNode(this._ownerElement,this,attr);
  233         return attr;
  234         
  235         
  236     },// raises: NOT_FOUND_ERR,NO_MODIFICATION_ALLOWED_ERR
  237     
  238     //for level2
  239     removeNamedItemNS:function(namespaceURI,localName){
  240         var attr = this.getNamedItemNS(namespaceURI,localName);
  241         _removeNamedNode(this._ownerElement,this,attr);
  242         return attr;
  243     },
  244     getNamedItemNS: function(namespaceURI, localName) {
  245         var i = this.length;
  246         while(i--){
  247             var node = this[i];
  248             if(node.localName == localName && node.namespaceURI == namespaceURI){
  249                 return node;
  250             }
  251         }
  252         return null;
  253     }
  254 };
  255 /**
  256  * @see http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-102161490
  257  */
  258 function DOMImplementation(/* Object */ features) {
  259     this._features = {};
  260     if (features) {
  261         for (var feature in features) {
  262              this._features = features[feature];
  263         }
  264     }
  265 };
  266 
  267 DOMImplementation.prototype = {
  268     hasFeature: function(/* string */ feature, /* string */ version) {
  269         var versions = this._features[feature.toLowerCase()];
  270         if (versions && (!version || version in versions)) {
  271             return true;
  272         } else {
  273             return false;
  274         }
  275     },
  276     // Introduced in DOM Level 2:
  277     createDocument:function(namespaceURI,  qualifiedName, doctype){// raises:INVALID_CHARACTER_ERR,NAMESPACE_ERR,WRONG_DOCUMENT_ERR
  278         var doc = new Document();
  279         doc.implementation = this;
  280         doc.childNodes = new NodeList();
  281         doc.doctype = doctype;
  282         if(doctype){
  283             doc.appendChild(doctype);
  284         }
  285         if(qualifiedName){
  286             var root = doc.createElementNS(namespaceURI,qualifiedName);
  287             doc.appendChild(root);
  288         }
  289         return doc;
  290     },
  291     // Introduced in DOM Level 2:
  292     createDocumentType:function(qualifiedName, publicId, systemId){// raises:INVALID_CHARACTER_ERR,NAMESPACE_ERR
  293         var node = new DocumentType();
  294         node.name = qualifiedName;
  295         node.nodeName = qualifiedName;
  296         node.publicId = publicId;
  297         node.systemId = systemId;
  298         // Introduced in DOM Level 2:
  299         //readonly attribute DOMString        internalSubset;
  300         
  301         //TODO:..
  302         //  readonly attribute NamedNodeMap     entities;
  303         //  readonly attribute NamedNodeMap     notations;
  304         return node;
  305     }
  306 };
  307 
  308 
  309 /**
  310  * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247
  311  */
  312 
  313 function Node() {
  314 };
  315 
  316 Node.prototype = {
  317     firstChild : null,
  318     lastChild : null,
  319     previousSibling : null,
  320     nextSibling : null,
  321     attributes : null,
  322     parentNode : null,
  323     childNodes : null,
  324     ownerDocument : null,
  325     nodeValue : null,
  326     namespaceURI : null,
  327     prefix : null,
  328     localName : null,
  329     // Modified in DOM Level 2:
  330     insertBefore:function(newChild, refChild){//raises 
  331         return _insertBefore(this,newChild,refChild);
  332     },
  333     replaceChild:function(newChild, oldChild){//raises 
  334         this.insertBefore(newChild,oldChild);
  335         if(oldChild){
  336             this.removeChild(oldChild);
  337         }
  338     },
  339     removeChild:function(oldChild){
  340         return _removeChild(this,oldChild);
  341     },
  342     appendChild:function(newChild){
  343         return this.insertBefore(newChild,null);
  344     },
  345     hasChildNodes:function(){
  346         return this.firstChild != null;
  347     },
  348     cloneNode:function(deep){
  349         return cloneNode(this.ownerDocument||this,this,deep);
  350     },
  351     // Modified in DOM Level 2:
  352     normalize:function(){
  353         var child = this.firstChild;
  354         while(child){
  355             var next = child.nextSibling;
  356             if(next && next.nodeType == TEXT_NODE && child.nodeType == TEXT_NODE){
  357                 this.removeChild(next);
  358                 child.appendData(next.data);
  359             }else{
  360                 child.normalize();
  361                 child = next;
  362             }
  363         }
  364     },
  365     // Introduced in DOM Level 2:
  366     isSupported:function(feature, version){
  367         return this.ownerDocument.implementation.hasFeature(feature,version);
  368     },
  369     // Introduced in DOM Level 2:
  370     hasAttributes:function(){
  371         return this.attributes.length>0;
  372     },
  373     lookupPrefix:function(namespaceURI){
  374         var el = this;
  375         while(el){
  376             var map = el._nsMap;
  377             //console.dir(map)
  378             if(map){
  379                 for(var n in map){
  380                     if(map[n] == namespaceURI){
  381                         return n;
  382                     }
  383                 }
  384             }
  385             el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
  386         }
  387         return null;
  388     },
  389     // Introduced in DOM Level 3:
  390     lookupNamespaceURI:function(prefix){
  391         var el = this;
  392         while(el){
  393             var map = el._nsMap;
  394             //console.dir(map)
  395             if(map){
  396                 if(prefix in map){
  397                     return map[prefix] ;
  398                 }
  399             }
  400             el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
  401         }
  402         return null;
  403     },
  404     // Introduced in DOM Level 3:
  405     isDefaultNamespace:function(namespaceURI){
  406         var prefix = this.lookupPrefix(namespaceURI);
  407         return prefix == null;
  408     }
  409 };
  410 
  411 
  412 function _xmlEncoder(c){
  413     return c == '<' && '&lt;' ||
  414          c == '>' && '&gt;' ||
  415          c == '&' && '&amp;' ||
  416          c == '"' && '&quot;' ||
  417          '&#'+c.charCodeAt()+';'
  418 }
  419 
  420 
  421 copy(NodeType,Node);
  422 copy(NodeType,Node.prototype);
  423 
  424 /**
  425  * @param callback return true for continue,false for break
  426  * @return boolean true: break visit;
  427  */
  428 function _visitNode(node,callback){
  429     if(callback(node)){
  430         return true;
  431     }
  432     if(node = node.firstChild){
  433         do{
  434             if(_visitNode(node,callback)){return true}
  435         }while(node=node.nextSibling)
  436     }
  437 }
  438 
  439 
  440 
  441 function Document(){
  442 }
  443 function _onAddAttribute(doc,el,newAttr){
  444     doc && doc._inc++;
  445     var ns = newAttr.namespaceURI ;
  446     if(ns == 'http://www.w3.org/2000/xmlns/'){
  447         //update namespace
  448         el._nsMap[newAttr.prefix?newAttr.localName:''] = newAttr.value
  449     }
  450 }
  451 function _onRemoveAttribute(doc,el,newAttr,remove){
  452     doc && doc._inc++;
  453     var ns = newAttr.namespaceURI ;
  454     if(ns == 'http://www.w3.org/2000/xmlns/'){
  455         //update namespace
  456         delete el._nsMap[newAttr.prefix?newAttr.localName:'']
  457     }
  458 }
  459 function _onUpdateChild(doc,el,newChild){
  460     if(doc && doc._inc){
  461         doc._inc++;
  462         //update childNodes
  463         var cs = el.childNodes;
  464         if(newChild){
  465             cs[cs.length++] = newChild;
  466         }else{
  467             //console.log(1)
  468             var child = el.firstChild;
  469             var i = 0;
  470             while(child){
  471                 cs[i++] = child;
  472                 child =child.nextSibling;
  473             }
  474             cs.length = i;
  475         }
  476     }
  477 }
  478 
  479 /**
  480  * attributes;
  481  * children;
  482  * 
  483  * writeable properties:
  484  * nodeValue,Attr:value,CharacterData:data
  485  * prefix
  486  */
  487 function _removeChild(parentNode,child){
  488     var previous = child.previousSibling;
  489     var next = child.nextSibling;
  490     if(previous){
  491         previous.nextSibling = next;
  492     }else{
  493         parentNode.firstChild = next
  494     }
  495     if(next){
  496         next.previousSibling = previous;
  497     }else{
  498         parentNode.lastChild = previous;
  499     }
  500     _onUpdateChild(parentNode.ownerDocument,parentNode);
  501     return child;
  502 }
  503 /**
  504  * preformance key(refChild == null)
  505  */
  506 function _insertBefore(parentNode,newChild,nextChild){
  507     var cp = newChild.parentNode;
  508     if(cp){
  509         cp.removeChild(newChild);//remove and update
  510     }
  511     if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
  512         var newFirst = newChild.firstChild;
  513         if (newFirst == null) {
  514             return newChild;
  515         }
  516         var newLast = newChild.lastChild;
  517     }else{
  518         newFirst = newLast = newChild;
  519     }
  520     var pre = nextChild ? nextChild.previousSibling : parentNode.lastChild;
  521 
  522     newFirst.previousSibling = pre;
  523     newLast.nextSibling = nextChild;
  524     
  525     
  526     if(pre){
  527         pre.nextSibling = newFirst;
  528     }else{
  529         parentNode.firstChild = newFirst;
  530     }
  531     if(nextChild == null){
  532         parentNode.lastChild = newLast;
  533     }else{
  534         nextChild.previousSibling = newLast;
  535     }
  536     do{
  537         newFirst.parentNode = parentNode;
  538     }while(newFirst !== newLast && (newFirst= newFirst.nextSibling))
  539     _onUpdateChild(parentNode.ownerDocument||parentNode,parentNode);
  540     //console.log(parentNode.lastChild.nextSibling == null)
  541     if (newChild.nodeType == DOCUMENT_FRAGMENT_NODE) {
  542         newChild.firstChild = newChild.lastChild = null;
  543     }
  544     return newChild;
  545 }
  546 function _appendSingleChild(parentNode,newChild){
  547     var cp = newChild.parentNode;
  548     if(cp){
  549         var pre = parentNode.lastChild;
  550         cp.removeChild(newChild);//remove and update
  551         var pre = parentNode.lastChild;
  552     }
  553     var pre = parentNode.lastChild;
  554     newChild.parentNode = parentNode;
  555     newChild.previousSibling = pre;
  556     newChild.nextSibling = null;
  557     if(pre){
  558         pre.nextSibling = newChild;
  559     }else{
  560         parentNode.firstChild = newChild;
  561     }
  562     parentNode.lastChild = newChild;
  563     _onUpdateChild(parentNode.ownerDocument,parentNode,newChild);
  564     return newChild;
  565     //console.log("__aa",parentNode.lastChild.nextSibling == null)
  566 }
  567 Document.prototype = {
  568     //implementation : null,
  569     nodeName :  '#document',
  570     nodeType :  DOCUMENT_NODE,
  571     doctype :  null,
  572     documentElement :  null,
  573     _inc : 1,
  574     
  575     insertBefore :  function(newChild, refChild){//raises 
  576         if(newChild.nodeType == DOCUMENT_FRAGMENT_NODE){
  577             var child = newChild.firstChild;
  578             while(child){
  579                 var next = child.nextSibling;
  580                 this.insertBefore(child,refChild);
  581                 child = next;
  582             }
  583             return newChild;
  584         }
  585         if(this.documentElement == null && newChild.nodeType == ELEMENT_NODE){
  586             this.documentElement = newChild;
  587         }
  588         
  589         return _insertBefore(this,newChild,refChild),(newChild.ownerDocument = this),newChild;
  590     },
  591     removeChild :  function(oldChild){
  592         if(this.documentElement == oldChild){
  593             this.documentElement = null;
  594         }
  595         return _removeChild(this,oldChild);
  596     },
  597     // Introduced in DOM Level 2:
  598     importNode : function(importedNode,deep){
  599         return importNode(this,importedNode,deep);
  600     },
  601     // Introduced in DOM Level 2:
  602     getElementById :    function(id){
  603         var rtv = null;
  604         _visitNode(this.documentElement,function(node){
  605             if(node.nodeType == ELEMENT_NODE){
  606                 if(node.getAttribute('id') == id){
  607                     rtv = node;
  608                     return true;
  609                 }
  610             }
  611         })
  612         return rtv;
  613     },
  614     
  615     //document factory method:
  616     createElement : function(tagName){
  617         var node = new Element();
  618         node.ownerDocument = this;
  619         node.nodeName = tagName;
  620         node.tagName = tagName;
  621         node.childNodes = new NodeList();
  622         var attrs   = node.attributes = new NamedNodeMap();
  623         attrs._ownerElement = node;
  624         return node;
  625     },
  626     createDocumentFragment :    function(){
  627         var node = new DocumentFragment();
  628         node.ownerDocument = this;
  629         node.childNodes = new NodeList();
  630         return node;
  631     },
  632     createTextNode :    function(data){
  633         var node = new Text();
  634         node.ownerDocument = this;
  635         node.appendData(data)
  636         return node;
  637     },
  638     createComment : function(data){
  639         var node = new Comment();
  640         node.ownerDocument = this;
  641         node.appendData(data)
  642         return node;
  643     },
  644     createCDATASection :    function(data){
  645         var node = new CDATASection();
  646         node.ownerDocument = this;
  647         node.appendData(data)
  648         return node;
  649     },
  650     createProcessingInstruction :   function(target,data){
  651         var node = new ProcessingInstruction();
  652         node.ownerDocument = this;
  653         node.tagName = node.target = target;
  654         node.nodeValue= node.data = data;
  655         return node;
  656     },
  657     createAttribute :   function(name){
  658         var node = new Attr();
  659         node.ownerDocument  = this;
  660         node.name = name;
  661         node.nodeName   = name;
  662         node.localName = name;
  663         node.specified = true;
  664         return node;
  665     },
  666     createEntityReference : function(name){
  667         var node = new EntityReference();
  668         node.ownerDocument  = this;
  669         node.nodeName   = name;
  670         return node;
  671     },
  672     // Introduced in DOM Level 2:
  673     createElementNS :   function(namespaceURI,qualifiedName){
  674         var node = new Element();
  675         var pl = qualifiedName.split(':');
  676         var attrs   = node.attributes = new NamedNodeMap();
  677         node.childNodes = new NodeList();
  678         node.ownerDocument = this;
  679         node.nodeName = qualifiedName;
  680         node.tagName = qualifiedName;
  681         node.namespaceURI = namespaceURI;
  682         if(pl.length == 2){
  683             node.prefix = pl[0];
  684             node.localName = pl[1];
  685         }else{
  686             //el.prefix = null;
  687             node.localName = qualifiedName;
  688         }
  689         attrs._ownerElement = node;
  690         return node;
  691     },
  692     // Introduced in DOM Level 2:
  693     createAttributeNS : function(namespaceURI,qualifiedName){
  694         var node = new Attr();
  695         var pl = qualifiedName.split(':');
  696         node.ownerDocument = this;
  697         node.nodeName = qualifiedName;
  698         node.name = qualifiedName;
  699         node.namespaceURI = namespaceURI;
  700         node.specified = true;
  701         if(pl.length == 2){
  702             node.prefix = pl[0];
  703             node.localName = pl[1];
  704         }else{
  705             //el.prefix = null;
  706             node.localName = qualifiedName;
  707         }
  708         return node;
  709     }
  710 };
  711 _extends(Document,Node);
  712 
  713 
  714 function Element() {
  715     this._nsMap = {};
  716 };
  717 Element.prototype = {
  718     nodeType : ELEMENT_NODE,
  719     hasAttribute : function(name){
  720         return this.getAttributeNode(name)!=null;
  721     },
  722     getAttribute : function(name){
  723         var attr = this.getAttributeNode(name);
  724         return attr && attr.value || '';
  725     },
  726     getAttributeNode : function(name){
  727         return this.attributes.getNamedItem(name);
  728     },
  729     setAttribute : function(name, value){
  730         var attr = this.ownerDocument.createAttribute(name);
  731         attr.value = attr.nodeValue = "" + value;
  732         this.setAttributeNode(attr)
  733     },
  734     removeAttribute : function(name){
  735         var attr = this.getAttributeNode(name)
  736         attr && this.removeAttributeNode(attr);
  737     },
  738     
  739     //four real opeartion method
  740     appendChild:function(newChild){
  741         if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
  742             return this.insertBefore(newChild,null);
  743         }else{
  744             return _appendSingleChild(this,newChild);
  745         }
  746     },
  747     setAttributeNode : function(newAttr){
  748         return this.attributes.setNamedItem(newAttr);
  749     },
  750     setAttributeNodeNS : function(newAttr){
  751         return this.attributes.setNamedItemNS(newAttr);
  752     },
  753     removeAttributeNode : function(oldAttr){
  754         //console.log(this == oldAttr.ownerElement)
  755         return this.attributes.removeNamedItem(oldAttr.nodeName);
  756     },
  757     //get real attribute name,and remove it by removeAttributeNode
  758     removeAttributeNS : function(namespaceURI, localName){
  759         var old = this.getAttributeNodeNS(namespaceURI, localName);
  760         old && this.removeAttributeNode(old);
  761     },
  762     
  763     hasAttributeNS : function(namespaceURI, localName){
  764         return this.getAttributeNodeNS(namespaceURI, localName)!=null;
  765     },
  766     getAttributeNS : function(namespaceURI, localName){
  767         var attr = this.getAttributeNodeNS(namespaceURI, localName);
  768         return attr && attr.value || '';
  769     },
  770     setAttributeNS : function(namespaceURI, qualifiedName, value){
  771         var attr = this.ownerDocument.createAttributeNS(namespaceURI, qualifiedName);
  772         attr.value = attr.nodeValue = "" + value;
  773         this.setAttributeNode(attr)
  774     },
  775     getAttributeNodeNS : function(namespaceURI, localName){
  776         return this.attributes.getNamedItemNS(namespaceURI, localName);
  777     },
  778     
  779     getElementsByTagName : function(tagName){
  780         return new LiveNodeList(this,function(base){
  781             var ls = [];
  782             _visitNode(base,function(node){
  783                 if(node !== base && node.nodeType == ELEMENT_NODE && (tagName === '*' || node.tagName == tagName)){
  784                     ls.push(node);
  785                 }
  786             });
  787             return ls;
  788         });
  789     },
  790     getElementsByTagNameNS : function(namespaceURI, localName){
  791         return new LiveNodeList(this,function(base){
  792             var ls = [];
  793             _visitNode(base,function(node){
  794                 if(node !== base && node.nodeType === ELEMENT_NODE && (namespaceURI === '*' || node.namespaceURI === namespaceURI) && (localName === '*' || node.localName == localName)){
  795                     ls.push(node);
  796                 }
  797             });
  798             return ls;
  799             
  800         });
  801     }
  802 };
  803 Document.prototype.getElementsByTagName = Element.prototype.getElementsByTagName;
  804 Document.prototype.getElementsByTagNameNS = Element.prototype.getElementsByTagNameNS;
  805 
  806 
  807 _extends(Element,Node);
  808 function Attr() {
  809 };
  810 Attr.prototype.nodeType = ATTRIBUTE_NODE;
  811 _extends(Attr,Node);
  812 
  813 
  814 function CharacterData() {
  815 };
  816 CharacterData.prototype = {
  817     data : '',
  818     substringData : function(offset, count) {
  819         return this.data.substring(offset, offset+count);
  820     },
  821     appendData: function(text) {
  822         text = this.data+text;
  823         this.nodeValue = this.data = text;
  824         this.length = text.length;
  825     },
  826     insertData: function(offset,text) {
  827         this.replaceData(offset,0,text);
  828     
  829     },
  830     appendChild:function(newChild){
  831         throw new Error(ExceptionMessage[HIERARCHY_REQUEST_ERR])
  832     },
  833     deleteData: function(offset, count) {
  834         this.replaceData(offset,count,"");
  835     },
  836     replaceData: function(offset, count, text) {
  837         var start = this.data.substring(0,offset);
  838         var end = this.data.substring(offset+count);
  839         text = start + text + end;
  840         this.nodeValue = this.data = text;
  841         this.length = text.length;
  842     }
  843 }
  844 _extends(CharacterData,Node);
  845 function Text() {
  846 };
  847 Text.prototype = {
  848     nodeName : "#text",
  849     nodeType : TEXT_NODE,
  850     splitText : function(offset) {
  851         var text = this.data;
  852         var newText = text.substring(offset);
  853         text = text.substring(0, offset);
  854         this.data = this.nodeValue = text;
  855         this.length = text.length;
  856         var newNode = this.ownerDocument.createTextNode(newText);
  857         if(this.parentNode){
  858             this.parentNode.insertBefore(newNode, this.nextSibling);
  859         }
  860         return newNode;
  861     }
  862 }
  863 _extends(Text,CharacterData);
  864 function Comment() {
  865 };
  866 Comment.prototype = {
  867     nodeName : "#comment",
  868     nodeType : COMMENT_NODE
  869 }
  870 _extends(Comment,CharacterData);
  871 
  872 function CDATASection() {
  873 };
  874 CDATASection.prototype = {
  875     nodeName : "#cdata-section",
  876     nodeType : CDATA_SECTION_NODE
  877 }
  878 _extends(CDATASection,CharacterData);
  879 
  880 
  881 function DocumentType() {
  882 };
  883 DocumentType.prototype.nodeType = DOCUMENT_TYPE_NODE;
  884 _extends(DocumentType,Node);
  885 
  886 function Notation() {
  887 };
  888 Notation.prototype.nodeType = NOTATION_NODE;
  889 _extends(Notation,Node);
  890 
  891 function Entity() {
  892 };
  893 Entity.prototype.nodeType = ENTITY_NODE;
  894 _extends(Entity,Node);
  895 
  896 function EntityReference() {
  897 };
  898 EntityReference.prototype.nodeType = ENTITY_REFERENCE_NODE;
  899 _extends(EntityReference,Node);
  900 
  901 function DocumentFragment() {
  902 };
  903 DocumentFragment.prototype.nodeName =   "#document-fragment";
  904 DocumentFragment.prototype.nodeType =   DOCUMENT_FRAGMENT_NODE;
  905 _extends(DocumentFragment,Node);
  906 
  907 
  908 function ProcessingInstruction() {
  909 }
  910 ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
  911 _extends(ProcessingInstruction,Node);
  912 function XMLSerializer(){}
  913 XMLSerializer.prototype.serializeToString = function(node,isHtml,nodeFilter){
  914     return nodeSerializeToString.call(node,isHtml,nodeFilter);
  915 }
  916 Node.prototype.toString = nodeSerializeToString;
  917 function nodeSerializeToString(isHtml,nodeFilter){
  918     var buf = [];
  919     var refNode = this.nodeType == 9?this.documentElement:this;
  920     var prefix = refNode.prefix;
  921     var uri = refNode.namespaceURI;
  922     
  923     if(uri && prefix == null){
  924         //console.log(prefix)
  925         var prefix = refNode.lookupPrefix(uri);
  926         if(prefix == null){
  927             //isHTML = true;
  928             var visibleNamespaces=[
  929             {namespace:uri,prefix:null}
  930             //{namespace:uri,prefix:''}
  931             ]
  932         }
  933     }
  934     serializeToString(this,buf,isHtml,nodeFilter,visibleNamespaces);
  935     //console.log('###',this.nodeType,uri,prefix,buf.join(''))
  936     return buf.join('');
  937 }
  938 function needNamespaceDefine(node,isHTML, visibleNamespaces) {
  939     var prefix = node.prefix||'';
  940     var uri = node.namespaceURI;
  941     if (!prefix && !uri){
  942         return false;
  943     }
  944     if (prefix === "xml" && uri === "http://www.w3.org/XML/1998/namespace" 
  945         || uri == 'http://www.w3.org/2000/xmlns/'){
  946         return false;
  947     }
  948     
  949     var i = visibleNamespaces.length 
  950     //console.log('@@@@',node.tagName,prefix,uri,visibleNamespaces)
  951     while (i--) {
  952         var ns = visibleNamespaces[i];
  953         // get namespace prefix
  954         //console.log(node.nodeType,node.tagName,ns.prefix,prefix)
  955         if (ns.prefix == prefix){
  956             return ns.namespace != uri;
  957         }
  958     }
  959     //console.log(isHTML,uri,prefix=='')
  960     //if(isHTML && prefix ==null && uri == 'http://www.w3.org/1999/xhtml'){
  961     //  return false;
  962     //}
  963     //node.flag = '11111'
  964     //console.error(3,true,node.flag,node.prefix,node.namespaceURI)
  965     return true;
  966 }
  967 function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
  968     if(nodeFilter){
  969         node = nodeFilter(node);
  970         if(node){
  971             if(typeof node == 'string'){
  972                 buf.push(node);
  973                 return;
  974             }
  975         }else{
  976             return;
  977         }
  978         //buf.sort.apply(attrs, attributeSorter);
  979     }
  980     switch(node.nodeType){
  981     case ELEMENT_NODE:
  982         if (!visibleNamespaces) visibleNamespaces = [];
  983         var startVisibleNamespaces = visibleNamespaces.length;
  984         var attrs = node.attributes;
  985         var len = attrs.length;
  986         var child = node.firstChild;
  987         var nodeName = node.tagName;
  988         
  989         isHTML =  (htmlns === node.namespaceURI) ||isHTML 
  990         buf.push('<',nodeName);
  991         
  992         
  993         
  994         for(var i=0;i<len;i++){
  995             // add namespaces for attributes
  996             var attr = attrs.item(i);
  997             if (attr.prefix == 'xmlns') {
  998                 visibleNamespaces.push({ prefix: attr.localName, namespace: attr.value });
  999             }else if(attr.nodeName == 'xmlns'){
 1000                 visibleNamespaces.push({ prefix: '', namespace: attr.value });
 1001             }
 1002         }
 1003         for(var i=0;i<len;i++){
 1004             var attr = attrs.item(i);
 1005             if (needNamespaceDefine(attr,isHTML, visibleNamespaces)) {
 1006                 var prefix = attr.prefix||'';
 1007                 var uri = attr.namespaceURI;
 1008                 var ns = prefix ? ' xmlns:' + prefix : " xmlns";
 1009                 buf.push(ns, '="' , uri , '"');
 1010                 visibleNamespaces.push({ prefix: prefix, namespace:uri });
 1011             }
 1012             serializeToString(attr,buf,isHTML,nodeFilter,visibleNamespaces);
 1013         }
 1014         // add namespace for current node       
 1015         if (needNamespaceDefine(node,isHTML, visibleNamespaces)) {
 1016             var prefix = node.prefix||'';
 1017             var uri = node.namespaceURI;
 1018             var ns = prefix ? ' xmlns:' + prefix : " xmlns";
 1019             buf.push(ns, '="' , uri , '"');
 1020             visibleNamespaces.push({ prefix: prefix, namespace:uri });
 1021         }
 1022         
 1023         if(child || isHTML && !/^(?:meta|link|img|br|hr|input)$/i.test(nodeName)){
 1024             buf.push('>');
 1025             //if is cdata child node
 1026             if(isHTML && /^script$/i.test(nodeName)){
 1027                 while(child){
 1028                     if(child.data){
 1029                         buf.push(child.data);
 1030                     }else{
 1031                         serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
 1032                     }
 1033                     child = child.nextSibling;
 1034                 }
 1035             }else
 1036             {
 1037                 while(child){
 1038                     serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
 1039                     child = child.nextSibling;
 1040                 }
 1041             }
 1042             buf.push('</',nodeName,'>');
 1043         }else{
 1044             buf.push('/>');
 1045         }
 1046         // remove added visible namespaces
 1047         //visibleNamespaces.length = startVisibleNamespaces;
 1048         return;
 1049     case DOCUMENT_NODE:
 1050     case DOCUMENT_FRAGMENT_NODE:
 1051         var child = node.firstChild;
 1052         while(child){
 1053             serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
 1054             child = child.nextSibling;
 1055         }
 1056         return;
 1057     case ATTRIBUTE_NODE:
 1058         return buf.push(' ',node.name,'="',node.value.replace(/[<&"]/g,_xmlEncoder),'"');
 1059     case TEXT_NODE:
 1060         return buf.push(node.data.replace(/[<&]/g,_xmlEncoder));
 1061     case CDATA_SECTION_NODE:
 1062         return buf.push( '<![CDATA[',node.data,']]>');
 1063     case COMMENT_NODE:
 1064         return buf.push( "<!--",node.data,"-->");
 1065     case DOCUMENT_TYPE_NODE:
 1066         var pubid = node.publicId;
 1067         var sysid = node.systemId;
 1068         buf.push('<!DOCTYPE ',node.name);
 1069         if(pubid){
 1070             buf.push(' PUBLIC "',pubid);
 1071             if (sysid && sysid!='.') {
 1072                 buf.push( '" "',sysid);
 1073             }
 1074             buf.push('">');
 1075         }else if(sysid && sysid!='.'){
 1076             buf.push(' SYSTEM "',sysid,'">');
 1077         }else{
 1078             var sub = node.internalSubset;
 1079             if(sub){
 1080                 buf.push(" [",sub,"]");
 1081             }
 1082             buf.push(">");
 1083         }
 1084         return;
 1085     case PROCESSING_INSTRUCTION_NODE:
 1086         return buf.push( "<?",node.target," ",node.data,"?>");
 1087     case ENTITY_REFERENCE_NODE:
 1088         return buf.push( '&',node.nodeName,';');
 1089     //case ENTITY_NODE:
 1090     //case NOTATION_NODE:
 1091     default:
 1092         buf.push('??',node.nodeName);
 1093     }
 1094 }
 1095 function importNode(doc,node,deep){
 1096     var node2;
 1097     switch (node.nodeType) {
 1098     case ELEMENT_NODE:
 1099         node2 = node.cloneNode(false);
 1100         node2.ownerDocument = doc;
 1101         //var attrs = node2.attributes;
 1102         //var len = attrs.length;
 1103         //for(var i=0;i<len;i++){
 1104             //node2.setAttributeNodeNS(importNode(doc,attrs.item(i),deep));
 1105         //}
 1106     case DOCUMENT_FRAGMENT_NODE:
 1107         break;
 1108     case ATTRIBUTE_NODE:
 1109         deep = true;
 1110         break;
 1111     //case ENTITY_REFERENCE_NODE:
 1112     //case PROCESSING_INSTRUCTION_NODE:
 1113     ////case TEXT_NODE:
 1114     //case CDATA_SECTION_NODE:
 1115     //case COMMENT_NODE:
 1116     //  deep = false;
 1117     //  break;
 1118     //case DOCUMENT_NODE:
 1119     //case DOCUMENT_TYPE_NODE:
 1120     //cannot be imported.
 1121     //case ENTITY_NODE:
 1122     //case NOTATION_NODEļ¼š
 1123     //can not hit in level3
 1124     //default:throw e;
 1125     }
 1126     if(!node2){
 1127         node2 = node.cloneNode(false);//false
 1128     }
 1129     node2.ownerDocument = doc;
 1130     node2.parentNode = null;
 1131     if(deep){
 1132         var child = node.firstChild;
 1133         while(child){
 1134             node2.appendChild(importNode(doc,child,deep));
 1135             child = child.nextSibling;
 1136         }
 1137     }
 1138     return node2;
 1139 }
 1140 //
 1141 //var _relationMap = {firstChild:1,lastChild:1,previousSibling:1,nextSibling:1,
 1142 //                  attributes:1,childNodes:1,parentNode:1,documentElement:1,doctype,};
 1143 function cloneNode(doc,node,deep){
 1144     var node2 = new node.constructor();
 1145     for(var n in node){
 1146         var v = node[n];
 1147         if(typeof v != 'object' ){
 1148             if(v != node2[n]){
 1149                 node2[n] = v;
 1150             }
 1151         }
 1152     }
 1153     if(node.childNodes){
 1154         node2.childNodes = new NodeList();
 1155     }
 1156     node2.ownerDocument = doc;
 1157     switch (node2.nodeType) {
 1158     case ELEMENT_NODE:
 1159         var attrs   = node.attributes;
 1160         var attrs2  = node2.attributes = new NamedNodeMap();
 1161         var len = attrs.length
 1162         attrs2._ownerElement = node2;
 1163         for(var i=0;i<len;i++){
 1164             node2.setAttributeNode(cloneNode(doc,attrs.item(i),true));
 1165         }
 1166         break;;
 1167     case ATTRIBUTE_NODE:
 1168         deep = true;
 1169     }
 1170     if(deep){
 1171         var child = node.firstChild;
 1172         while(child){
 1173             node2.appendChild(cloneNode(doc,child,deep));
 1174             child = child.nextSibling;
 1175         }
 1176     }
 1177     return node2;
 1178 }
 1179 
 1180 function __set__(object,key,value){
 1181     object[key] = value
 1182 }
 1183 //do dynamic
 1184 try{
 1185     if(Object.defineProperty){
 1186         Object.defineProperty(LiveNodeList.prototype,'length',{
 1187             get:function(){
 1188                 _updateLiveList(this);
 1189                 return this.$$length;
 1190             }
 1191         });
 1192         Object.defineProperty(Node.prototype,'textContent',{
 1193             get:function(){
 1194                 return getTextContent(this);
 1195             },
 1196             set:function(data){
 1197                 switch(this.nodeType){
 1198                 case ELEMENT_NODE:
 1199                 case DOCUMENT_FRAGMENT_NODE:
 1200                     while(this.firstChild){
 1201                         this.removeChild(this.firstChild);
 1202                     }
 1203                     if(data || String(data)){
 1204                         this.appendChild(this.ownerDocument.createTextNode(data));
 1205                     }
 1206                     break;
 1207                 default:
 1208                     //TODO:
 1209                     this.data = data;
 1210                     this.value = data;
 1211                     this.nodeValue = data;
 1212                 }
 1213             }
 1214         })
 1215         
 1216         function getTextContent(node){
 1217             switch(node.nodeType){
 1218             case ELEMENT_NODE:
 1219             case DOCUMENT_FRAGMENT_NODE:
 1220                 var buf = [];
 1221                 node = node.firstChild;
 1222                 while(node){
 1223                     if(node.nodeType!==7 && node.nodeType !==8){
 1224                         buf.push(getTextContent(node));
 1225                     }
 1226                     node = node.nextSibling;
 1227                 }
 1228                 return buf.join('');
 1229             default:
 1230                 return node.nodeValue;
 1231             }
 1232         }
 1233         __set__ = function(object,key,value){
 1234             //console.log(value)
 1235             object['$$'+key] = value
 1236         }
 1237     }
 1238 }catch(e){//ie8
 1239 }
 1240 
 1241 //if(typeof require == 'function'){
 1242     exports.DOMImplementation = DOMImplementation;
 1243     exports.XMLSerializer = XMLSerializer;
 1244 //}