dom.js 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. var conventions = require("./conventions");
  2. var find = conventions.find;
  3. var NAMESPACE = conventions.NAMESPACE;
  4. /**
  5. * A prerequisite for `[].filter`, to drop elements that are empty
  6. * @param {string} input
  7. * @returns {boolean}
  8. */
  9. function notEmptyString (input) {
  10. return input !== ''
  11. }
  12. /**
  13. * @see https://infra.spec.whatwg.org/#split-on-ascii-whitespace
  14. * @see https://infra.spec.whatwg.org/#ascii-whitespace
  15. *
  16. * @param {string} input
  17. * @returns {string[]} (can be empty)
  18. */
  19. function splitOnASCIIWhitespace(input) {
  20. // U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, U+0020 SPACE
  21. return input ? input.split(/[\t\n\f\r ]+/).filter(notEmptyString) : []
  22. }
  23. /**
  24. * Adds element as a key to current if it is not already present.
  25. *
  26. * @param {Record<string, boolean | undefined>} current
  27. * @param {string} element
  28. * @returns {Record<string, boolean | undefined>}
  29. */
  30. function orderedSetReducer (current, element) {
  31. if (!current.hasOwnProperty(element)) {
  32. current[element] = true;
  33. }
  34. return current;
  35. }
  36. /**
  37. * @see https://infra.spec.whatwg.org/#ordered-set
  38. * @param {string} input
  39. * @returns {string[]}
  40. */
  41. function toOrderedSet(input) {
  42. if (!input) return [];
  43. var list = splitOnASCIIWhitespace(input);
  44. return Object.keys(list.reduce(orderedSetReducer, {}))
  45. }
  46. /**
  47. * Uses `list.indexOf` to implement something like `Array.prototype.includes`,
  48. * which we can not rely on being available.
  49. *
  50. * @param {any[]} list
  51. * @returns {function(any): boolean}
  52. */
  53. function arrayIncludes (list) {
  54. return function(element) {
  55. return list && list.indexOf(element) !== -1;
  56. }
  57. }
  58. function copy(src,dest){
  59. for(var p in src){
  60. if (Object.prototype.hasOwnProperty.call(src, p)) {
  61. dest[p] = src[p];
  62. }
  63. }
  64. }
  65. /**
  66. ^\w+\.prototype\.([_\w]+)\s*=\s*((?:.*\{\s*?[\r\n][\s\S]*?^})|\S.*?(?=[;\r\n]));?
  67. ^\w+\.prototype\.([_\w]+)\s*=\s*(\S.*?(?=[;\r\n]));?
  68. */
  69. function _extends(Class,Super){
  70. var pt = Class.prototype;
  71. if(!(pt instanceof Super)){
  72. function t(){};
  73. t.prototype = Super.prototype;
  74. t = new t();
  75. copy(pt,t);
  76. Class.prototype = pt = t;
  77. }
  78. if(pt.constructor != Class){
  79. if(typeof Class != 'function'){
  80. console.error("unknown Class:"+Class)
  81. }
  82. pt.constructor = Class
  83. }
  84. }
  85. // Node Types
  86. var NodeType = {}
  87. var ELEMENT_NODE = NodeType.ELEMENT_NODE = 1;
  88. var ATTRIBUTE_NODE = NodeType.ATTRIBUTE_NODE = 2;
  89. var TEXT_NODE = NodeType.TEXT_NODE = 3;
  90. var CDATA_SECTION_NODE = NodeType.CDATA_SECTION_NODE = 4;
  91. var ENTITY_REFERENCE_NODE = NodeType.ENTITY_REFERENCE_NODE = 5;
  92. var ENTITY_NODE = NodeType.ENTITY_NODE = 6;
  93. var PROCESSING_INSTRUCTION_NODE = NodeType.PROCESSING_INSTRUCTION_NODE = 7;
  94. var COMMENT_NODE = NodeType.COMMENT_NODE = 8;
  95. var DOCUMENT_NODE = NodeType.DOCUMENT_NODE = 9;
  96. var DOCUMENT_TYPE_NODE = NodeType.DOCUMENT_TYPE_NODE = 10;
  97. var DOCUMENT_FRAGMENT_NODE = NodeType.DOCUMENT_FRAGMENT_NODE = 11;
  98. var NOTATION_NODE = NodeType.NOTATION_NODE = 12;
  99. // ExceptionCode
  100. var ExceptionCode = {}
  101. var ExceptionMessage = {};
  102. var INDEX_SIZE_ERR = ExceptionCode.INDEX_SIZE_ERR = ((ExceptionMessage[1]="Index size error"),1);
  103. var DOMSTRING_SIZE_ERR = ExceptionCode.DOMSTRING_SIZE_ERR = ((ExceptionMessage[2]="DOMString size error"),2);
  104. var HIERARCHY_REQUEST_ERR = ExceptionCode.HIERARCHY_REQUEST_ERR = ((ExceptionMessage[3]="Hierarchy request error"),3);
  105. var WRONG_DOCUMENT_ERR = ExceptionCode.WRONG_DOCUMENT_ERR = ((ExceptionMessage[4]="Wrong document"),4);
  106. var INVALID_CHARACTER_ERR = ExceptionCode.INVALID_CHARACTER_ERR = ((ExceptionMessage[5]="Invalid character"),5);
  107. var NO_DATA_ALLOWED_ERR = ExceptionCode.NO_DATA_ALLOWED_ERR = ((ExceptionMessage[6]="No data allowed"),6);
  108. var NO_MODIFICATION_ALLOWED_ERR = ExceptionCode.NO_MODIFICATION_ALLOWED_ERR = ((ExceptionMessage[7]="No modification allowed"),7);
  109. var NOT_FOUND_ERR = ExceptionCode.NOT_FOUND_ERR = ((ExceptionMessage[8]="Not found"),8);
  110. var NOT_SUPPORTED_ERR = ExceptionCode.NOT_SUPPORTED_ERR = ((ExceptionMessage[9]="Not supported"),9);
  111. var INUSE_ATTRIBUTE_ERR = ExceptionCode.INUSE_ATTRIBUTE_ERR = ((ExceptionMessage[10]="Attribute in use"),10);
  112. //level2
  113. var INVALID_STATE_ERR = ExceptionCode.INVALID_STATE_ERR = ((ExceptionMessage[11]="Invalid state"),11);
  114. var SYNTAX_ERR = ExceptionCode.SYNTAX_ERR = ((ExceptionMessage[12]="Syntax error"),12);
  115. var INVALID_MODIFICATION_ERR = ExceptionCode.INVALID_MODIFICATION_ERR = ((ExceptionMessage[13]="Invalid modification"),13);
  116. var NAMESPACE_ERR = ExceptionCode.NAMESPACE_ERR = ((ExceptionMessage[14]="Invalid namespace"),14);
  117. var INVALID_ACCESS_ERR = ExceptionCode.INVALID_ACCESS_ERR = ((ExceptionMessage[15]="Invalid access"),15);
  118. /**
  119. * DOM Level 2
  120. * Object DOMException
  121. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html
  122. * @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html
  123. */
  124. function DOMException(code, message) {
  125. if(message instanceof Error){
  126. var error = message;
  127. }else{
  128. error = this;
  129. Error.call(this, ExceptionMessage[code]);
  130. this.message = ExceptionMessage[code];
  131. if(Error.captureStackTrace) Error.captureStackTrace(this, DOMException);
  132. }
  133. error.code = code;
  134. if(message) this.message = this.message + ": " + message;
  135. return error;
  136. };
  137. DOMException.prototype = Error.prototype;
  138. copy(ExceptionCode,DOMException)
  139. /**
  140. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-536297177
  141. * 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.
  142. * The items in the NodeList are accessible via an integral index, starting from 0.
  143. */
  144. function NodeList() {
  145. };
  146. NodeList.prototype = {
  147. /**
  148. * The number of nodes in the list. The range of valid child node indices is 0 to length-1 inclusive.
  149. * @standard level1
  150. */
  151. length:0,
  152. /**
  153. * 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.
  154. * @standard level1
  155. * @param index unsigned long
  156. * Index into the collection.
  157. * @return Node
  158. * The node at the indexth position in the NodeList, or null if that is not a valid index.
  159. */
  160. item: function(index) {
  161. return index >= 0 && index < this.length ? this[index] : null;
  162. },
  163. toString:function(isHTML,nodeFilter){
  164. for(var buf = [], i = 0;i<this.length;i++){
  165. serializeToString(this[i],buf,isHTML,nodeFilter);
  166. }
  167. return buf.join('');
  168. },
  169. /**
  170. * @private
  171. * @param {function (Node):boolean} predicate
  172. * @returns {Node[]}
  173. */
  174. filter: function (predicate) {
  175. return Array.prototype.filter.call(this, predicate);
  176. },
  177. /**
  178. * @private
  179. * @param {Node} item
  180. * @returns {number}
  181. */
  182. indexOf: function (item) {
  183. return Array.prototype.indexOf.call(this, item);
  184. },
  185. };
  186. function LiveNodeList(node,refresh){
  187. this._node = node;
  188. this._refresh = refresh
  189. _updateLiveList(this);
  190. }
  191. function _updateLiveList(list){
  192. var inc = list._node._inc || list._node.ownerDocument._inc;
  193. if (list._inc !== inc) {
  194. var ls = list._refresh(list._node);
  195. __set__(list,'length',ls.length);
  196. if (!list.$$length || ls.length < list.$$length) {
  197. for (var i = ls.length; i in list; i++) {
  198. if (Object.prototype.hasOwnProperty.call(list, i)) {
  199. delete list[i];
  200. }
  201. }
  202. }
  203. copy(ls,list);
  204. list._inc = inc;
  205. }
  206. }
  207. LiveNodeList.prototype.item = function(i){
  208. _updateLiveList(this);
  209. return this[i] || null;
  210. }
  211. _extends(LiveNodeList,NodeList);
  212. /**
  213. * Objects implementing the NamedNodeMap interface are used
  214. * to represent collections of nodes that can be accessed by name.
  215. * Note that NamedNodeMap does not inherit from NodeList;
  216. * NamedNodeMaps are not maintained in any particular order.
  217. * Objects contained in an object implementing NamedNodeMap may also be accessed by an ordinal index,
  218. * but this is simply to allow convenient enumeration of the contents of a NamedNodeMap,
  219. * and does not imply that the DOM specifies an order to these Nodes.
  220. * NamedNodeMap objects in the DOM are live.
  221. * used for attributes or DocumentType entities
  222. */
  223. function NamedNodeMap() {
  224. };
  225. function _findNodeIndex(list,node){
  226. var i = list.length;
  227. while(i--){
  228. if(list[i] === node){return i}
  229. }
  230. }
  231. function _addNamedNode(el,list,newAttr,oldAttr){
  232. if(oldAttr){
  233. list[_findNodeIndex(list,oldAttr)] = newAttr;
  234. }else{
  235. list[list.length++] = newAttr;
  236. }
  237. if(el){
  238. newAttr.ownerElement = el;
  239. var doc = el.ownerDocument;
  240. if(doc){
  241. oldAttr && _onRemoveAttribute(doc,el,oldAttr);
  242. _onAddAttribute(doc,el,newAttr);
  243. }
  244. }
  245. }
  246. function _removeNamedNode(el,list,attr){
  247. //console.log('remove attr:'+attr)
  248. var i = _findNodeIndex(list,attr);
  249. if(i>=0){
  250. var lastIndex = list.length-1
  251. while(i<lastIndex){
  252. list[i] = list[++i]
  253. }
  254. list.length = lastIndex;
  255. if(el){
  256. var doc = el.ownerDocument;
  257. if(doc){
  258. _onRemoveAttribute(doc,el,attr);
  259. attr.ownerElement = null;
  260. }
  261. }
  262. }else{
  263. throw new DOMException(NOT_FOUND_ERR,new Error(el.tagName+'@'+attr))
  264. }
  265. }
  266. NamedNodeMap.prototype = {
  267. length:0,
  268. item:NodeList.prototype.item,
  269. getNamedItem: function(key) {
  270. // if(key.indexOf(':')>0 || key == 'xmlns'){
  271. // return null;
  272. // }
  273. //console.log()
  274. var i = this.length;
  275. while(i--){
  276. var attr = this[i];
  277. //console.log(attr.nodeName,key)
  278. if(attr.nodeName == key){
  279. return attr;
  280. }
  281. }
  282. },
  283. setNamedItem: function(attr) {
  284. var el = attr.ownerElement;
  285. if(el && el!=this._ownerElement){
  286. throw new DOMException(INUSE_ATTRIBUTE_ERR);
  287. }
  288. var oldAttr = this.getNamedItem(attr.nodeName);
  289. _addNamedNode(this._ownerElement,this,attr,oldAttr);
  290. return oldAttr;
  291. },
  292. /* returns Node */
  293. setNamedItemNS: function(attr) {// raises: WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR
  294. var el = attr.ownerElement, oldAttr;
  295. if(el && el!=this._ownerElement){
  296. throw new DOMException(INUSE_ATTRIBUTE_ERR);
  297. }
  298. oldAttr = this.getNamedItemNS(attr.namespaceURI,attr.localName);
  299. _addNamedNode(this._ownerElement,this,attr,oldAttr);
  300. return oldAttr;
  301. },
  302. /* returns Node */
  303. removeNamedItem: function(key) {
  304. var attr = this.getNamedItem(key);
  305. _removeNamedNode(this._ownerElement,this,attr);
  306. return attr;
  307. },// raises: NOT_FOUND_ERR,NO_MODIFICATION_ALLOWED_ERR
  308. //for level2
  309. removeNamedItemNS:function(namespaceURI,localName){
  310. var attr = this.getNamedItemNS(namespaceURI,localName);
  311. _removeNamedNode(this._ownerElement,this,attr);
  312. return attr;
  313. },
  314. getNamedItemNS: function(namespaceURI, localName) {
  315. var i = this.length;
  316. while(i--){
  317. var node = this[i];
  318. if(node.localName == localName && node.namespaceURI == namespaceURI){
  319. return node;
  320. }
  321. }
  322. return null;
  323. }
  324. };
  325. /**
  326. * The DOMImplementation interface represents an object providing methods
  327. * which are not dependent on any particular document.
  328. * Such an object is returned by the `Document.implementation` property.
  329. *
  330. * __The individual methods describe the differences compared to the specs.__
  331. *
  332. * @constructor
  333. *
  334. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation MDN
  335. * @see https://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-102161490 DOM Level 1 Core (Initial)
  336. * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-102161490 DOM Level 2 Core
  337. * @see https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-102161490 DOM Level 3 Core
  338. * @see https://dom.spec.whatwg.org/#domimplementation DOM Living Standard
  339. */
  340. function DOMImplementation() {
  341. }
  342. DOMImplementation.prototype = {
  343. /**
  344. * The DOMImplementation.hasFeature() method returns a Boolean flag indicating if a given feature is supported.
  345. * The different implementations fairly diverged in what kind of features were reported.
  346. * The latest version of the spec settled to force this method to always return true, where the functionality was accurate and in use.
  347. *
  348. * @deprecated It is deprecated and modern browsers return true in all cases.
  349. *
  350. * @param {string} feature
  351. * @param {string} [version]
  352. * @returns {boolean} always true
  353. *
  354. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/hasFeature MDN
  355. * @see https://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-5CED94D7 DOM Level 1 Core
  356. * @see https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature DOM Living Standard
  357. */
  358. hasFeature: function(feature, version) {
  359. return true;
  360. },
  361. /**
  362. * Creates an XML Document object of the specified type with its document element.
  363. *
  364. * __It behaves slightly different from the description in the living standard__:
  365. * - There is no interface/class `XMLDocument`, it returns a `Document` instance.
  366. * - `contentType`, `encoding`, `mode`, `origin`, `url` fields are currently not declared.
  367. * - this implementation is not validating names or qualified names
  368. * (when parsing XML strings, the SAX parser takes care of that)
  369. *
  370. * @param {string|null} namespaceURI
  371. * @param {string} qualifiedName
  372. * @param {DocumentType=null} doctype
  373. * @returns {Document}
  374. *
  375. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocument MDN
  376. * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#Level-2-Core-DOM-createDocument DOM Level 2 Core (initial)
  377. * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument DOM Level 2 Core
  378. *
  379. * @see https://dom.spec.whatwg.org/#validate-and-extract DOM: Validate and extract
  380. * @see https://www.w3.org/TR/xml/#NT-NameStartChar XML Spec: Names
  381. * @see https://www.w3.org/TR/xml-names/#ns-qualnames XML Namespaces: Qualified names
  382. */
  383. createDocument: function(namespaceURI, qualifiedName, doctype){
  384. var doc = new Document();
  385. doc.implementation = this;
  386. doc.childNodes = new NodeList();
  387. doc.doctype = doctype || null;
  388. if (doctype){
  389. doc.appendChild(doctype);
  390. }
  391. if (qualifiedName){
  392. var root = doc.createElementNS(namespaceURI, qualifiedName);
  393. doc.appendChild(root);
  394. }
  395. return doc;
  396. },
  397. /**
  398. * Returns a doctype, with the given `qualifiedName`, `publicId`, and `systemId`.
  399. *
  400. * __This behavior is slightly different from the in the specs__:
  401. * - this implementation is not validating names or qualified names
  402. * (when parsing XML strings, the SAX parser takes care of that)
  403. *
  404. * @param {string} qualifiedName
  405. * @param {string} [publicId]
  406. * @param {string} [systemId]
  407. * @returns {DocumentType} which can either be used with `DOMImplementation.createDocument` upon document creation
  408. * or can be put into the document via methods like `Node.insertBefore()` or `Node.replaceChild()`
  409. *
  410. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocumentType MDN
  411. * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#Level-2-Core-DOM-createDocType DOM Level 2 Core
  412. * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype DOM Living Standard
  413. *
  414. * @see https://dom.spec.whatwg.org/#validate-and-extract DOM: Validate and extract
  415. * @see https://www.w3.org/TR/xml/#NT-NameStartChar XML Spec: Names
  416. * @see https://www.w3.org/TR/xml-names/#ns-qualnames XML Namespaces: Qualified names
  417. */
  418. createDocumentType: function(qualifiedName, publicId, systemId){
  419. var node = new DocumentType();
  420. node.name = qualifiedName;
  421. node.nodeName = qualifiedName;
  422. node.publicId = publicId || '';
  423. node.systemId = systemId || '';
  424. return node;
  425. }
  426. };
  427. /**
  428. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247
  429. */
  430. function Node() {
  431. };
  432. Node.prototype = {
  433. firstChild : null,
  434. lastChild : null,
  435. previousSibling : null,
  436. nextSibling : null,
  437. attributes : null,
  438. parentNode : null,
  439. childNodes : null,
  440. ownerDocument : null,
  441. nodeValue : null,
  442. namespaceURI : null,
  443. prefix : null,
  444. localName : null,
  445. // Modified in DOM Level 2:
  446. insertBefore:function(newChild, refChild){//raises
  447. return _insertBefore(this,newChild,refChild);
  448. },
  449. replaceChild:function(newChild, oldChild){//raises
  450. _insertBefore(this, newChild,oldChild, assertPreReplacementValidityInDocument);
  451. if(oldChild){
  452. this.removeChild(oldChild);
  453. }
  454. },
  455. removeChild:function(oldChild){
  456. return _removeChild(this,oldChild);
  457. },
  458. appendChild:function(newChild){
  459. return this.insertBefore(newChild,null);
  460. },
  461. hasChildNodes:function(){
  462. return this.firstChild != null;
  463. },
  464. cloneNode:function(deep){
  465. return cloneNode(this.ownerDocument||this,this,deep);
  466. },
  467. // Modified in DOM Level 2:
  468. normalize:function(){
  469. var child = this.firstChild;
  470. while(child){
  471. var next = child.nextSibling;
  472. if(next && next.nodeType == TEXT_NODE && child.nodeType == TEXT_NODE){
  473. this.removeChild(next);
  474. child.appendData(next.data);
  475. }else{
  476. child.normalize();
  477. child = next;
  478. }
  479. }
  480. },
  481. // Introduced in DOM Level 2:
  482. isSupported:function(feature, version){
  483. return this.ownerDocument.implementation.hasFeature(feature,version);
  484. },
  485. // Introduced in DOM Level 2:
  486. hasAttributes:function(){
  487. return this.attributes.length>0;
  488. },
  489. /**
  490. * Look up the prefix associated to the given namespace URI, starting from this node.
  491. * **The default namespace declarations are ignored by this method.**
  492. * See Namespace Prefix Lookup for details on the algorithm used by this method.
  493. *
  494. * _Note: The implementation seems to be incomplete when compared to the algorithm described in the specs._
  495. *
  496. * @param {string | null} namespaceURI
  497. * @returns {string | null}
  498. * @see https://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespacePrefix
  499. * @see https://www.w3.org/TR/DOM-Level-3-Core/namespaces-algorithms.html#lookupNamespacePrefixAlgo
  500. * @see https://dom.spec.whatwg.org/#dom-node-lookupprefix
  501. * @see https://github.com/xmldom/xmldom/issues/322
  502. */
  503. lookupPrefix:function(namespaceURI){
  504. var el = this;
  505. while(el){
  506. var map = el._nsMap;
  507. //console.dir(map)
  508. if(map){
  509. for(var n in map){
  510. if (Object.prototype.hasOwnProperty.call(map, n) && map[n] === namespaceURI) {
  511. return n;
  512. }
  513. }
  514. }
  515. el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
  516. }
  517. return null;
  518. },
  519. // Introduced in DOM Level 3:
  520. lookupNamespaceURI:function(prefix){
  521. var el = this;
  522. while(el){
  523. var map = el._nsMap;
  524. //console.dir(map)
  525. if(map){
  526. if(Object.prototype.hasOwnProperty.call(map, prefix)){
  527. return map[prefix] ;
  528. }
  529. }
  530. el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
  531. }
  532. return null;
  533. },
  534. // Introduced in DOM Level 3:
  535. isDefaultNamespace:function(namespaceURI){
  536. var prefix = this.lookupPrefix(namespaceURI);
  537. return prefix == null;
  538. }
  539. };
  540. function _xmlEncoder(c){
  541. return c == '<' && '&lt;' ||
  542. c == '>' && '&gt;' ||
  543. c == '&' && '&amp;' ||
  544. c == '"' && '&quot;' ||
  545. '&#'+c.charCodeAt()+';'
  546. }
  547. copy(NodeType,Node);
  548. copy(NodeType,Node.prototype);
  549. /**
  550. * @param callback return true for continue,false for break
  551. * @return boolean true: break visit;
  552. */
  553. function _visitNode(node,callback){
  554. if(callback(node)){
  555. return true;
  556. }
  557. if(node = node.firstChild){
  558. do{
  559. if(_visitNode(node,callback)){return true}
  560. }while(node=node.nextSibling)
  561. }
  562. }
  563. function Document(){
  564. this.ownerDocument = this;
  565. }
  566. function _onAddAttribute(doc,el,newAttr){
  567. doc && doc._inc++;
  568. var ns = newAttr.namespaceURI ;
  569. if(ns === NAMESPACE.XMLNS){
  570. //update namespace
  571. el._nsMap[newAttr.prefix?newAttr.localName:''] = newAttr.value
  572. }
  573. }
  574. function _onRemoveAttribute(doc,el,newAttr,remove){
  575. doc && doc._inc++;
  576. var ns = newAttr.namespaceURI ;
  577. if(ns === NAMESPACE.XMLNS){
  578. //update namespace
  579. delete el._nsMap[newAttr.prefix?newAttr.localName:'']
  580. }
  581. }
  582. /**
  583. * Updates `el.childNodes`, updating the indexed items and it's `length`.
  584. * Passing `newChild` means it will be appended.
  585. * Otherwise it's assumed that an item has been removed,
  586. * and `el.firstNode` and it's `.nextSibling` are used
  587. * to walk the current list of child nodes.
  588. *
  589. * @param {Document} doc
  590. * @param {Node} el
  591. * @param {Node} [newChild]
  592. * @private
  593. */
  594. function _onUpdateChild (doc, el, newChild) {
  595. if(doc && doc._inc){
  596. doc._inc++;
  597. //update childNodes
  598. var cs = el.childNodes;
  599. if (newChild) {
  600. cs[cs.length++] = newChild;
  601. } else {
  602. var child = el.firstChild;
  603. var i = 0;
  604. while (child) {
  605. cs[i++] = child;
  606. child = child.nextSibling;
  607. }
  608. cs.length = i;
  609. delete cs[cs.length];
  610. }
  611. }
  612. }
  613. /**
  614. * Removes the connections between `parentNode` and `child`
  615. * and any existing `child.previousSibling` or `child.nextSibling`.
  616. *
  617. * @see https://github.com/xmldom/xmldom/issues/135
  618. * @see https://github.com/xmldom/xmldom/issues/145
  619. *
  620. * @param {Node} parentNode
  621. * @param {Node} child
  622. * @returns {Node} the child that was removed.
  623. * @private
  624. */
  625. function _removeChild (parentNode, child) {
  626. var previous = child.previousSibling;
  627. var next = child.nextSibling;
  628. if (previous) {
  629. previous.nextSibling = next;
  630. } else {
  631. parentNode.firstChild = next;
  632. }
  633. if (next) {
  634. next.previousSibling = previous;
  635. } else {
  636. parentNode.lastChild = previous;
  637. }
  638. child.parentNode = null;
  639. child.previousSibling = null;
  640. child.nextSibling = null;
  641. _onUpdateChild(parentNode.ownerDocument, parentNode);
  642. return child;
  643. }
  644. /**
  645. * Returns `true` if `node` can be a parent for insertion.
  646. * @param {Node} node
  647. * @returns {boolean}
  648. */
  649. function hasValidParentNodeType(node) {
  650. return (
  651. node &&
  652. (node.nodeType === Node.DOCUMENT_NODE || node.nodeType === Node.DOCUMENT_FRAGMENT_NODE || node.nodeType === Node.ELEMENT_NODE)
  653. );
  654. }
  655. /**
  656. * Returns `true` if `node` can be inserted according to it's `nodeType`.
  657. * @param {Node} node
  658. * @returns {boolean}
  659. */
  660. function hasInsertableNodeType(node) {
  661. return (
  662. node &&
  663. (isElementNode(node) ||
  664. isTextNode(node) ||
  665. isDocTypeNode(node) ||
  666. node.nodeType === Node.DOCUMENT_FRAGMENT_NODE ||
  667. node.nodeType === Node.COMMENT_NODE ||
  668. node.nodeType === Node.PROCESSING_INSTRUCTION_NODE)
  669. );
  670. }
  671. /**
  672. * Returns true if `node` is a DOCTYPE node
  673. * @param {Node} node
  674. * @returns {boolean}
  675. */
  676. function isDocTypeNode(node) {
  677. return node && node.nodeType === Node.DOCUMENT_TYPE_NODE;
  678. }
  679. /**
  680. * Returns true if the node is an element
  681. * @param {Node} node
  682. * @returns {boolean}
  683. */
  684. function isElementNode(node) {
  685. return node && node.nodeType === Node.ELEMENT_NODE;
  686. }
  687. /**
  688. * Returns true if `node` is a text node
  689. * @param {Node} node
  690. * @returns {boolean}
  691. */
  692. function isTextNode(node) {
  693. return node && node.nodeType === Node.TEXT_NODE;
  694. }
  695. /**
  696. * Check if en element node can be inserted before `child`, or at the end if child is falsy,
  697. * according to the presence and position of a doctype node on the same level.
  698. *
  699. * @param {Document} doc The document node
  700. * @param {Node} child the node that would become the nextSibling if the element would be inserted
  701. * @returns {boolean} `true` if an element can be inserted before child
  702. * @private
  703. * https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
  704. */
  705. function isElementInsertionPossible(doc, child) {
  706. var parentChildNodes = doc.childNodes || [];
  707. if (find(parentChildNodes, isElementNode) || isDocTypeNode(child)) {
  708. return false;
  709. }
  710. var docTypeNode = find(parentChildNodes, isDocTypeNode);
  711. return !(child && docTypeNode && parentChildNodes.indexOf(docTypeNode) > parentChildNodes.indexOf(child));
  712. }
  713. /**
  714. * Check if en element node can be inserted before `child`, or at the end if child is falsy,
  715. * according to the presence and position of a doctype node on the same level.
  716. *
  717. * @param {Node} doc The document node
  718. * @param {Node} child the node that would become the nextSibling if the element would be inserted
  719. * @returns {boolean} `true` if an element can be inserted before child
  720. * @private
  721. * https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
  722. */
  723. function isElementReplacementPossible(doc, child) {
  724. var parentChildNodes = doc.childNodes || [];
  725. function hasElementChildThatIsNotChild(node) {
  726. return isElementNode(node) && node !== child;
  727. }
  728. if (find(parentChildNodes, hasElementChildThatIsNotChild)) {
  729. return false;
  730. }
  731. var docTypeNode = find(parentChildNodes, isDocTypeNode);
  732. return !(child && docTypeNode && parentChildNodes.indexOf(docTypeNode) > parentChildNodes.indexOf(child));
  733. }
  734. /**
  735. * @private
  736. * Steps 1-5 of the checks before inserting and before replacing a child are the same.
  737. *
  738. * @param {Node} parent the parent node to insert `node` into
  739. * @param {Node} node the node to insert
  740. * @param {Node=} child the node that should become the `nextSibling` of `node`
  741. * @returns {Node}
  742. * @throws DOMException for several node combinations that would create a DOM that is not well-formed.
  743. * @throws DOMException if `child` is provided but is not a child of `parent`.
  744. * @see https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
  745. * @see https://dom.spec.whatwg.org/#concept-node-replace
  746. */
  747. function assertPreInsertionValidity1to5(parent, node, child) {
  748. // 1. If `parent` is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException.
  749. if (!hasValidParentNodeType(parent)) {
  750. throw new DOMException(HIERARCHY_REQUEST_ERR, 'Unexpected parent node type ' + parent.nodeType);
  751. }
  752. // 2. If `node` is a host-including inclusive ancestor of `parent`, then throw a "HierarchyRequestError" DOMException.
  753. // not implemented!
  754. // 3. If `child` is non-null and its parent is not `parent`, then throw a "NotFoundError" DOMException.
  755. if (child && child.parentNode !== parent) {
  756. throw new DOMException(NOT_FOUND_ERR, 'child not in parent');
  757. }
  758. if (
  759. // 4. If `node` is not a DocumentFragment, DocumentType, Element, or CharacterData node, then throw a "HierarchyRequestError" DOMException.
  760. !hasInsertableNodeType(node) ||
  761. // 5. If either `node` is a Text node and `parent` is a document,
  762. // the sax parser currently adds top level text nodes, this will be fixed in 0.9.0
  763. // || (node.nodeType === Node.TEXT_NODE && parent.nodeType === Node.DOCUMENT_NODE)
  764. // or `node` is a doctype and `parent` is not a document, then throw a "HierarchyRequestError" DOMException.
  765. (isDocTypeNode(node) && parent.nodeType !== Node.DOCUMENT_NODE)
  766. ) {
  767. throw new DOMException(
  768. HIERARCHY_REQUEST_ERR,
  769. 'Unexpected node type ' + node.nodeType + ' for parent node type ' + parent.nodeType
  770. );
  771. }
  772. }
  773. /**
  774. * @private
  775. * Step 6 of the checks before inserting and before replacing a child are different.
  776. *
  777. * @param {Document} parent the parent node to insert `node` into
  778. * @param {Node} node the node to insert
  779. * @param {Node | undefined} child the node that should become the `nextSibling` of `node`
  780. * @returns {Node}
  781. * @throws DOMException for several node combinations that would create a DOM that is not well-formed.
  782. * @throws DOMException if `child` is provided but is not a child of `parent`.
  783. * @see https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
  784. * @see https://dom.spec.whatwg.org/#concept-node-replace
  785. */
  786. function assertPreInsertionValidityInDocument(parent, node, child) {
  787. var parentChildNodes = parent.childNodes || [];
  788. var nodeChildNodes = node.childNodes || [];
  789. // DocumentFragment
  790. if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
  791. var nodeChildElements = nodeChildNodes.filter(isElementNode);
  792. // If node has more than one element child or has a Text node child.
  793. if (nodeChildElements.length > 1 || find(nodeChildNodes, isTextNode)) {
  794. throw new DOMException(HIERARCHY_REQUEST_ERR, 'More than one element or text in fragment');
  795. }
  796. // Otherwise, if `node` has one element child and either `parent` has an element child,
  797. // `child` is a doctype, or `child` is non-null and a doctype is following `child`.
  798. if (nodeChildElements.length === 1 && !isElementInsertionPossible(parent, child)) {
  799. throw new DOMException(HIERARCHY_REQUEST_ERR, 'Element in fragment can not be inserted before doctype');
  800. }
  801. }
  802. // Element
  803. if (isElementNode(node)) {
  804. // `parent` has an element child, `child` is a doctype,
  805. // or `child` is non-null and a doctype is following `child`.
  806. if (!isElementInsertionPossible(parent, child)) {
  807. throw new DOMException(HIERARCHY_REQUEST_ERR, 'Only one element can be added and only after doctype');
  808. }
  809. }
  810. // DocumentType
  811. if (isDocTypeNode(node)) {
  812. // `parent` has a doctype child,
  813. if (find(parentChildNodes, isDocTypeNode)) {
  814. throw new DOMException(HIERARCHY_REQUEST_ERR, 'Only one doctype is allowed');
  815. }
  816. var parentElementChild = find(parentChildNodes, isElementNode);
  817. // `child` is non-null and an element is preceding `child`,
  818. if (child && parentChildNodes.indexOf(parentElementChild) < parentChildNodes.indexOf(child)) {
  819. throw new DOMException(HIERARCHY_REQUEST_ERR, 'Doctype can only be inserted before an element');
  820. }
  821. // or `child` is null and `parent` has an element child.
  822. if (!child && parentElementChild) {
  823. throw new DOMException(HIERARCHY_REQUEST_ERR, 'Doctype can not be appended since element is present');
  824. }
  825. }
  826. }
  827. /**
  828. * @private
  829. * Step 6 of the checks before inserting and before replacing a child are different.
  830. *
  831. * @param {Document} parent the parent node to insert `node` into
  832. * @param {Node} node the node to insert
  833. * @param {Node | undefined} child the node that should become the `nextSibling` of `node`
  834. * @returns {Node}
  835. * @throws DOMException for several node combinations that would create a DOM that is not well-formed.
  836. * @throws DOMException if `child` is provided but is not a child of `parent`.
  837. * @see https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
  838. * @see https://dom.spec.whatwg.org/#concept-node-replace
  839. */
  840. function assertPreReplacementValidityInDocument(parent, node, child) {
  841. var parentChildNodes = parent.childNodes || [];
  842. var nodeChildNodes = node.childNodes || [];
  843. // DocumentFragment
  844. if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
  845. var nodeChildElements = nodeChildNodes.filter(isElementNode);
  846. // If `node` has more than one element child or has a Text node child.
  847. if (nodeChildElements.length > 1 || find(nodeChildNodes, isTextNode)) {
  848. throw new DOMException(HIERARCHY_REQUEST_ERR, 'More than one element or text in fragment');
  849. }
  850. // Otherwise, if `node` has one element child and either `parent` has an element child that is not `child` or a doctype is following `child`.
  851. if (nodeChildElements.length === 1 && !isElementReplacementPossible(parent, child)) {
  852. throw new DOMException(HIERARCHY_REQUEST_ERR, 'Element in fragment can not be inserted before doctype');
  853. }
  854. }
  855. // Element
  856. if (isElementNode(node)) {
  857. // `parent` has an element child that is not `child` or a doctype is following `child`.
  858. if (!isElementReplacementPossible(parent, child)) {
  859. throw new DOMException(HIERARCHY_REQUEST_ERR, 'Only one element can be added and only after doctype');
  860. }
  861. }
  862. // DocumentType
  863. if (isDocTypeNode(node)) {
  864. function hasDoctypeChildThatIsNotChild(node) {
  865. return isDocTypeNode(node) && node !== child;
  866. }
  867. // `parent` has a doctype child that is not `child`,
  868. if (find(parentChildNodes, hasDoctypeChildThatIsNotChild)) {
  869. throw new DOMException(HIERARCHY_REQUEST_ERR, 'Only one doctype is allowed');
  870. }
  871. var parentElementChild = find(parentChildNodes, isElementNode);
  872. // or an element is preceding `child`.
  873. if (child && parentChildNodes.indexOf(parentElementChild) < parentChildNodes.indexOf(child)) {
  874. throw new DOMException(HIERARCHY_REQUEST_ERR, 'Doctype can only be inserted before an element');
  875. }
  876. }
  877. }
  878. /**
  879. * @private
  880. * @param {Node} parent the parent node to insert `node` into
  881. * @param {Node} node the node to insert
  882. * @param {Node=} child the node that should become the `nextSibling` of `node`
  883. * @returns {Node}
  884. * @throws DOMException for several node combinations that would create a DOM that is not well-formed.
  885. * @throws DOMException if `child` is provided but is not a child of `parent`.
  886. * @see https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
  887. */
  888. function _insertBefore(parent, node, child, _inDocumentAssertion) {
  889. // To ensure pre-insertion validity of a node into a parent before a child, run these steps:
  890. assertPreInsertionValidity1to5(parent, node, child);
  891. // If parent is a document, and any of the statements below, switched on the interface node implements,
  892. // are true, then throw a "HierarchyRequestError" DOMException.
  893. if (parent.nodeType === Node.DOCUMENT_NODE) {
  894. (_inDocumentAssertion || assertPreInsertionValidityInDocument)(parent, node, child);
  895. }
  896. var cp = node.parentNode;
  897. if(cp){
  898. cp.removeChild(node);//remove and update
  899. }
  900. if(node.nodeType === DOCUMENT_FRAGMENT_NODE){
  901. var newFirst = node.firstChild;
  902. if (newFirst == null) {
  903. return node;
  904. }
  905. var newLast = node.lastChild;
  906. }else{
  907. newFirst = newLast = node;
  908. }
  909. var pre = child ? child.previousSibling : parent.lastChild;
  910. newFirst.previousSibling = pre;
  911. newLast.nextSibling = child;
  912. if(pre){
  913. pre.nextSibling = newFirst;
  914. }else{
  915. parent.firstChild = newFirst;
  916. }
  917. if(child == null){
  918. parent.lastChild = newLast;
  919. }else{
  920. child.previousSibling = newLast;
  921. }
  922. do{
  923. newFirst.parentNode = parent;
  924. }while(newFirst !== newLast && (newFirst= newFirst.nextSibling))
  925. _onUpdateChild(parent.ownerDocument||parent, parent);
  926. //console.log(parent.lastChild.nextSibling == null)
  927. if (node.nodeType == DOCUMENT_FRAGMENT_NODE) {
  928. node.firstChild = node.lastChild = null;
  929. }
  930. return node;
  931. }
  932. /**
  933. * Appends `newChild` to `parentNode`.
  934. * If `newChild` is already connected to a `parentNode` it is first removed from it.
  935. *
  936. * @see https://github.com/xmldom/xmldom/issues/135
  937. * @see https://github.com/xmldom/xmldom/issues/145
  938. * @param {Node} parentNode
  939. * @param {Node} newChild
  940. * @returns {Node}
  941. * @private
  942. */
  943. function _appendSingleChild (parentNode, newChild) {
  944. if (newChild.parentNode) {
  945. newChild.parentNode.removeChild(newChild);
  946. }
  947. newChild.parentNode = parentNode;
  948. newChild.previousSibling = parentNode.lastChild;
  949. newChild.nextSibling = null;
  950. if (newChild.previousSibling) {
  951. newChild.previousSibling.nextSibling = newChild;
  952. } else {
  953. parentNode.firstChild = newChild;
  954. }
  955. parentNode.lastChild = newChild;
  956. _onUpdateChild(parentNode.ownerDocument, parentNode, newChild);
  957. return newChild;
  958. }
  959. Document.prototype = {
  960. //implementation : null,
  961. nodeName : '#document',
  962. nodeType : DOCUMENT_NODE,
  963. /**
  964. * The DocumentType node of the document.
  965. *
  966. * @readonly
  967. * @type DocumentType
  968. */
  969. doctype : null,
  970. documentElement : null,
  971. _inc : 1,
  972. insertBefore : function(newChild, refChild){//raises
  973. if(newChild.nodeType == DOCUMENT_FRAGMENT_NODE){
  974. var child = newChild.firstChild;
  975. while(child){
  976. var next = child.nextSibling;
  977. this.insertBefore(child,refChild);
  978. child = next;
  979. }
  980. return newChild;
  981. }
  982. _insertBefore(this, newChild, refChild);
  983. newChild.ownerDocument = this;
  984. if (this.documentElement === null && newChild.nodeType === ELEMENT_NODE) {
  985. this.documentElement = newChild;
  986. }
  987. return newChild;
  988. },
  989. removeChild : function(oldChild){
  990. if(this.documentElement == oldChild){
  991. this.documentElement = null;
  992. }
  993. return _removeChild(this,oldChild);
  994. },
  995. replaceChild: function (newChild, oldChild) {
  996. //raises
  997. _insertBefore(this, newChild, oldChild, assertPreReplacementValidityInDocument);
  998. newChild.ownerDocument = this;
  999. if (oldChild) {
  1000. this.removeChild(oldChild);
  1001. }
  1002. if (isElementNode(newChild)) {
  1003. this.documentElement = newChild;
  1004. }
  1005. },
  1006. // Introduced in DOM Level 2:
  1007. importNode : function(importedNode,deep){
  1008. return importNode(this,importedNode,deep);
  1009. },
  1010. // Introduced in DOM Level 2:
  1011. getElementById : function(id){
  1012. var rtv = null;
  1013. _visitNode(this.documentElement,function(node){
  1014. if(node.nodeType == ELEMENT_NODE){
  1015. if(node.getAttribute('id') == id){
  1016. rtv = node;
  1017. return true;
  1018. }
  1019. }
  1020. })
  1021. return rtv;
  1022. },
  1023. /**
  1024. * The `getElementsByClassName` method of `Document` interface returns an array-like object
  1025. * of all child elements which have **all** of the given class name(s).
  1026. *
  1027. * Returns an empty list if `classeNames` is an empty string or only contains HTML white space characters.
  1028. *
  1029. *
  1030. * Warning: This is a live LiveNodeList.
  1031. * Changes in the DOM will reflect in the array as the changes occur.
  1032. * If an element selected by this array no longer qualifies for the selector,
  1033. * it will automatically be removed. Be aware of this for iteration purposes.
  1034. *
  1035. * @param {string} classNames is a string representing the class name(s) to match; multiple class names are separated by (ASCII-)whitespace
  1036. *
  1037. * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName
  1038. * @see https://dom.spec.whatwg.org/#concept-getelementsbyclassname
  1039. */
  1040. getElementsByClassName: function(classNames) {
  1041. var classNamesSet = toOrderedSet(classNames)
  1042. return new LiveNodeList(this, function(base) {
  1043. var ls = [];
  1044. if (classNamesSet.length > 0) {
  1045. _visitNode(base.documentElement, function(node) {
  1046. if(node !== base && node.nodeType === ELEMENT_NODE) {
  1047. var nodeClassNames = node.getAttribute('class')
  1048. // can be null if the attribute does not exist
  1049. if (nodeClassNames) {
  1050. // before splitting and iterating just compare them for the most common case
  1051. var matches = classNames === nodeClassNames;
  1052. if (!matches) {
  1053. var nodeClassNamesSet = toOrderedSet(nodeClassNames)
  1054. matches = classNamesSet.every(arrayIncludes(nodeClassNamesSet))
  1055. }
  1056. if(matches) {
  1057. ls.push(node);
  1058. }
  1059. }
  1060. }
  1061. });
  1062. }
  1063. return ls;
  1064. });
  1065. },
  1066. //document factory method:
  1067. createElement : function(tagName){
  1068. var node = new Element();
  1069. node.ownerDocument = this;
  1070. node.nodeName = tagName;
  1071. node.tagName = tagName;
  1072. node.localName = tagName;
  1073. node.childNodes = new NodeList();
  1074. var attrs = node.attributes = new NamedNodeMap();
  1075. attrs._ownerElement = node;
  1076. return node;
  1077. },
  1078. createDocumentFragment : function(){
  1079. var node = new DocumentFragment();
  1080. node.ownerDocument = this;
  1081. node.childNodes = new NodeList();
  1082. return node;
  1083. },
  1084. createTextNode : function(data){
  1085. var node = new Text();
  1086. node.ownerDocument = this;
  1087. node.appendData(data)
  1088. return node;
  1089. },
  1090. createComment : function(data){
  1091. var node = new Comment();
  1092. node.ownerDocument = this;
  1093. node.appendData(data)
  1094. return node;
  1095. },
  1096. createCDATASection : function(data){
  1097. var node = new CDATASection();
  1098. node.ownerDocument = this;
  1099. node.appendData(data)
  1100. return node;
  1101. },
  1102. createProcessingInstruction : function(target,data){
  1103. var node = new ProcessingInstruction();
  1104. node.ownerDocument = this;
  1105. node.tagName = node.nodeName = node.target = target;
  1106. node.nodeValue = node.data = data;
  1107. return node;
  1108. },
  1109. createAttribute : function(name){
  1110. var node = new Attr();
  1111. node.ownerDocument = this;
  1112. node.name = name;
  1113. node.nodeName = name;
  1114. node.localName = name;
  1115. node.specified = true;
  1116. return node;
  1117. },
  1118. createEntityReference : function(name){
  1119. var node = new EntityReference();
  1120. node.ownerDocument = this;
  1121. node.nodeName = name;
  1122. return node;
  1123. },
  1124. // Introduced in DOM Level 2:
  1125. createElementNS : function(namespaceURI,qualifiedName){
  1126. var node = new Element();
  1127. var pl = qualifiedName.split(':');
  1128. var attrs = node.attributes = new NamedNodeMap();
  1129. node.childNodes = new NodeList();
  1130. node.ownerDocument = this;
  1131. node.nodeName = qualifiedName;
  1132. node.tagName = qualifiedName;
  1133. node.namespaceURI = namespaceURI;
  1134. if(pl.length == 2){
  1135. node.prefix = pl[0];
  1136. node.localName = pl[1];
  1137. }else{
  1138. //el.prefix = null;
  1139. node.localName = qualifiedName;
  1140. }
  1141. attrs._ownerElement = node;
  1142. return node;
  1143. },
  1144. // Introduced in DOM Level 2:
  1145. createAttributeNS : function(namespaceURI,qualifiedName){
  1146. var node = new Attr();
  1147. var pl = qualifiedName.split(':');
  1148. node.ownerDocument = this;
  1149. node.nodeName = qualifiedName;
  1150. node.name = qualifiedName;
  1151. node.namespaceURI = namespaceURI;
  1152. node.specified = true;
  1153. if(pl.length == 2){
  1154. node.prefix = pl[0];
  1155. node.localName = pl[1];
  1156. }else{
  1157. //el.prefix = null;
  1158. node.localName = qualifiedName;
  1159. }
  1160. return node;
  1161. }
  1162. };
  1163. _extends(Document,Node);
  1164. function Element() {
  1165. this._nsMap = {};
  1166. };
  1167. Element.prototype = {
  1168. nodeType : ELEMENT_NODE,
  1169. hasAttribute : function(name){
  1170. return this.getAttributeNode(name)!=null;
  1171. },
  1172. getAttribute : function(name){
  1173. var attr = this.getAttributeNode(name);
  1174. return attr && attr.value || '';
  1175. },
  1176. getAttributeNode : function(name){
  1177. return this.attributes.getNamedItem(name);
  1178. },
  1179. setAttribute : function(name, value){
  1180. var attr = this.ownerDocument.createAttribute(name);
  1181. attr.value = attr.nodeValue = "" + value;
  1182. this.setAttributeNode(attr)
  1183. },
  1184. removeAttribute : function(name){
  1185. var attr = this.getAttributeNode(name)
  1186. attr && this.removeAttributeNode(attr);
  1187. },
  1188. //four real opeartion method
  1189. appendChild:function(newChild){
  1190. if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
  1191. return this.insertBefore(newChild,null);
  1192. }else{
  1193. return _appendSingleChild(this,newChild);
  1194. }
  1195. },
  1196. setAttributeNode : function(newAttr){
  1197. return this.attributes.setNamedItem(newAttr);
  1198. },
  1199. setAttributeNodeNS : function(newAttr){
  1200. return this.attributes.setNamedItemNS(newAttr);
  1201. },
  1202. removeAttributeNode : function(oldAttr){
  1203. //console.log(this == oldAttr.ownerElement)
  1204. return this.attributes.removeNamedItem(oldAttr.nodeName);
  1205. },
  1206. //get real attribute name,and remove it by removeAttributeNode
  1207. removeAttributeNS : function(namespaceURI, localName){
  1208. var old = this.getAttributeNodeNS(namespaceURI, localName);
  1209. old && this.removeAttributeNode(old);
  1210. },
  1211. hasAttributeNS : function(namespaceURI, localName){
  1212. return this.getAttributeNodeNS(namespaceURI, localName)!=null;
  1213. },
  1214. getAttributeNS : function(namespaceURI, localName){
  1215. var attr = this.getAttributeNodeNS(namespaceURI, localName);
  1216. return attr && attr.value || '';
  1217. },
  1218. setAttributeNS : function(namespaceURI, qualifiedName, value){
  1219. var attr = this.ownerDocument.createAttributeNS(namespaceURI, qualifiedName);
  1220. attr.value = attr.nodeValue = "" + value;
  1221. this.setAttributeNode(attr)
  1222. },
  1223. getAttributeNodeNS : function(namespaceURI, localName){
  1224. return this.attributes.getNamedItemNS(namespaceURI, localName);
  1225. },
  1226. getElementsByTagName : function(tagName){
  1227. return new LiveNodeList(this,function(base){
  1228. var ls = [];
  1229. _visitNode(base,function(node){
  1230. if(node !== base && node.nodeType == ELEMENT_NODE && (tagName === '*' || node.tagName == tagName)){
  1231. ls.push(node);
  1232. }
  1233. });
  1234. return ls;
  1235. });
  1236. },
  1237. getElementsByTagNameNS : function(namespaceURI, localName){
  1238. return new LiveNodeList(this,function(base){
  1239. var ls = [];
  1240. _visitNode(base,function(node){
  1241. if(node !== base && node.nodeType === ELEMENT_NODE && (namespaceURI === '*' || node.namespaceURI === namespaceURI) && (localName === '*' || node.localName == localName)){
  1242. ls.push(node);
  1243. }
  1244. });
  1245. return ls;
  1246. });
  1247. }
  1248. };
  1249. Document.prototype.getElementsByTagName = Element.prototype.getElementsByTagName;
  1250. Document.prototype.getElementsByTagNameNS = Element.prototype.getElementsByTagNameNS;
  1251. _extends(Element,Node);
  1252. function Attr() {
  1253. };
  1254. Attr.prototype.nodeType = ATTRIBUTE_NODE;
  1255. _extends(Attr,Node);
  1256. function CharacterData() {
  1257. };
  1258. CharacterData.prototype = {
  1259. data : '',
  1260. substringData : function(offset, count) {
  1261. return this.data.substring(offset, offset+count);
  1262. },
  1263. appendData: function(text) {
  1264. text = this.data+text;
  1265. this.nodeValue = this.data = text;
  1266. this.length = text.length;
  1267. },
  1268. insertData: function(offset,text) {
  1269. this.replaceData(offset,0,text);
  1270. },
  1271. appendChild:function(newChild){
  1272. throw new Error(ExceptionMessage[HIERARCHY_REQUEST_ERR])
  1273. },
  1274. deleteData: function(offset, count) {
  1275. this.replaceData(offset,count,"");
  1276. },
  1277. replaceData: function(offset, count, text) {
  1278. var start = this.data.substring(0,offset);
  1279. var end = this.data.substring(offset+count);
  1280. text = start + text + end;
  1281. this.nodeValue = this.data = text;
  1282. this.length = text.length;
  1283. }
  1284. }
  1285. _extends(CharacterData,Node);
  1286. function Text() {
  1287. };
  1288. Text.prototype = {
  1289. nodeName : "#text",
  1290. nodeType : TEXT_NODE,
  1291. splitText : function(offset) {
  1292. var text = this.data;
  1293. var newText = text.substring(offset);
  1294. text = text.substring(0, offset);
  1295. this.data = this.nodeValue = text;
  1296. this.length = text.length;
  1297. var newNode = this.ownerDocument.createTextNode(newText);
  1298. if(this.parentNode){
  1299. this.parentNode.insertBefore(newNode, this.nextSibling);
  1300. }
  1301. return newNode;
  1302. }
  1303. }
  1304. _extends(Text,CharacterData);
  1305. function Comment() {
  1306. };
  1307. Comment.prototype = {
  1308. nodeName : "#comment",
  1309. nodeType : COMMENT_NODE
  1310. }
  1311. _extends(Comment,CharacterData);
  1312. function CDATASection() {
  1313. };
  1314. CDATASection.prototype = {
  1315. nodeName : "#cdata-section",
  1316. nodeType : CDATA_SECTION_NODE
  1317. }
  1318. _extends(CDATASection,CharacterData);
  1319. function DocumentType() {
  1320. };
  1321. DocumentType.prototype.nodeType = DOCUMENT_TYPE_NODE;
  1322. _extends(DocumentType,Node);
  1323. function Notation() {
  1324. };
  1325. Notation.prototype.nodeType = NOTATION_NODE;
  1326. _extends(Notation,Node);
  1327. function Entity() {
  1328. };
  1329. Entity.prototype.nodeType = ENTITY_NODE;
  1330. _extends(Entity,Node);
  1331. function EntityReference() {
  1332. };
  1333. EntityReference.prototype.nodeType = ENTITY_REFERENCE_NODE;
  1334. _extends(EntityReference,Node);
  1335. function DocumentFragment() {
  1336. };
  1337. DocumentFragment.prototype.nodeName = "#document-fragment";
  1338. DocumentFragment.prototype.nodeType = DOCUMENT_FRAGMENT_NODE;
  1339. _extends(DocumentFragment,Node);
  1340. function ProcessingInstruction() {
  1341. }
  1342. ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
  1343. _extends(ProcessingInstruction,Node);
  1344. function XMLSerializer(){}
  1345. XMLSerializer.prototype.serializeToString = function(node,isHtml,nodeFilter){
  1346. return nodeSerializeToString.call(node,isHtml,nodeFilter);
  1347. }
  1348. Node.prototype.toString = nodeSerializeToString;
  1349. function nodeSerializeToString(isHtml,nodeFilter){
  1350. var buf = [];
  1351. var refNode = this.nodeType == 9 && this.documentElement || this;
  1352. var prefix = refNode.prefix;
  1353. var uri = refNode.namespaceURI;
  1354. if(uri && prefix == null){
  1355. //console.log(prefix)
  1356. var prefix = refNode.lookupPrefix(uri);
  1357. if(prefix == null){
  1358. //isHTML = true;
  1359. var visibleNamespaces=[
  1360. {namespace:uri,prefix:null}
  1361. //{namespace:uri,prefix:''}
  1362. ]
  1363. }
  1364. }
  1365. serializeToString(this,buf,isHtml,nodeFilter,visibleNamespaces);
  1366. //console.log('###',this.nodeType,uri,prefix,buf.join(''))
  1367. return buf.join('');
  1368. }
  1369. function needNamespaceDefine(node, isHTML, visibleNamespaces) {
  1370. var prefix = node.prefix || '';
  1371. var uri = node.namespaceURI;
  1372. // According to [Namespaces in XML 1.0](https://www.w3.org/TR/REC-xml-names/#ns-using) ,
  1373. // and more specifically https://www.w3.org/TR/REC-xml-names/#nsc-NoPrefixUndecl :
  1374. // > In a namespace declaration for a prefix [...], the attribute value MUST NOT be empty.
  1375. // in a similar manner [Namespaces in XML 1.1](https://www.w3.org/TR/xml-names11/#ns-using)
  1376. // and more specifically https://www.w3.org/TR/xml-names11/#nsc-NSDeclared :
  1377. // > [...] Furthermore, the attribute value [...] must not be an empty string.
  1378. // so serializing empty namespace value like xmlns:ds="" would produce an invalid XML document.
  1379. if (!uri) {
  1380. return false;
  1381. }
  1382. if (prefix === "xml" && uri === NAMESPACE.XML || uri === NAMESPACE.XMLNS) {
  1383. return false;
  1384. }
  1385. var i = visibleNamespaces.length
  1386. while (i--) {
  1387. var ns = visibleNamespaces[i];
  1388. // get namespace prefix
  1389. if (ns.prefix === prefix) {
  1390. return ns.namespace !== uri;
  1391. }
  1392. }
  1393. return true;
  1394. }
  1395. /**
  1396. * Well-formed constraint: No < in Attribute Values
  1397. * > The replacement text of any entity referred to directly or indirectly
  1398. * > in an attribute value must not contain a <.
  1399. * @see https://www.w3.org/TR/xml11/#CleanAttrVals
  1400. * @see https://www.w3.org/TR/xml11/#NT-AttValue
  1401. *
  1402. * Literal whitespace other than space that appear in attribute values
  1403. * are serialized as their entity references, so they will be preserved.
  1404. * (In contrast to whitespace literals in the input which are normalized to spaces)
  1405. * @see https://www.w3.org/TR/xml11/#AVNormalize
  1406. * @see https://w3c.github.io/DOM-Parsing/#serializing-an-element-s-attributes
  1407. */
  1408. function addSerializedAttribute(buf, qualifiedName, value) {
  1409. buf.push(' ', qualifiedName, '="', value.replace(/[<>&"\t\n\r]/g, _xmlEncoder), '"')
  1410. }
  1411. function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
  1412. if (!visibleNamespaces) {
  1413. visibleNamespaces = [];
  1414. }
  1415. if(nodeFilter){
  1416. node = nodeFilter(node);
  1417. if(node){
  1418. if(typeof node == 'string'){
  1419. buf.push(node);
  1420. return;
  1421. }
  1422. }else{
  1423. return;
  1424. }
  1425. //buf.sort.apply(attrs, attributeSorter);
  1426. }
  1427. switch(node.nodeType){
  1428. case ELEMENT_NODE:
  1429. var attrs = node.attributes;
  1430. var len = attrs.length;
  1431. var child = node.firstChild;
  1432. var nodeName = node.tagName;
  1433. isHTML = NAMESPACE.isHTML(node.namespaceURI) || isHTML
  1434. var prefixedNodeName = nodeName
  1435. if (!isHTML && !node.prefix && node.namespaceURI) {
  1436. var defaultNS
  1437. // lookup current default ns from `xmlns` attribute
  1438. for (var ai = 0; ai < attrs.length; ai++) {
  1439. if (attrs.item(ai).name === 'xmlns') {
  1440. defaultNS = attrs.item(ai).value
  1441. break
  1442. }
  1443. }
  1444. if (!defaultNS) {
  1445. // lookup current default ns in visibleNamespaces
  1446. for (var nsi = visibleNamespaces.length - 1; nsi >= 0; nsi--) {
  1447. var namespace = visibleNamespaces[nsi]
  1448. if (namespace.prefix === '' && namespace.namespace === node.namespaceURI) {
  1449. defaultNS = namespace.namespace
  1450. break
  1451. }
  1452. }
  1453. }
  1454. if (defaultNS !== node.namespaceURI) {
  1455. for (var nsi = visibleNamespaces.length - 1; nsi >= 0; nsi--) {
  1456. var namespace = visibleNamespaces[nsi]
  1457. if (namespace.namespace === node.namespaceURI) {
  1458. if (namespace.prefix) {
  1459. prefixedNodeName = namespace.prefix + ':' + nodeName
  1460. }
  1461. break
  1462. }
  1463. }
  1464. }
  1465. }
  1466. buf.push('<', prefixedNodeName);
  1467. for(var i=0;i<len;i++){
  1468. // add namespaces for attributes
  1469. var attr = attrs.item(i);
  1470. if (attr.prefix == 'xmlns') {
  1471. visibleNamespaces.push({ prefix: attr.localName, namespace: attr.value });
  1472. }else if(attr.nodeName == 'xmlns'){
  1473. visibleNamespaces.push({ prefix: '', namespace: attr.value });
  1474. }
  1475. }
  1476. for(var i=0;i<len;i++){
  1477. var attr = attrs.item(i);
  1478. if (needNamespaceDefine(attr,isHTML, visibleNamespaces)) {
  1479. var prefix = attr.prefix||'';
  1480. var uri = attr.namespaceURI;
  1481. addSerializedAttribute(buf, prefix ? 'xmlns:' + prefix : "xmlns", uri);
  1482. visibleNamespaces.push({ prefix: prefix, namespace:uri });
  1483. }
  1484. serializeToString(attr,buf,isHTML,nodeFilter,visibleNamespaces);
  1485. }
  1486. // add namespace for current node
  1487. if (nodeName === prefixedNodeName && needNamespaceDefine(node, isHTML, visibleNamespaces)) {
  1488. var prefix = node.prefix||'';
  1489. var uri = node.namespaceURI;
  1490. addSerializedAttribute(buf, prefix ? 'xmlns:' + prefix : "xmlns", uri);
  1491. visibleNamespaces.push({ prefix: prefix, namespace:uri });
  1492. }
  1493. if(child || isHTML && !/^(?:meta|link|img|br|hr|input)$/i.test(nodeName)){
  1494. buf.push('>');
  1495. //if is cdata child node
  1496. if(isHTML && /^script$/i.test(nodeName)){
  1497. while(child){
  1498. if(child.data){
  1499. buf.push(child.data);
  1500. }else{
  1501. serializeToString(child, buf, isHTML, nodeFilter, visibleNamespaces.slice());
  1502. }
  1503. child = child.nextSibling;
  1504. }
  1505. }else
  1506. {
  1507. while(child){
  1508. serializeToString(child, buf, isHTML, nodeFilter, visibleNamespaces.slice());
  1509. child = child.nextSibling;
  1510. }
  1511. }
  1512. buf.push('</',prefixedNodeName,'>');
  1513. }else{
  1514. buf.push('/>');
  1515. }
  1516. // remove added visible namespaces
  1517. //visibleNamespaces.length = startVisibleNamespaces;
  1518. return;
  1519. case DOCUMENT_NODE:
  1520. case DOCUMENT_FRAGMENT_NODE:
  1521. var child = node.firstChild;
  1522. while(child){
  1523. serializeToString(child, buf, isHTML, nodeFilter, visibleNamespaces.slice());
  1524. child = child.nextSibling;
  1525. }
  1526. return;
  1527. case ATTRIBUTE_NODE:
  1528. return addSerializedAttribute(buf, node.name, node.value);
  1529. case TEXT_NODE:
  1530. /**
  1531. * The ampersand character (&) and the left angle bracket (<) must not appear in their literal form,
  1532. * except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section.
  1533. * If they are needed elsewhere, they must be escaped using either numeric character references or the strings
  1534. * `&amp;` and `&lt;` respectively.
  1535. * The right angle bracket (>) may be represented using the string " &gt; ", and must, for compatibility,
  1536. * be escaped using either `&gt;` or a character reference when it appears in the string `]]>` in content,
  1537. * when that string is not marking the end of a CDATA section.
  1538. *
  1539. * In the content of elements, character data is any string of characters
  1540. * which does not contain the start-delimiter of any markup
  1541. * and does not include the CDATA-section-close delimiter, `]]>`.
  1542. *
  1543. * @see https://www.w3.org/TR/xml/#NT-CharData
  1544. * @see https://w3c.github.io/DOM-Parsing/#xml-serializing-a-text-node
  1545. */
  1546. return buf.push(node.data
  1547. .replace(/[<&>]/g,_xmlEncoder)
  1548. );
  1549. case CDATA_SECTION_NODE:
  1550. return buf.push( '<![CDATA[',node.data,']]>');
  1551. case COMMENT_NODE:
  1552. return buf.push( "<!--",node.data,"-->");
  1553. case DOCUMENT_TYPE_NODE:
  1554. var pubid = node.publicId;
  1555. var sysid = node.systemId;
  1556. buf.push('<!DOCTYPE ',node.name);
  1557. if(pubid){
  1558. buf.push(' PUBLIC ', pubid);
  1559. if (sysid && sysid!='.') {
  1560. buf.push(' ', sysid);
  1561. }
  1562. buf.push('>');
  1563. }else if(sysid && sysid!='.'){
  1564. buf.push(' SYSTEM ', sysid, '>');
  1565. }else{
  1566. var sub = node.internalSubset;
  1567. if(sub){
  1568. buf.push(" [",sub,"]");
  1569. }
  1570. buf.push(">");
  1571. }
  1572. return;
  1573. case PROCESSING_INSTRUCTION_NODE:
  1574. return buf.push( "<?",node.target," ",node.data,"?>");
  1575. case ENTITY_REFERENCE_NODE:
  1576. return buf.push( '&',node.nodeName,';');
  1577. //case ENTITY_NODE:
  1578. //case NOTATION_NODE:
  1579. default:
  1580. buf.push('??',node.nodeName);
  1581. }
  1582. }
  1583. function importNode(doc,node,deep){
  1584. var node2;
  1585. switch (node.nodeType) {
  1586. case ELEMENT_NODE:
  1587. node2 = node.cloneNode(false);
  1588. node2.ownerDocument = doc;
  1589. //var attrs = node2.attributes;
  1590. //var len = attrs.length;
  1591. //for(var i=0;i<len;i++){
  1592. //node2.setAttributeNodeNS(importNode(doc,attrs.item(i),deep));
  1593. //}
  1594. case DOCUMENT_FRAGMENT_NODE:
  1595. break;
  1596. case ATTRIBUTE_NODE:
  1597. deep = true;
  1598. break;
  1599. //case ENTITY_REFERENCE_NODE:
  1600. //case PROCESSING_INSTRUCTION_NODE:
  1601. ////case TEXT_NODE:
  1602. //case CDATA_SECTION_NODE:
  1603. //case COMMENT_NODE:
  1604. // deep = false;
  1605. // break;
  1606. //case DOCUMENT_NODE:
  1607. //case DOCUMENT_TYPE_NODE:
  1608. //cannot be imported.
  1609. //case ENTITY_NODE:
  1610. //case NOTATION_NODE:
  1611. //can not hit in level3
  1612. //default:throw e;
  1613. }
  1614. if(!node2){
  1615. node2 = node.cloneNode(false);//false
  1616. }
  1617. node2.ownerDocument = doc;
  1618. node2.parentNode = null;
  1619. if(deep){
  1620. var child = node.firstChild;
  1621. while(child){
  1622. node2.appendChild(importNode(doc,child,deep));
  1623. child = child.nextSibling;
  1624. }
  1625. }
  1626. return node2;
  1627. }
  1628. //
  1629. //var _relationMap = {firstChild:1,lastChild:1,previousSibling:1,nextSibling:1,
  1630. // attributes:1,childNodes:1,parentNode:1,documentElement:1,doctype,};
  1631. function cloneNode(doc,node,deep){
  1632. var node2 = new node.constructor();
  1633. for (var n in node) {
  1634. if (Object.prototype.hasOwnProperty.call(node, n)) {
  1635. var v = node[n];
  1636. if (typeof v != "object") {
  1637. if (v != node2[n]) {
  1638. node2[n] = v;
  1639. }
  1640. }
  1641. }
  1642. }
  1643. if(node.childNodes){
  1644. node2.childNodes = new NodeList();
  1645. }
  1646. node2.ownerDocument = doc;
  1647. switch (node2.nodeType) {
  1648. case ELEMENT_NODE:
  1649. var attrs = node.attributes;
  1650. var attrs2 = node2.attributes = new NamedNodeMap();
  1651. var len = attrs.length
  1652. attrs2._ownerElement = node2;
  1653. for(var i=0;i<len;i++){
  1654. node2.setAttributeNode(cloneNode(doc,attrs.item(i),true));
  1655. }
  1656. break;;
  1657. case ATTRIBUTE_NODE:
  1658. deep = true;
  1659. }
  1660. if(deep){
  1661. var child = node.firstChild;
  1662. while(child){
  1663. node2.appendChild(cloneNode(doc,child,deep));
  1664. child = child.nextSibling;
  1665. }
  1666. }
  1667. return node2;
  1668. }
  1669. function __set__(object,key,value){
  1670. object[key] = value
  1671. }
  1672. //do dynamic
  1673. try{
  1674. if(Object.defineProperty){
  1675. Object.defineProperty(LiveNodeList.prototype,'length',{
  1676. get:function(){
  1677. _updateLiveList(this);
  1678. return this.$$length;
  1679. }
  1680. });
  1681. Object.defineProperty(Node.prototype,'textContent',{
  1682. get:function(){
  1683. return getTextContent(this);
  1684. },
  1685. set:function(data){
  1686. switch(this.nodeType){
  1687. case ELEMENT_NODE:
  1688. case DOCUMENT_FRAGMENT_NODE:
  1689. while(this.firstChild){
  1690. this.removeChild(this.firstChild);
  1691. }
  1692. if(data || String(data)){
  1693. this.appendChild(this.ownerDocument.createTextNode(data));
  1694. }
  1695. break;
  1696. default:
  1697. this.data = data;
  1698. this.value = data;
  1699. this.nodeValue = data;
  1700. }
  1701. }
  1702. })
  1703. function getTextContent(node){
  1704. switch(node.nodeType){
  1705. case ELEMENT_NODE:
  1706. case DOCUMENT_FRAGMENT_NODE:
  1707. var buf = [];
  1708. node = node.firstChild;
  1709. while(node){
  1710. if(node.nodeType!==7 && node.nodeType !==8){
  1711. buf.push(getTextContent(node));
  1712. }
  1713. node = node.nextSibling;
  1714. }
  1715. return buf.join('');
  1716. default:
  1717. return node.nodeValue;
  1718. }
  1719. }
  1720. __set__ = function(object,key,value){
  1721. //console.log(value)
  1722. object['$$'+key] = value
  1723. }
  1724. }
  1725. }catch(e){//ie8
  1726. }
  1727. //if(typeof require == 'function'){
  1728. exports.DocumentType = DocumentType;
  1729. exports.DOMException = DOMException;
  1730. exports.DOMImplementation = DOMImplementation;
  1731. exports.Element = Element;
  1732. exports.Node = Node;
  1733. exports.NodeList = NodeList;
  1734. exports.XMLSerializer = XMLSerializer;
  1735. //}