domparserinone.js 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  1. var entityMap = {
  2. lt: '<',
  3. gt: '>',
  4. amp: '&',
  5. quot: '"',
  6. apos: "'",
  7. Agrave: "À",
  8. Aacute: "Á",
  9. Acirc: "Â",
  10. Atilde: "Ã",
  11. Auml: "Ä",
  12. Aring: "Å",
  13. AElig: "Æ",
  14. Ccedil: "Ç",
  15. Egrave: "È",
  16. Eacute: "É",
  17. Ecirc: "Ê",
  18. Euml: "Ë",
  19. Igrave: "Ì",
  20. Iacute: "Í",
  21. Icirc: "Î",
  22. Iuml: "Ï",
  23. ETH: "Ð",
  24. Ntilde: "Ñ",
  25. Ograve: "Ò",
  26. Oacute: "Ó",
  27. Ocirc: "Ô",
  28. Otilde: "Õ",
  29. Ouml: "Ö",
  30. Oslash: "Ø",
  31. Ugrave: "Ù",
  32. Uacute: "Ú",
  33. Ucirc: "Û",
  34. Uuml: "Ü",
  35. Yacute: "Ý",
  36. THORN: "Þ",
  37. szlig: "ß",
  38. agrave: "à",
  39. aacute: "á",
  40. acirc: "â",
  41. atilde: "ã",
  42. auml: "ä",
  43. aring: "å",
  44. aelig: "æ",
  45. ccedil: "ç",
  46. egrave: "è",
  47. eacute: "é",
  48. ecirc: "ê",
  49. euml: "ë",
  50. igrave: "ì",
  51. iacute: "í",
  52. icirc: "î",
  53. iuml: "ï",
  54. eth: "ð",
  55. ntilde: "ñ",
  56. ograve: "ò",
  57. oacute: "ó",
  58. ocirc: "ô",
  59. otilde: "õ",
  60. ouml: "ö",
  61. oslash: "ø",
  62. ugrave: "ù",
  63. uacute: "ú",
  64. ucirc: "û",
  65. uuml: "ü",
  66. yacute: "ý",
  67. thorn: "þ",
  68. yuml: "ÿ",
  69. nbsp: " ",
  70. iexcl: "¡",
  71. cent: "¢",
  72. pound: "£",
  73. curren: "¤",
  74. yen: "¥",
  75. brvbar: "¦",
  76. sect: "§",
  77. uml: "¨",
  78. copy: "©",
  79. ordf: "ª",
  80. laquo: "«",
  81. not: "¬",
  82. shy: "­­",
  83. reg: "®",
  84. macr: "¯",
  85. deg: "°",
  86. plusmn: "±",
  87. sup2: "²",
  88. sup3: "³",
  89. acute: "´",
  90. micro: "µ",
  91. para: "¶",
  92. middot: "·",
  93. cedil: "¸",
  94. sup1: "¹",
  95. ordm: "º",
  96. raquo: "»",
  97. frac14: "¼",
  98. frac12: "½",
  99. frac34: "¾",
  100. iquest: "¿",
  101. times: "×",
  102. divide: "÷",
  103. forall: "∀",
  104. part: "∂",
  105. exist: "∃",
  106. empty: "∅",
  107. nabla: "∇",
  108. isin: "∈",
  109. notin: "∉",
  110. ni: "∋",
  111. prod: "∏",
  112. sum: "∑",
  113. minus: "−",
  114. lowast: "∗",
  115. radic: "√",
  116. prop: "∝",
  117. infin: "∞",
  118. ang: "∠",
  119. and: "∧",
  120. or: "∨",
  121. cap: "∩",
  122. cup: "∪",
  123. 'int': "∫",
  124. there4: "∴",
  125. sim: "∼",
  126. cong: "≅",
  127. asymp: "≈",
  128. ne: "≠",
  129. equiv: "≡",
  130. le: "≤",
  131. ge: "≥",
  132. sub: "⊂",
  133. sup: "⊃",
  134. nsub: "⊄",
  135. sube: "⊆",
  136. supe: "⊇",
  137. oplus: "⊕",
  138. otimes: "⊗",
  139. perp: "⊥",
  140. sdot: "⋅",
  141. Alpha: "Α",
  142. Beta: "Β",
  143. Gamma: "Γ",
  144. Delta: "Δ",
  145. Epsilon: "Ε",
  146. Zeta: "Ζ",
  147. Eta: "Η",
  148. Theta: "Θ",
  149. Iota: "Ι",
  150. Kappa: "Κ",
  151. Lambda: "Λ",
  152. Mu: "Μ",
  153. Nu: "Ν",
  154. Xi: "Ξ",
  155. Omicron: "Ο",
  156. Pi: "Π",
  157. Rho: "Ρ",
  158. Sigma: "Σ",
  159. Tau: "Τ",
  160. Upsilon: "Υ",
  161. Phi: "Φ",
  162. Chi: "Χ",
  163. Psi: "Ψ",
  164. Omega: "Ω",
  165. alpha: "α",
  166. beta: "β",
  167. gamma: "γ",
  168. delta: "δ",
  169. epsilon: "ε",
  170. zeta: "ζ",
  171. eta: "η",
  172. theta: "θ",
  173. iota: "ι",
  174. kappa: "κ",
  175. lambda: "λ",
  176. mu: "μ",
  177. nu: "ν",
  178. xi: "ξ",
  179. omicron: "ο",
  180. pi: "π",
  181. rho: "ρ",
  182. sigmaf: "ς",
  183. sigma: "σ",
  184. tau: "τ",
  185. upsilon: "υ",
  186. phi: "φ",
  187. chi: "χ",
  188. psi: "ψ",
  189. omega: "ω",
  190. thetasym: "ϑ",
  191. upsih: "ϒ",
  192. piv: "ϖ",
  193. OElig: "Œ",
  194. oelig: "œ",
  195. Scaron: "Š",
  196. scaron: "š",
  197. Yuml: "Ÿ",
  198. fnof: "ƒ",
  199. circ: "ˆ",
  200. tilde: "˜",
  201. ensp: " ",
  202. emsp: " ",
  203. thinsp: " ",
  204. zwnj: "‌",
  205. zwj: "‍",
  206. lrm: "‎",
  207. rlm: "‏",
  208. ndash: "–",
  209. mdash: "—",
  210. lsquo: "‘",
  211. rsquo: "’",
  212. sbquo: "‚",
  213. ldquo: "“",
  214. rdquo: "”",
  215. bdquo: "„",
  216. dagger: "†",
  217. Dagger: "‡",
  218. bull: "•",
  219. hellip: "…",
  220. permil: "‰",
  221. prime: "′",
  222. Prime: "″",
  223. lsaquo: "‹",
  224. rsaquo: "›",
  225. oline: "‾",
  226. euro: "€",
  227. trade: "™",
  228. larr: "←",
  229. uarr: "↑",
  230. rarr: "→",
  231. darr: "↓",
  232. harr: "↔",
  233. crarr: "↵",
  234. lceil: "⌈",
  235. rceil: "⌉",
  236. lfloor: "⌊",
  237. rfloor: "⌋",
  238. loz: "◊",
  239. spades: "♠",
  240. clubs: "♣",
  241. hearts: "♥",
  242. diams: "♦"
  243. };
  244. //for(var n in exports.entityMap){console.log(exports.entityMap[n].charCodeAt())}
  245. //[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
  246. //[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
  247. //[5] Name ::= NameStartChar (NameChar)*
  248. var nameStartChar = /[A-Z_a-z\xC0-\xD6\xD8-\xF6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]///\u10000-\uEFFFF
  249. var nameChar = new RegExp("[\\-\\.0-9"+nameStartChar.source.slice(1,-1)+"\\u00B7\\u0300-\\u036F\\u203F-\\u2040]");
  250. var tagNamePattern = new RegExp('^'+nameStartChar.source+nameChar.source+'*(?:\:'+nameStartChar.source+nameChar.source+'*)?$');
  251. //var tagNamePattern = /^[a-zA-Z_][\w\-\.]*(?:\:[a-zA-Z_][\w\-\.]*)?$/
  252. //var handlers = 'resolveEntity,getExternalSubset,characters,endDocument,endElement,endPrefixMapping,ignorableWhitespace,processingInstruction,setDocumentLocator,skippedEntity,startDocument,startElement,startPrefixMapping,notationDecl,unparsedEntityDecl,error,fatalError,warning,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,comment,endCDATA,endDTD,endEntity,startCDATA,startDTD,startEntity'.split(',')
  253. //S_TAG, S_ATTR, S_EQ, S_ATTR_NOQUOT_VALUE
  254. //S_ATTR_SPACE, S_ATTR_END, S_TAG_SPACE, S_TAG_CLOSE
  255. var S_TAG = 0;//tag name offerring
  256. var S_ATTR = 1;//attr name offerring
  257. var S_ATTR_SPACE=2;//attr name end and space offer
  258. var S_EQ = 3;//=space?
  259. var S_ATTR_NOQUOT_VALUE = 4;//attr value(no quot value only)
  260. var S_ATTR_END = 5;//attr value end and no space(quot end)
  261. var S_TAG_SPACE = 6;//(attr value end || tag end ) && (space offer)
  262. var S_TAG_CLOSE = 7;//closed el<el />
  263. SaxO={};
  264. function XMLReader(){
  265. }
  266. XMLReader.prototype = {
  267. parse:function(source,defaultNSMap,entityMap){
  268. var domBuilder = this.domBuilder;
  269. domBuilder.startDocument();
  270. _copy(defaultNSMap ,defaultNSMap = {})
  271. parse(source,defaultNSMap,entityMap,
  272. domBuilder,this.errorHandler);
  273. domBuilder.endDocument();
  274. }
  275. }
  276. function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
  277. function fixedFromCharCode(code) {
  278. // String.prototype.fromCharCode does not supports
  279. // > 2 bytes unicode chars directly
  280. if (code > 0xffff) {
  281. code -= 0x10000;
  282. var surrogate1 = 0xd800 + (code >> 10)
  283. , surrogate2 = 0xdc00 + (code & 0x3ff);
  284. return String.fromCharCode(surrogate1, surrogate2);
  285. } else {
  286. return String.fromCharCode(code);
  287. }
  288. }
  289. function entityReplacer(a){
  290. var k = a.slice(1,-1);
  291. if(k in entityMap){
  292. return entityMap[k];
  293. }else if(k.charAt(0) === '#'){
  294. return fixedFromCharCode(parseInt(k.substr(1).replace('x','0x')))
  295. }else{
  296. errorHandler.error('entity not found:'+a);
  297. return a;
  298. }
  299. }
  300. function appendText(end){//has some bugs
  301. if(end>start){
  302. var xt = source.substring(start,end).replace(/&#?\w+;/g,entityReplacer);
  303. locator&&position(start);
  304. domBuilder.characters(xt,0,end-start);
  305. start = end
  306. }
  307. }
  308. function position(p,m){
  309. while(p>=lineEnd && (m = linePattern.exec(source))){
  310. lineStart = m.index;
  311. lineEnd = lineStart + m[0].length;
  312. locator.lineNumber++;
  313. //console.log('line++:',locator,startPos,endPos)
  314. }
  315. locator.columnNumber = p-lineStart+1;
  316. }
  317. var lineStart = 0;
  318. var lineEnd = 0;
  319. var linePattern = /.*(?:\r\n?|\n)|.*$/g
  320. var locator = domBuilder.locator;
  321. var parseStack = [{currentNSMap:defaultNSMapCopy}]
  322. var closeMap = {};
  323. var start = 0;
  324. while(true){
  325. try{
  326. var tagStart = source.indexOf('<',start);
  327. if(tagStart<0){
  328. if(!source.substr(start).match(/^\s*$/)){
  329. var doc = domBuilder.doc;
  330. var text = doc.createTextNode(source.substr(start));
  331. doc.appendChild(text);
  332. domBuilder.currentElement = text;
  333. }
  334. return;
  335. }
  336. if(tagStart>start){
  337. appendText(tagStart);
  338. }
  339. switch(source.charAt(tagStart+1)){
  340. case '/':
  341. var end = source.indexOf('>',tagStart+3);
  342. var tagName = source.substring(tagStart+2,end);
  343. var config = parseStack.pop();
  344. if(end<0){
  345. tagName = source.substring(tagStart+2).replace(/[\s<].*/,'');
  346. //console.error('#@@@@@@'+tagName)
  347. errorHandler.error("end tag name: "+tagName+' is not complete:'+config.tagName);
  348. end = tagStart+1+tagName.length;
  349. }else if(tagName.match(/\s</)){
  350. tagName = tagName.replace(/[\s<].*/,'');
  351. errorHandler.error("end tag name: "+tagName+' maybe not complete');
  352. end = tagStart+1+tagName.length;
  353. }
  354. //console.error(parseStack.length,parseStack)
  355. //console.error(config);
  356. var localNSMap = config.localNSMap;
  357. var endMatch = config.tagName == tagName;
  358. var endIgnoreCaseMach = endMatch || config.tagName&&config.tagName.toLowerCase() == tagName.toLowerCase()
  359. if(endIgnoreCaseMach){
  360. domBuilder.endElement(config.uri,config.localName,tagName);
  361. if(localNSMap){
  362. for(var prefix in localNSMap){
  363. domBuilder.endPrefixMapping(prefix) ;
  364. }
  365. }
  366. if(!endMatch){
  367. errorHandler.fatalError("end tag name: "+tagName+' is not match the current start tagName:'+config.tagName );
  368. }
  369. }else{
  370. parseStack.push(config)
  371. }
  372. end++;
  373. break;
  374. // end elment
  375. case '?':// <?...?>
  376. locator&&position(tagStart);
  377. end = parseInstruction(source,tagStart,domBuilder);
  378. break;
  379. case '!':// <!doctype,<![CDATA,<!--
  380. locator&&position(tagStart);
  381. end = parseDCC(source,tagStart,domBuilder,errorHandler);
  382. break;
  383. default:
  384. locator&&position(tagStart);
  385. var el = new ElementAttributes();
  386. var currentNSMap = parseStack[parseStack.length-1].currentNSMap;
  387. //elStartEnd
  388. var end = parseElementStartPart(source,tagStart,el,currentNSMap,entityReplacer,errorHandler);
  389. var len = el.length;
  390. if(!el.closed && fixSelfClosed(source,end,el.tagName,closeMap)){
  391. el.closed = true;
  392. if(!entityMap.nbsp){
  393. errorHandler.warning('unclosed xml attribute');
  394. }
  395. }
  396. if(locator && len){
  397. var locator2 = copyLocator(locator,{});
  398. //try{//attribute position fixed
  399. for(var i = 0;i<len;i++){
  400. var a = el[i];
  401. position(a.offset);
  402. a.locator = copyLocator(locator,{});
  403. }
  404. //}catch(e){console.error('@@@@@'+e)}
  405. domBuilder.locator = locator2
  406. if(SaxO.appendElement(el,domBuilder,currentNSMap)){
  407. parseStack.push(el)
  408. }
  409. domBuilder.locator = locator;
  410. }else{
  411. if(SaxO.appendElement(el,domBuilder,currentNSMap)){
  412. parseStack.push(el)
  413. }
  414. }
  415. if(el.uri === 'http://www.w3.org/1999/xhtml' && !el.closed){
  416. end = parseHtmlSpecialContent(source,end,el.tagName,entityReplacer,domBuilder)
  417. }else{
  418. end++;
  419. }
  420. }
  421. }catch(e){
  422. errorHandler.error('element parse error: '+e)
  423. //errorHandler.error('element parse error: '+e);
  424. end = -1;
  425. //throw e;
  426. }
  427. if(end>start){
  428. start = end;
  429. }else{
  430. //TODO: 这里有可能sax回退,有位置错误风险
  431. appendText(Math.max(tagStart,start)+1);
  432. }
  433. }
  434. }
  435. function copyLocator(f,t){
  436. t.lineNumber = f.lineNumber;
  437. t.columnNumber = f.columnNumber;
  438. return t;
  439. }
  440. /**
  441. * @see #appendElement(source,elStartEnd,el,selfClosed,entityReplacer,domBuilder,parseStack);
  442. * @return end of the elementStartPart(end of elementEndPart for selfClosed el)
  443. */
  444. function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,errorHandler){
  445. var attrName;
  446. var value;
  447. var p = ++start;
  448. var s = S_TAG;//status
  449. while(true){
  450. var c = source.charAt(p);
  451. switch(c){
  452. case '=':
  453. if(s === S_ATTR){//attrName
  454. attrName = source.slice(start,p);
  455. s = S_EQ;
  456. }else if(s === S_ATTR_SPACE){
  457. s = S_EQ;
  458. }else{
  459. //fatalError: equal must after attrName or space after attrName
  460. throw new Error('attribute equal must after attrName');
  461. }
  462. break;
  463. case '\'':
  464. case '"':
  465. if(s === S_EQ || s === S_ATTR //|| s == S_ATTR_SPACE
  466. ){//equal
  467. if(s === S_ATTR){
  468. errorHandler.warning('attribute value must after "="')
  469. attrName = source.slice(start,p)
  470. }
  471. start = p+1;
  472. p = source.indexOf(c,start)
  473. if(p>0){
  474. value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
  475. el.add(attrName,value,start-1);
  476. s = S_ATTR_END;
  477. }else{
  478. //fatalError: no end quot match
  479. throw new Error('attribute value no end \''+c+'\' match');
  480. }
  481. }else if(s == S_ATTR_NOQUOT_VALUE){
  482. value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
  483. //console.log(attrName,value,start,p)
  484. el.add(attrName,value,start);
  485. //console.dir(el)
  486. errorHandler.warning('attribute "'+attrName+'" missed start quot('+c+')!!');
  487. start = p+1;
  488. s = S_ATTR_END
  489. }else{
  490. //fatalError: no equal before
  491. throw new Error('attribute value must after "="');
  492. }
  493. break;
  494. case '/':
  495. switch(s){
  496. case S_TAG:
  497. el.setTagName(source.slice(start,p));
  498. case S_ATTR_END:
  499. case S_TAG_SPACE:
  500. case S_TAG_CLOSE:
  501. s =S_TAG_CLOSE;
  502. el.closed = true;
  503. case S_ATTR_NOQUOT_VALUE:
  504. case S_ATTR:
  505. case S_ATTR_SPACE:
  506. break;
  507. //case S_EQ:
  508. default:
  509. throw new Error("attribute invalid close char('/')")
  510. }
  511. break;
  512. case ''://end document
  513. //throw new Error('unexpected end of input')
  514. errorHandler.error('unexpected end of input');
  515. if(s == S_TAG){
  516. el.setTagName(source.slice(start,p));
  517. }
  518. return p;
  519. case '>':
  520. switch(s){
  521. case S_TAG:
  522. el.setTagName(source.slice(start,p));
  523. case S_ATTR_END:
  524. case S_TAG_SPACE:
  525. case S_TAG_CLOSE:
  526. break;//normal
  527. case S_ATTR_NOQUOT_VALUE://Compatible state
  528. case S_ATTR:
  529. value = source.slice(start,p);
  530. if(value.slice(-1) === '/'){
  531. el.closed = true;
  532. value = value.slice(0,-1)
  533. }
  534. case S_ATTR_SPACE:
  535. if(s === S_ATTR_SPACE){
  536. value = attrName;
  537. }
  538. if(s == S_ATTR_NOQUOT_VALUE){
  539. errorHandler.warning('attribute "'+value+'" missed quot(")!!');
  540. el.add(attrName,value.replace(/&#?\w+;/g,entityReplacer),start)
  541. }else{
  542. if(currentNSMap[''] !== 'http://www.w3.org/1999/xhtml' || !value.match(/^(?:disabled|checked|selected)$/i)){
  543. errorHandler.warning('attribute "'+value+'" missed value!! "'+value+'" instead!!')
  544. }
  545. el.add(value,value,start)
  546. }
  547. break;
  548. case S_EQ:
  549. throw new Error('attribute value missed!!');
  550. }
  551. // console.log(tagName,tagNamePattern,tagNamePattern.test(tagName))
  552. return p;
  553. /*xml space '\x20' | #x9 | #xD | #xA; */
  554. case '\u0080':
  555. c = ' ';
  556. default:
  557. if(c<= ' '){//space
  558. switch(s){
  559. case S_TAG:
  560. el.setTagName(source.slice(start,p));//tagName
  561. s = S_TAG_SPACE;
  562. break;
  563. case S_ATTR:
  564. attrName = source.slice(start,p)
  565. s = S_ATTR_SPACE;
  566. break;
  567. case S_ATTR_NOQUOT_VALUE:
  568. var value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
  569. errorHandler.warning('attribute "'+value+'" missed quot(")!!');
  570. el.add(attrName,value,start)
  571. case S_ATTR_END:
  572. s = S_TAG_SPACE;
  573. break;
  574. //case S_TAG_SPACE:
  575. //case S_EQ:
  576. //case S_ATTR_SPACE:
  577. // void();break;
  578. //case S_TAG_CLOSE:
  579. //ignore warning
  580. }
  581. }else{//not space
  582. //S_TAG, S_ATTR, S_EQ, S_ATTR_NOQUOT_VALUE
  583. //S_ATTR_SPACE, S_ATTR_END, S_TAG_SPACE, S_TAG_CLOSE
  584. switch(s){
  585. //case S_TAG:void();break;
  586. //case S_ATTR:void();break;
  587. //case S_ATTR_NOQUOT_VALUE:void();break;
  588. case S_ATTR_SPACE:
  589. var tagName = el.tagName;
  590. if(currentNSMap[''] !== 'http://www.w3.org/1999/xhtml' || !attrName.match(/^(?:disabled|checked|selected)$/i)){
  591. errorHandler.warning('attribute "'+attrName+'" missed value!! "'+attrName+'" instead2!!')
  592. }
  593. el.add(attrName,attrName,start);
  594. start = p;
  595. s = S_ATTR;
  596. break;
  597. case S_ATTR_END:
  598. errorHandler.warning('attribute space is required"'+attrName+'"!!')
  599. case S_TAG_SPACE:
  600. s = S_ATTR;
  601. start = p;
  602. break;
  603. case S_EQ:
  604. s = S_ATTR_NOQUOT_VALUE;
  605. start = p;
  606. break;
  607. case S_TAG_CLOSE:
  608. throw new Error("elements closed character '/' and '>' must be connected to");
  609. }
  610. }
  611. }//end outer switch
  612. //console.log('p++',p)
  613. p++;
  614. }
  615. }
  616. /**
  617. * @return true if has new namespace define
  618. */
  619. SaxO.appendElement=function appendElement(el,domBuilder,currentNSMap){
  620. var tagName = el.tagName;
  621. var localNSMap = null;
  622. //var currentNSMap = parseStack[parseStack.length-1].currentNSMap;
  623. var i = el.length;
  624. while(i--){
  625. var a = el[i];
  626. var qName = a.qName;
  627. var value = a.value;
  628. var nsp = qName.indexOf(':');
  629. if(nsp>0){
  630. var prefix = a.prefix = qName.slice(0,nsp);
  631. var localName = qName.slice(nsp+1);
  632. var nsPrefix = prefix === 'xmlns' && localName
  633. }else{
  634. localName = qName;
  635. prefix = null
  636. nsPrefix = qName === 'xmlns' && ''
  637. }
  638. //can not set prefix,because prefix !== ''
  639. a.localName = localName ;
  640. //prefix == null for no ns prefix attribute
  641. if(nsPrefix !== false){//hack!!
  642. if(localNSMap == null){
  643. localNSMap = {}
  644. //console.log(currentNSMap,0)
  645. _copy(currentNSMap,currentNSMap={})
  646. //console.log(currentNSMap,1)
  647. }
  648. currentNSMap[nsPrefix] = localNSMap[nsPrefix] = value;
  649. a.uri = 'http://www.w3.org/2000/xmlns/'
  650. domBuilder.startPrefixMapping(nsPrefix, value)
  651. }
  652. }
  653. var i = el.length;
  654. while(i--){
  655. a = el[i];
  656. var prefix = a.prefix;
  657. if(prefix){//no prefix attribute has no namespace
  658. if(prefix === 'xml'){
  659. a.uri = 'http://www.w3.org/XML/1998/namespace';
  660. }if(prefix !== 'xmlns'){
  661. a.uri = currentNSMap[prefix || '']
  662. //{console.log('###'+a.qName,domBuilder.locator.systemId+'',currentNSMap,a.uri)}
  663. }
  664. }
  665. }
  666. var nsp = tagName.indexOf(':');
  667. if(nsp>0){
  668. prefix = el.prefix = tagName.slice(0,nsp);
  669. localName = el.localName = tagName.slice(nsp+1);
  670. }else{
  671. prefix = null;//important!!
  672. localName = el.localName = tagName;
  673. }
  674. //no prefix element has default namespace
  675. var ns = el.uri = currentNSMap[prefix || ''];
  676. domBuilder.startElement(ns,localName,tagName,el);
  677. //endPrefixMapping and startPrefixMapping have not any help for dom builder
  678. //localNSMap = null
  679. if(el.closed){
  680. domBuilder.endElement(ns,localName,tagName);
  681. if(localNSMap){
  682. for(prefix in localNSMap){
  683. domBuilder.endPrefixMapping(prefix)
  684. }
  685. }
  686. }else{
  687. el.currentNSMap = currentNSMap;
  688. el.localNSMap = localNSMap;
  689. //parseStack.push(el);
  690. return true;
  691. }
  692. }
  693. function parseHtmlSpecialContent(source,elStartEnd,tagName,entityReplacer,domBuilder){
  694. if(/^(?:script|textarea)$/i.test(tagName)){
  695. var elEndStart = source.indexOf('</'+tagName+'>',elStartEnd);
  696. var text = source.substring(elStartEnd+1,elEndStart);
  697. if(/[&<]/.test(text)){
  698. if(/^script$/i.test(tagName)){
  699. //if(!/\]\]>/.test(text)){
  700. //lexHandler.startCDATA();
  701. domBuilder.characters(text,0,text.length);
  702. //lexHandler.endCDATA();
  703. return elEndStart;
  704. //}
  705. }//}else{//text area
  706. text = text.replace(/&#?\w+;/g,entityReplacer);
  707. domBuilder.characters(text,0,text.length);
  708. return elEndStart;
  709. //}
  710. }
  711. }
  712. return elStartEnd+1;
  713. }
  714. function fixSelfClosed(source,elStartEnd,tagName,closeMap){
  715. //if(tagName in closeMap){
  716. var pos = closeMap[tagName];
  717. if(pos == null){
  718. //console.log(tagName)
  719. pos = source.lastIndexOf('</'+tagName+'>')
  720. if(pos<elStartEnd){//忘记闭合
  721. pos = source.lastIndexOf('</'+tagName)
  722. }
  723. closeMap[tagName] =pos
  724. }
  725. return pos<elStartEnd;
  726. //}
  727. }
  728. function _copy(source,target){
  729. for(var n in source){target[n] = source[n]}
  730. }
  731. function parseDCC(source,start,domBuilder,errorHandler){//sure start with '<!'
  732. var next= source.charAt(start+2)
  733. switch(next){
  734. case '-':
  735. if(source.charAt(start + 3) === '-'){
  736. var end = source.indexOf('-->',start+4);
  737. //append comment source.substring(4,end)//<!--
  738. if(end>start){
  739. domBuilder.comment(source,start+4,end-start-4);
  740. return end+3;
  741. }else{
  742. errorHandler.error("Unclosed comment");
  743. return -1;
  744. }
  745. }else{
  746. //error
  747. return -1;
  748. }
  749. default:
  750. if(source.substr(start+3,6) == 'CDATA['){
  751. var end = source.indexOf(']]>',start+9);
  752. domBuilder.startCDATA();
  753. domBuilder.characters(source,start+9,end-start-9);
  754. domBuilder.endCDATA()
  755. return end+3;
  756. }
  757. //<!DOCTYPE
  758. //startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
  759. var matchs = split(source,start);
  760. var len = matchs.length;
  761. if(len>1 && /!doctype/i.test(matchs[0][0])){
  762. var name = matchs[1][0];
  763. var pubid = len>3 && /^public$/i.test(matchs[2][0]) && matchs[3][0]
  764. var sysid = len>4 && matchs[4][0];
  765. var lastMatch = matchs[len-1]
  766. domBuilder.startDTD(name,pubid && pubid.replace(/^(['"])(.*?)\1$/,'$2'),
  767. sysid && sysid.replace(/^(['"])(.*?)\1$/,'$2'));
  768. domBuilder.endDTD();
  769. return lastMatch.index+lastMatch[0].length
  770. }
  771. }
  772. return -1;
  773. }
  774. function parseInstruction(source,start,domBuilder){
  775. var end = source.indexOf('?>',start);
  776. if(end){
  777. var match = source.substring(start,end).match(/^<\?(\S*)\s*([\s\S]*?)\s*$/);
  778. if(match){
  779. var len = match[0].length;
  780. domBuilder.processingInstruction(match[1], match[2]) ;
  781. return end+2;
  782. }else{//error
  783. return -1;
  784. }
  785. }
  786. return -1;
  787. }
  788. /**
  789. * @param source
  790. */
  791. function ElementAttributes(source){
  792. }
  793. ElementAttributes.prototype = {
  794. setTagName:function(tagName){
  795. if(!tagNamePattern.test(tagName)){
  796. throw new Error('invalid tagName:'+tagName)
  797. }
  798. this.tagName = tagName
  799. },
  800. add:function(qName,value,offset){
  801. if(!tagNamePattern.test(qName)){
  802. throw new Error('invalid attribute:'+qName)
  803. }
  804. this[this.length++] = {qName:qName,value:value,offset:offset}
  805. },
  806. length:0,
  807. getLocalName:function(i){return this[i].localName},
  808. getLocator:function(i){return this[i].locator},
  809. getQName:function(i){return this[i].qName},
  810. getURI:function(i){return this[i].uri},
  811. getValue:function(i){return this[i].value}
  812. // ,getIndex:function(uri, localName)){
  813. // if(localName){
  814. //
  815. // }else{
  816. // var qName = uri
  817. // }
  818. // },
  819. // getValue:function(){return this.getValue(this.getIndex.apply(this,arguments))},
  820. // getType:function(uri,localName){}
  821. // getType:function(i){},
  822. }
  823. function split(source,start){
  824. var match;
  825. var buf = [];
  826. var reg = /'[^']+'|"[^"]+"|[^\s<>\/=]+=?|(\/?\s*>|<)/g;
  827. reg.lastIndex = start;
  828. reg.exec(source);//skip <
  829. while(match = reg.exec(source)){
  830. buf.push(match);
  831. if(match[1])return buf;
  832. }
  833. }
  834. if(typeof require == 'function')
  835. {
  836. exports.XMLReader = XMLReader;
  837. }
  838. /*
  839. * DOM Level 2
  840. * Object DOMException
  841. * @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html
  842. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html
  843. */
  844. function copy(src,dest){
  845. for(var p in src){
  846. dest[p] = src[p];
  847. }
  848. }
  849. /**
  850. ^\w+\.prototype\.([_\w]+)\s*=\s*((?:.*\{\s*?[\r\n][\s\S]*?^})|\S.*?(?=[;\r\n]));?
  851. ^\w+\.prototype\.([_\w]+)\s*=\s*(\S.*?(?=[;\r\n]));?
  852. */
  853. function _extends(Class,Super){
  854. var pt = Class.prototype;
  855. if(!(pt instanceof Super)){
  856. function t(){};
  857. t.prototype = Super.prototype;
  858. t = new t();
  859. copy(pt,t);
  860. Class.prototype = pt = t;
  861. }
  862. if(pt.constructor != Class){
  863. if(typeof Class != 'function'){
  864. console.error("unknow Class:"+Class)
  865. }
  866. pt.constructor = Class
  867. }
  868. }
  869. var htmlns = 'http://www.w3.org/1999/xhtml' ;
  870. // Node Types
  871. var NodeType = {}
  872. var ELEMENT_NODE = NodeType.ELEMENT_NODE = 1;
  873. var ATTRIBUTE_NODE = NodeType.ATTRIBUTE_NODE = 2;
  874. var TEXT_NODE = NodeType.TEXT_NODE = 3;
  875. var CDATA_SECTION_NODE = NodeType.CDATA_SECTION_NODE = 4;
  876. var ENTITY_REFERENCE_NODE = NodeType.ENTITY_REFERENCE_NODE = 5;
  877. var ENTITY_NODE = NodeType.ENTITY_NODE = 6;
  878. var PROCESSING_INSTRUCTION_NODE = NodeType.PROCESSING_INSTRUCTION_NODE = 7;
  879. var COMMENT_NODE = NodeType.COMMENT_NODE = 8;
  880. var DOCUMENT_NODE = NodeType.DOCUMENT_NODE = 9;
  881. var DOCUMENT_TYPE_NODE = NodeType.DOCUMENT_TYPE_NODE = 10;
  882. var DOCUMENT_FRAGMENT_NODE = NodeType.DOCUMENT_FRAGMENT_NODE = 11;
  883. var NOTATION_NODE = NodeType.NOTATION_NODE = 12;
  884. // ExceptionCode
  885. var ExceptionCode = {}
  886. var ExceptionMessage = {};
  887. var INDEX_SIZE_ERR = ExceptionCode.INDEX_SIZE_ERR = ((ExceptionMessage[1]="Index size error"),1);
  888. var DOMSTRING_SIZE_ERR = ExceptionCode.DOMSTRING_SIZE_ERR = ((ExceptionMessage[2]="DOMString size error"),2);
  889. var HIERARCHY_REQUEST_ERR = ExceptionCode.HIERARCHY_REQUEST_ERR = ((ExceptionMessage[3]="Hierarchy request error"),3);
  890. var WRONG_DOCUMENT_ERR = ExceptionCode.WRONG_DOCUMENT_ERR = ((ExceptionMessage[4]="Wrong document"),4);
  891. var INVALID_CHARACTER_ERR = ExceptionCode.INVALID_CHARACTER_ERR = ((ExceptionMessage[5]="Invalid character"),5);
  892. var NO_DATA_ALLOWED_ERR = ExceptionCode.NO_DATA_ALLOWED_ERR = ((ExceptionMessage[6]="No data allowed"),6);
  893. var NO_MODIFICATION_ALLOWED_ERR = ExceptionCode.NO_MODIFICATION_ALLOWED_ERR = ((ExceptionMessage[7]="No modification allowed"),7);
  894. var NOT_FOUND_ERR = ExceptionCode.NOT_FOUND_ERR = ((ExceptionMessage[8]="Not found"),8);
  895. var NOT_SUPPORTED_ERR = ExceptionCode.NOT_SUPPORTED_ERR = ((ExceptionMessage[9]="Not supported"),9);
  896. var INUSE_ATTRIBUTE_ERR = ExceptionCode.INUSE_ATTRIBUTE_ERR = ((ExceptionMessage[10]="Attribute in use"),10);
  897. //level2
  898. var INVALID_STATE_ERR = ExceptionCode.INVALID_STATE_ERR = ((ExceptionMessage[11]="Invalid state"),11);
  899. var SYNTAX_ERR = ExceptionCode.SYNTAX_ERR = ((ExceptionMessage[12]="Syntax error"),12);
  900. var INVALID_MODIFICATION_ERR = ExceptionCode.INVALID_MODIFICATION_ERR = ((ExceptionMessage[13]="Invalid modification"),13);
  901. var NAMESPACE_ERR = ExceptionCode.NAMESPACE_ERR = ((ExceptionMessage[14]="Invalid namespace"),14);
  902. var INVALID_ACCESS_ERR = ExceptionCode.INVALID_ACCESS_ERR = ((ExceptionMessage[15]="Invalid access"),15);
  903. function DOMException(code, message) {
  904. if(message instanceof Error){
  905. var error = message;
  906. }else{
  907. error = this;
  908. Error.call(this, ExceptionMessage[code]);
  909. this.message = ExceptionMessage[code];
  910. if(Error.captureStackTrace) Error.captureStackTrace(this, DOMException);
  911. }
  912. error.code = code;
  913. if(message) this.message = this.message + ": " + message;
  914. return error;
  915. };
  916. DOMException.prototype = Error.prototype;
  917. copy(ExceptionCode,DOMException)
  918. /**
  919. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-536297177
  920. * 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.
  921. * The items in the NodeList are accessible via an integral index, starting from 0.
  922. */
  923. function NodeList() {
  924. };
  925. NodeList.prototype = {
  926. /**
  927. * The number of nodes in the list. The range of valid child node indices is 0 to length-1 inclusive.
  928. * @standard level1
  929. */
  930. length:0,
  931. /**
  932. * 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.
  933. * @standard level1
  934. * @param index unsigned long
  935. * Index into the collection.
  936. * @return Node
  937. * The node at the indexth position in the NodeList, or null if that is not a valid index.
  938. */
  939. item: function(index) {
  940. return this[index] || null;
  941. },
  942. toString:function(isHTML,nodeFilter){
  943. for(var buf = [], i = 0;i<this.length;i++){
  944. serializeToString(this[i],buf,isHTML,nodeFilter);
  945. }
  946. return buf.join('');
  947. }
  948. };
  949. function LiveNodeList(node,refresh){
  950. this._node = node;
  951. this._refresh = refresh
  952. _updateLiveList(this);
  953. }
  954. function _updateLiveList(list){
  955. var inc = list._node._inc || list._node.ownerDocument._inc;
  956. if(list._inc != inc){
  957. var ls = list._refresh(list._node);
  958. //console.log(ls.length)
  959. __set__(list,'length',ls.length);
  960. copy(ls,list);
  961. list._inc = inc;
  962. }
  963. }
  964. LiveNodeList.prototype.item = function(i){
  965. _updateLiveList(this);
  966. return this[i];
  967. }
  968. _extends(LiveNodeList,NodeList);
  969. /**
  970. *
  971. * Objects implementing the NamedNodeMap interface are used to represent collections of nodes that can be accessed by name. Note that NamedNodeMap does not inherit from NodeList; NamedNodeMaps are not maintained in any particular order. Objects contained in an object implementing NamedNodeMap may also be accessed by an ordinal index, but this is simply to allow convenient enumeration of the contents of a NamedNodeMap, and does not imply that the DOM specifies an order to these Nodes.
  972. * NamedNodeMap objects in the DOM are live.
  973. * used for attributes or DocumentType entities
  974. */
  975. function NamedNodeMap() {
  976. };
  977. function _findNodeIndex(list,node){
  978. var i = list.length;
  979. while(i--){
  980. if(list[i] === node){return i}
  981. }
  982. }
  983. function _addNamedNode(el,list,newAttr,oldAttr){
  984. if(oldAttr){
  985. list[_findNodeIndex(list,oldAttr)] = newAttr;
  986. }else{
  987. list[list.length++] = newAttr;
  988. }
  989. if(el){
  990. newAttr.ownerElement = el;
  991. var doc = el.ownerDocument;
  992. if(doc){
  993. oldAttr && _onRemoveAttribute(doc,el,oldAttr);
  994. _onAddAttribute(doc,el,newAttr);
  995. }
  996. }
  997. }
  998. function _removeNamedNode(el,list,attr){
  999. //console.log('remove attr:'+attr)
  1000. var i = _findNodeIndex(list,attr);
  1001. if(i>=0){
  1002. var lastIndex = list.length-1
  1003. while(i<lastIndex){
  1004. list[i] = list[++i]
  1005. }
  1006. list.length = lastIndex;
  1007. if(el){
  1008. var doc = el.ownerDocument;
  1009. if(doc){
  1010. _onRemoveAttribute(doc,el,attr);
  1011. attr.ownerElement = null;
  1012. }
  1013. }
  1014. }else{
  1015. throw DOMException(NOT_FOUND_ERR,new Error(el.tagName+'@'+attr))
  1016. }
  1017. }
  1018. NamedNodeMap.prototype = {
  1019. length:0,
  1020. item:NodeList.prototype.item,
  1021. getNamedItem: function(key) {
  1022. // if(key.indexOf(':')>0 || key == 'xmlns'){
  1023. // return null;
  1024. // }
  1025. //console.log()
  1026. var i = this.length;
  1027. while(i--){
  1028. var attr = this[i];
  1029. //console.log(attr.nodeName,key)
  1030. if(attr.nodeName == key){
  1031. return attr;
  1032. }
  1033. }
  1034. },
  1035. setNamedItem: function(attr) {
  1036. var el = attr.ownerElement;
  1037. if(el && el!=this._ownerElement){
  1038. throw new DOMException(INUSE_ATTRIBUTE_ERR);
  1039. }
  1040. var oldAttr = this.getNamedItem(attr.nodeName);
  1041. _addNamedNode(this._ownerElement,this,attr,oldAttr);
  1042. return oldAttr;
  1043. },
  1044. /* returns Node */
  1045. setNamedItemNS: function(attr) {// raises: WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR
  1046. var el = attr.ownerElement, oldAttr;
  1047. if(el && el!=this._ownerElement){
  1048. throw new DOMException(INUSE_ATTRIBUTE_ERR);
  1049. }
  1050. oldAttr = this.getNamedItemNS(attr.namespaceURI,attr.localName);
  1051. _addNamedNode(this._ownerElement,this,attr,oldAttr);
  1052. return oldAttr;
  1053. },
  1054. /* returns Node */
  1055. removeNamedItem: function(key) {
  1056. var attr = this.getNamedItem(key);
  1057. _removeNamedNode(this._ownerElement,this,attr);
  1058. return attr;
  1059. },// raises: NOT_FOUND_ERR,NO_MODIFICATION_ALLOWED_ERR
  1060. //for level2
  1061. removeNamedItemNS:function(namespaceURI,localName){
  1062. var attr = this.getNamedItemNS(namespaceURI,localName);
  1063. _removeNamedNode(this._ownerElement,this,attr);
  1064. return attr;
  1065. },
  1066. getNamedItemNS: function(namespaceURI, localName) {
  1067. var i = this.length;
  1068. while(i--){
  1069. var node = this[i];
  1070. if(node.localName == localName && node.namespaceURI == namespaceURI){
  1071. return node;
  1072. }
  1073. }
  1074. return null;
  1075. }
  1076. };
  1077. /**
  1078. * @see http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-102161490
  1079. */
  1080. function DOMImplementation(/* Object */ features) {
  1081. this._features = {};
  1082. if (features) {
  1083. for (var feature in features) {
  1084. this._features = features[feature];
  1085. }
  1086. }
  1087. };
  1088. DOMImplementation.prototype = {
  1089. hasFeature: function(/* string */ feature, /* string */ version) {
  1090. var versions = this._features[feature.toLowerCase()];
  1091. if (versions && (!version || version in versions)) {
  1092. return true;
  1093. } else {
  1094. return false;
  1095. }
  1096. },
  1097. // Introduced in DOM Level 2:
  1098. createDocument:function(namespaceURI, qualifiedName, doctype){// raises:INVALID_CHARACTER_ERR,NAMESPACE_ERR,WRONG_DOCUMENT_ERR
  1099. var doc = new Document();
  1100. doc.implementation = this;
  1101. doc.childNodes = new NodeList();
  1102. doc.doctype = doctype;
  1103. if(doctype){
  1104. doc.appendChild(doctype);
  1105. }
  1106. if(qualifiedName){
  1107. var root = doc.createElementNS(namespaceURI,qualifiedName);
  1108. doc.appendChild(root);
  1109. }
  1110. return doc;
  1111. },
  1112. // Introduced in DOM Level 2:
  1113. createDocumentType:function(qualifiedName, publicId, systemId){// raises:INVALID_CHARACTER_ERR,NAMESPACE_ERR
  1114. var node = new DocumentType();
  1115. node.name = qualifiedName;
  1116. node.nodeName = qualifiedName;
  1117. node.publicId = publicId;
  1118. node.systemId = systemId;
  1119. // Introduced in DOM Level 2:
  1120. //readonly attribute DOMString internalSubset;
  1121. //TODO:..
  1122. // readonly attribute NamedNodeMap entities;
  1123. // readonly attribute NamedNodeMap notations;
  1124. return node;
  1125. }
  1126. };
  1127. /**
  1128. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247
  1129. */
  1130. function Node() {
  1131. };
  1132. Node.prototype = {
  1133. firstChild : null,
  1134. lastChild : null,
  1135. previousSibling : null,
  1136. nextSibling : null,
  1137. attributes : null,
  1138. parentNode : null,
  1139. childNodes : null,
  1140. ownerDocument : null,
  1141. nodeValue : null,
  1142. namespaceURI : null,
  1143. prefix : null,
  1144. localName : null,
  1145. // Modified in DOM Level 2:
  1146. insertBefore:function(newChild, refChild){//raises
  1147. return _insertBefore(this,newChild,refChild);
  1148. },
  1149. replaceChild:function(newChild, oldChild){//raises
  1150. this.insertBefore(newChild,oldChild);
  1151. if(oldChild){
  1152. this.removeChild(oldChild);
  1153. }
  1154. },
  1155. removeChild:function(oldChild){
  1156. return _removeChild(this,oldChild);
  1157. },
  1158. appendChild:function(newChild){
  1159. return this.insertBefore(newChild,null);
  1160. },
  1161. hasChildNodes:function(){
  1162. return this.firstChild != null;
  1163. },
  1164. cloneNode:function(deep){
  1165. return cloneNode(this.ownerDocument||this,this,deep);
  1166. },
  1167. // Modified in DOM Level 2:
  1168. normalize:function(){
  1169. var child = this.firstChild;
  1170. while(child){
  1171. var next = child.nextSibling;
  1172. if(next && next.nodeType == TEXT_NODE && child.nodeType == TEXT_NODE){
  1173. this.removeChild(next);
  1174. child.appendData(next.data);
  1175. }else{
  1176. child.normalize();
  1177. child = next;
  1178. }
  1179. }
  1180. },
  1181. // Introduced in DOM Level 2:
  1182. isSupported:function(feature, version){
  1183. return this.ownerDocument.implementation.hasFeature(feature,version);
  1184. },
  1185. // Introduced in DOM Level 2:
  1186. hasAttributes:function(){
  1187. return this.attributes.length>0;
  1188. },
  1189. lookupPrefix:function(namespaceURI){
  1190. var el = this;
  1191. while(el){
  1192. var map = el._nsMap;
  1193. //console.dir(map)
  1194. if(map){
  1195. for(var n in map){
  1196. if(map[n] == namespaceURI){
  1197. return n;
  1198. }
  1199. }
  1200. }
  1201. el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
  1202. }
  1203. return null;
  1204. },
  1205. // Introduced in DOM Level 3:
  1206. lookupNamespaceURI:function(prefix){
  1207. var el = this;
  1208. while(el){
  1209. var map = el._nsMap;
  1210. //console.dir(map)
  1211. if(map){
  1212. if(prefix in map){
  1213. return map[prefix] ;
  1214. }
  1215. }
  1216. el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
  1217. }
  1218. return null;
  1219. },
  1220. // Introduced in DOM Level 3:
  1221. isDefaultNamespace:function(namespaceURI){
  1222. var prefix = this.lookupPrefix(namespaceURI);
  1223. return prefix == null;
  1224. }
  1225. };
  1226. function _xmlEncoder(c){
  1227. return c == '<' && '&lt;' ||
  1228. c == '>' && '&gt;' ||
  1229. c == '&' && '&amp;' ||
  1230. c == '"' && '&quot;' ||
  1231. '&#'+c.charCodeAt()+';'
  1232. }
  1233. copy(NodeType,Node);
  1234. copy(NodeType,Node.prototype);
  1235. /**
  1236. * @param callback return true for continue,false for break
  1237. * @return boolean true: break visit;
  1238. */
  1239. function _visitNode(node,callback){
  1240. if(callback(node)){
  1241. return true;
  1242. }
  1243. if(node = node.firstChild){
  1244. do{
  1245. if(_visitNode(node,callback)){return true}
  1246. }while(node=node.nextSibling)
  1247. }
  1248. }
  1249. function Document(){
  1250. }
  1251. function _onAddAttribute(doc,el,newAttr){
  1252. doc && doc._inc++;
  1253. var ns = newAttr.namespaceURI ;
  1254. if(ns == 'http://www.w3.org/2000/xmlns/'){
  1255. //update namespace
  1256. el._nsMap[newAttr.prefix?newAttr.localName:''] = newAttr.value
  1257. }
  1258. }
  1259. function _onRemoveAttribute(doc,el,newAttr,remove){
  1260. doc && doc._inc++;
  1261. var ns = newAttr.namespaceURI ;
  1262. if(ns == 'http://www.w3.org/2000/xmlns/'){
  1263. //update namespace
  1264. delete el._nsMap[newAttr.prefix?newAttr.localName:'']
  1265. }
  1266. }
  1267. function _onUpdateChild(doc,el,newChild){
  1268. if(doc && doc._inc){
  1269. doc._inc++;
  1270. //update childNodes
  1271. var cs = el.childNodes;
  1272. if(newChild){
  1273. cs[cs.length++] = newChild;
  1274. }else{
  1275. //console.log(1)
  1276. var child = el.firstChild;
  1277. var i = 0;
  1278. while(child){
  1279. cs[i++] = child;
  1280. child =child.nextSibling;
  1281. }
  1282. cs.length = i;
  1283. }
  1284. }
  1285. }
  1286. /**
  1287. * attributes;
  1288. * children;
  1289. *
  1290. * writeable properties:
  1291. * nodeValue,Attr:value,CharacterData:data
  1292. * prefix
  1293. */
  1294. function _removeChild(parentNode,child){
  1295. var previous = child.previousSibling;
  1296. var next = child.nextSibling;
  1297. if(previous){
  1298. previous.nextSibling = next;
  1299. }else{
  1300. parentNode.firstChild = next
  1301. }
  1302. if(next){
  1303. next.previousSibling = previous;
  1304. }else{
  1305. parentNode.lastChild = previous;
  1306. }
  1307. _onUpdateChild(parentNode.ownerDocument,parentNode);
  1308. return child;
  1309. }
  1310. /**
  1311. * preformance key(refChild == null)
  1312. */
  1313. function _insertBefore(parentNode,newChild,nextChild){
  1314. var cp = newChild.parentNode;
  1315. if(cp){
  1316. cp.removeChild(newChild);//remove and update
  1317. }
  1318. if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
  1319. var newFirst = newChild.firstChild;
  1320. if (newFirst == null) {
  1321. return newChild;
  1322. }
  1323. var newLast = newChild.lastChild;
  1324. }else{
  1325. newFirst = newLast = newChild;
  1326. }
  1327. var pre = nextChild ? nextChild.previousSibling : parentNode.lastChild;
  1328. newFirst.previousSibling = pre;
  1329. newLast.nextSibling = nextChild;
  1330. if(pre){
  1331. pre.nextSibling = newFirst;
  1332. }else{
  1333. parentNode.firstChild = newFirst;
  1334. }
  1335. if(nextChild == null){
  1336. parentNode.lastChild = newLast;
  1337. }else{
  1338. nextChild.previousSibling = newLast;
  1339. }
  1340. do{
  1341. newFirst.parentNode = parentNode;
  1342. }while(newFirst !== newLast && (newFirst= newFirst.nextSibling))
  1343. _onUpdateChild(parentNode.ownerDocument||parentNode,parentNode);
  1344. //console.log(parentNode.lastChild.nextSibling == null)
  1345. if (newChild.nodeType == DOCUMENT_FRAGMENT_NODE) {
  1346. newChild.firstChild = newChild.lastChild = null;
  1347. }
  1348. return newChild;
  1349. }
  1350. function _appendSingleChild(parentNode,newChild){
  1351. var cp = newChild.parentNode;
  1352. if(cp){
  1353. var pre = parentNode.lastChild;
  1354. cp.removeChild(newChild);//remove and update
  1355. var pre = parentNode.lastChild;
  1356. }
  1357. var pre = parentNode.lastChild;
  1358. newChild.parentNode = parentNode;
  1359. newChild.previousSibling = pre;
  1360. newChild.nextSibling = null;
  1361. if(pre){
  1362. pre.nextSibling = newChild;
  1363. }else{
  1364. parentNode.firstChild = newChild;
  1365. }
  1366. parentNode.lastChild = newChild;
  1367. _onUpdateChild(parentNode.ownerDocument,parentNode,newChild);
  1368. return newChild;
  1369. //console.log("__aa",parentNode.lastChild.nextSibling == null)
  1370. }
  1371. Document.prototype = {
  1372. //implementation : null,
  1373. nodeName : '#document',
  1374. nodeType : DOCUMENT_NODE,
  1375. doctype : null,
  1376. documentElement : null,
  1377. _inc : 1,
  1378. insertBefore : function(newChild, refChild){//raises
  1379. if(newChild.nodeType == DOCUMENT_FRAGMENT_NODE){
  1380. var child = newChild.firstChild;
  1381. while(child){
  1382. var next = child.nextSibling;
  1383. this.insertBefore(child,refChild);
  1384. child = next;
  1385. }
  1386. return newChild;
  1387. }
  1388. if(this.documentElement == null && newChild.nodeType == ELEMENT_NODE){
  1389. this.documentElement = newChild;
  1390. }
  1391. return _insertBefore(this,newChild,refChild),(newChild.ownerDocument = this),newChild;
  1392. },
  1393. removeChild : function(oldChild){
  1394. if(this.documentElement == oldChild){
  1395. this.documentElement = null;
  1396. }
  1397. return _removeChild(this,oldChild);
  1398. },
  1399. // Introduced in DOM Level 2:
  1400. importNode : function(importedNode,deep){
  1401. return importNode(this,importedNode,deep);
  1402. },
  1403. // Introduced in DOM Level 2:
  1404. getElementById : function(id){
  1405. var rtv = null;
  1406. _visitNode(this.documentElement,function(node){
  1407. if(node.nodeType == ELEMENT_NODE){
  1408. if(node.getAttribute('id') == id){
  1409. rtv = node;
  1410. return true;
  1411. }
  1412. }
  1413. })
  1414. return rtv;
  1415. },
  1416. //document factory method:
  1417. createElement : function(tagName){
  1418. var node = new Element();
  1419. node.ownerDocument = this;
  1420. node.nodeName = tagName;
  1421. node.tagName = tagName;
  1422. node.childNodes = new NodeList();
  1423. var attrs = node.attributes = new NamedNodeMap();
  1424. attrs._ownerElement = node;
  1425. return node;
  1426. },
  1427. createDocumentFragment : function(){
  1428. var node = new DocumentFragment();
  1429. node.ownerDocument = this;
  1430. node.childNodes = new NodeList();
  1431. return node;
  1432. },
  1433. createTextNode : function(data){
  1434. var node = new Text();
  1435. node.ownerDocument = this;
  1436. node.appendData(data)
  1437. return node;
  1438. },
  1439. createComment : function(data){
  1440. var node = new Comment();
  1441. node.ownerDocument = this;
  1442. node.appendData(data)
  1443. return node;
  1444. },
  1445. createCDATASection : function(data){
  1446. var node = new CDATASection();
  1447. node.ownerDocument = this;
  1448. node.appendData(data)
  1449. return node;
  1450. },
  1451. createProcessingInstruction : function(target,data){
  1452. var node = new ProcessingInstruction();
  1453. node.ownerDocument = this;
  1454. node.tagName = node.target = target;
  1455. node.nodeValue= node.data = data;
  1456. return node;
  1457. },
  1458. createAttribute : function(name){
  1459. var node = new Attr();
  1460. node.ownerDocument = this;
  1461. node.name = name;
  1462. node.nodeName = name;
  1463. node.localName = name;
  1464. node.specified = true;
  1465. return node;
  1466. },
  1467. createEntityReference : function(name){
  1468. var node = new EntityReference();
  1469. node.ownerDocument = this;
  1470. node.nodeName = name;
  1471. return node;
  1472. },
  1473. // Introduced in DOM Level 2:
  1474. createElementNS : function(namespaceURI,qualifiedName){
  1475. var node = new Element();
  1476. var pl = qualifiedName.split(':');
  1477. var attrs = node.attributes = new NamedNodeMap();
  1478. node.childNodes = new NodeList();
  1479. node.ownerDocument = this;
  1480. node.nodeName = qualifiedName;
  1481. node.tagName = qualifiedName;
  1482. node.namespaceURI = namespaceURI;
  1483. if(pl.length == 2){
  1484. node.prefix = pl[0];
  1485. node.localName = pl[1];
  1486. }else{
  1487. //el.prefix = null;
  1488. node.localName = qualifiedName;
  1489. }
  1490. attrs._ownerElement = node;
  1491. return node;
  1492. },
  1493. // Introduced in DOM Level 2:
  1494. createAttributeNS : function(namespaceURI,qualifiedName){
  1495. var node = new Attr();
  1496. var pl = qualifiedName.split(':');
  1497. node.ownerDocument = this;
  1498. node.nodeName = qualifiedName;
  1499. node.name = qualifiedName;
  1500. node.namespaceURI = namespaceURI;
  1501. node.specified = true;
  1502. if(pl.length == 2){
  1503. node.prefix = pl[0];
  1504. node.localName = pl[1];
  1505. }else{
  1506. //el.prefix = null;
  1507. node.localName = qualifiedName;
  1508. }
  1509. return node;
  1510. }
  1511. };
  1512. _extends(Document,Node);
  1513. function Element() {
  1514. this._nsMap = {};
  1515. };
  1516. Element.prototype = {
  1517. nodeType : ELEMENT_NODE,
  1518. hasAttribute : function(name){
  1519. return this.getAttributeNode(name)!=null;
  1520. },
  1521. getAttribute : function(name){
  1522. var attr = this.getAttributeNode(name);
  1523. return attr && attr.value || '';
  1524. },
  1525. getAttributeNode : function(name){
  1526. return this.attributes.getNamedItem(name);
  1527. },
  1528. setAttribute : function(name, value){
  1529. var attr = this.ownerDocument.createAttribute(name);
  1530. attr.value = attr.nodeValue = "" + value;
  1531. this.setAttributeNode(attr)
  1532. },
  1533. removeAttribute : function(name){
  1534. var attr = this.getAttributeNode(name)
  1535. attr && this.removeAttributeNode(attr);
  1536. },
  1537. //four real opeartion method
  1538. appendChild:function(newChild){
  1539. if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
  1540. return this.insertBefore(newChild,null);
  1541. }else{
  1542. return _appendSingleChild(this,newChild);
  1543. }
  1544. },
  1545. setAttributeNode : function(newAttr){
  1546. return this.attributes.setNamedItem(newAttr);
  1547. },
  1548. setAttributeNodeNS : function(newAttr){
  1549. return this.attributes.setNamedItemNS(newAttr);
  1550. },
  1551. removeAttributeNode : function(oldAttr){
  1552. //console.log(this == oldAttr.ownerElement)
  1553. return this.attributes.removeNamedItem(oldAttr.nodeName);
  1554. },
  1555. //get real attribute name,and remove it by removeAttributeNode
  1556. removeAttributeNS : function(namespaceURI, localName){
  1557. var old = this.getAttributeNodeNS(namespaceURI, localName);
  1558. old && this.removeAttributeNode(old);
  1559. },
  1560. hasAttributeNS : function(namespaceURI, localName){
  1561. return this.getAttributeNodeNS(namespaceURI, localName)!=null;
  1562. },
  1563. getAttributeNS : function(namespaceURI, localName){
  1564. var attr = this.getAttributeNodeNS(namespaceURI, localName);
  1565. return attr && attr.value || '';
  1566. },
  1567. setAttributeNS : function(namespaceURI, qualifiedName, value){
  1568. var attr = this.ownerDocument.createAttributeNS(namespaceURI, qualifiedName);
  1569. attr.value = attr.nodeValue = "" + value;
  1570. this.setAttributeNode(attr)
  1571. },
  1572. getAttributeNodeNS : function(namespaceURI, localName){
  1573. return this.attributes.getNamedItemNS(namespaceURI, localName);
  1574. },
  1575. getElementsByTagName : function(tagName){
  1576. return new LiveNodeList(this,function(base){
  1577. var ls = [];
  1578. _visitNode(base,function(node){
  1579. if(node !== base && node.nodeType == ELEMENT_NODE && (tagName === '*' || node.tagName == tagName)){
  1580. ls.push(node);
  1581. }
  1582. });
  1583. return ls;
  1584. });
  1585. },
  1586. getElementsByTagNameNS : function(namespaceURI, localName){
  1587. return new LiveNodeList(this,function(base){
  1588. var ls = [];
  1589. _visitNode(base,function(node){
  1590. if(node !== base && node.nodeType === ELEMENT_NODE && (namespaceURI === '*' || node.namespaceURI === namespaceURI) && (localName === '*' || node.localName == localName)){
  1591. ls.push(node);
  1592. }
  1593. });
  1594. return ls;
  1595. });
  1596. }
  1597. };
  1598. Document.prototype.getElementsByTagName = Element.prototype.getElementsByTagName;
  1599. Document.prototype.getElementsByTagNameNS = Element.prototype.getElementsByTagNameNS;
  1600. _extends(Element,Node);
  1601. function Attr() {
  1602. };
  1603. Attr.prototype.nodeType = ATTRIBUTE_NODE;
  1604. _extends(Attr,Node);
  1605. function CharacterData() {
  1606. };
  1607. CharacterData.prototype = {
  1608. data : '',
  1609. substringData : function(offset, count) {
  1610. return this.data.substring(offset, offset+count);
  1611. },
  1612. appendData: function(text) {
  1613. text = this.data+text;
  1614. this.nodeValue = this.data = text;
  1615. this.length = text.length;
  1616. },
  1617. insertData: function(offset,text) {
  1618. this.replaceData(offset,0,text);
  1619. },
  1620. appendChild:function(newChild){
  1621. throw new Error(ExceptionMessage[HIERARCHY_REQUEST_ERR])
  1622. },
  1623. deleteData: function(offset, count) {
  1624. this.replaceData(offset,count,"");
  1625. },
  1626. replaceData: function(offset, count, text) {
  1627. var start = this.data.substring(0,offset);
  1628. var end = this.data.substring(offset+count);
  1629. text = start + text + end;
  1630. this.nodeValue = this.data = text;
  1631. this.length = text.length;
  1632. }
  1633. }
  1634. _extends(CharacterData,Node);
  1635. function Text() {
  1636. };
  1637. Text.prototype = {
  1638. nodeName : "#text",
  1639. nodeType : TEXT_NODE,
  1640. splitText : function(offset) {
  1641. var text = this.data;
  1642. var newText = text.substring(offset);
  1643. text = text.substring(0, offset);
  1644. this.data = this.nodeValue = text;
  1645. this.length = text.length;
  1646. var newNode = this.ownerDocument.createTextNode(newText);
  1647. if(this.parentNode){
  1648. this.parentNode.insertBefore(newNode, this.nextSibling);
  1649. }
  1650. return newNode;
  1651. }
  1652. }
  1653. _extends(Text,CharacterData);
  1654. function Comment() {
  1655. };
  1656. Comment.prototype = {
  1657. nodeName : "#comment",
  1658. nodeType : COMMENT_NODE
  1659. }
  1660. _extends(Comment,CharacterData);
  1661. function CDATASection() {
  1662. };
  1663. CDATASection.prototype = {
  1664. nodeName : "#cdata-section",
  1665. nodeType : CDATA_SECTION_NODE
  1666. }
  1667. _extends(CDATASection,CharacterData);
  1668. function DocumentType() {
  1669. };
  1670. DocumentType.prototype.nodeType = DOCUMENT_TYPE_NODE;
  1671. _extends(DocumentType,Node);
  1672. function Notation() {
  1673. };
  1674. Notation.prototype.nodeType = NOTATION_NODE;
  1675. _extends(Notation,Node);
  1676. function Entity() {
  1677. };
  1678. Entity.prototype.nodeType = ENTITY_NODE;
  1679. _extends(Entity,Node);
  1680. function EntityReference() {
  1681. };
  1682. EntityReference.prototype.nodeType = ENTITY_REFERENCE_NODE;
  1683. _extends(EntityReference,Node);
  1684. function DocumentFragment() {
  1685. };
  1686. DocumentFragment.prototype.nodeName = "#document-fragment";
  1687. DocumentFragment.prototype.nodeType = DOCUMENT_FRAGMENT_NODE;
  1688. _extends(DocumentFragment,Node);
  1689. function ProcessingInstruction() {
  1690. }
  1691. ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
  1692. _extends(ProcessingInstruction,Node);
  1693. function XMLSerializer(){}
  1694. XMLSerializer.prototype.serializeToString = function(node,isHtml,nodeFilter){
  1695. return nodeSerializeToString.call(node,isHtml,nodeFilter);
  1696. }
  1697. Node.prototype.toString = nodeSerializeToString;
  1698. function nodeSerializeToString(isHtml,nodeFilter){
  1699. var buf = [];
  1700. var refNode = this.nodeType == 9 && this.documentElement || this;
  1701. var prefix = refNode.prefix;
  1702. var uri = refNode.namespaceURI;
  1703. if(uri && prefix == null){
  1704. //console.log(prefix)
  1705. var prefix = refNode.lookupPrefix(uri);
  1706. if(prefix == null){
  1707. //isHTML = true;
  1708. var visibleNamespaces=[
  1709. {namespace:uri,prefix:null}
  1710. //{namespace:uri,prefix:''}
  1711. ]
  1712. }
  1713. }
  1714. serializeToString(this,buf,isHtml,nodeFilter,visibleNamespaces);
  1715. //console.log('###',this.nodeType,uri,prefix,buf.join(''))
  1716. return buf.join('');
  1717. }
  1718. function needNamespaceDefine(node,isHTML, visibleNamespaces) {
  1719. var prefix = node.prefix||'';
  1720. var uri = node.namespaceURI;
  1721. if (!prefix && !uri){
  1722. return false;
  1723. }
  1724. if (prefix === "xml" && uri === "http://www.w3.org/XML/1998/namespace"
  1725. || uri == 'http://www.w3.org/2000/xmlns/'){
  1726. return false;
  1727. }
  1728. var i = visibleNamespaces.length
  1729. //console.log('@@@@',node.tagName,prefix,uri,visibleNamespaces)
  1730. while (i--) {
  1731. var ns = visibleNamespaces[i];
  1732. // get namespace prefix
  1733. //console.log(node.nodeType,node.tagName,ns.prefix,prefix)
  1734. if (ns.prefix == prefix){
  1735. return ns.namespace != uri;
  1736. }
  1737. }
  1738. //console.log(isHTML,uri,prefix=='')
  1739. //if(isHTML && prefix ==null && uri == 'http://www.w3.org/1999/xhtml'){
  1740. // return false;
  1741. //}
  1742. //node.flag = '11111'
  1743. //console.error(3,true,node.flag,node.prefix,node.namespaceURI)
  1744. return true;
  1745. }
  1746. function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
  1747. if(nodeFilter){
  1748. node = nodeFilter(node);
  1749. if(node){
  1750. if(typeof node == 'string'){
  1751. buf.push(node);
  1752. return;
  1753. }
  1754. }else{
  1755. return;
  1756. }
  1757. //buf.sort.apply(attrs, attributeSorter);
  1758. }
  1759. switch(node.nodeType){
  1760. case ELEMENT_NODE:
  1761. if (!visibleNamespaces) visibleNamespaces = [];
  1762. var startVisibleNamespaces = visibleNamespaces.length;
  1763. var attrs = node.attributes;
  1764. var len = attrs.length;
  1765. var child = node.firstChild;
  1766. var nodeName = node.tagName;
  1767. isHTML = (htmlns === node.namespaceURI) ||isHTML
  1768. buf.push('<',nodeName);
  1769. for(var i=0;i<len;i++){
  1770. // add namespaces for attributes
  1771. var attr = attrs.item(i);
  1772. if (attr.prefix == 'xmlns') {
  1773. visibleNamespaces.push({ prefix: attr.localName, namespace: attr.value });
  1774. }else if(attr.nodeName == 'xmlns'){
  1775. visibleNamespaces.push({ prefix: '', namespace: attr.value });
  1776. }
  1777. }
  1778. for(var i=0;i<len;i++){
  1779. var attr = attrs.item(i);
  1780. if (needNamespaceDefine(attr,isHTML, visibleNamespaces)) {
  1781. var prefix = attr.prefix||'';
  1782. var uri = attr.namespaceURI;
  1783. var ns = prefix ? ' xmlns:' + prefix : " xmlns";
  1784. buf.push(ns, '="' , uri , '"');
  1785. visibleNamespaces.push({ prefix: prefix, namespace:uri });
  1786. }
  1787. serializeToString(attr,buf,isHTML,nodeFilter,visibleNamespaces);
  1788. }
  1789. // add namespace for current node
  1790. if (needNamespaceDefine(node,isHTML, visibleNamespaces)) {
  1791. var prefix = node.prefix||'';
  1792. var uri = node.namespaceURI;
  1793. var ns = prefix ? ' xmlns:' + prefix : " xmlns";
  1794. buf.push(ns, '="' , uri , '"');
  1795. visibleNamespaces.push({ prefix: prefix, namespace:uri });
  1796. }
  1797. if(child || isHTML && !/^(?:meta|link|img|br|hr|input)$/i.test(nodeName)){
  1798. buf.push('>');
  1799. //if is cdata child node
  1800. if(isHTML && /^script$/i.test(nodeName)){
  1801. while(child){
  1802. if(child.data){
  1803. buf.push(child.data);
  1804. }else{
  1805. serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
  1806. }
  1807. child = child.nextSibling;
  1808. }
  1809. }else
  1810. {
  1811. while(child){
  1812. serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
  1813. child = child.nextSibling;
  1814. }
  1815. }
  1816. buf.push('</',nodeName,'>');
  1817. }else{
  1818. buf.push('/>');
  1819. }
  1820. // remove added visible namespaces
  1821. //visibleNamespaces.length = startVisibleNamespaces;
  1822. return;
  1823. case DOCUMENT_NODE:
  1824. case DOCUMENT_FRAGMENT_NODE:
  1825. var child = node.firstChild;
  1826. while(child){
  1827. serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
  1828. child = child.nextSibling;
  1829. }
  1830. return;
  1831. case ATTRIBUTE_NODE:
  1832. return buf.push(' ',node.name,'="',node.value.replace(/[<&"]/g,_xmlEncoder),'"');
  1833. case TEXT_NODE:
  1834. return buf.push(node.data.replace(/[<&]/g,_xmlEncoder));
  1835. case CDATA_SECTION_NODE:
  1836. return buf.push( '<![CDATA[',node.data,']]>');
  1837. case COMMENT_NODE:
  1838. return buf.push( "<!--",node.data,"-->");
  1839. case DOCUMENT_TYPE_NODE:
  1840. var pubid = node.publicId;
  1841. var sysid = node.systemId;
  1842. buf.push('<!DOCTYPE ',node.name);
  1843. if(pubid){
  1844. buf.push(' PUBLIC "',pubid);
  1845. if (sysid && sysid!='.') {
  1846. buf.push( '" "',sysid);
  1847. }
  1848. buf.push('">');
  1849. }else if(sysid && sysid!='.'){
  1850. buf.push(' SYSTEM "',sysid,'">');
  1851. }else{
  1852. var sub = node.internalSubset;
  1853. if(sub){
  1854. buf.push(" [",sub,"]");
  1855. }
  1856. buf.push(">");
  1857. }
  1858. return;
  1859. case PROCESSING_INSTRUCTION_NODE:
  1860. return buf.push( "<?",node.target," ",node.data,"?>");
  1861. case ENTITY_REFERENCE_NODE:
  1862. return buf.push( '&',node.nodeName,';');
  1863. //case ENTITY_NODE:
  1864. //case NOTATION_NODE:
  1865. default:
  1866. buf.push('??',node.nodeName);
  1867. }
  1868. }
  1869. function importNode(doc,node,deep){
  1870. var node2;
  1871. switch (node.nodeType) {
  1872. case ELEMENT_NODE:
  1873. node2 = node.cloneNode(false);
  1874. node2.ownerDocument = doc;
  1875. //var attrs = node2.attributes;
  1876. //var len = attrs.length;
  1877. //for(var i=0;i<len;i++){
  1878. //node2.setAttributeNodeNS(importNode(doc,attrs.item(i),deep));
  1879. //}
  1880. case DOCUMENT_FRAGMENT_NODE:
  1881. break;
  1882. case ATTRIBUTE_NODE:
  1883. deep = true;
  1884. break;
  1885. //case ENTITY_REFERENCE_NODE:
  1886. //case PROCESSING_INSTRUCTION_NODE:
  1887. ////case TEXT_NODE:
  1888. //case CDATA_SECTION_NODE:
  1889. //case COMMENT_NODE:
  1890. // deep = false;
  1891. // break;
  1892. //case DOCUMENT_NODE:
  1893. //case DOCUMENT_TYPE_NODE:
  1894. //cannot be imported.
  1895. //case ENTITY_NODE:
  1896. //case NOTATION_NODE:
  1897. //can not hit in level3
  1898. //default:throw e;
  1899. }
  1900. if(!node2){
  1901. node2 = node.cloneNode(false);//false
  1902. }
  1903. node2.ownerDocument = doc;
  1904. node2.parentNode = null;
  1905. if(deep){
  1906. var child = node.firstChild;
  1907. while(child){
  1908. node2.appendChild(importNode(doc,child,deep));
  1909. child = child.nextSibling;
  1910. }
  1911. }
  1912. return node2;
  1913. }
  1914. //
  1915. //var _relationMap = {firstChild:1,lastChild:1,previousSibling:1,nextSibling:1,
  1916. // attributes:1,childNodes:1,parentNode:1,documentElement:1,doctype,};
  1917. function cloneNode(doc,node,deep){
  1918. var node2 = new node.constructor();
  1919. for(var n in node){
  1920. var v = node[n];
  1921. if(typeof v != 'object' ){
  1922. if(v != node2[n]){
  1923. node2[n] = v;
  1924. }
  1925. }
  1926. }
  1927. if(node.childNodes){
  1928. node2.childNodes = new NodeList();
  1929. }
  1930. node2.ownerDocument = doc;
  1931. switch (node2.nodeType) {
  1932. case ELEMENT_NODE:
  1933. var attrs = node.attributes;
  1934. var attrs2 = node2.attributes = new NamedNodeMap();
  1935. var len = attrs.length
  1936. attrs2._ownerElement = node2;
  1937. for(var i=0;i<len;i++){
  1938. node2.setAttributeNode(cloneNode(doc,attrs.item(i),true));
  1939. }
  1940. break;;
  1941. case ATTRIBUTE_NODE:
  1942. deep = true;
  1943. }
  1944. if(deep){
  1945. var child = node.firstChild;
  1946. while(child){
  1947. node2.appendChild(cloneNode(doc,child,deep));
  1948. child = child.nextSibling;
  1949. }
  1950. }
  1951. return node2;
  1952. }
  1953. function __set__(object,key,value){
  1954. object[key] = value
  1955. }
  1956. //do dynamic
  1957. try{
  1958. if(Object.defineProperty){
  1959. Object.defineProperty(LiveNodeList.prototype,'length',{
  1960. get:function(){
  1961. _updateLiveList(this);
  1962. return this.$$length;
  1963. }
  1964. });
  1965. Object.defineProperty(Node.prototype,'textContent',{
  1966. get:function(){
  1967. return getTextContent(this);
  1968. },
  1969. set:function(data){
  1970. switch(this.nodeType){
  1971. case ELEMENT_NODE:
  1972. case DOCUMENT_FRAGMENT_NODE:
  1973. while(this.firstChild){
  1974. this.removeChild(this.firstChild);
  1975. }
  1976. if(data || String(data)){
  1977. this.appendChild(this.ownerDocument.createTextNode(data));
  1978. }
  1979. break;
  1980. default:
  1981. //TODO:
  1982. this.data = data;
  1983. this.value = data;
  1984. this.nodeValue = data;
  1985. }
  1986. }
  1987. })
  1988. function getTextContent(node){
  1989. switch(node.nodeType){
  1990. case ELEMENT_NODE:
  1991. case DOCUMENT_FRAGMENT_NODE:
  1992. var buf = [];
  1993. node = node.firstChild;
  1994. while(node){
  1995. if(node.nodeType!==7 && node.nodeType !==8){
  1996. buf.push(getTextContent(node));
  1997. }
  1998. node = node.nextSibling;
  1999. }
  2000. return buf.join('');
  2001. default:
  2002. return node.nodeValue;
  2003. }
  2004. }
  2005. __set__ = function(object,key,value){
  2006. //console.log(value)
  2007. object['$$'+key] = value
  2008. }
  2009. }
  2010. }catch(e){//ie8
  2011. }
  2012. if(typeof require == 'function'){
  2013. exports.DOMImplementation = DOMImplementation;
  2014. exports.XMLSerializer = XMLSerializer;
  2015. }
  2016. function DOMParser(options){
  2017. this.options = options ||{locator:{}};
  2018. }
  2019. DOMParser.prototype.parseFromString = function(source,mimeType){
  2020. var options = this.options;
  2021. var sax = new XMLReader();
  2022. var domBuilder = options.domBuilder || new DOMHandler();//contentHandler and LexicalHandler
  2023. var errorHandler = options.errorHandler;
  2024. var locator = options.locator;
  2025. var defaultNSMap = options.xmlns||{};
  2026. var isHTML = /\/x?html?$/.test(mimeType);//mimeType.toLowerCase().indexOf('html') > -1;
  2027. var entityMap = isHTML?htmlEntity.entityMap:{'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"};
  2028. if(locator){
  2029. domBuilder.setDocumentLocator(locator)
  2030. }
  2031. sax.errorHandler = buildErrorHandler(errorHandler,domBuilder,locator);
  2032. sax.domBuilder = options.domBuilder || domBuilder;
  2033. if(isHTML){
  2034. defaultNSMap['']= 'http://www.w3.org/1999/xhtml';
  2035. }
  2036. defaultNSMap.xml = defaultNSMap.xml || 'http://www.w3.org/XML/1998/namespace';
  2037. if(source){
  2038. sax.parse(source,defaultNSMap,entityMap);
  2039. }else{
  2040. sax.errorHandler.error("invalid doc source");
  2041. }
  2042. return domBuilder.doc;
  2043. }
  2044. function buildErrorHandler(errorImpl,domBuilder,locator){
  2045. if(!errorImpl){
  2046. if(domBuilder instanceof DOMHandler){
  2047. return domBuilder;
  2048. }
  2049. errorImpl = domBuilder ;
  2050. }
  2051. var errorHandler = {}
  2052. var isCallback = errorImpl instanceof Function;
  2053. locator = locator||{}
  2054. function build(key){
  2055. var fn = errorImpl[key];
  2056. if(!fn && isCallback){
  2057. fn = errorImpl.length == 2?function(msg){errorImpl(key,msg)}:errorImpl;
  2058. }
  2059. errorHandler[key] = fn && function(msg){
  2060. fn('[xmldom '+key+']\t'+msg+_locator(locator));
  2061. }||function(){};
  2062. }
  2063. build('warning');
  2064. build('error');
  2065. build('fatalError');
  2066. return errorHandler;
  2067. }
  2068. //console.log('#\n\n\n\n\n\n\n####')
  2069. /**
  2070. * +ContentHandler+ErrorHandler
  2071. * +LexicalHandler+EntityResolver2
  2072. * -DeclHandler-DTDHandler
  2073. *
  2074. * DefaultHandler:EntityResolver, DTDHandler, ContentHandler, ErrorHandler
  2075. * DefaultHandler2:DefaultHandler,LexicalHandler, DeclHandler, EntityResolver2
  2076. * @link http://www.saxproject.org/apidoc/org/xml/sax/helpers/DefaultHandler.html
  2077. */
  2078. function DOMHandler() {
  2079. this.cdata = false;
  2080. }
  2081. function position(locator,node){
  2082. node.lineNumber = locator.lineNumber;
  2083. node.columnNumber = locator.columnNumber;
  2084. }
  2085. /**
  2086. * @see org.xml.sax.ContentHandler#startDocument
  2087. * @link http://www.saxproject.org/apidoc/org/xml/sax/ContentHandler.html
  2088. */
  2089. DOMHandler.prototype = {
  2090. startDocument : function() {
  2091. this.doc = new DOMImplementation().createDocument(null, null, null);
  2092. if (this.locator) {
  2093. this.doc.documentURI = this.locator.systemId;
  2094. }
  2095. },
  2096. startElement:function(namespaceURI, localName, qName, attrs) {
  2097. var doc = this.doc;
  2098. var el = doc.createElementNS(namespaceURI, qName||localName);
  2099. var len = attrs.length;
  2100. appendElement(this, el);
  2101. this.currentElement = el;
  2102. this.locator && position(this.locator,el)
  2103. for (var i = 0 ; i < len; i++) {
  2104. var namespaceURI = attrs.getURI(i);
  2105. var value = attrs.getValue(i);
  2106. var qName = attrs.getQName(i);
  2107. var attr = doc.createAttributeNS(namespaceURI, qName);
  2108. this.locator &&position(attrs.getLocator(i),attr);
  2109. attr.value = attr.nodeValue = value;
  2110. el.setAttributeNode(attr)
  2111. }
  2112. },
  2113. endElement:function(namespaceURI, localName, qName) {
  2114. var current = this.currentElement
  2115. var tagName = current.tagName;
  2116. this.currentElement = current.parentNode;
  2117. },
  2118. startPrefixMapping:function(prefix, uri) {
  2119. },
  2120. endPrefixMapping:function(prefix) {
  2121. },
  2122. processingInstruction:function(target, data) {
  2123. var ins = this.doc.createProcessingInstruction(target, data);
  2124. this.locator && position(this.locator,ins)
  2125. appendElement(this, ins);
  2126. },
  2127. ignorableWhitespace:function(ch, start, length) {
  2128. },
  2129. characters:function(chars, start, length) {
  2130. chars = _toString.apply(this,arguments)
  2131. //console.log(chars)
  2132. if(chars){
  2133. if (this.cdata) {
  2134. var charNode = this.doc.createCDATASection(chars);
  2135. } else {
  2136. var charNode = this.doc.createTextNode(chars);
  2137. }
  2138. if(this.currentElement){
  2139. this.currentElement.appendChild(charNode);
  2140. }else if(/^\s*$/.test(chars)){
  2141. this.doc.appendChild(charNode);
  2142. //process xml
  2143. }
  2144. this.locator && position(this.locator,charNode)
  2145. }
  2146. },
  2147. skippedEntity:function(name) {
  2148. },
  2149. endDocument:function() {
  2150. this.doc.normalize();
  2151. },
  2152. setDocumentLocator:function (locator) {
  2153. if(this.locator = locator){// && !('lineNumber' in locator)){
  2154. locator.lineNumber = 0;
  2155. }
  2156. },
  2157. //LexicalHandler
  2158. comment:function(chars, start, length) {
  2159. chars = _toString.apply(this,arguments)
  2160. var comm = this.doc.createComment(chars);
  2161. this.locator && position(this.locator,comm)
  2162. appendElement(this, comm);
  2163. },
  2164. startCDATA:function() {
  2165. //used in characters() methods
  2166. this.cdata = true;
  2167. },
  2168. endCDATA:function() {
  2169. this.cdata = false;
  2170. },
  2171. startDTD:function(name, publicId, systemId) {
  2172. var impl = this.doc.implementation;
  2173. if (impl && impl.createDocumentType) {
  2174. var dt = impl.createDocumentType(name, publicId, systemId);
  2175. this.locator && position(this.locator,dt)
  2176. appendElement(this, dt);
  2177. }
  2178. },
  2179. /**
  2180. * @see org.xml.sax.ErrorHandler
  2181. * @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html
  2182. */
  2183. warning:function(error) {
  2184. console.warn('[xmldom warning]\t'+error,_locator(this.locator));
  2185. },
  2186. error:function(error) {
  2187. console.error('[xmldom error]\t'+error,_locator(this.locator));
  2188. },
  2189. fatalError:function(error) {
  2190. console.error('[xmldom fatalError]\t'+error,_locator(this.locator));
  2191. throw error;
  2192. }
  2193. }
  2194. function _locator(l){
  2195. if(l){
  2196. return '\n@'+(l.systemId ||'')+'#[line:'+l.lineNumber+',col:'+l.columnNumber+']'
  2197. }
  2198. }
  2199. function _toString(chars,start,length){
  2200. if(typeof chars == 'string'){
  2201. return chars.substr(start,length)
  2202. }else{//java sax connect width xmldom on rhino(what about: "? && !(chars instanceof String)")
  2203. if(chars.length >= start+length || start){
  2204. return new java.lang.String(chars,start,length)+'';
  2205. }
  2206. return chars;
  2207. }
  2208. }
  2209. /*
  2210. * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/LexicalHandler.html
  2211. * used method of org.xml.sax.ext.LexicalHandler:
  2212. * #comment(chars, start, length)
  2213. * #startCDATA()
  2214. * #endCDATA()
  2215. * #startDTD(name, publicId, systemId)
  2216. *
  2217. *
  2218. * IGNORED method of org.xml.sax.ext.LexicalHandler:
  2219. * #endDTD()
  2220. * #startEntity(name)
  2221. * #endEntity(name)
  2222. *
  2223. *
  2224. * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/DeclHandler.html
  2225. * IGNORED method of org.xml.sax.ext.DeclHandler
  2226. * #attributeDecl(eName, aName, type, mode, value)
  2227. * #elementDecl(name, model)
  2228. * #externalEntityDecl(name, publicId, systemId)
  2229. * #internalEntityDecl(name, value)
  2230. * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/EntityResolver2.html
  2231. * IGNORED method of org.xml.sax.EntityResolver2
  2232. * #resolveEntity(String name,String publicId,String baseURI,String systemId)
  2233. * #resolveEntity(publicId, systemId)
  2234. * #getExternalSubset(name, baseURI)
  2235. * @link http://www.saxproject.org/apidoc/org/xml/sax/DTDHandler.html
  2236. * IGNORED method of org.xml.sax.DTDHandler
  2237. * #notationDecl(name, publicId, systemId) {};
  2238. * #unparsedEntityDecl(name, publicId, systemId, notationName) {};
  2239. */
  2240. "endDTD,startEntity,endEntity,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,resolveEntity,getExternalSubset,notationDecl,unparsedEntityDecl".replace(/\w+/g,function(key){
  2241. DOMHandler.prototype[key] = function(){return null}
  2242. })
  2243. /* Private static helpers treated below as private instance methods, so don't need to add these to the public API; we might use a Relator to also get rid of non-standard public properties */
  2244. function appendElement (hander,node) {
  2245. if (!hander.currentElement) {
  2246. hander.doc.appendChild(node);
  2247. } else {
  2248. hander.currentElement.appendChild(node);
  2249. }
  2250. }//appendChild and setAttributeNS are preformance key
  2251. htmlEntity={};
  2252. htmlEntity.entityMap=entityMap;
  2253. window.Parser = { DOMParser :DOMParser};