dom.js 56 KB

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