dom.js 33 KB

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