XmlLib.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  1. /*
  2. XPath.js, an JavaScript implementation of XML Path Language (XPath) Version 1.0
  3. Copyright (C) 2008 Henrik Lindqvist <henrik.lindqvist@llamalab.com>
  4. This library is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. (function (w, d, f) {
  16. if (document.implementation.hasFeature("XPath", "3.0")
  17. && !document.createNSResolver)
  18. {
  19. function XPath(e) {
  20. this.e = e;
  21. this.i = 0;
  22. this.js = [ 'with(XPath){return ', '}' ];
  23. this.expression(1, 1) || this.error();
  24. return new Function('n', 'nsr', this.js.join(''));
  25. }
  26. XPath.ie = /MSIE/.test(navigator.userAgent);
  27. XPath.prototype = {
  28. match : function(rx, x) {
  29. var m, r;
  30. if (!(m = rx.exec(this.e.substr(this.i)))
  31. || (typeof x == 'number' && !(r = m[x]))
  32. || (typeof x == 'object' && !(r = x[m[1]])))
  33. {
  34. return false;
  35. }
  36. this.m = m;
  37. this.i += m[0].length;
  38. return r || m;
  39. },
  40. error : function(m) {
  41. m = (m || 'Syntax error') + ' at index ' + this.i + ': ' + this.e.substr(this.i);
  42. var e;
  43. try {
  44. e = new XPathException(51, m);
  45. } catch (x) {
  46. e = new Error(m);
  47. }
  48. throw e;
  49. },
  50. step : function(l, r, s, n) {
  51. var i = 3;
  52. if (this.match(/^(\/\/?|\.\.?|@)\s*/, 1)) {
  53. switch (this.m[1]) {
  54. case '/':
  55. if (s)
  56. {
  57. this.error();
  58. }
  59. if (!n)
  60. {
  61. return this.step(l, r, 1);
  62. }
  63. this.js.splice(l, 0, ' axis(axes["', 'document-root', '"],');
  64. i += this.nodeTypes.node.call(this, l + i);
  65. s = 1;
  66. break;
  67. case '//':
  68. if (s)
  69. {
  70. this.error();
  71. }
  72. this.js.splice(l, 0, ' axis(axes["', 'descendant-or-self', '"],');
  73. i += this.nodeTypes.node.call(this, l + i);
  74. s = 1;
  75. break;
  76. case '.':
  77. if (!s && !n)
  78. {
  79. this.error();
  80. }
  81. this.js.splice(l, 0, ' axis(axes["', 'self', '"],');
  82. i += this.nodeTypes.node.call(this, l + i);
  83. s = 0;
  84. break;
  85. case '..':
  86. if (!s && !n)
  87. {
  88. this.error();
  89. }
  90. this.js.splice(l, 0, ' axis(axes["', 'parent', '"],');
  91. i += this.nodeTypes.node.call(this, l + i);
  92. s = 0;
  93. break;
  94. case '@':
  95. if (!s && !n)
  96. {
  97. this.error();
  98. }
  99. this.js.splice(l, 0, ' axis(axes["', 'attribute', '"],');
  100. i += this.nodeTest(l + i, 'node') || this.error('Missing nodeTest after @');
  101. s = 0;
  102. }
  103. }
  104. else if (!s && !n)
  105. {
  106. return s ? this.error() : 0;
  107. }
  108. else if (this.match(/^([ㄱ-ㅎ가-힣a-z]+(?:-[ㄱ-ㅎ가-힣a-z]+)*)\s*::\s*/, XPath.axes))
  109. {
  110. this.js.splice(l, 0, ' axis(axes["', this.m[1], '"],');
  111. i += this.nodeTest(l + i, (this.m[1] == 'attribute') ? 'node' : 'element') || this.error('Missing nodeTest after ::');
  112. s = 0;
  113. }
  114. else if (i = this.nodeTest(l, 'element'))
  115. {
  116. this.js.splice(l, 0, ' axis(axes["', 'child', '"],');
  117. i += 3;
  118. s = 0;
  119. }
  120. else
  121. {
  122. return 0;
  123. }
  124. for ( var j; j = this.predicate(l + i); i += j)
  125. {
  126. ;
  127. }
  128. if (n)
  129. {
  130. this.js.splice(r + i++, 0, n);
  131. }
  132. i += this.step(l, r + i, s);
  133. this.js.splice(r + i++, 0, ')');
  134. return i;
  135. },
  136. expression : function(l, r, p)
  137. {
  138. var o, i = this.operand(l);
  139. while (o = this.match(/^(or|and|!?=|[<>]=?|[|*+-]|div|mod)\s*/, this.operators))
  140. {
  141. if (p && p[0] >= o[0])
  142. {
  143. this.i -= this.m[0].length;
  144. break;
  145. }
  146. this.js.splice(l, 0, o[1]);
  147. i++;
  148. this.js.splice(l + i++, 0, o[2]);
  149. i += this.expression(l + i, r, o) || this.error('Missing operand');
  150. this.js.splice(l + i++, 0, o[3]);
  151. }
  152. return i;
  153. },
  154. operand : function(l)
  155. {
  156. if (this.match(/^(-?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)|"[^"]*"|'[^']*')\s*/, 1))
  157. {
  158. this.js.splice(l, 0, this.m[1]);
  159. return 1;
  160. }
  161. var fn;
  162. if (fn = this.match(/^([ㄱ-ㅎ가-힣a-z]+(?:-[ㄱ-ㅎ가-힣a-z]+)*)\s*\(\s*/, this.functions))
  163. {
  164. var i = 1, j;
  165. this.js.splice(l, 0, fn[1]);
  166. do {
  167. if (j)
  168. {
  169. this.js.splice(l + i++, 0, ',');
  170. }
  171. i += (j = this.expression(l + i, l + i));
  172. } while (j && this.match(/^,\s*/));
  173. this.match(/^\)\s*/) || this.error('Missing (');
  174. if (fn[0])
  175. {
  176. if (j)
  177. {
  178. this.js.splice(l + i++, 0, ',');
  179. }
  180. this.js.splice(l + i++, 0, fn[0]);
  181. }
  182. if (fn[2])
  183. {
  184. this.js.splice(l + i++, 0, fn[2]);
  185. }
  186. else if (j > 1)
  187. {
  188. this.error('Function has arguments');
  189. }
  190. i += this.step(l, l + i);
  191. return i;
  192. }
  193. if (this.match(/^\(\s*/))
  194. {
  195. var i = 1;
  196. this.js.splice(l, 0, '(');
  197. i += this.expression(l + i, l + i);
  198. this.match(/^\)\s*/) || this.error('Missing )');
  199. this.js.splice(l + i++, ')');
  200. return i;
  201. }
  202. return this.step(l, l, 0, '[n]');
  203. },
  204. operators : {
  205. '|' : [ 1, 'union(', ',', ')' ],
  206. 'or' : [ 1, 'bool(', ')||bool(', ')' ],
  207. 'and' : [ 2, 'bool(', ')&&bool(', ')' ],
  208. '=' : [ 3, 'compare(eq,', ',', ')' ],
  209. '!=' : [ 3, 'compare(ne,', ',', ')' ],
  210. '<' : [ 4, 'compare(lt,', ',', ')' ],
  211. '>' : [ 4, 'compare(gt,', ',', ')' ],
  212. '<=' : [ 4, 'compare(le,', ',', ')' ],
  213. '>=' : [ 4, 'compare(ge,', ',', ')' ],
  214. '+' : [ 5, 'number(', ')+number(', ')' ],
  215. '-' : [ 5, 'number(', ')-number(', ')' ],
  216. '*' : [ 6, 'number(', ')*number(', ')' ],
  217. 'div' : [ 6, 'number(', ')/number(', ')' ],
  218. 'mod' : [ 6, 'number(', ')%number(', ')' ]
  219. },
  220. functions : {
  221. // Node Set
  222. 'last' : [ 0, 'nl.length' ],
  223. 'position' : [ 0, '(i+1)' ],
  224. 'count' : [ 'nl', '(', '.length||0)' ],
  225. 'id' : [ 'n', 'id(', ')' ],
  226. 'local-name' : [ 'nl', 'localName(', ')' ],
  227. 'namespace-uri' : [ 'nl', 'namespaceURI(', ')' ],
  228. 'name' : [ 'nl', 'qName(', ')' ],
  229. // String
  230. 'string' : [ 'n', 'string(', ')' ],
  231. 'concat' : [ 0, 'concat(', ')' ],
  232. 'starts-with' : [ 0, 'startsWith(', ')' ],
  233. 'contains' : [ 0, 'contains(', ')' ],
  234. 'substring-before' : [ 0, 'substringBefore(', ')' ],
  235. 'substring-after' : [ 0, 'substringAfter(', ')' ],
  236. 'substring' : [ 0, 'substring(', ')' ],
  237. 'string-length' : [ 'n', 'string(', ').length' ],
  238. 'normalize-space' : [ 'n', 'normalizeSpace(', ')' ],
  239. 'translate' : [ 0, 'translate(', ')' ],
  240. // Boolean
  241. 'boolean' : [ 0, 'bool(', ')' ],
  242. 'not' : [ 0, '!bool(', ')' ],
  243. 'true' : [ 0, 'true ' ],
  244. 'false' : [ 0, 'false ' ],
  245. // 'lang' : [],
  246. // Number
  247. 'number' : [ 'n', 'number(', ')' ],
  248. 'floor' : [ 0, 'Math.floor(number(', '))' ],
  249. 'ceiling' : [ 0, 'Math.ceil(number(', '))' ],
  250. 'round' : [ 0, 'Math.round(number(', '))' ],
  251. 'sum' : [ 0, 'sum(', ')' ]
  252. },
  253. predicate : function(l)
  254. {
  255. var i = 0;
  256. if (this.match(/^\[\s*/))
  257. {
  258. if (i = this.expression(l, l))
  259. {
  260. this.js.splice(l, 0, 'function(n,i,nl){with(XPath){var r=');
  261. i++;
  262. this.js.splice(l + i++, 0, ';return typeof r=="number"?Math.round(r)==i+1:bool(r)}},');
  263. }
  264. this.match(/^\]\s*/) || this.error('Missing ]');
  265. }
  266. return i;
  267. },
  268. nodeTest : function(l, t)
  269. {
  270. var fn;
  271. if (fn = this.match(/^([ㄱ-ㅎ가-힣a-z]+(?:-[ㄱ-ㅎ가-힣a-z]+)*)\(([^)]*)\)\s*/, this.nodeTypes))
  272. {
  273. return fn.call(this, l, this.m[2]);
  274. }
  275. if (this.match(/^\*\s*/))
  276. {
  277. return this.nodeTypes[t].call(this, l);
  278. }
  279. return this.nodeName(l);
  280. },
  281. nodeType : function(l, t)
  282. {
  283. this.js.splice(l, 0, 'function(n){return n.nodeType==', t, '},');
  284. return 3;
  285. },
  286. nodeTypes :
  287. {
  288. 'node' : function(l)
  289. {
  290. this.js.splice(l, 0, 'null,');
  291. return 1;
  292. },
  293. 'element' : function(l)
  294. {
  295. return this.nodeType(l, 1);
  296. },
  297. 'attribute' : function(l)
  298. {
  299. return this.nodeType(l, 2);
  300. },
  301. 'text' : function(l)
  302. {
  303. return this.nodeType(l, 3);
  304. },
  305. 'processing-instruction' : function(l, t)
  306. {
  307. if (!t)
  308. {
  309. return this.nodeType(l, 7);
  310. }
  311. this.js.splice(l, 0, 'function(n){return n.nodeType==7&&n.target==', t, '},');
  312. return 3;
  313. },
  314. 'comment' : function(l)
  315. {
  316. return this.nodeType(l, 8);
  317. }
  318. },
  319. nodeName : function(l)
  320. {
  321. if (!this.match(/^([ㄱ-ㅎ가-힣a-zA-Z0-9_]+(?:-?[ㄱ-ㅎ가-힣a-zA-Z0-9]+)*)(?::([ㄱ-ㅎ가-힣a-zA-Z0-9_]+(?:-?[ㄱ-ㅎ가-힣a-zA-Z0-9]+)*))?\s*/, 1))
  322. {
  323. return 0;
  324. }
  325. if (this.m[2])
  326. {
  327. this.js.splice(l, 0, 'function(n){if(!nsr)throw new XPathException(14);return "',
  328. this.m[2], '"==', XPath.ie ? 'n.baseName' : 'n.localName',
  329. '&&nsr.lookupNamespaceURI("', this.m[1],
  330. '")==n.namespaceURI},');
  331. return 7;
  332. }
  333. else
  334. {
  335. this.js.splice(l, 0, 'function(n){return/^', this.m[1], '$/i.test(n.nodeName)},');
  336. return 3;
  337. }
  338. }
  339. };
  340. XPath.order = function(l, r)
  341. {
  342. var x = l.compareDocumentPosition ? l.compareDocumentPosition(r) : XPath.compareDocumentPosition.call(l, r);
  343. if (x & 32)
  344. {
  345. l = Array.prototype.indexOf.call(l.attributes, l);
  346. r = Array.prototype.indexOf.call(r.attributes, r);
  347. return (l < r) ? -1 : (l > r) ? 1 : 0;
  348. }
  349. if (!x)
  350. {
  351. if (l == r)
  352. {
  353. return 0;
  354. }
  355. if ((l = l.ownerElement) && (r = r.ownerElement))
  356. {
  357. return XPath.order(l, r);
  358. }
  359. return XPath.ie ? 1 : 0;
  360. }
  361. return 3 - ((x & 6) || 3);
  362. };
  363. // Runtime - Operand
  364. XPath.compare = function(fn, l, r)
  365. {
  366. if (l instanceof Array && r instanceof Array)
  367. {
  368. var ls = l.map(this.string), rs = r.map(this.string);
  369. for (l = ls.length; --l >= 0;)
  370. {
  371. for (r = rs.length; --r >= 0;)
  372. {
  373. if (!fn(ls[l], rs[r]))
  374. {
  375. return false;
  376. }
  377. }
  378. }
  379. return true;
  380. }
  381. if (l instanceof Array)
  382. {
  383. for ( var i = l.length; --i >= 0;)
  384. {
  385. if (!fn(this[typeof r](l[i]), r))
  386. {
  387. return false;
  388. }
  389. }
  390. return l.length > 0;
  391. }
  392. if (r instanceof Array)
  393. {
  394. for ( var i = r.length; --i >= 0;)
  395. {
  396. if (!fn(l, this[typeof l](r[i])))
  397. {
  398. return false;
  399. }
  400. }
  401. return r.length > 0;
  402. }
  403. if (typeof l == 'boolean' || typeof r == 'boolean')
  404. {
  405. return fn(this.bool(l), this.bool(r));
  406. }
  407. if (typeof l == 'number' || typeof r == 'number')
  408. {
  409. return fn(this.number(l), this.number(r));
  410. }
  411. return fn(this.string(l), this.string(r));
  412. };
  413. XPath.eq = function(l, r)
  414. {
  415. return l == r;
  416. };
  417. XPath.ne = function(l, r)
  418. {
  419. return l != r;
  420. };
  421. XPath.lt = function(l, r)
  422. {
  423. return l < r;
  424. };
  425. XPath.gt = function(l, r)
  426. {
  427. return l > r;
  428. };
  429. XPath.le = function(l, r)
  430. {
  431. return l <= r;
  432. };
  433. XPath.ge = function(l, r)
  434. {
  435. return l >= r;
  436. };
  437. // Runtime - Node Set
  438. XPath.id = function(s, n)
  439. {
  440. if (arguments.length == 1)
  441. {
  442. n = s;
  443. }
  444. var nl = [];
  445. for ( var id = this.string(s).split(/\s+/), i = id.length; --i >= 0;)
  446. {
  447. if (s = (n.ownerDocument || n).getElementById(id[i]))
  448. {
  449. nl.push(s);
  450. }
  451. }
  452. return nl.sort(this.order);
  453. };
  454. XPath.localName = new Function('nl', 'return (nl.length&&nl[0].' + (XPath.ie ? 'baseName' : 'localName') + ')||""');
  455. XPath.namespaceURI = function(nl)
  456. {
  457. return (nl.length && nl[0].namespaceURI) || '';
  458. };
  459. XPath.qName = function(nl)
  460. {
  461. return (nl.length && nl[0].nodeName) || '';
  462. };
  463. XPath.union = function(a, b)
  464. {
  465. if (!a.length)
  466. {
  467. return b;
  468. }
  469. if (!b.length)
  470. {
  471. return a;
  472. }
  473. var nl = [], i = a.length - 1, j = b.length - 1;
  474. for (;;)
  475. {
  476. switch (this.order(a[i], b[j]))
  477. {
  478. case -1:
  479. nl.unshift(b[j--]);
  480. break;
  481. case 0:
  482. j--;
  483. case 1:
  484. nl.unshift(a[i--]);
  485. break;
  486. default:
  487. throw new Error('Invalid order');
  488. }
  489. if (i < 0)
  490. {
  491. if (++j > 0)
  492. {
  493. nl.unshift.apply(nl, nl.slice.call(b, 0, j));
  494. }
  495. break;
  496. }
  497. if (j < 0)
  498. {
  499. if (++i > 0)
  500. {
  501. nl.unshift.apply(nl, nl.slice.call(a, 0, i));
  502. }
  503. break;
  504. }
  505. }
  506. return nl;
  507. };
  508. // Runtime - String
  509. XPath.string = XPath.object = function(v)
  510. {
  511. if (v instanceof Array && typeof (v = v[0]) == 'undefined')
  512. {
  513. return '';
  514. }
  515. if (typeof v == 'string')
  516. {
  517. return v;
  518. }
  519. switch (v.nodeType)
  520. {
  521. case 1:
  522. case 9:
  523. case 11:
  524. return Array.prototype.map.call(v.childNodes, this.string, this).join('');
  525. default:
  526. return v.nodeValue || '';
  527. }
  528. return String(v);
  529. };
  530. XPath.concat = function()
  531. {
  532. return Array.prototype.map.call(arguments, this.string, this).join('');
  533. };
  534. XPath.startsWith = function(a, b)
  535. {
  536. return this.string(a).substr(0, (b = this.string(b)).length) == b;
  537. };
  538. XPath.contains = function(a, b)
  539. {
  540. return this.string(a).indexOf(this.string(b)) != -1;
  541. };
  542. XPath.substringBefore = function(a, b)
  543. {
  544. a = this.string(a);
  545. b = a.indexOf(this.string(b));
  546. return b != -1 ? a.substr(0, b) : '';
  547. };
  548. XPath.substringAfter = function(a, b)
  549. {
  550. a = this.string(a);
  551. b = this.string(b);
  552. var i = a.indexOf(b);
  553. return i != -1 ? a.substr(i + b.length) : '';
  554. };
  555. XPath.substring = function(s, i, l)
  556. {
  557. s = this.string(s);
  558. i = Math.round(this.number(i)) - 1;
  559. return (arguments.length == 2) ? s.substr(i < 0 ? 0 : i) : s.substr(i < 0 ? 0 : i, Math.round(this.number(l)) - Math.max(0, -i));
  560. };
  561. XPath.normalizeSpace = function(s)
  562. {
  563. return this.string(s).replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/g, ' ');
  564. };
  565. XPath.translate = function(a, b, c)
  566. {
  567. a = this.string(a);
  568. b = this.string(b);
  569. c = this.string(c);
  570. var o = [], l = a.length, i = 0, j, x;
  571. while (--l >= 0)
  572. {
  573. if ((j = b.indexOf(x = a.charAt(i++))) == -1 || (x = c.charAt(j)))
  574. {
  575. o.push(x);
  576. }
  577. }
  578. return o.join('');
  579. };
  580. // Runtime - Boolean
  581. XPath.bool = XPath['boolean'] = function(v)
  582. {
  583. if (typeof v == 'boolean')
  584. {
  585. return v;
  586. }
  587. if (v instanceof Array || typeof v == 'string')
  588. {
  589. return v.length > 0;
  590. }
  591. return Boolean(v);
  592. };
  593. // Runtime - Number
  594. XPath.number = function(v)
  595. {
  596. if (v instanceof Array && typeof (v = v[0]) == 'undefined')
  597. {
  598. return 0;
  599. }
  600. if (typeof v == 'number')
  601. {
  602. return v;
  603. }
  604. if (typeof v == 'boolean')
  605. {
  606. return v ? 1 : 0;
  607. }
  608. return Number(this.string(v));
  609. };
  610. XPath.sum = function(nl)
  611. {
  612. var r = 0, i = nl.length;
  613. while (--i >= 0)
  614. {
  615. r += this.number(nl[i]);
  616. }
  617. return r;
  618. };
  619. // Runtime - Axis
  620. XPath.walk = function(n, nl)
  621. {
  622. var x, c = n.firstChild;
  623. while (c)
  624. {
  625. nl.push(c);
  626. if (x = c.firstChild)
  627. {
  628. c = x;
  629. }
  630. else
  631. {
  632. for (x = c; !(c = x.nextSibling) && (x = x.parentNode) && (x != n);)
  633. {
  634. ;
  635. }
  636. }
  637. }
  638. return nl;
  639. };
  640. XPath.axes = {
  641. 'ancestor' : function(n)
  642. {
  643. var nl = [];
  644. while (n = n.parentNode)
  645. {
  646. nl.unshift(n);
  647. }
  648. return nl;
  649. },
  650. 'ancestor-or-self' : function(n)
  651. {
  652. var nl = [];
  653. do
  654. {
  655. nl.unshift(n)
  656. }
  657. while (n = n.parentNode);
  658. return nl;
  659. },
  660. 'attribute' : new Function
  661. (
  662. 'n',
  663. 'var nl = [], a = n.attributes;if(a){attr:for(var x,i=a.length;--i>=0;){if(!(x=a[i]).specified){' + (XPath.ie ? 'switch(x.nodeName){case"selected":case"value":if(x.nodeValue)break;default:continue attr;}'
  664. : 'continue;') + '}nl.unshift(x);}}return nl;'),
  665. 'child' : function(n)
  666. {
  667. return n.childNodes || [];
  668. },
  669. 'descendant' : function(n)
  670. {
  671. return this.walk(n, []);
  672. },
  673. 'descendant-or-self' : function(n)
  674. {
  675. return this.walk(n, [ n ]);
  676. },
  677. 'following' : function(n)
  678. {
  679. var nl = [], x;
  680. while (n)
  681. {
  682. if (x = n.nextSibling)
  683. {
  684. nl.push(n = x);
  685. if (x = n.firstChild)
  686. {
  687. nl.push(n = x);
  688. }
  689. }
  690. else
  691. {
  692. n = n.parentNode;
  693. }
  694. }
  695. return nl;
  696. },
  697. 'following-sibling' : function(n)
  698. {
  699. var nl = [];
  700. while (n = n.nextSibling)
  701. {
  702. nl.push(n);
  703. }
  704. return nl;
  705. },
  706. 'parent' : function(n)
  707. {
  708. return n.parentNode ? [ n.parentNode ] : [];
  709. },
  710. 'preceding' : function(n)
  711. {
  712. var nl = [], x, p = n.parentNode;
  713. while (n)
  714. {
  715. if (x = n.previousSibling)
  716. {
  717. for (n = x; x = n.lastChild; n = x)
  718. {
  719. ;
  720. }
  721. nl.unshift(n);
  722. }
  723. else if (n = n.parentNode)
  724. {
  725. if (n == p)
  726. {
  727. p = p.parentNode;
  728. }
  729. else
  730. {
  731. nl.unshift(n);
  732. }
  733. }
  734. }
  735. return nl;
  736. },
  737. 'preceding-sibling' : function(n)
  738. {
  739. var nl = [];
  740. while (n = n.previousSibling)
  741. {
  742. nl.unshift(n);
  743. }
  744. return nl;
  745. },
  746. 'self' : function(n)
  747. {
  748. return [ n ];
  749. },
  750. 'document-root' : function(n)
  751. {
  752. return [ n.ownerDocument || n ];
  753. }
  754. };
  755. XPath.axis = function(fn, nt/* , pr..., nl */)
  756. {
  757. var r, x, al = arguments.length - 1, nl = arguments[al], ap = Array.prototype;
  758. for ( var i = 0, j, l = nl.length; --l >= 0;)
  759. {
  760. x = fn.call(this, nl[i++]);
  761. if (nt && x.length)
  762. {
  763. x = ap.filter.call(x, nt, this);
  764. }
  765. for (j = 2; j < al && x.length; x = ap.filter.call(x, arguments[j++], this))
  766. {
  767. ;
  768. }
  769. r = r ? this.union(r, x) : x;
  770. }
  771. return r || [];
  772. };
  773. XPath.cache = {};
  774. /**
  775. * Extends the native <code>Node</code> class with additional
  776. * functionality.
  777. * <p>
  778. * Not available in Internet Exporer which don&rsquo;t have a
  779. * <code>Node</code> class.
  780. * </p>
  781. * <p>
  782. * See <a
  783. * href="http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/core.html#ID-1950641247"
  784. * target="_blank">http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/core.html#ID-1950641247</a></code>.
  785. * </p>
  786. *
  787. * @class Node
  788. * @author Henrik Lindqvist &lt;<a
  789. * href="mailto:henrik.lindqvist@llamalab.com">henrik.lindqvist@llamalab.com</a>&gt;
  790. */
  791. /**
  792. * Compares a node with this node with regard to their position in the
  793. * document and according to the document order.
  794. * <p>
  795. * When comparing two attribute nodes; <code>32</code> is returned if they
  796. * have the same <code>ownerElement</code>, otherwise <code>0</code>.
  797. * This is probably not standard, but it&rsquo;s what Firefox return, so we
  798. * do the same.
  799. * </p>
  800. *
  801. * <pre>
  802. * DOCUMENT_POSITION_DISCONNECTED = 1;
  803. * DOCUMENT_POSITION_PRECEDING = 2;
  804. * DOCUMENT_POSITION_FOLLOWING = 4;
  805. * DOCUMENT_POSITION_CONTAINS = 8;
  806. * DOCUMENT_POSITION_IS_CONTAINED = 16;
  807. * DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 32;
  808. * </pre>
  809. *
  810. * <p>
  811. * See <a
  812. * href="http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/core.html#Node3-compareDocumentPosition"
  813. * target="_blank">http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/core.html#Node3-compareDocumentPosition</a></code>.
  814. * </p>
  815. *
  816. * @function {number} compareDocumentPosition
  817. * @param {Node}
  818. * n - node to compare against.
  819. * @returns <code>0</code> for nodes are equals or a number with some of
  820. * the above bits set.
  821. */
  822. /**
  823. * Check if this node contains another node.
  824. *
  825. * @function {boolean} contains
  826. * @param {Node}
  827. * n - node to compare against.
  828. * @returns <code>true</code> if <code>this</code> node cotains node
  829. * <code>n</code>.
  830. */
  831. function compareDocumentPosition(n)
  832. {
  833. if (this == n)
  834. {
  835. return 0; // Same
  836. }
  837. if (this.nodeType == 2 && n.nodeType == 2)
  838. {
  839. return (this.ownerElement && this.ownerElement == n.ownerElement) ? 32 : 0; // IMPLEMENT_SPECIFIC
  840. }
  841. var l = this.ownerElement || this, r = n.ownerElement || n;
  842. if (l.sourceIndex >= 0 && r.sourceIndex >= 0 && l.contains && r.contains)
  843. {
  844. return (((l.contains(r) && 16) || (r.contains(l) && 8)) | ((l.sourceIndex < r.sourceIndex && 4) || (r.sourceIndex < l.sourceIndex && 2))) || 1;
  845. }
  846. var la = l, ra = r, ld = 0, rd = 0;
  847. while (la = la.parentNode)
  848. {
  849. ld++;
  850. }
  851. while (ra = ra.parentNode)
  852. {
  853. rd++;
  854. }
  855. if (ld > rd)
  856. {
  857. while (ld-- != rd)
  858. {
  859. l = l.parentNode;
  860. }
  861. if (l == r)
  862. {
  863. return 2 | 8; // Preceding|Contains
  864. }
  865. }
  866. else if (rd > ld)
  867. {
  868. while (rd-- != ld)
  869. {
  870. r = r.parentNode;
  871. }
  872. if (r == l)
  873. {
  874. return 4 | 16; // Following|Contained By
  875. }
  876. }
  877. while ((la = l.parentNode) != (ra = r.parentNode))
  878. {
  879. if (!(l = la) || !(r = ra))
  880. {
  881. return 1; // Disconnected
  882. }
  883. }
  884. while (l = l.nextSibling)
  885. {
  886. if (l == r)
  887. {
  888. return 4; // Following
  889. }
  890. }
  891. return 2; // Preceding
  892. };
  893. if (w.Node)
  894. {
  895. var np = w.Node.prototype;
  896. if (f || !np.compareDocumentPosition)
  897. {
  898. np.compareDocumentPosition = compareDocumentPosition;
  899. }
  900. if (f || !np.contains)
  901. {
  902. np.contains = function(n)
  903. {
  904. return Boolean(this.compareDocumentPosition(n) & 16);
  905. };
  906. }
  907. }
  908. else
  909. {
  910. XPath.compareDocumentPosition = compareDocumentPosition;
  911. }
  912. /**
  913. * Exception throw when parser or expression fails.
  914. * <p>
  915. * See
  916. * <code><a href="http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathException" target="_blank">http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathException</a></code>.
  917. * </p>
  918. *
  919. * @class XPathException
  920. * @author Henrik Lindqvist &lt;<a
  921. * href="mailto:henrik.lindqvist@llamalab.com">henrik.lindqvist@llamalab.com</a>&gt;
  922. */
  923. /**
  924. * Namespace error.
  925. *
  926. * @property {static read number} NAMESPACE_ERR
  927. */
  928. /**
  929. * Expression syntax error.
  930. *
  931. * @property {static read number} INVALID_EXPRESSION_ERR
  932. */
  933. /**
  934. * Result type error.
  935. *
  936. * @property {static read number} TYPE_ERR
  937. */
  938. /**
  939. * XPathException constructor.
  940. *
  941. * @constructor XPathException
  942. * @param {number}
  943. * c - error code.
  944. * @param {string}
  945. * m - error message.
  946. * @see NAMESPACE_ERR
  947. * @see INVALID_EXPRESSION_ERR
  948. * @see TYPE_ERR
  949. */
  950. /**
  951. * Exception name.
  952. *
  953. * @property {read string} name
  954. */
  955. /**
  956. * Exception code.
  957. *
  958. * @property {read number} code
  959. * @see NAMESPACE_ERR
  960. * @see INVALID_EXPRESSION_ERR
  961. * @see TYPE_ERR
  962. */
  963. /**
  964. * Exception message.
  965. *
  966. * @property {read string} message
  967. */
  968. if (f || !w.XPathException)
  969. {
  970. function XPathException(c, m)
  971. {
  972. this.name = 'XPathException';
  973. this.code = c;
  974. this.message = m;
  975. };
  976. var e = XPathException, p = new Error;
  977. p.toString = function()
  978. {
  979. return this.name + ':' + this.message;
  980. };
  981. e.prototype = p;
  982. e.NAMESPACE_ERR = 14;
  983. e.INVALID_EXPRESSION_ERR = 51;
  984. e.TYPE_ERR = 52;
  985. w.XPathException = e;
  986. }
  987. /**
  988. * Namespace resolver.
  989. * <p>
  990. * See
  991. * <code><a href="http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathNSResolver" target="_blank">http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathNSResolver</a></code>.
  992. * </p>
  993. *
  994. * @class XPathNSResolver
  995. * @see XPathEvaluator.createNSResolver
  996. * @author Henrik Lindqvist &lt;<a
  997. * href="mailto:henrik.lindqvist@llamalab.com">henrik.lindqvist@llamalab.com</a>&gt;
  998. */
  999. /**
  1000. * Look up a namespace URI by it&rsquo;s prefix use in document.
  1001. *
  1002. * @function {string} lookupNamespaceURI
  1003. * @param {string}
  1004. * p - <code>xmlns:</code> prefix, empty string for
  1005. * <code>targetNamespace</code>.
  1006. * @returns associated namespace URI, or <code>undefined</code> if none is
  1007. * found.
  1008. */
  1009. if (f || !w.XPathNSResolver)
  1010. {
  1011. function XPathNSResolver(n)
  1012. {
  1013. this.ns = {};
  1014. for ( var m, a, i = n.attributes.length; --i >= 0;)
  1015. {
  1016. if (m = /xmlns:(.+)/.exec((a = n.attributes[i]).nodeName))
  1017. {
  1018. this.ns[m[1]] = a.nodeValue;
  1019. }
  1020. }
  1021. this.ns[''] = n.getAttribute('targetNamespace');
  1022. }
  1023. XPathNSResolver.prototype = {
  1024. lookupNamespaceURI : function(p) {
  1025. return this.ns[p || ''];
  1026. }
  1027. };
  1028. w.XPathNSResolver = XPathNSResolver;
  1029. }
  1030. /**
  1031. * A pre-parsed XPath expression.
  1032. * <p>
  1033. * See
  1034. * <code><a href="http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathExpression" target="_blank">http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathExpression</a></code>.
  1035. * </p>
  1036. *
  1037. * @class XPathExpression
  1038. * @see XPathEvaluator.createExpression
  1039. * @author Henrik Lindqvist &lt;<a
  1040. * href="mailto:henrik.lindqvist@llamalab.com">henrik.lindqvist@llamalab.com</a>&gt;
  1041. */
  1042. /**
  1043. * Evaluate this pre-parsed expression.
  1044. *
  1045. * @function {XPathResult} evaluate
  1046. * @param {Node}
  1047. * n - context node.
  1048. * @param {number}
  1049. * rt - return type, see <code>{@link XPathResult}</code>.
  1050. * @param {XPathResult}
  1051. * r - <code>{@link XPathResult}</code> that maybe reuse, or
  1052. * <code>null</code>.
  1053. * @returns a <code>{@link XPathResult}</code>.
  1054. */
  1055. if (f || !w.XPathExpression)
  1056. {
  1057. function XPathExpression(e, nsr)
  1058. {
  1059. this.fn = XPath.cache[e] || (XPath.cache[e] = new XPath(e));
  1060. this.nsr = nsr;
  1061. }
  1062. XPathExpression.prototype =
  1063. {
  1064. evaluate : function(n, rt)
  1065. {
  1066. return new XPathResult(this.fn(n, this.nsr), rt);
  1067. }
  1068. };
  1069. w.XPathExpression = XPathExpression;
  1070. }
  1071. /**
  1072. * Container for XPath results.
  1073. * <p>
  1074. * See
  1075. * <code><a href="http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathResult" target="_blank">http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathResult</a></code>.
  1076. * </p>
  1077. *
  1078. * @class XPathResult
  1079. * @see XPathEvaluator.evaluate
  1080. * @see XPathExpression.evaluate
  1081. * @author Henrik Lindqvist &lt;<a
  1082. * href="mailto:henrik.lindqvist@llamalab.com">henrik.lindqvist@llamalab.com</a>&gt;
  1083. */
  1084. /**
  1085. * Result will be accessed unconverted as the expression returned it.
  1086. *
  1087. * @property {static read number} ANY_TYPE
  1088. */
  1089. /**
  1090. * Result will be accessed as a number.
  1091. *
  1092. * @property {static read number} NUMBER_TYPE
  1093. * @see numberValue
  1094. */
  1095. /**
  1096. * Result will be accessed as a string.
  1097. *
  1098. * @property {static read number} STRING_TYPE
  1099. * @see stringValue
  1100. */
  1101. /**
  1102. * Result will be accessed as boolean.
  1103. *
  1104. * @property {static read number} BOOLEAN_TYPE
  1105. * @see booleanValue
  1106. */
  1107. /**
  1108. * Result will be accessed iteratively, node order insignificant.
  1109. * <p>
  1110. * This is equal to <code>{@link ORDERED_NODE_ITERATOR_TYPE}</code> since
  1111. * the result is always document-ordered.
  1112. * </p>
  1113. *
  1114. * @property {static read number} UNORDERED_NODE_ITERATOR_TYPE
  1115. * @see iterateNext
  1116. */
  1117. /**
  1118. * Result will be accessed iteratively which must be document-ordered.
  1119. *
  1120. * @property {static read number} ORDERED_NODE_ITERATOR_TYPE
  1121. * @see iterateNext
  1122. */
  1123. /**
  1124. * Result will be accessed as a snapshot list of nodes, node order
  1125. * insignificant.
  1126. * <p>
  1127. * This is equal to <code>{@link ORDERED_NODE_SNAPSHOT_TYPE}</code> since
  1128. * the result is always document-ordered.
  1129. * </p>
  1130. *
  1131. * @property {static read number} UNORDERED_NODE_SNAPSHOT_TYPE
  1132. * @see snapshotLength
  1133. * @see snapshotItem
  1134. */
  1135. /**
  1136. * Result will be accessed as a snapshot list of nodes which must be
  1137. * document-ordered.
  1138. *
  1139. * @property {static read number} ORDERED_NODE_SNAPSHOT_TYPE
  1140. * @see snapshotLength
  1141. * @see snapshotItem
  1142. */
  1143. /**
  1144. * Result will be accessed as a single node value, any of the resulting
  1145. * nodes.
  1146. * <p>
  1147. * This is equal to <code>{@link FIRST_ORDERED_NODE_TYPE}</code> since the
  1148. * result is always document-ordered.
  1149. * </p>
  1150. *
  1151. * @property {static read number} ANY_UNORDERED_NODE_TYPE
  1152. * @see singleNodeValue
  1153. */
  1154. /**
  1155. * Result will be accessed as a single node value, the first resulting node
  1156. * in document-ordered.
  1157. *
  1158. * @property {static read number} FIRST_ORDERED_NODE_TYPE
  1159. * @see singleNodeValue
  1160. */
  1161. /**
  1162. * Convert result to number.
  1163. *
  1164. * @property {static read number} NUMBER_TYPE
  1165. */
  1166. /**
  1167. * Convert result to number.
  1168. *
  1169. * @property {static read number} NUMBER_TYPE
  1170. */
  1171. /**
  1172. * Convert result to number.
  1173. *
  1174. * @property {static read number} NUMBER_TYPE
  1175. */
  1176. /**
  1177. * Convert result to number.
  1178. *
  1179. * @property {static read number} NUMBER_TYPE
  1180. */
  1181. /**
  1182. * Convert result to number.
  1183. *
  1184. * @property {static read number} NUMBER_TYPE
  1185. */
  1186. /**
  1187. * Resulting number.
  1188. *
  1189. * @property {read number} numberValue
  1190. * @see NUMBER_TYPE
  1191. */
  1192. /**
  1193. * Resulting string.
  1194. *
  1195. * @property {read string} stringValue
  1196. * @see STRING_TYPE
  1197. */
  1198. /**
  1199. * Resulting boolean.
  1200. *
  1201. * @property {read boolean} booleanValue
  1202. * @see BOOLEAN_TYPE
  1203. */
  1204. /**
  1205. * Signifies that the iterator has become invalid.
  1206. *
  1207. * @property {read boolean} invalidIteratorState
  1208. * @see UNORDERED_NODE_ITERATOR_TYPE
  1209. * @see ORDERED_NODE_ITERATOR_TYPE
  1210. */
  1211. /**
  1212. * The number of nodes in the result snapshot.
  1213. *
  1214. * @property {read number} snapshotLength
  1215. * @see UNORDERED_NODE_SNAPSHOT_TYPE
  1216. * @see ORDERED_NODE_SNAPSHOT_TYPE
  1217. */
  1218. /**
  1219. * The value of this single node result, maybe <code>undefined</code>.
  1220. *
  1221. * @property {read object} singleNodeValue
  1222. * @see ANY_UNORDERED_NODE_TYPE
  1223. * @see FIRST_ORDERED_NODE_TYPE
  1224. */
  1225. /**
  1226. * Unconverted result as returned by our internal evaluator.
  1227. * <p>
  1228. * This is a non-standard property which is set to the raw unconverted
  1229. * result from our expression evaluator. It&rsquo;s of the type
  1230. * <code>number</code>, <code>string</code>, <code>boolean</code> or
  1231. * an <code>{@link Array}</code> with nodes depending on expression. If
  1232. * you prefer to work with arrays instead of
  1233. * <code>{@link XPathResult.snapshotItem}</code> You can check for this
  1234. * property and use it directly.
  1235. * </p>
  1236. * <h3>Example</h3>
  1237. *
  1238. * <pre>
  1239. * function selectNodes(expr) {
  1240. * // Cross-browser safe way of selecting nodes and return an Array
  1241. * var r = document.evaluate('//LI', document, null, 7, null);
  1242. * if (typeof r.value != 'undefined')
  1243. * return r.value;
  1244. * var a = [];
  1245. * for ( var i = r.snapshotLength; --i &gt;= 0; a[i] = r.snapshotItem(i))
  1246. * ;
  1247. * return a;
  1248. * }
  1249. * </pre>
  1250. *
  1251. * @property {read object} value
  1252. * @see ANY_TYPE
  1253. */
  1254. /**
  1255. * Iterates and returns the next node from the resuling nodes.
  1256. *
  1257. * @function {object} iterateNext
  1258. * @returns a <code>Node</code>, or <code>undefined</code> if there are
  1259. * no more nodes.
  1260. */
  1261. /**
  1262. * Returns the <code>index</code>th item in the snapshot collection.
  1263. *
  1264. * @function {object} snapshotItem
  1265. * @param {number}
  1266. * i - index of resuling node to return.
  1267. * @returns the <code>Node</code>, at provided index or
  1268. * <code>undefined</code> if invalid.
  1269. */
  1270. if (f || !w.XPathResult)
  1271. {
  1272. function XPathResult(r, rt)
  1273. {
  1274. if (rt == 0)
  1275. {
  1276. switch (typeof r)
  1277. {
  1278. default:
  1279. rt++;
  1280. case 'boolean':
  1281. rt++;
  1282. case 'string':
  1283. rt++;
  1284. case 'number':
  1285. rt++;
  1286. }
  1287. }
  1288. this.resultType = rt;
  1289. switch (rt)
  1290. {
  1291. case 1:
  1292. this.numberValue = XPath.number(r);
  1293. return;
  1294. case 2:
  1295. this.stringValue = XPath.string(r);
  1296. return;
  1297. case 3:
  1298. this.booleanValue = XPath.bool(r);
  1299. return;
  1300. case 4:
  1301. case 5:
  1302. if (r instanceof Array)
  1303. {
  1304. this.value = r;
  1305. this.index = 0;
  1306. this.invalidIteratorState = false;
  1307. return;
  1308. }
  1309. break;
  1310. case 6:
  1311. case 7:
  1312. if (r instanceof Array)
  1313. {
  1314. this.value = r;
  1315. this.snapshotLength = r.length;
  1316. return;
  1317. }
  1318. else if (r instanceof NodeList)
  1319. {
  1320. this.value = r;
  1321. this.snapshotLength = r.length;
  1322. return;
  1323. }
  1324. break;
  1325. case 8:
  1326. case 9:
  1327. if (r instanceof Array)
  1328. {
  1329. this.singleNodeValue = r[0];
  1330. return;
  1331. }
  1332. }
  1333. throw new XPathException(52);
  1334. }
  1335. var r = XPathResult;
  1336. r.ANY_TYPE = 0;
  1337. r.NUMBER_TYPE = 1;
  1338. r.STRING_TYPE = 2;
  1339. r.BOOLEAN_TYPE = 3;
  1340. r.UNORDERED_NODE_ITERATOR_TYPE = 4;
  1341. r.ORDERED_NODE_ITERATOR_TYPE = 5;
  1342. r.UNORDERED_NODE_SNAPSHOT_TYPE = 6;
  1343. r.ORDERED_NODE_SNAPSHOT_TYPE = 7;
  1344. r.ANY_UNORDERED_NODE_TYPE = 8;
  1345. r.FIRST_ORDERED_NODE_TYPE = 9;
  1346. r.prototype = {
  1347. iterateNext : function()
  1348. {
  1349. switch (this.resultType)
  1350. {
  1351. case 4:
  1352. case 5:
  1353. return this.value[this.index++];
  1354. }
  1355. throw new XPathException(52);
  1356. },
  1357. snapshotItem : function(i)
  1358. {
  1359. switch (this.resultType)
  1360. {
  1361. case 6:
  1362. case 7:
  1363. return this.value[i];
  1364. }
  1365. throw new XPathException(52);
  1366. }
  1367. };
  1368. w.XPathResult = r;
  1369. }
  1370. /**
  1371. * An interface with the XPath functionality.
  1372. * <p>
  1373. * <code>Document.prototype</code> and/or <code>document</code> will be
  1374. * extended using <code>{@link install}</code> to implements it&rsquo;s
  1375. * functions.
  1376. * </p>
  1377. * <p>
  1378. * See
  1379. * <code><a href="http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator" target="_blank">http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator</a></code>.
  1380. * </p>
  1381. *
  1382. * @interface XPathEvaluator
  1383. * @author Henrik Lindqvist &lt;<a
  1384. * href="mailto:henrik.lindqvist@llamalab.com">henrik.lindqvist@llamalab.com</a>&gt;
  1385. */
  1386. /**
  1387. * Non-standard function that extends the provided object with
  1388. * <code>{@link XPathEvaluator}</code> functions.
  1389. *
  1390. * @function {static} install
  1391. * @param {object}
  1392. * o - object (i.e document node) to extend.
  1393. * @param {optional
  1394. * boolean} f - force replace the build-in function even if they
  1395. * exists.
  1396. */
  1397. /**
  1398. * Creates a pre-parsed expression.
  1399. *
  1400. * @function {XPathExpression} createExpression
  1401. * @param {string}
  1402. * e - expression.
  1403. * @param {XPathNSResolver}
  1404. * nsr - namespace resolver to use when evaluating, or
  1405. * <code>null</code>.
  1406. * @returns a new <code>{@link XPathExpression}</code>.
  1407. */
  1408. /**
  1409. * Create a namespace resolver by scanning a node for <code>xmlns:</code>
  1410. * attributes.
  1411. *
  1412. * @function {XPathNSResolver} createNSResolver
  1413. * @param {Node}
  1414. * n - an <code>Node</code> with defined namespace attributes
  1415. * (i.e the documentElement).
  1416. * @returns a new <code>{@link XPathNSResolver}</code>.
  1417. */
  1418. /**
  1419. * Evaluate an expression.
  1420. * <p>
  1421. * Same as <code>new XPathExpression(e, nsr).evaluate(n, rt)</code>.
  1422. * </p>
  1423. *
  1424. * @function {XPathResult} evaluate
  1425. * @param {string}
  1426. * e - XPath expression string.
  1427. * @param {Node}
  1428. * n - context node.
  1429. * @param {XPathNSResolver}
  1430. * nsr - namespace resolver to use when evaluating, or
  1431. * <code>null</code>.
  1432. * @param {number}
  1433. * rt - return type, see <code>{@link XPathResult}</code>.
  1434. * @param {XPathResult}
  1435. * r - <code>{@link XPathResult}</code> that maybe reuse, or
  1436. * <code>null</code>. Ignored.
  1437. * @returns a <code>{@link XPathResult}</code>.
  1438. */
  1439. if (f || !w.XPathEvaluator)
  1440. {
  1441. function XPathEvaluator() {};
  1442. var e = XPathEvaluator;
  1443. e.prototype = {
  1444. createExpression : function(e, nsr)
  1445. {
  1446. return new XPathExpression(e, nsr);
  1447. },
  1448. createNSResolver : function(n)
  1449. {
  1450. return new XPathNSResolver(n);
  1451. },
  1452. evaluate : function(e, n, nsr, rt)
  1453. {
  1454. return new XPathExpression(e, nsr).evaluate(n, rt);
  1455. }
  1456. };
  1457. e.install = function(o, f)
  1458. {
  1459. for ( var k in XPathEvaluator.prototype)
  1460. {
  1461. if (f || !o[k])
  1462. {
  1463. o[k] = XPathEvaluator.prototype[k];
  1464. }
  1465. }
  1466. };
  1467. w.XPathEvaluator = e;
  1468. if (w.Document)
  1469. {
  1470. e.install(w.Document.prototype, f);
  1471. }
  1472. else
  1473. {
  1474. e.install(document, f);
  1475. }
  1476. w.XPath = XPath;
  1477. }
  1478. }
  1479. })(window, document, /WebKit/.test(navigator.userAgent));
  1480. function XmlLib ()
  1481. {
  1482. };
  1483. XmlLib.loadXMLFromString = function (strXml)
  1484. {
  1485. var docXML = null;
  1486. // IE
  1487. if(is_ie)
  1488. {
  1489. docXML = new ActiveXObject("Microsoft.XMLDOM");
  1490. docXML.async = false;
  1491. docXML.resolveExternals = true;
  1492. docXML.validateOnParse = false;
  1493. docXML.preserveWhiteSpace = true;
  1494. docXML.loadXML(strXml);
  1495. docXML.setProperty("SelectionLanguage", "XPath");
  1496. }
  1497. // Other
  1498. else
  1499. {
  1500. var parser = new DOMParser();
  1501. docXML = parser.parseFromString(strXml, "text/xml");
  1502. }
  1503. return docXML;
  1504. };
  1505. XmlLib.createXML = function (strRoot)
  1506. {
  1507. var docXML = null;
  1508. // IE
  1509. if(is_ie)
  1510. {
  1511. docXML = new ActiveXObject("Microsoft.XMLDOM");
  1512. docXML.async = false;
  1513. docXML.resolveExternals = true;
  1514. docXML.validateOnParse = false;
  1515. docXML.documentElement = docXML.createElement(strRoot);
  1516. }
  1517. // Other
  1518. else
  1519. {
  1520. docXML = document.implementation.createDocument("", strRoot, null);
  1521. }
  1522. return docXML;
  1523. };
  1524. XmlLib.serializeToString = function (xnNode)
  1525. {
  1526. var strXml = null;
  1527. // IE
  1528. if (is_ie)
  1529. {
  1530. strXml = xnNode.xml;
  1531. }
  1532. // Other
  1533. else
  1534. {
  1535. var serializer = new XMLSerializer();
  1536. strXml = serializer.serializeToString(xnNode);
  1537. }
  1538. return strXml;
  1539. };
  1540. /**
  1541. * Xml Node attribute 값을 얻어 온다.
  1542. */
  1543. XmlLib.getAttribute = function (xnNode, strAttName)
  1544. {
  1545. if (null != xnNode)
  1546. {
  1547. var nAttrSize = xnNode.attributes.length;
  1548. for (var i = 0; i < nAttrSize; i++)
  1549. {
  1550. if (xnNode.attributes[i].nodeName == strAttName)
  1551. {
  1552. return xnNode.attributes[i].value;
  1553. }
  1554. }
  1555. }
  1556. return null;
  1557. };
  1558. /**
  1559. * Xml Node attribute 를 설정한다
  1560. *
  1561. * Node.setAttribute(Node, "속성이름", "값")
  1562. */
  1563. XmlLib.setAttribute = function (xnNode, strAttName, strAttValue)
  1564. {
  1565. xnNode.setAttribute(strAttName, strAttValue);
  1566. };
  1567. XmlLib.createChild = function (xnParent, strNodeName)
  1568. {
  1569. var xnDoc = xnParent.ownerDocument;
  1570. var xnChild = xnDoc.createElement(strNodeName);
  1571. xnParent.appendChild(xnChild);
  1572. return xnChild;
  1573. };
  1574. /**
  1575. * 값을 가진 자식 노드를 생성한다.
  1576. * @param nxParent (부모 노드)
  1577. * @param strNodeName (생성할 자식노드 이름)
  1578. * @param strValue (자식노드에 부여할 값)
  1579. * @return xnNode (생성한 자식 노드)
  1580. */
  1581. XmlLib.createChildWithValue = function (xnParent, strNodeName, strValue)
  1582. {
  1583. if (null == strValue)
  1584. {
  1585. strValue = "";
  1586. }
  1587. var xnChild = XmlLib.createChild(xnParent, strNodeName);
  1588. XmlLib.setTextValue(xnChild, strValue);
  1589. return xnChild;
  1590. };
  1591. XmlLib.getTextValue = function (xnNode)
  1592. {
  1593. var strValue = "";
  1594. try
  1595. {
  1596. if (Node.TEXT_NODE == xnNode.firstChild.nodeType || Node.CDATA_SECTION_NODE == xnNode.firstChild.nodeType)
  1597. {
  1598. strValue = xnNode.firstChild.nodeValue;
  1599. }
  1600. }
  1601. catch (e)
  1602. {
  1603. }
  1604. return strValue;
  1605. };
  1606. /**
  1607. * @param xmlDoc 새로운 XML Node, impor된 Node가 child로 append 됩니다.
  1608. * @param oldNode import 할 Node
  1609. * @param bDeep 하위 노드 import 여부
  1610. * @return 새롭게 import 된 Node
  1611. */
  1612. XmlLib.importChildNode = function (xnParent, xnChild, deep)
  1613. {
  1614. var xnNewChild = XmlLib.importNode(xnParent.ownerDocument, xnChild, deep);
  1615. xnParent.appendChild(xnNewChild);
  1616. return xnNewChild;
  1617. };
  1618. /**
  1619. * @param xmlDoc 새로운 XML Document
  1620. * @param xnNode import 할 Node
  1621. * @param deep 하위 노드 import 여부
  1622. * @return 새롭게 import 된 Node
  1623. */
  1624. XmlLib.importNode = function (docXML, xnNode, deep)
  1625. {
  1626. if (docXML.importNode)
  1627. {
  1628. return docXML.importNode(xnNode, deep);
  1629. }
  1630. else
  1631. {
  1632. return XmlLib.importNodeOld(docXML, xnNode, deep);
  1633. }
  1634. };
  1635. XmlLib.importNodeOld = function (docXML, xnNode, bDeep)
  1636. {
  1637. bDeep = (bDeep == null) ? true : bDeep;
  1638. var importChildNode = function(doc, srcNode, parent)
  1639. {
  1640. if (bDeep)
  1641. {
  1642. for (var i = 0; i < srcNode.childNodes.length; i++)
  1643. {
  1644. var node = XmlLib.importNode(doc, srcNode.childNodes.item(i), true);
  1645. parent.appendChild(node);
  1646. }
  1647. }
  1648. };
  1649. var newNode = null;
  1650. switch(xnNode.nodeType)
  1651. {
  1652. case Node.ATTRIBUTE_NODE:
  1653. {
  1654. if (null != docXML.createAttributeNS)
  1655. {
  1656. newNode = docXML.createAttributeNS(xnNode.namespaceURI, xnNode.nodeName);
  1657. }
  1658. else
  1659. {
  1660. newNode = docXML.createAttribute(xnNode.nodeName);
  1661. }
  1662. newNode.value = xnNode.value;
  1663. break;
  1664. }
  1665. case Node.DOCUMENT_FRAGMENT_NODE:
  1666. {
  1667. newNode = docXML.createDocumentFragment();
  1668. importChildNode(docXML, xnNode, newNode);
  1669. break;
  1670. }
  1671. case Node.ELEMENT_NODE:
  1672. {
  1673. if (null != docXML.createElementNS)
  1674. {
  1675. newNode = docXML.createElementNS(xnNode.namespaceURI, xnNode.tagName);
  1676. }
  1677. else
  1678. {
  1679. newNode = docXML.createElement(xnNode.tagName);
  1680. }
  1681. for (var i = 0; i < xnNode.attributes.length; i++)
  1682. {
  1683. var attr = XmlLib.importNode(docXML, xnNode.attributes.item(i), bDeep);
  1684. if (null != newNode.setAttributeNodeNS)
  1685. {
  1686. newNode.setAttributeNodeNS(attr);
  1687. }
  1688. else
  1689. {
  1690. newNode.setAttributeNode(attr);
  1691. }
  1692. }
  1693. importChildNode(docXML, xnNode, newNode);
  1694. break;
  1695. }
  1696. case Node.ENTITY_REFERENCE_NODE:
  1697. {
  1698. newNode = xnNode;
  1699. break;
  1700. }
  1701. case Node.PROCESSING_INSTRUCTION_NODE:
  1702. {
  1703. newNode = docXML.createProcessingInstruction(xnNode.target, xnNode.data);
  1704. break;
  1705. }
  1706. case Node.TEXT_NODE:
  1707. case Node.CDATA_SECTION_NODE:
  1708. case Node.COMMENT_NODE:
  1709. {
  1710. newNode = docXML.createTextNode(xnNode.nodeValue);
  1711. break;
  1712. }
  1713. case Node.DOCUMENT_NODE:
  1714. case Node.DOCUMENT_TYPE_NODE:
  1715. case Node.NOTATION_NODE:
  1716. case Node.ENTITY_NODE:
  1717. throw "can not import " + xnNode.tagName;
  1718. break;
  1719. }
  1720. return newNode;
  1721. };
  1722. XmlLib.setTextValue = function (xnNode, strValue)
  1723. {
  1724. if (xnNode.hasChildNodes() && (Node.TEXT_NODE == xnNode.firstChild.nodeType || Node.CDATA_SECTION_NODE == xnNode.firstChild.nodeType))
  1725. {
  1726. xnNode.firstChild.nodeValue = strValue;
  1727. }
  1728. else
  1729. {
  1730. var xmlText = xnNode.ownerDocument.createTextNode(strValue);
  1731. xnNode.appendChild(xmlText);
  1732. }
  1733. };
  1734. XmlLib.createCDATASection = function (xnNode, strValue)
  1735. {
  1736. var docXML = xnNode.ownerDocument;
  1737. var xmlCDATA = docXML.createCDATASection(strValue);
  1738. xnNode.appendChild(xmlCDATA);
  1739. };
  1740. XmlLib.transformToHTMLElement = function (heElement, xnNode, xnXSL)
  1741. {
  1742. try
  1743. {
  1744. // IE
  1745. if (is_ie)
  1746. {
  1747. heElement.innerHTML = xnNode.transformNode(xnXSL);
  1748. }
  1749. // Other
  1750. else
  1751. {
  1752. var objXslt = new XSLTProcessor();
  1753. objXslt.importStylesheet(xnXSL);
  1754. var resultFragment = objXslt.transformToFragment(xnNode, document);
  1755. heElement.appendChild(resultFragment.firstChild);
  1756. objXslt.reset();
  1757. return;
  1758. }
  1759. }
  1760. catch (e)
  1761. {
  1762. window.alert(e + " TrustFormSoonsu : transformToHTMLElement");
  1763. }
  1764. };
  1765. XmlLib.selectSingleNode = function (xnNode, strPath)
  1766. {
  1767. if (null == xnNode || strPath.isEmpty())
  1768. {
  1769. return null;
  1770. }
  1771. var xmlSelectNode = null;
  1772. xmlSelectNode = xnNode.selectSingleNode(strPath);
  1773. return xmlSelectNode;
  1774. };
  1775. XmlLib.selectNodes = function (xnNode, strPath)
  1776. {
  1777. if (typeof(strPath) != "string")
  1778. {
  1779. return null;
  1780. }
  1781. if (null == xnNode || strPath.isEmpty())
  1782. {
  1783. return null;
  1784. }
  1785. var xlNode = null;
  1786. xlNode = xnNode.selectNodes(strPath);
  1787. return xlNode;
  1788. };
  1789. XmlLib.hasChildNodesExceptText = function (xnNode)
  1790. {
  1791. if (xnNode.hasChildNodes())
  1792. {
  1793. var xlChild = xnNode.childNodes;
  1794. for (var i=0; i<xlChild.length; i++)
  1795. {
  1796. var xnChild = xlChild[i];
  1797. if (Node.TEXT_NODE != xnChild.nodeType && Node.CDATA_SECTION_NODE != xnChild.nodeType)
  1798. {
  1799. return true;
  1800. }
  1801. }
  1802. }
  1803. return false;
  1804. };
  1805. /**
  1806. * Document에서 Root ElementNode를 찾는다.
  1807. */
  1808. XmlLib.getRootElementNode = function (xnDoc)
  1809. {
  1810. var xnRoot;
  1811. if (null != xnDoc)
  1812. {
  1813. if (Node.DOCUMENT_NODE == xnDoc.nodeType)
  1814. {
  1815. var xlChild = xnDoc.childNodes;
  1816. for (var i=0; i<xlChild.length; i++)
  1817. {
  1818. var xnChild = xlChild[i];
  1819. if (Node.PROCESSING_INSTRUCTION_NODE != xnChild.nodeType)
  1820. {
  1821. xnRoot = xnChild;
  1822. break;
  1823. }
  1824. }
  1825. }
  1826. else
  1827. {
  1828. if (Node.PROCESSING_INSTRUCTION_NODE != xnChild.nodeType)
  1829. {
  1830. xnRoot = xnChild;
  1831. }
  1832. }
  1833. }
  1834. return xnRoot;
  1835. };
  1836. if (!window.Node)
  1837. {
  1838. var Node =
  1839. {
  1840. ELEMENT_NODE : 1,
  1841. ATTRIBUTE_NODE : 2,
  1842. TEXT_NODE : 3,
  1843. CDATA_SECTION_NODE : 4,
  1844. ENTITY_REFERENCE_NODE : 5,
  1845. ENTITY_NODE : 6,
  1846. PROCESSING_INSTRUCTION_NODE : 7,
  1847. COMMENT_NODE : 8,
  1848. DOCUMENT_NODE : 9,
  1849. DOCUMENT_TYPE_NODE : 10,
  1850. DOCUMENT_FRAGMENT_NODE : 11,
  1851. NOTATION_NODE : 12
  1852. };
  1853. };
  1854. /**
  1855. * XML DOM에 Property 추가
  1856. *
  1857. * selectNodes()
  1858. * selectSingleNode()
  1859. */
  1860. if (document.implementation.hasFeature("XPath", "3.0"))
  1861. {
  1862. // prototying the XMLDocument
  1863. XMLDocument.prototype.selectNodes = function (strXPath, xnDoc)
  1864. {
  1865. try
  1866. {
  1867. if (!xnDoc)
  1868. {
  1869. xnDoc = this;
  1870. }
  1871. var xlNodeList = new DOMNodeList();
  1872. if (this.createNSResolver)
  1873. {
  1874. var arItemList = this.evaluate(strXPath, xnDoc, this.createNSResolver(this.documentElement), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  1875. for (var i = 0; i < arItemList.snapshotLength; i++)
  1876. {
  1877. xlNodeList.add(arItemList.snapshotItem(i));
  1878. }
  1879. }
  1880. return xlNodeList;
  1881. }
  1882. catch (e)
  1883. {
  1884. window.alert(e + " TrustFormSoonsu : selectNodes");
  1885. }
  1886. };
  1887. // prototying the Element
  1888. Element.prototype.selectNodes = function (strXPath)
  1889. {
  1890. if (this.ownerDocument.selectNodes)
  1891. {
  1892. return this.ownerDocument.selectNodes(strXPath, this);
  1893. }
  1894. else
  1895. {
  1896. throw "Web Browser not Support Element.selectNodes()";
  1897. }
  1898. };
  1899. // prototying the XMLDocument
  1900. XMLDocument.prototype.selectSingleNode = function (strXPath, xnDoc)
  1901. {
  1902. if (!xnDoc)
  1903. {
  1904. xnDoc = this;
  1905. }
  1906. var xlNodeList = this.selectNodes(strXPath, xnDoc);
  1907. if (xlNodeList.length > 0)
  1908. {
  1909. return xlNodeList.item(0);
  1910. }
  1911. else
  1912. {
  1913. return null;
  1914. }
  1915. };
  1916. // prototying the Element
  1917. Element.prototype.selectSingleNode = function (strXPath)
  1918. {
  1919. if (this.ownerDocument.selectSingleNode)
  1920. {
  1921. return this.ownerDocument.selectSingleNode(strXPath, this);
  1922. }
  1923. else
  1924. {
  1925. throw "Web Browser not Support Element.selectSingleNode()";
  1926. }
  1927. };
  1928. };