xpath.js 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796
  1. var xpath = require('../../lib/jsdom/level3/xpath');
  2. var level3 = require('../../lib/jsdom/level3/core').dom.level3.core;
  3. var jsdom = require('../../lib/jsdom');
  4. var domTestHelper = require('../DOMTestCase');
  5. exports.tests = {};
  6. function all1(re, s) {
  7. var l = [];
  8. while (s.length) {
  9. var m = re.exec(s);
  10. if (!m) break;
  11. l.push(m[1]);
  12. s = s.substr(m[0].length);
  13. }
  14. return l;
  15. }
  16. exports.testTokenizeRegexp = function(test) {
  17. var re = xpath.Stream.prototype.re;
  18. test.deepEqual(['8', 'a'], all1(re, '8a'));
  19. test.deepEqual(['8', 'a'], all1(re, ' 8a'));
  20. test.deepEqual(['fun'], all1(re, 'fun'));
  21. test.deepEqual(['hi', '+', '3'], all1(re, 'hi+3'));
  22. test.deepEqual(['fun', '(', ')'], all1(re, 'fun()'));
  23. test.deepEqual(['..', '/', 'a', '//', 'b'], all1(re, '../a//b'));
  24. test.deepEqual(['1', '<=', '2', '<', '3', '>=', '4', '=', '5', '!=', '6'], all1(re, '1<=2<3>=4=5!=6'));
  25. test.deepEqual(['<', '='], all1(re, '< ='));
  26. test.deepEqual(['a','::','b'], all1(re, 'a::b'));
  27. test.deepEqual(['a','::','b'], all1(re, 'a :: b'));
  28. test.deepEqual(['a:b'], all1(re, 'a:b'));
  29. test.deepEqual(['a'], all1(re, 'a : b')); // can't tokenize : alone
  30. test.deepEqual(['a:b', '::', 'c'], all1(re, 'a:b::c'));
  31. test.deepEqual(['a', '::', 'b:c'], all1(re, 'a::b:c'));
  32. test.deepEqual(['a', '::', 'b:c'], all1(re, 'a::b:c'));
  33. test.deepEqual(['"hi there\'"'], all1(re, '"hi there\'"'));
  34. test.deepEqual(['*'], all1(re, '*'));
  35. test.deepEqual(['ncname:*'], all1(re, 'ncname:*'));
  36. test.deepEqual(['q:name'], all1(re, 'q:name:*')); // can't tokenize : alone
  37. test.deepEqual(['-', 'b'], all1(re, '-b'));
  38. test.deepEqual(['a-b'], all1(re, 'a-b'));
  39. test.deepEqual(['a', '-', 'b'], all1(re, 'a -b'));
  40. test.deepEqual(['a', '-', 'b'], all1(re, 'a - b'));
  41. test.deepEqual(['.3'], all1(re, '.3'));
  42. test.done();
  43. };
  44. exports.testPeekPop = function(test) {
  45. var s = new xpath.Stream('a b c');
  46. test.equals('a', s.peek());
  47. test.equals(' b c', s.str);
  48. test.equals('a', s.pop());
  49. test.equals('b', s.pop());
  50. test.equals('c', s.pop());
  51. test.equals(null, s.pop());
  52. test.done();
  53. };
  54. exports.testPopFuncName = function(test) {
  55. var s = new xpath.Stream('f( node( mod( string( comment()))))');
  56. test.equals('f', s.trypopfuncname());
  57. test.equals('(', s.pop());
  58. test.equals(null, s.trypopfuncname());
  59. test.equals('node', s.pop());
  60. test.equals('(', s.pop());
  61. test.equals('mod', s.trypopfuncname());
  62. test.equals('(', s.pop());
  63. test.equals('string', s.trypopfuncname());
  64. test.equals('(', s.pop());
  65. test.equals(null, s.trypopfuncname());
  66. test.equals('comment', s.pop());
  67. test.equals('(', s.pop());
  68. test.equals(')', s.pop());
  69. test.equals(')', s.pop());
  70. test.equals(')', s.pop());
  71. test.equals(')', s.pop());
  72. test.equals(')', s.pop());
  73. test.equals(null, s.trypopfuncname());
  74. test.equals(null, s.pop());
  75. test.done();
  76. };
  77. exports.testPopFuncWithSpaces = function(test) {
  78. var s = new xpath.Stream('f(n-s(" "), 2, 3)');
  79. test.equals('f', s.trypopfuncname());
  80. test.equals('(', s.pop());
  81. test.equals('n-s', s.trypopfuncname());
  82. test.equals('(', s.pop());
  83. test.equals(' ', s.trypopliteral());
  84. test.equals(')', s.pop());
  85. test.equals(',', s.pop());
  86. test.equals('2', s.pop());
  87. test.equals(',', s.pop());
  88. test.equals('3', s.pop());
  89. test.equals(')', s.pop());
  90. test.equals(null, s.pop());
  91. test.done();
  92. };
  93. exports.testTryPopNameTest = function(test) {
  94. var s = new xpath.Stream('a:b + c:* + *');
  95. test.equals('a:b', s.trypopnametest());
  96. test.equals(null, s.trypopnametest());
  97. test.equals('+', s.pop());
  98. test.equals('c:*', s.trypopnametest());
  99. test.equals(null, s.trypopnametest());
  100. test.equals('+', s.pop());
  101. test.equals('*', s.trypopnametest());
  102. test.equals(null, s.trypopnametest());
  103. test.equals(null, s.pop());
  104. test.done();
  105. };
  106. exports.testTryPopLiteral = function(test) {
  107. var s = new xpath.Stream('"ab" + \'c d\' e "'); // dangling " at end
  108. test.equals('ab', s.trypopliteral());
  109. test.equals(null, s.trypopliteral());
  110. test.equals('+', s.pop());
  111. test.equals('c d', s.trypopliteral());
  112. test.equals(null, s.trypopliteral());
  113. test.equals('e', s.pop());
  114. test.equals(null, s.trypopliteral()); // dangling " doesn't become a token.
  115. test.equals(null, s.pop());
  116. test.done();
  117. };
  118. exports.testTryPopNumber = function(test) {
  119. var s = new xpath.Stream('.2 + 3.4 -5 .');
  120. test.equals(.2, s.trypopnumber());
  121. test.equals(null, s.trypopnumber());
  122. test.equals('+', s.pop());
  123. test.equals('3.4', s.trypopnumber());
  124. test.equals(null, s.trypopnumber());
  125. test.equals('-', s.pop());
  126. test.equals('5', s.trypopnumber());
  127. // . by itself isn't a number.
  128. test.equals(null, s.trypopnumber());
  129. test.equals('.', s.pop());
  130. test.equals(null, s.trypopnumber()); // dangling " doesn't become a token.
  131. test.equals(null, s.pop());
  132. test.done();
  133. };
  134. exports.testTryPopVarRef = function(test) {
  135. var s = new xpath.Stream('$a + $b:c $');
  136. test.equals('a', s.trypopvarref());
  137. test.equals(null, s.trypopvarref());
  138. test.equals('+', s.pop());
  139. test.equals('b:c', s.trypopvarref());
  140. test.equals(null, s.trypopvarref());
  141. test.equals(null, s.pop());
  142. test.done();
  143. };
  144. var astFactory = {
  145. node: function() {return Array.prototype.slice.call(arguments);},
  146. i: 0,
  147. };
  148. exports.testParseNumber = function(test) {
  149. var s = new xpath.Stream('32');
  150. test.deepEqual(32, xpath.parse(s, astFactory));
  151. test.done();
  152. };
  153. exports.testParseLiteral = function(test) {
  154. var s = new xpath.Stream('"hi"');
  155. test.deepEqual("hi", xpath.parse(s, astFactory));
  156. test.done();
  157. };
  158. exports.testParseFunctionCall = function(test) {
  159. var s = new xpath.Stream('concat(1, 1+1, "hi")');
  160. test.deepEqual(['FunctionCall', 'concat', [1, ['+', 1, 1], 'hi']], xpath.parse(s, astFactory));
  161. test.done();
  162. };
  163. exports.testParseFunctionOfEmptyString = function(test) {
  164. var s = new xpath.Stream('string("")');
  165. test.deepEqual(['FunctionCall', 'string', [""]], xpath.parse(s, astFactory));
  166. test.done();
  167. };
  168. exports.testParseVariableReference = function(test) {
  169. var s = new xpath.Stream('$hi');
  170. test.deepEqual(['VariableReference', 'hi'], xpath.parse(s, astFactory));
  171. test.done();
  172. };
  173. exports.testParsePrimative = function(test) {
  174. var s = new xpath.Stream('32 + -1 + "3"');
  175. test.deepEqual(['+', ['+', 32, ['UnaryMinus', 1]], '3'], xpath.parse(s, astFactory));
  176. test.done();
  177. };
  178. exports.testPrimaryParens = function(test) {
  179. var s = new xpath.Stream('(div)');
  180. test.deepEqual(['PathExpr', ['Axis', 'child', 'element', 'div']], xpath.parse(s, astFactory));
  181. test.done();
  182. };
  183. exports.testParseStepShorthands = function(test) {
  184. var s = new xpath.Stream('../.');
  185. test.deepEqual(
  186. [ 'PathExpr',
  187. [ '/',
  188. [ 'Axis', 'parent', 'node' ],
  189. [ 'Axis', 'self', 'node' ] ] ],
  190. xpath.parse(s, astFactory));
  191. test.done();
  192. };
  193. exports.testParseWildcard = function(test) {
  194. var s = new xpath.Stream('*/self::*/@*');
  195. test.deepEqual(
  196. [ 'PathExpr',
  197. [ '/',
  198. [ '/',
  199. [ 'Axis', 'child', 'element', '*' ],
  200. [ 'Axis', 'self', 'element', '*' ] ],
  201. [ 'Axis', 'attribute', 'attribute', '*' ] ] ],
  202. xpath.parse(s, astFactory));
  203. test.done();
  204. };
  205. exports.testParseFilter = function(test) {
  206. // tests FilterExpr, which is Primary followed by predicates.
  207. // Not to be confused with Step, which is node test followed by predicate.
  208. var s = new xpath.Stream('1[2][3]');
  209. test.deepEqual(['Predicate', ['Predicate', 1, 2], 3],
  210. xpath.parse(s, astFactory));
  211. test.done();
  212. };
  213. exports.testParseStepWithPredicate = function(test) {
  214. // tests Step, which is node test followed by predicate.
  215. // Not to be confused with FilterExpr, which is Primary followed by predicates.
  216. var s = new xpath.Stream('a[2][3]');
  217. test.deepEqual(['PathExpr',
  218. ['Predicate',
  219. ['Predicate',
  220. ['Axis', 'child', 'element', 'a'],
  221. 2],
  222. 3]],
  223. xpath.parse(s, astFactory));
  224. test.done();
  225. };
  226. exports.testParsePathWithPredicate = function(test) {
  227. // tests Step, which is node test followed by predicate.
  228. // Not to be confused with FilterExpr, which is Primary followed by predicates.
  229. var s = new xpath.Stream('a/b[1]');
  230. test.deepEqual(['PathExpr', [ '/',
  231. [ 'Axis', 'child', 'element', 'a' ],
  232. [ 'Predicate', [ 'Axis', 'child', 'element', 'b' ], 1 ] ]],
  233. xpath.parse(s, astFactory));
  234. test.done();
  235. };
  236. exports.testParseAbsoluteLocationPath = function(test) {
  237. var s = new xpath.Stream('/a/b/c');
  238. test.deepEqual(
  239. ['PathExpr',
  240. [ '/',
  241. [ '/',
  242. [ '/',
  243. [ 'Root' ],
  244. ['Axis', 'child', 'element', 'a' ] ],
  245. [ 'Axis', 'child', 'element', 'b' ] ],
  246. [ 'Axis', 'child', 'element', 'c' ] ] ],
  247. xpath.parse(s, astFactory));
  248. test.done();
  249. };
  250. exports.testParseRelativeLocationPath = function(test) {
  251. var s = new xpath.Stream('a/b/c');
  252. test.deepEqual(
  253. ['PathExpr',
  254. [ '/',
  255. [ '/',
  256. [ 'Axis', 'child', 'element', 'a' ],
  257. [ 'Axis', 'child', 'element', 'b' ] ],
  258. [ 'Axis', 'child', 'element', 'c' ] ] ],
  259. xpath.parse(s, astFactory));
  260. test.done();
  261. };
  262. exports.testParseNodeTest = function(test) {
  263. var s = new xpath.Stream('self::node()');
  264. test.deepEqual(['PathExpr', ['Axis', 'self', 'node', undefined]],
  265. xpath.parse(s, astFactory));
  266. test.done();
  267. };
  268. exports.testParseAbsoluteShorthand = function(test) {
  269. var s2 = new xpath.Stream('/descendant-or-self::node()/a');
  270. var s1 = new xpath.Stream('//a');
  271. test.deepEqual(xpath.parse(s2, astFactory), xpath.parse(s1, astFactory));
  272. test.done();
  273. };
  274. exports.testParseLocationShorthand = function(test) {
  275. var s1 = new xpath.Stream('a//b');
  276. var s2 = new xpath.Stream('a/descendant-or-self::node()/b');
  277. test.deepEqual(xpath.parse(s2, astFactory), xpath.parse(s1, astFactory));
  278. test.done();
  279. };
  280. exports.testParseRoot = function(test) {
  281. var s = new xpath.Stream('/');
  282. test.deepEqual(['PathExpr', ['Root']], xpath.parse(s, astFactory));
  283. test.done();
  284. };
  285. exports.testEvaluateNumber = function(test) {
  286. var x = xpath.evaluate('3', null, 'CTX');
  287. test.deepEqual(3, x);
  288. test.done();
  289. };
  290. exports.testEvaluateExtraParens = function(test) {
  291. var x = xpath.evaluate('(((3)))', null, 'CTX');
  292. test.deepEqual(3, x);
  293. test.done();
  294. };
  295. exports.testEvaluateNumberFunction = function(test) {
  296. var x = xpath.evaluate('number("3")', null, 'CTX');
  297. test.equal(3, x);
  298. test.done();
  299. };
  300. exports.testEvaluateUnaryMinus = function(test) {
  301. var x = xpath.evaluate('-3', null, 'CTX');
  302. test.deepEqual(-3, x);
  303. test.done();
  304. };
  305. exports.testEvaluateUnaryMinusCoerced = function(test) {
  306. var x = xpath.evaluate('--"3"', null, 'CTX');
  307. test.deepEqual(3, x);
  308. test.done();
  309. };
  310. exports.testEvaluateArithmetic = function(test) {
  311. var x = xpath.evaluate('(2*11 + 5)mod 10', null, 'CTX');
  312. test.deepEqual(7, x);
  313. test.done();
  314. };
  315. exports.testEvaluateArithmetic2 = function(test) {
  316. var x = xpath.evaluate(
  317. '1>.5 and 1>=.5 and (2=6div 3) and false()<.5 and true()>.5', null, 'CTX');
  318. test.deepEqual(true, x);
  319. test.done();
  320. };
  321. exports.testEvaluateWildcardChild = function(test) {
  322. var doc = jsdom.jsdom('<html><body><div>3</div><div>4</div></body></html>'),
  323. body = doc.getElementsByTagName('body')[0],
  324. div0 = doc.getElementsByTagName('div')[0],
  325. div1 = doc.getElementsByTagName('div')[1];
  326. var x = xpath.evaluate('*', doc, body);
  327. test.deepEqual(xpath.stringifyObject({nodes:[div0,div1], pos: [[1],[2]], lasts: [[2],[2]]}), xpath.stringifyObject(x));
  328. test.done();
  329. };
  330. exports.testEvaluateArithmetic3 = function(test) {
  331. var doc = jsdom.jsdom('<html><body><div>3</div><div>4</div></body></html>'),
  332. body = doc.getElementsByTagName('body')[0];
  333. var x = xpath.evaluate(
  334. '*<*', doc, body);
  335. test.deepEqual(true, x);
  336. test.done();
  337. };
  338. exports.testEvaluateRoot = function(test) {
  339. var doc = jsdom.jsdom('Hello.');
  340. var x = xpath.evaluate('/', doc, doc);
  341. test.deepEqual(xpath.stringifyObject({nodes:[doc]}), xpath.stringifyObject(x));
  342. test.done();
  343. };
  344. exports.testEvaluateSelf = function(test) {
  345. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div></body></html>'),
  346. div0 = doc.getElementsByTagName('div')[0],
  347. div1 = doc.getElementsByTagName('div')[1];
  348. var newCtx = xpath.axes.self([doc, div0, div1], xpath.nodeTypes.element, null, true);
  349. test.deepEqual(
  350. xpath.stringifyObject(
  351. {nodes: [div0, div1],
  352. pos: [[1], [1]],
  353. lasts: [[1],[1]]}),
  354. xpath.stringifyObject(newCtx));
  355. test.done();
  356. };
  357. exports.testEvaluateParent = function(test) {
  358. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><span></span></body></html>'),
  359. div0 = doc.getElementsByTagName('div')[0],
  360. span = doc.getElementsByTagName('span')[0],
  361. body = div0.parentNode;
  362. var newCtx = xpath.axes.parent([doc, div0, span], xpath.nodeTypes.element, null, true);
  363. test.deepEqual(
  364. xpath.stringifyObject(
  365. {nodes: [body],
  366. pos: [[1]],
  367. lasts: [[1]]}),
  368. xpath.stringifyObject(newCtx));
  369. test.done();
  370. };
  371. exports.testSortUniqDocumentOrder = function(test) {
  372. var doc = jsdom.jsdom('<html><body><div id=x><a></a><div>b</div></div><span></span></body></html>'),
  373. body = doc.getElementsByTagName('body')[0],
  374. div0 = doc.getElementsByTagName('div')[0],
  375. id = doc.getElementById('x').getAttributeNode('id'),
  376. a = doc.getElementsByTagName('a')[0],
  377. span = doc.getElementsByTagName('span')[0];
  378. var ctx = {nodes: [id, body, span, div0, a, span]};
  379. var ctx2 = {nodes: xpath.sortUniqDocumentOrder(ctx.nodes)};
  380. test.deepEqual(
  381. xpath.stringifyObject(
  382. {nodes: [body, div0, id, a, span]}),
  383. xpath.stringifyObject(ctx2));
  384. test.done();
  385. };
  386. exports.testId = function(test) {
  387. var doc = jsdom.jsdom(
  388. '<html><body><div id=test>b c d</div><br id=b><br id=c><br id=d></body></html>'),
  389. b = doc.getElementById('b'),
  390. c = doc.getElementById('c'),
  391. d = doc.getElementById('d');
  392. test.deepEqual(
  393. xpath.stringifyObject(
  394. {nodes: [b,c,d]}),
  395. xpath.stringifyObject(
  396. xpath.evaluate('id(id("test"))', doc, doc)));
  397. test.done();
  398. };
  399. function outerHtml(node) { return node.outerHTML; }
  400. exports.testEvaluateChildAxis = function(test) {
  401. var doc = jsdom.jsdom('<html><body>Hello.</body></html>');
  402. var ctx = doc.body;
  403. var x = xpath.evaluate('child::text()', doc, ctx);
  404. test.deepEqual([doc.body.firstChild], x.nodes);
  405. test.done();
  406. };
  407. exports.testDescendantDfs1 = function(test) {
  408. var doc = jsdom.jsdom('<html><body><a><b><i></i></b></a><u></u></body></html>');
  409. var body = doc.getElementsByTagName('body')[0],
  410. a = doc.getElementsByTagName('a')[0],
  411. b = doc.getElementsByTagName('b')[0],
  412. i = doc.getElementsByTagName('i')[0],
  413. u = doc.getElementsByTagName('u')[0];
  414. var newCtx = xpath.axes.descendant([body], xpath.nodeTypes.element, null, true).simplify();
  415. test.deepEqual(
  416. xpath.stringifyObject(
  417. {nodes: [a, b, i, u],
  418. pos:[[1],[2],[3],[4]],lasts:[[4],[4],[4],[4]]}),
  419. xpath.stringifyObject(newCtx));
  420. test.done();
  421. };
  422. exports.testDescendantOrSelfChild = function(test) {
  423. // from http://trac.webkit.org/export/73247/trunk/LayoutTests/fast/xpath/xpath-functional-test.html
  424. var doc = jsdom.jsdom(
  425. '<html><body>' +
  426. '<blockquote id="n12" title="12" class="15">' +
  427. ' <!--blockquoteComment-->' +
  428. ' blockquoteText1:' +
  429. ' <br id="n13" title="13" class="10">' +
  430. ' blockquoteText2' +
  431. ' <p id="n14" title="14" class="13">' +
  432. ' <del id="n15" title="15" class="11">del</del>' +
  433. ' <ins id="n16" title="16" class="12">ins</ins>' +
  434. ' </p>' +
  435. ' <!--?pi name="value"?-->' +
  436. ' <font id="n17" title="17" class="14" face="n8 n26">font</font>' +
  437. '</blockquote>' +
  438. '</html></body>'
  439. );
  440. var newCtx = xpath.evaluate('.//*[ancestor::blockquote]', doc, doc);
  441. var nodeNames = newCtx.nodes
  442. .map(function(n) {return n.nodeName;})
  443. .join(' ').toLowerCase();
  444. test.deepEqual('br p del ins font', nodeNames);
  445. test.done();
  446. };
  447. function testDescendantDfsAndSelfBase(test, andSelf) {
  448. var andSelf = !!andSelf;
  449. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div></body></html>');
  450. var div0 = doc.getElementsByTagName('div')[0],
  451. div1 = doc.getElementsByTagName('div')[1];
  452. if (andSelf) fn = xpath.axes['descendant-or-self'];
  453. else fn = xpath.axes.descendant;
  454. var newCtx = fn([div0], xpath.nodeTypes.element, 'div', true).simplify();
  455. var expectedNodes = andSelf ? [div0, div1] : [div1];
  456. var expectedPos = andSelf ? [[1], [2]] : [[1]];
  457. var expectedLasts = andSelf ? [[2], [2]] : [[1]];
  458. test.deepEqual(
  459. xpath.stringifyObject(
  460. {nodes: expectedNodes, pos: expectedPos, lasts: expectedLasts}),
  461. xpath.stringifyObject(newCtx));
  462. test.done();
  463. };
  464. exports.testDescendantDfs = function(test) {
  465. testDescendantDfsAndSelfBase(test, false);
  466. };
  467. exports.testDescendantDfsAndSelf = function(test) {
  468. testDescendantDfsAndSelfBase(test, true);
  469. };
  470. exports.testDescendantDfsMultipleRoots = function(test) {
  471. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div></body></html>');
  472. var html = doc.getElementsByTagName('html')[0],
  473. body = doc.getElementsByTagName('body')[0],
  474. div0 = doc.getElementsByTagName('div')[0],
  475. div1 = doc.getElementsByTagName('div')[1];
  476. var newCtx = xpath.axes.descendant([html, div0], xpath.nodeTypes.element, null, true).simplify();
  477. test.deepEqual(
  478. xpath.stringifyObject(
  479. {nodes: [body, div0, div1], pos: [[1], [2], [3, 1]],
  480. lasts: [[3], [3], [3, 1]]}),
  481. xpath.stringifyObject(newCtx));
  482. test.done();
  483. };
  484. exports.testFollowing = function(test) {
  485. var doc = jsdom.jsdom(
  486. '<html><head><title></title></head>' +
  487. '<body>' +
  488. '<div><a></a><b></b></div>' +
  489. '<div><i></i><u></u></div>' +
  490. '</body></html>');
  491. var body = doc.getElementsByTagName('body')[0],
  492. div0 = doc.getElementsByTagName('div')[0],
  493. a = doc.getElementsByTagName('a')[0],
  494. b = doc.getElementsByTagName('b')[0],
  495. div1 = doc.getElementsByTagName('div')[1],
  496. i = doc.getElementsByTagName('i')[0],
  497. u = doc.getElementsByTagName('u')[0];
  498. var newCtx = xpath.axes.following([body, div0, a], xpath.nodeTypes.element, null, true).simplify();
  499. test.deepEqual(
  500. xpath.stringifyObject(
  501. { nodes:
  502. [ b, div1, i, u ],
  503. pos: [ [ 1 ], [ 2, 1 ], [ 3, 2 ], [ 4, 3 ] ],
  504. lasts: [ [ 4 ], [ 4, 3 ], [ 4, 3 ], [ 4, 3 ] ] }),
  505. xpath.stringifyObject(newCtx));
  506. test.done();
  507. };
  508. exports.testPreceding = function(test) {
  509. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  510. var html = doc.getElementsByTagName('html')[0],
  511. body = doc.getElementsByTagName('body')[0],
  512. div0 = doc.getElementsByTagName('div')[0],
  513. div1 = doc.getElementsByTagName('div')[1],
  514. img = doc.getElementsByTagName('img')[0];
  515. var newCtx = xpath.axes.preceding([img], xpath.nodeTypes.element, null, true);
  516. test.deepEqual(
  517. xpath.stringifyObject(
  518. {nodes: [div0,div1], pos: [[2], [1]], lasts:[[2],[2]] }),
  519. xpath.stringifyObject(newCtx));
  520. test.done();
  521. };
  522. exports.testPreceding2 = function(test) {
  523. var doc = jsdom.jsdom(
  524. '<html><head><title></title></head>' +
  525. '<body>' +
  526. '<div><a></a><b></b></div>' +
  527. '<div><i></i><u></u></div>' +
  528. '</body></html>');
  529. var head = doc.getElementsByTagName('head')[0],
  530. title = doc.getElementsByTagName('title')[0],
  531. a = doc.getElementsByTagName('a')[0],
  532. b = doc.getElementsByTagName('b')[0],
  533. i = doc.getElementsByTagName('i')[0],
  534. u = doc.getElementsByTagName('u')[0],
  535. div0 = doc.getElementsByTagName('div')[0],
  536. div1 = doc.getElementsByTagName('div')[1];
  537. var newCtx = xpath.axes.preceding([b, i], xpath.nodeTypes.element, null, true);
  538. test.deepEqual(
  539. xpath.stringifyObject(
  540. {nodes: [head, title, div0, a, b],
  541. pos: [ [ 5, 3 ], [ 4, 2 ], [ 3 ], [ 2, 1 ], [ 1 ] ],
  542. lasts: [[5, 3], [5, 3], [5], [5, 3], [5]]}),
  543. xpath.stringifyObject(newCtx));
  544. test.done();
  545. };
  546. exports.testFollowingSibling = function(test) {
  547. var doc = jsdom.jsdom('<html><body><a>one</a><a>two</a><a>three</a><a>four</a><a>five</a><a>six</a></body></html>');
  548. var one = doc.getElementsByTagName('a')[0],
  549. two = doc.getElementsByTagName('a')[1],
  550. three = doc.getElementsByTagName('a')[2],
  551. four = doc.getElementsByTagName('a')[3],
  552. five = doc.getElementsByTagName('a')[4],
  553. six = doc.getElementsByTagName('a')[5];
  554. var newCtx = xpath.evaluate('a[3]/following-sibling::*', doc, doc.body);
  555. test.deepEqual(
  556. xpath.stringifyObject({nodes:[four,five,six]}),
  557. xpath.stringifyObject(newCtx));
  558. test.done();
  559. };
  560. exports.testPrecedingSibling = function(test) {
  561. var doc = jsdom.jsdom('<html><body><a>one</a><a>two</a><a>three</a><a>four</a><a>five</a><a>six</a></body></html>');
  562. var one = doc.getElementsByTagName('a')[0],
  563. two = doc.getElementsByTagName('a')[1],
  564. three = doc.getElementsByTagName('a')[2],
  565. four = doc.getElementsByTagName('a')[3],
  566. five = doc.getElementsByTagName('a')[4],
  567. six = doc.getElementsByTagName('a')[5];
  568. var newCtx = xpath.evaluate('a[3]/preceding-sibling::*', doc, doc.body);
  569. test.deepEqual(
  570. xpath.stringifyObject({nodes:[one,two]}),
  571. xpath.stringifyObject(newCtx));
  572. test.done();
  573. };
  574. exports.testAncestor = function(test) {
  575. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  576. var html = doc.getElementsByTagName('html')[0],
  577. body = doc.getElementsByTagName('body')[0],
  578. div0 = doc.getElementsByTagName('div')[0],
  579. div1 = doc.getElementsByTagName('div')[1],
  580. img = doc.getElementsByTagName('img')[0];
  581. var newCtx = xpath.axes.ancestor([div1, img], xpath.nodeTypes.element, null, true);
  582. test.deepEqual(
  583. xpath.stringifyObject(
  584. {nodes: [html, body, div0],
  585. pos: [ [ 3, 2 ], [ 2, 1 ], [ 1 ] ],
  586. lasts: [ [ 3, 2 ], [ 3, 2 ], [ 3 ] ]}),
  587. xpath.stringifyObject(newCtx));
  588. test.done();
  589. };
  590. exports.testChild = function(test) {
  591. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  592. var html = doc.getElementsByTagName('html')[0],
  593. body = doc.getElementsByTagName('body')[0],
  594. div0 = doc.getElementsByTagName('div')[0],
  595. div1 = doc.getElementsByTagName('div')[1],
  596. img = doc.getElementsByTagName('img')[0];
  597. var newCtx = xpath.axes.child([body], xpath.nodeTypes.element, null, true);
  598. test.deepEqual(
  599. xpath.stringifyObject(
  600. {nodes: [div0, img], pos: [[1], [2]], lasts: [[2],[2]] }),
  601. xpath.stringifyObject(newCtx));
  602. test.done();
  603. }
  604. // TODO: 'concat(a[1], a[1][1])'
  605. // TODO: 'concat(a[1], a[position()>1][1])'
  606. exports.testEvaluatePosition = function(test) {
  607. var doc = jsdom.jsdom('<html><body><a>one</a><a>two</a><a>three</a></body></html>');
  608. var x = xpath.evaluate('concat(a[1], a[1][1])', doc, doc.body);
  609. test.deepEqual('oneone', x);
  610. test.done();
  611. };
  612. exports.testEvaluatePositionAndLast = function(test) {
  613. var doc = jsdom.jsdom('<html><body><a>one</a><a>two</a><a>three</a><a>four</a><a>five</a><a>six</a></body></html>');
  614. var one = doc.getElementsByTagName('a')[0],
  615. two = doc.getElementsByTagName('a')[1],
  616. three = doc.getElementsByTagName('a')[2],
  617. four = doc.getElementsByTagName('a')[3],
  618. five = doc.getElementsByTagName('a')[4],
  619. six = doc.getElementsByTagName('a')[5];
  620. var newCtx = xpath.evaluate('//a[last() mod position()=0]', doc, doc.body);
  621. test.deepEqual(
  622. xpath.stringifyObject({nodes:[one,two,three,six]}),
  623. xpath.stringifyObject(newCtx));
  624. test.done();
  625. };
  626. exports.testAttributePredicate = function(test) {
  627. var doc = jsdom.jsdom('<html><body><a href="x" rel=alternate>a</a></body></html>');
  628. var a = doc.getElementsByTagName('a')[0];
  629. var newCtx = xpath.evaluate('//*[@href="x"]', doc, doc.body);
  630. test.deepEqual(
  631. xpath.stringifyObject({nodes:[a]}),
  632. xpath.stringifyObject(newCtx));
  633. test.done();
  634. };
  635. exports.testMorePredicates = function(test) {
  636. var doc = jsdom.jsdom('<html><body><blockquote><a></a></blockquote></body></html>');
  637. var blockquote = doc.getElementsByTagName('blockquote')[0],
  638. a = doc.getElementsByTagName('a')[0];
  639. var newCtx = xpath.evaluate('//*[ancestor::blockquote]', doc, doc.body);
  640. test.deepEqual(
  641. xpath.stringifyObject({nodes:[a]}),
  642. xpath.stringifyObject(newCtx));
  643. test.done();
  644. };
  645. exports.testAttributeWildcard = function(test) {
  646. var doc = jsdom.jsdom('<html><body><a href="x" rel=alternate>a</a></body></html>');
  647. var a = doc.getElementsByTagName('a')[0];
  648. var newCtx = xpath.evaluate('//*[@*="alternate"]', doc, doc.body);
  649. test.deepEqual(
  650. xpath.stringifyObject({nodes:[a]}),
  651. xpath.stringifyObject(newCtx));
  652. test.done();
  653. };
  654. exports.testEvaluatePath = function(test) {
  655. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  656. var html = doc.getElementsByTagName('html')[0],
  657. body = doc.getElementsByTagName('body')[0],
  658. div0 = doc.getElementsByTagName('div')[0],
  659. div1 = doc.getElementsByTagName('div')[1],
  660. img = doc.getElementsByTagName('img')[0];
  661. var newCtx = xpath.evaluate('div/div', doc, doc.body);
  662. test.deepEqual(
  663. xpath.stringifyObject(
  664. {nodes: [div1], pos: [[1]], lasts: [[1]]}),
  665. xpath.stringifyObject(newCtx));
  666. test.done();
  667. };
  668. exports.testEvaluateName = function(test) {
  669. var doc = jsdom.jsdom('<html><head></head><body></body></html>');
  670. test.equal('body', xpath.evaluate('name()', doc, doc.body));
  671. test.equal('body', xpath.evaluate('local-name()', doc, doc.body));
  672. test.done();
  673. };
  674. exports.testEvaluateSubstringBefore = function(test) {
  675. var doc = jsdom.jsdom('<html></html>');
  676. var newCtx = xpath.evaluate('substring-before("1999/04/01","/")', doc, doc.body);
  677. test.equal('1999', newCtx);
  678. test.done();
  679. };
  680. exports.testEvaluateSubstringAfter = function(test) {
  681. var doc = jsdom.jsdom('<html></html>');
  682. var newCtx = xpath.evaluate('substring-after("1999/04/01","/")', doc, doc.body);
  683. test.deepEqual('04/01', newCtx);
  684. test.done();
  685. };
  686. exports.testEvaluateSubstring = function(test) {
  687. var doc = jsdom.jsdom('<html></html>');
  688. test.equal('04', xpath.evaluate('substring("1999/04/01", 6, 2)', doc, doc));
  689. test.equal('04/01', xpath.evaluate('substring("1999/04/01", 6)', doc, doc));
  690. test.done();
  691. };
  692. exports.testEvaluateContains = function(test) {
  693. var doc = jsdom.jsdom('<html></html>');
  694. test.equal(true, xpath.evaluate('contains("hello", "el")', doc, doc));
  695. test.equal(false, xpath.evaluate('contains("hello", "mm")', doc, doc));
  696. test.done();
  697. };
  698. exports.testEvaluateTranslate = function(test) {
  699. var doc = jsdom.jsdom('<html></html>');
  700. test.equal('BAr', xpath.evaluate('translate("bar","abc","ABC")', doc, doc));
  701. test.equal('AAA', xpath.evaluate('translate("--aaa--", "abc-", "ABC")', doc, doc));
  702. test.equal('sub', xpath.evaluate('translate(normalize-space(" s u b"), " ", "")', doc, doc));
  703. test.done();
  704. };
  705. exports.testUnion = function(test) {
  706. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  707. var html = doc.getElementsByTagName('html')[0],
  708. body = doc.getElementsByTagName('body')[0],
  709. div0 = doc.getElementsByTagName('div')[0],
  710. div1 = doc.getElementsByTagName('div')[1],
  711. img = doc.getElementsByTagName('img')[0];
  712. var newCtx = xpath.evaluate('img|div/div', doc, doc.body);
  713. test.deepEqual(
  714. xpath.stringifyObject(
  715. {nodes: [div1, img]}),
  716. xpath.stringifyObject(newCtx));
  717. test.done();
  718. };
  719. exports.testUnion2 = function(test) {
  720. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  721. var html = doc.getElementsByTagName('html')[0],
  722. body = doc.getElementsByTagName('body')[0],
  723. div0 = doc.getElementsByTagName('div')[0],
  724. div1 = doc.getElementsByTagName('div')[1],
  725. img = doc.getElementsByTagName('img')[0];
  726. var newCtx = xpath.evaluate('div|zz', doc, doc.body);
  727. test.deepEqual(
  728. xpath.stringifyObject(
  729. {nodes: [div0]}),
  730. xpath.stringifyObject(newCtx));
  731. test.done();
  732. };
  733. exports.testUnion3 = function(test) {
  734. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  735. var html = doc.getElementsByTagName('html')[0],
  736. body = doc.getElementsByTagName('body')[0],
  737. div0 = doc.getElementsByTagName('div')[0],
  738. div1 = doc.getElementsByTagName('div')[1],
  739. img = doc.getElementsByTagName('img')[0];
  740. var newCtx = xpath.evaluate('zz|div', doc, doc.body);
  741. test.deepEqual(
  742. xpath.stringifyObject(
  743. {nodes: [div0]}),
  744. xpath.stringifyObject(newCtx));
  745. test.done();
  746. };
  747. exports.testAttributesHaveNoChildren = function(test) {
  748. var doc = jsdom.jsdom('<html><body><a></a><b id=hi>btext</b><i></i></body></html>');
  749. var a = doc.getElementsByTagName('a')[0],
  750. b = doc.getElementsByTagName('b')[0],
  751. btext = b.firstChild,
  752. attr = b.getAttributeNode('id'),
  753. i = doc.getElementsByTagName('i')[0];
  754. test.deepEqual(
  755. xpath.stringifyObject({nodes: [b], pos: [[1]], lasts: [[1]]}),
  756. xpath.stringifyObject(xpath.evaluate('parent::node()', doc, attr)));
  757. test.deepEqual(
  758. xpath.stringifyObject({nodes: [attr], pos: [[1]], lasts: [[1]]}),
  759. xpath.stringifyObject(xpath.evaluate('self::node()', doc, attr)));
  760. test.deepEqual(
  761. xpath.stringifyObject({nodes: [], pos: [], lasts: []}),
  762. xpath.stringifyObject(xpath.evaluate('child::node()', doc, attr)));
  763. test.deepEqual(
  764. xpath.stringifyObject({nodes: []}),
  765. xpath.stringifyObject(xpath.evaluate('descendant::node()', doc, attr)));
  766. test.deepEqual(
  767. xpath.stringifyObject({nodes: [attr]}),
  768. xpath.stringifyObject(xpath.evaluate('descendant-or-self::node()', doc, attr)));
  769. // Note: following DOES include the children of the element that the
  770. // attribute belongs to.
  771. test.deepEqual(
  772. xpath.stringifyObject({nodes: [btext, i]}),
  773. xpath.stringifyObject(xpath.evaluate('following::node()', doc, attr)));
  774. test.deepEqual(
  775. xpath.stringifyObject({nodes: []}),
  776. xpath.stringifyObject(xpath.evaluate('following-sibling::node()', doc, attr)));
  777. test.deepEqual(
  778. xpath.stringifyObject({nodes: [a], pos: [[1]], lasts: [[1]]}),
  779. xpath.stringifyObject(xpath.evaluate('preceding::node()', doc, attr)));
  780. test.deepEqual(
  781. xpath.stringifyObject({nodes: []}),
  782. xpath.stringifyObject(xpath.evaluate('preceding-sibling::node()', doc, attr)));
  783. test.done();
  784. };
  785. function stringifyNodeList(l) {
  786. var r = [];
  787. for (var i = 0; i < l.length; ++i) {
  788. r.push(l[i].outerHTML);
  789. }
  790. return r;
  791. }
  792. exports.testDocumentEvaluate = function(test) {
  793. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  794. var html = doc.getElementsByTagName('html')[0],
  795. body = doc.getElementsByTagName('body')[0],
  796. div0 = doc.getElementsByTagName('div')[0],
  797. div1 = doc.getElementsByTagName('div')[1],
  798. img = doc.getElementsByTagName('img')[0];
  799. var res = doc.evaluate('img', doc.body, null, 0, null);
  800. var r = [], x;
  801. while (x = res.iterateNext())
  802. r.push(x);
  803. test.deepEqual(
  804. stringifyNodeList([img]),
  805. stringifyNodeList(r));
  806. test.done();
  807. };
  808. exports.testDocumentEvaluate2 = function(test) {
  809. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  810. var html = doc.getElementsByTagName('html')[0],
  811. body = doc.getElementsByTagName('body')[0],
  812. div0 = doc.getElementsByTagName('div')[0],
  813. div1 = doc.getElementsByTagName('div')[1],
  814. img = doc.getElementsByTagName('img')[0];
  815. var res = doc.evaluate('//div', doc, null, 0, null);
  816. var r = [], x;
  817. while (x = res.iterateNext())
  818. r.push(x);
  819. test.deepEqual(
  820. stringifyNodeList([div0, div1]),
  821. stringifyNodeList(r));
  822. test.done();
  823. };
  824. exports.testDocumentEvaluateWildcard = function(test) {
  825. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  826. var html = doc.getElementsByTagName('html')[0],
  827. body = doc.getElementsByTagName('body')[0],
  828. div0 = doc.getElementsByTagName('div')[0],
  829. div1 = doc.getElementsByTagName('div')[1],
  830. img = doc.getElementsByTagName('img')[0];
  831. var res = doc.evaluate('//div/*', doc, null, 0, null);
  832. var r = [], x;
  833. while (x = res.iterateNext())
  834. r.push(x);
  835. test.deepEqual(
  836. stringifyNodeList([div1]),
  837. stringifyNodeList(r));
  838. test.done();
  839. };
  840. exports.testDocumentEvaluateStringPred = function(test) {
  841. var doc = jsdom.jsdom('<html><body><div>a<div>b</div></div><img></body></html>');
  842. var html = doc.getElementsByTagName('html')[0],
  843. body = doc.getElementsByTagName('body')[0],
  844. div0 = doc.getElementsByTagName('div')[0],
  845. div1 = doc.getElementsByTagName('div')[1],
  846. img = doc.getElementsByTagName('img')[0];
  847. var res = doc.evaluate('//div[1]', doc, null, 0, null);
  848. var r = [], x;
  849. while (x = res.iterateNext())
  850. r.push(x);
  851. test.deepEqual(
  852. stringifyNodeList([div0, div1]),
  853. stringifyNodeList(r));
  854. test.done();
  855. };
  856. exports.testAttributeNodePredicate = function(test) {
  857. // copied from Webkit LayoutTests/fast/xpath/attribute-node-predicate.html
  858. var doc = jsdom.jsdom('<html></html>');
  859. var root = doc.createElement('div');
  860. root.innerHTML =
  861. '<p>a</p><div><span id="21"></span><span id="22"></span><span id="23"></span></div>';
  862. var child1 = root.firstChild,
  863. child1text = child1.firstChild,
  864. child2 = root.lastChild,
  865. child21 = child2.firstChild,
  866. child22 = child21.nextSibling,
  867. child23 = child22.nextSibling;
  868. var result = xpath.evaluate(".//@id[false]", doc, root);
  869. test.deepEqual(xpath.stringifyObject({nodes:[]}), xpath.stringifyObject(result));
  870. result = xpath.evaluate(".//@id[1]/parent::*", doc, root);
  871. test.deepEqual(
  872. xpath.stringifyObject({nodes:[child21, child22, child23],
  873. pos: [ [ 1 ], [ 1 ], [ 1 ] ],
  874. lasts: [ [ 1 ], [ 1 ], [ 1 ] ]}),
  875. xpath.stringifyObject(result));
  876. result = xpath.evaluate(".//@id[2]/parent::*", doc, root);
  877. test.deepEqual(xpath.stringifyObject({nodes:[],pos:[],lasts:[]}), xpath.stringifyObject(result));
  878. result = xpath.evaluate(".//@id[string()='21']/parent::*", doc, root);
  879. test.deepEqual(
  880. xpath.stringifyObject({nodes:[child21], pos:[[1]],lasts:[[1]]}),
  881. xpath.stringifyObject(result));
  882. result = xpath.evaluate(".//@id[string()='22']/parent::*", doc, root);
  883. test.deepEqual(
  884. xpath.stringifyObject({nodes:[child22], pos:[[1]],lasts:[[1]]}),
  885. xpath.stringifyObject(result));
  886. test.done();
  887. };
  888. // The following test cases are taken from the NIST XSLT/XPath test suite.
  889. // http://web.archive.org/web/20041019015748/http://xw2k.sdct.itl.nist.gov/xml/page5.html
  890. // Only test cases applicable to XPath are included.
  891. exports.tests.NIST_coreFunction001 = function(test) {
  892. var document = new level3.Document();
  893. test.equal("correct substring",
  894. xpath.evaluate("substring(substring('internalexternalcorrect substring',9),9)", document, document), "correct substring");
  895. test.done();
  896. };
  897. exports.tests.NIST_coreFunction002 = function(test) {
  898. var document = new level3.Document();
  899. test.equal("correct substring",
  900. xpath.evaluate("substring(substring('internalexternalcorrect substring',9,25),9,17)", document, document), "correct substring");
  901. test.done();
  902. };
  903. exports.tests.NIST_coreFunction003 = function(test) {
  904. var document = new level3.Document();
  905. test.equal("A New Concatenated String",
  906. xpath.evaluate("concat(concat('A ','N','e'),'w ','Concatenated String')", document, document));
  907. test.done()
  908. };
  909. exports.tests.NIST_coreFunction004 = function(test) {
  910. var document = new level3.Document();
  911. test.equal("Unchanged String",
  912. xpath.evaluate("string(string('Unchanged String'))", document, document));
  913. test.done();
  914. };
  915. exports.tests.NIST_coreFunction005 = function(test) {
  916. var document = new level3.Document();
  917. test.equal("Correct Substring After",
  918. xpath.evaluate("substring-after(substring-after('wrongnogoodCorrect Substring After','wrong'),'nogood')", document, document));
  919. test.done();
  920. };
  921. exports.tests.NIST_coreFunction006 = function(test) {
  922. var document = new level3.Document();
  923. test.equal("correct substring Before",
  924. xpath.evaluate("substring-before(substring-before('correct substring Beforenogoodwrong','wrong'),'nogood')", document, document));
  925. test.done();
  926. };
  927. exports.tests.NIST_coreFunction007 = function(test) {
  928. var document = new level3.Document();
  929. test.equal("new string",
  930. xpath.evaluate("translate(translate('old string','old','123'),'123','new')", document, document));
  931. test.done();
  932. };
  933. exports.tests.NIST_coreFunction008 = function(test) {
  934. var document = new level3.Document();
  935. test.equal("new string",
  936. xpath.evaluate("translate('old string',translate('123','123','old'),'new')", document, document));
  937. test.done();
  938. };
  939. exports.tests.NIST_coreFunction009 = function(test) {
  940. var document = new level3.Document();
  941. test.equal("new string",
  942. xpath.evaluate("translate(translate('old string','old string','old string'),translate('123','123','old'),translate('123','123','new'))", document, document));
  943. test.done();
  944. };
  945. exports.tests.NIST_coreFunction010 = function(test) {
  946. var document = new level3.Document();
  947. test.equal("new string",
  948. xpath.evaluate("translate(translate('old string','old string','old string'),translate('123','123','old'),translate('123','123','new'))", document, document));
  949. test.done();
  950. };
  951. exports.tests.NIST_coreFunction011 = function(test) {
  952. var document = new level3.Document();
  953. test.equal("A New Concatenated String",
  954. xpath.evaluate("concat('A New ',concat('Conca','tena','ted '),'String')", document, document));
  955. test.done();
  956. };
  957. exports.tests.NIST_coreFunction012 = function(test) {
  958. var document = new level3.Document();
  959. test.equal("A New Concatenated String",
  960. xpath.evaluate("concat('A New ','Concatenated ',concat('St','ri','ng'))", document, document));
  961. test.done();
  962. };
  963. exports.tests.NIST_coreFunction013 = function(test) {
  964. var document = new level3.Document();
  965. test.equal("A New Concatenated String",
  966. xpath.evaluate("concat(concat('A ','Ne','w '),concat('Conca','tena','ted '),concat('St','ri','ng'))", document, document));
  967. test.done();
  968. };
  969. exports.tests.NIST_coreFunction014 = function(test) {
  970. var document = new level3.Document();
  971. test.equal("Correct Substring After",
  972. xpath.evaluate("substring-after('wrongCorrect Substring After',substring-after('nogoodstringwrong','nogoodstring'))", document, document));
  973. test.done();
  974. };
  975. exports.tests.NIST_coreFunction015 = function(test) {
  976. var document = new level3.Document();
  977. test.equal("Correct Substring After",
  978. xpath.evaluate("substring-after(substring-after('nogoodwrongCorrect Substring After','nogood'),substring-after('nogoodstringwrong','nogoodstring'))", document, document));
  979. test.done();
  980. };
  981. exports.tests.NIST_coreFunction016 = function(test) {
  982. var document = new level3.Document();
  983. test.equal("Correct Substring Before",
  984. xpath.evaluate("substring-before('Correct Substring Beforewrong',substring-before('wrongnogood','nogood'))", document, document));
  985. test.done();
  986. };
  987. exports.tests.NIST_coreFunction017 = function(test) {
  988. var document = new level3.Document();
  989. test.equal("Correct Substring Before",
  990. xpath.evaluate("substring-before(substring-before('Correct Substring Beforewrongcut here','cut here'),substring-before('wrongnogood','nogood'))", document, document));
  991. test.done();
  992. };
  993. // coreFunction018 thru coreFunction035 are omitted because they test XPath
  994. // variables, but DOM 3 XPath does not provide any facility to set variables.
  995. //
  996. // The tests are reproduced here anyway in case in the future jsdom provides
  997. // some non-standard mechanism for setting variables.
  998. //
  999. //
  1000. // exports.tests.NIST_coreFunction018 = function(test) {
  1001. // var document = new level3.Document();
  1002. // // set $variable1 = "String From Variable"
  1003. // test.equal("String From Variable",
  1004. // xpath.evaluate("string($variable1)", document, document));
  1005. // };
  1006. //
  1007. // exports.tests.NIST_coreFunction019 = function(test) {
  1008. // var document = new level3.Document();
  1009. // // set $variable1 = "String "
  1010. // test.equal("String From Variable",
  1011. // xpath.evaluate("concat($variable1,'From ','Variable')", document, document));
  1012. // };
  1013. //
  1014. // exports.tests.NIST_coreFunction020 = function(test) {
  1015. // var document = new level3.Document();
  1016. // // set $variable1 = "From "
  1017. // test.equal("String From Variable",
  1018. // xpath.evaluate("concat('String ',$variable1,'Variable')", document, document));
  1019. // };
  1020. //
  1021. // exports.tests.NIST_coreFunction021 = function(test) {
  1022. // var document = new level3.Document();
  1023. // // set $variable1 = "Variable"
  1024. // test.equal("String From Variable",
  1025. // xpath.evaluate("concat('String ','From ',$variable1)", document, document));
  1026. // };
  1027. //
  1028. // exports.tests.NIST_coreFunction022 = function(test) {
  1029. // var document = new level3.Document();
  1030. // // set $variable1 = "String "
  1031. // // set $variable2 = "From "
  1032. // // set $variable3 = "Variable"
  1033. // test.equal("String From Variable",
  1034. // xpath.evaluate("concat($variable1,$variable2,$variable3)", document, document));
  1035. // };
  1036. //
  1037. // exports.tests.NIST_coreFunction023 = function(test) {
  1038. // var document = new level3.Document();
  1039. // // set $variable1 = "substring-before with variablecut this"
  1040. // test.equal("substring-before with variable",
  1041. // xpath.evaluate("substring-before($variable1,'cut this')", document, document));
  1042. // };
  1043. //
  1044. // exports.tests.NIST_coreFunction024 = function(test) {
  1045. // var document = new level3.Document();
  1046. // // set $variable1 = "cut this"
  1047. // test.equal("substring-before with variable",
  1048. // xpath.evaluate("substring-before('substring-before with variablecut this',$variable1)", document, document));
  1049. // };
  1050. //
  1051. // exports.tests.NIST_coreFunction025 = function(test) {
  1052. // var document = new level3.Document();
  1053. // // set $variable1 = "substring before with variablecut this"
  1054. // // set $variable2 = "cut this"
  1055. // test.equal("substring before with variable",
  1056. // xpath.evaluate("substring-before($variable1,$variable2)", document, document));
  1057. // };
  1058. //
  1059. // exports.tests.NIST_coreFunction026 = function(test) {
  1060. // var document = new level3.Document();
  1061. // // set $variable1 = "cut thissubstring-after with variable"
  1062. // test.equal("substring-after with variable",
  1063. // xpath.evaluate("substring-after($variable1,'cut this')", document, document));
  1064. // };
  1065. //
  1066. // exports.tests.NIST_coreFunction027 = function(test) {
  1067. // var document = new level3.Document();
  1068. // // set $variable1 = "cut this"
  1069. // test.equal("substring after with variable",
  1070. // xpath.evaluate("substring-after('cut thissubstring after with variable',$variable1)", document, document));
  1071. // };
  1072. //
  1073. // exports.tests.NIST_coreFunction028 = function(test) {
  1074. // var document = new level3.Document();
  1075. // // set $variable1 = "cut thissubstring-after with variable"
  1076. // // set $variable2 = "cut this"
  1077. // test.equal("substring-after with variable",
  1078. // xpath.evaluate("substring-after($variable1,$variable2)", document, document));
  1079. // };
  1080. //
  1081. // exports.tests.NIST_coreFunction029 = function(test) {
  1082. // var document = new level3.Document();
  1083. // // set $variable1 = "cut thissubstring with variable"
  1084. // test.equal("substring with variable",
  1085. // xpath.evaluate("substring($variable1,9)", document, document));
  1086. // };
  1087. //
  1088. // exports.tests.NIST_coreFunction030 = function(test) {
  1089. // var document = new level3.Document();
  1090. // // set $variable1 = "cut thissubstring with variable"
  1091. // test.equal("substring with variable",
  1092. // xpath.evaluate("substring($variable1,9,23)", document, document));
  1093. // };
  1094. //
  1095. // exports.tests.NIST_coreFunction031 = function(test) {
  1096. // var document = new level3.Document();
  1097. // // set $variable1 = "should return the value 26"
  1098. // test.equal(26,
  1099. // xpath.evaluate("string-length($variable1)", document, document));
  1100. // };
  1101. //
  1102. // exports.tests.NIST_coreFunction032 = function(test) {
  1103. // var document = new level3.Document();
  1104. // // set $variable1 = "translate 1234 variable"
  1105. // test.equal("translate with variable",
  1106. // xpath.evaluate("translate($variable1,'1234','with')", document, document));
  1107. // };
  1108. //
  1109. // exports.tests.NIST_coreFunction033 = function(test) {
  1110. // var document = new level3.Document();
  1111. // // set $variable1 = "1234"
  1112. // test.equal("translate with variable",
  1113. // xpath.evaluate("translate('translate 1234 variable',$variable1,'with')", document, document));
  1114. // };
  1115. //
  1116. // exports.tests.NIST_coreFunction034 = function(test) {
  1117. // var document = new level3.Document();
  1118. // // set $variable1 = "with"
  1119. // test.equal("translate with variable",
  1120. // xpath.evaluate("translate('translate 1234 variable','1234',$variable1)", document, document));
  1121. // };
  1122. //
  1123. // exports.tests.NIST_coreFunction035 = function(test) {
  1124. // var document = new level3.Document();
  1125. // // set $variable1 = "translate 1234 variable"
  1126. // // set $variable2 = "1234"
  1127. // // set $variable3 = "with"
  1128. // test.equal("translate with variable",
  1129. // xpath.evaluate("translate($variable1,$variable2,$variable3)", document, document));
  1130. // };
  1131. // coreFunction036 thru coreFunction059 are omitted since they test XSLT
  1132. // parameters. Outside the context of XSLT, they are effectively redundant
  1133. // with coreFunction018 thru coreFunction035.
  1134. exports.tests.NIST_coreFunction060 = function(test) {
  1135. var document = new level3.Document();
  1136. test.equal(-2,
  1137. xpath.evaluate("floor(-1.99999)", document, document));
  1138. test.done();
  1139. };
  1140. exports.tests.NIST_coreFunction061 = function(test) {
  1141. var document = new level3.Document();
  1142. test.equal(-2,
  1143. xpath.evaluate("floor(-1.0001)", document, document));
  1144. test.done();
  1145. };
  1146. // coreFunction062 is omitted because it tests XPath variables, as above.
  1147. // exports.tests.NIST_coreFunction062 = function(test) {
  1148. // var document = new level3.Document();
  1149. // // set $variable1 = "3.1"
  1150. // test.equal(3,
  1151. // xpath.evaluate("floor($variable1)", document, document));
  1152. // };
  1153. // coreFunction063 is omitted because it tests XSLT parameters, as above.
  1154. exports.tests.NIST_coreFunction064 = function(test) {
  1155. var document = new level3.Document();
  1156. test.equal(2,
  1157. xpath.evaluate("floor(ceiling(1.2))", document, document));
  1158. test.done();
  1159. };
  1160. exports.tests.NIST_coreFunction065 = function(test) {
  1161. var document = new level3.Document();
  1162. test.equal(1,
  1163. xpath.evaluate("floor(round(1.2))", document, document));
  1164. test.done();
  1165. };
  1166. exports.tests.NIST_coreFunction066 = function(test) {
  1167. var document = new level3.Document();
  1168. test.equal(1,
  1169. xpath.evaluate("floor(floor(1.2))", document, document));
  1170. test.done();
  1171. };
  1172. exports.tests.NIST_coreFunction067 = function(test) {
  1173. var document = new level3.Document();
  1174. test.equal(1,
  1175. xpath.evaluate("floor((((((2*10)-4)+9) div 5) mod 2))", document, document));
  1176. test.done();
  1177. };
  1178. exports.tests.NIST_coreFunction068 = function(test) {
  1179. var document = new level3.Document();
  1180. test.equal(-1,
  1181. xpath.evaluate("ceiling(-1.0001)", document, document));
  1182. test.done();
  1183. };
  1184. exports.tests.NIST_coreFunction069 = function(test) {
  1185. var document = new level3.Document();
  1186. test.equal(-1,
  1187. xpath.evaluate("ceiling(-1.9999)", document, document));
  1188. test.done();
  1189. };
  1190. // coreFunction070 is omitted because it tests XPath variables, as above.
  1191. //
  1192. // exports.tests.NIST_coreFunction070 = function(test) {
  1193. // var document = new level3.Document();
  1194. // // set $variable1 = "2.5"
  1195. // test.equal(3,
  1196. // xpath.evaluate("ceiling($variable1)", document, document));
  1197. // };
  1198. exports.tests.NIST_coreFunction071 = function(test) {
  1199. var document = new level3.Document();
  1200. test.equal(2,
  1201. xpath.evaluate("ceiling(floor(2.2))", document, document));
  1202. test.done();
  1203. };
  1204. exports.tests.NIST_coreFunction072 = function(test) {
  1205. var document = new level3.Document();
  1206. test.equal(4,
  1207. xpath.evaluate("ceiling(ceiling(3.2))", document, document));
  1208. test.done();
  1209. };
  1210. // coreFunction073 is omitted because it tests XSLT parameters, as above.
  1211. exports.tests.NIST_coreFunction074 = function(test) {
  1212. var document = new level3.Document();
  1213. test.equal(3,
  1214. xpath.evaluate("ceiling((((((2*10)-4)+9) div 5) div 2))", document, document));
  1215. test.done();
  1216. };
  1217. exports.tests.NIST_coreFunction075 = function(test) {
  1218. var document = new level3.Document();
  1219. test.equal(-2,
  1220. xpath.evaluate("round(-1.9999)", document, document));
  1221. test.done();
  1222. };
  1223. // coreFunction076 is omitted because it tests XPath variables, as above.
  1224. //
  1225. // exports.tests.NIST_coreFunction076 = function(test) {
  1226. // var document = new level3.Document();
  1227. // // set $variable1 = "2.3"
  1228. // test.equal(2
  1229. // xpath.evaluate("round($variable1)", document, document));
  1230. // };
  1231. // coreFunction077 is omitted because it tests XSLT parameters, as above.
  1232. exports.tests.NIST_coreFunction078 = function(test) {
  1233. var document = new level3.Document();
  1234. test.equal(4,
  1235. xpath.evaluate("round(ceiling(3.2))", document, document));
  1236. test.done();
  1237. };
  1238. exports.tests.NIST_coreFunction079 = function(test) {
  1239. var document = new level3.Document();
  1240. test.equal(3,
  1241. xpath.evaluate("round((((((2*10)-4)+9) div 5) div 2))", document, document));
  1242. test.done();
  1243. };
  1244. exports.tests.NIST_coreFunction080 = function(test) {
  1245. var document = new level3.Document();
  1246. test.ok(isNaN(xpath.evaluate("round(NaN)", document, document)));
  1247. test.done();
  1248. };
  1249. exports.tests.NIST_coreFunction081 = function(test) {
  1250. var document = new level3.Document();
  1251. test.equal(0,
  1252. xpath.evaluate("round(-0)", document, document));
  1253. test.done();
  1254. };
  1255. exports.tests.NIST_coreFunction082 = function(test) {
  1256. var document = new level3.Document();
  1257. test.equal(0,
  1258. xpath.evaluate("round(-0.25)", document, document));
  1259. test.done();
  1260. };
  1261. exports.tests.NIST_coreFunction083 = function(test) {
  1262. var document = new level3.Document();
  1263. test.equal(2,
  1264. xpath.evaluate("round(round(2.3))", document, document));
  1265. test.done();
  1266. };
  1267. exports.tests.NIST_coreFunction084 = function(test) {
  1268. var document = new level3.Document();
  1269. test.equal(Number.POSITIVE_INFINITY,
  1270. xpath.evaluate("round(2.3 div 0)", document, document));
  1271. test.done();
  1272. };
  1273. exports.tests.NIST_coreFunction085 = function(test) {
  1274. var document = new level3.Document();
  1275. test.equal(Number.NEGATIVE_INFINITY,
  1276. xpath.evaluate("round(-2.3 div 0)", document, document));
  1277. test.done();
  1278. };
  1279. exports.tests.NIST_coreFunction086 = function(test) {
  1280. var document = new level3.Document();
  1281. test.equal(-1.9999,
  1282. xpath.evaluate("number('-1.9999')", document, document));
  1283. test.done();
  1284. };
  1285. exports.tests.NIST_coreFunction087 = function(test) {
  1286. var document = new level3.Document();
  1287. test.equal(1.9999,
  1288. xpath.evaluate("number('1.9999')", document, document));
  1289. test.done();
  1290. };
  1291. exports.tests.NIST_coreFunction088 = function(test) {
  1292. var document = new level3.Document();
  1293. var doc = document.createElement("doc");
  1294. document.appendChild(doc);
  1295. var element1 = document.createElement("element1");
  1296. doc.appendChild(element1);
  1297. var child1 = document.createElement("child1");
  1298. element1.appendChild(child1);
  1299. var text = document.createTextNode("Test executed Successfully!!");
  1300. child1.appendChild(text);
  1301. var element2 = document.createElement("element2");
  1302. doc.appendChild(element2);
  1303. child1 = document.createElement("child1");
  1304. text = document.createTextNode("Incorrect execution!!");
  1305. child1.appendChild(text);
  1306. test.equal(1,
  1307. xpath.evaluate("count(//child1[ancestor::element1])", document, doc));
  1308. test.done();
  1309. };
  1310. exports.tests.NIST_coreFunction089 = function(test) {
  1311. var document = new level3.Document();
  1312. var doc = document.createElement("doc");
  1313. document.appendChild(doc);
  1314. var element1 = document.createElement("element1");
  1315. doc.appendChild(element1);
  1316. var text = document.createTextNode("Incorrect Execution!!");
  1317. element1.appendChild(text);
  1318. element1 = document.createElement("element1");
  1319. doc.appendChild(element1);
  1320. text = document.createTextNode("Test executed Successfully!!");
  1321. element1.appendChild(text);
  1322. domTestHelper.arrayEqual(test, [element1],
  1323. xpath.evaluate("element1[2]", document, doc).nodes);
  1324. test.done();
  1325. };
  1326. // Many of the NIST dataManipulation tests include more than one XPath query,
  1327. // so here they're split into multiple test cases.
  1328. //
  1329. // Some dataManipulation tests test XSLT features that aren't part of XPath,
  1330. // so those tests are omitted here.
  1331. exports.tests.NIST_dataManipulation001a = function(test) {
  1332. var document = new level3.Document();
  1333. test.equal(true,
  1334. xpath.evaluate("2 > 1", document, document));
  1335. test.done();
  1336. };
  1337. exports.tests.NIST_dataManipulation001b = function(test) {
  1338. var document = new level3.Document();
  1339. test.equal(true,
  1340. xpath.evaluate("9 mod 3 = 0", document, document));
  1341. test.done();
  1342. };
  1343. exports.tests.NIST_dataManipulation002a = function(test) {
  1344. var document = new level3.Document();
  1345. test.equal(false,
  1346. xpath.evaluate("2 > 3", document, document));
  1347. test.done();
  1348. };
  1349. exports.tests.NIST_dataManipulation003 = function(test) {
  1350. var document = new level3.Document();
  1351. test.equal(true,
  1352. xpath.evaluate("(((((2*10)-4)+9) div 5) div 2) > 2", document, document));
  1353. test.done();
  1354. };
  1355. exports.tests.NIST_dataManipulation004 = function(test) {
  1356. var document = new level3.Document();
  1357. test.equal(false,
  1358. xpath.evaluate("(((((2*10)-4)+9) div 5) div 2) > 4", document, document));
  1359. test.done();
  1360. };
  1361. exports.tests.NIST_dataManipulation007 = function(test) {
  1362. var document = new level3.Document();
  1363. test.equal(true,
  1364. xpath.evaluate("(round(3.7) > 3)", document, document));
  1365. test.done();
  1366. };
  1367. exports.tests.NIST_dataManipulation009 = function(test) {
  1368. var document = new level3.Document();
  1369. var doc = document.createElement("doc");
  1370. document.appendChild(doc);
  1371. var element1 = document.createElement("element1");
  1372. doc.appendChild(element1);
  1373. var text = document.createTextNode("Test executed successfully!!");
  1374. element1.appendChild(text);
  1375. var element2 = document.createElement("element2");
  1376. text = document.createTextNode("Incorrect execution!!");
  1377. element2.appendChild(text);
  1378. domTestHelper.arrayEqual(test, [element1],
  1379. xpath.evaluate("doc/element1", document, document).nodes);
  1380. test.done();
  1381. };
  1382. exports.tests.NIST_dataManipulation013 = function(test) {
  1383. var document = new level3.Document();
  1384. var doc = document.createElement("doc");
  1385. document.appendChild(doc);
  1386. var element1 = document.createElement("element1");
  1387. doc.appendChild(element1);
  1388. var text = document.createTextNode("Incorrect execution!!");
  1389. element1.appendChild(text);
  1390. element1 = document.createElement("element1");
  1391. doc.appendChild(element1);
  1392. text = document.createTextNode("Incorrect execution!!");
  1393. element1.appendChild(text);
  1394. element1 = document.createElement("element1");
  1395. doc.appendChild(element1);
  1396. text = document.createTextNode("Test Executed Successfully!!");
  1397. element1.appendChild(text);
  1398. var element2 = document.createElement("element2");
  1399. doc.appendChild(element2);
  1400. text = document.createTextNode("Incorrect execution!!");
  1401. element2.appendChild(text);
  1402. domTestHelper.arrayEqual(test, [element1],
  1403. xpath.evaluate("doc/element1[last()]", document, document).nodes);
  1404. test.done();
  1405. };
  1406. exports.tests.NIST_dataManipulation014 = function(test) {
  1407. var document = new level3.Document();
  1408. var doc = document.createElement("doc");
  1409. document.appendChild(doc);
  1410. var element1 = document.createElement("element1");
  1411. doc.appendChild(element1);
  1412. var text = document.createTextNode("Incorrect execution!!");
  1413. element1.appendChild(text);
  1414. element1 = document.createElement("element1");
  1415. doc.appendChild(element1);
  1416. text = document.createTextNode("Incorrect execution!!");
  1417. element1.appendChild(text);
  1418. element1 = document.createElement("element1");
  1419. doc.appendChild(element1);
  1420. text = document.createTextNode("Test Executed Successfully!!");
  1421. element1.appendChild(text);
  1422. var element2 = document.createElement("element2");
  1423. doc.appendChild(element2);
  1424. text = document.createTextNode("Incorrect execution!!");
  1425. element2.appendChild(text);
  1426. domTestHelper.arrayEqual(test, [element1],
  1427. xpath.evaluate("doc/element1[((((((2*10)-4)+9) div 5) mod 3)+1)]", document, document).nodes);
  1428. test.done();
  1429. };
  1430. exports.tests.NIST_dataManipulation016 = function(test) {
  1431. var document = new level3.Document();
  1432. var doc = document.createElement("doc");
  1433. document.appendChild(doc);
  1434. var element1 = document.createElement("element1");
  1435. doc.appendChild(element1);
  1436. var good_child1 = document.createElement("child1");
  1437. element1.appendChild(good_child1);
  1438. var text = document.createTextNode("Test Executed Successfully!!");
  1439. good_child1.appendChild(text);
  1440. var element2 = document.createElement("element2");
  1441. doc.appendChild(element2);
  1442. child1 = document.createElement("child1");
  1443. element2.appendChild(child1);
  1444. text = document.createTextNode("Incorrect Execution!!");
  1445. child1.appendChild(text);
  1446. domTestHelper.arrayEqual(test, [good_child1],
  1447. xpath.evaluate("//child1[ancestor::element1]", document, document).nodes);
  1448. test.done();
  1449. };
  1450. exports.tests.NIST_expression001 = function(test) {
  1451. var document = new level3.Document();
  1452. var doc = document.createElement("doc");
  1453. document.appendChild(doc);
  1454. var sub1 = document.createElement("sub1");
  1455. doc.appendChild(sub1);
  1456. var child1 = document.createElement("child1");
  1457. sub1.appendChild(child1);
  1458. var text = document.createTextNode("child1");
  1459. child1.appendChild(text);
  1460. var sub2 = document.createElement("sub2");
  1461. doc.appendChild(sub2);
  1462. var child2 = document.createElement("child2");
  1463. sub2.appendChild(child2);
  1464. text = document.createTextNode("child2");
  1465. child2.appendChild(text);
  1466. domTestHelper.arrayEqual(test, [child1,child2],
  1467. xpath.evaluate("/doc/sub1/child1|/doc/sub2/child2", document, doc).nodes);
  1468. test.done();
  1469. };
  1470. exports.tests.NIST_expression002 = function(test) {
  1471. var document = new level3.Document();
  1472. var doc = document.createElement("doc");
  1473. document.appendChild(doc);
  1474. var sub1 = document.createElement("sub1");
  1475. doc.appendChild(sub1);
  1476. var child1 = document.createElement("child1");
  1477. sub1.appendChild(child1);
  1478. var text = document.createTextNode("child1");
  1479. child1.appendChild(text);
  1480. var sub2 = document.createElement("sub2");
  1481. doc.appendChild(sub2);
  1482. var child2 = document.createElement("child2");
  1483. sub2.appendChild(child2);
  1484. text = document.createTextNode("child2");
  1485. child2.appendChild(text);
  1486. domTestHelper.arrayEqual(test, [child1,child2],
  1487. xpath.evaluate("sub1/child1|/doc/sub2/child2", document, doc).nodes);
  1488. test.done();
  1489. };
  1490. exports.tests.NIST_expression003 = function(test) {
  1491. var document = new level3.Document();
  1492. var doc = document.createElement("doc");
  1493. document.appendChild(doc);
  1494. var sub1 = document.createElement("sub1");
  1495. doc.appendChild(sub1);
  1496. var child1 = document.createElement("child1");
  1497. sub1.appendChild(child1);
  1498. var text = document.createTextNode("descendant number 1");
  1499. child1.appendChild(text);
  1500. var sub2 = document.createElement("sub2");
  1501. doc.appendChild(sub2);
  1502. var child2 = document.createElement("child2");
  1503. sub2.appendChild(child2);
  1504. text = document.createTextNode("descendant number 2");
  1505. child2.appendChild(text);
  1506. domTestHelper.arrayEqual(test, [child1,child2],
  1507. xpath.evaluate("//child1|//child2", document, doc).nodes);
  1508. domTestHelper.arrayEqual(test, [sub1],
  1509. xpath.evaluate("ancestor::sub1|ancestor::sub2", document, child1).nodes);
  1510. domTestHelper.arrayEqual(test, [sub2],
  1511. xpath.evaluate("ancestor::sub1|ancestor::sub2", document, child2).nodes);
  1512. test.done();
  1513. };
  1514. exports.tests.NIST_expression004 = function(test) {
  1515. var document = new level3.Document();
  1516. var doc = document.createElement("doc");
  1517. document.appendChild(doc);
  1518. var sub1 = document.createElement("sub1");
  1519. doc.appendChild(sub1);
  1520. var child1 = document.createElement("child1");
  1521. sub1.appendChild(child1);
  1522. var text = document.createTextNode("descendant number 1");
  1523. child1.appendChild(text);
  1524. var sub2 = document.createElement("sub2");
  1525. doc.appendChild(sub2);
  1526. var child2 = document.createElement("child2");
  1527. sub2.appendChild(child2);
  1528. text = document.createTextNode("descendant number 2");
  1529. child2.appendChild(text);
  1530. domTestHelper.arrayEqual(test, [child1,child2],
  1531. xpath.evaluate("//child1|//child2", document, doc).nodes);
  1532. domTestHelper.arrayEqual(test, [sub1],
  1533. xpath.evaluate("ancestor-or-self::sub1|ancestor-or-self::sub2", document, child1).nodes);
  1534. domTestHelper.arrayEqual(test, [sub2],
  1535. xpath.evaluate("ancestor-or-self::sub1|ancestor-or-self::sub2", document, child2).nodes);
  1536. test.done();
  1537. };
  1538. exports.tests.NIST_expression005 = function(test) {
  1539. var document = new level3.Document();
  1540. var doc = document.createElement("doc");
  1541. document.appendChild(doc);
  1542. var book1 = document.createElement("book");
  1543. doc.appendChild(book1);
  1544. var author1 = document.createElement("author");
  1545. book1.appendChild(author1);
  1546. var name = document.createElement("name");
  1547. author1.appendChild(name);
  1548. name.setAttribute("real", "no");
  1549. var text = document.createTextNode("Carmelo Montanez");
  1550. name.appendChild(text);
  1551. var chapters = document.createElement("chapters");
  1552. author1.appendChild(chapters);
  1553. text = document.createTextNode("Nine");
  1554. chapters.appendChild(text);
  1555. var bibliography = document.createElement("bibliography");
  1556. author1.appendChild(bibliography);
  1557. var book2 = document.createElement("book");
  1558. doc.appendChild(book2);
  1559. var author2 = document.createElement("author");
  1560. book2.appendChild(author2);
  1561. name = document.createElement("name");
  1562. author2.appendChild(name);
  1563. name.setAttribute("real", "na");
  1564. text = document.createTextNode("David Marston");
  1565. name.appendChild(text);
  1566. chapters = document.createElement("chapters");
  1567. author2.appendChild(chapters);
  1568. text = document.createTextNode("Seven");
  1569. chapters.appendChild(text);
  1570. bibliography = document.createElement("bibliography");
  1571. author2.appendChild(bibliography);
  1572. var book3 = document.createElement("book");
  1573. doc.appendChild(book3);
  1574. var author3 = document.createElement("author");
  1575. book3.appendChild(author3);
  1576. name = document.createElement("name");
  1577. author3.appendChild(name);
  1578. name.setAttribute("real", "yes");
  1579. text = document.createTextNode("Mary Brady");
  1580. name.appendChild(text);
  1581. chapters = document.createElement("chapters");
  1582. author3.appendChild(chapters);
  1583. text = document.createTextNode("Ten");
  1584. bibliography = document.createElement("bibliography");
  1585. author3.appendChild(bibliography);
  1586. domTestHelper.arrayEqual(test, [author1],
  1587. xpath.evaluate("author[name/@real='no']|author[name/@real='yes']", document, book1).nodes);
  1588. domTestHelper.arrayEqual(test, [],
  1589. xpath.evaluate("author[name/@real='no']|author[name/@real='yes']", document, book2).nodes);
  1590. domTestHelper.arrayEqual(test, [author3],
  1591. xpath.evaluate("author[name/@real='no']|author[name/@real='yes']", document, book3).nodes);
  1592. test.done();
  1593. };
  1594. exports.tests.NIST_expression006 = function(test) {
  1595. var document = new level3.Document();
  1596. var doc = document.createElement("doc");
  1597. doc.setAttribute("attr1", "attribute 1 ");
  1598. doc.setAttribute("attr2", "attribute 2");
  1599. document.appendChild(doc);
  1600. var sub1 = document.createElement("sub1");
  1601. doc.appendChild(sub1);
  1602. var child1 = document.createElement("child1");
  1603. sub1.appendChild(child1);
  1604. var text = document.createTextNode("child number 1");
  1605. child1.appendChild(text);
  1606. var sub2 = document.createElement("sub2");
  1607. doc.appendChild(sub2);
  1608. var child2 = document.createElement("child2");
  1609. text = document.createTextNode("child number 2");
  1610. child2.appendChild(text);
  1611. domTestHelper.arrayEqual(test, [doc.getAttributeNode("attr1"), doc.getAttributeNode("attr2")],
  1612. xpath.evaluate("attribute::attr1|attribute::attr2", document, doc).nodes);
  1613. test.done();
  1614. };
  1615. exports.tests.NIST_expression007 = function(test) {
  1616. var document = new level3.Document();
  1617. var doc = document.createElement("doc");
  1618. doc.setAttribute("attr1", "attribute 1 ");
  1619. doc.setAttribute("attr2", "attribute 2");
  1620. document.appendChild(doc);
  1621. var sub1 = document.createElement("sub1");
  1622. doc.appendChild(sub1);
  1623. var child1 = document.createElement("child1");
  1624. sub1.appendChild(child1);
  1625. var text = document.createTextNode("child number 1");
  1626. child1.appendChild(text);
  1627. var sub2 = document.createElement("sub2");
  1628. doc.appendChild(sub2);
  1629. var child2 = document.createElement("child2");
  1630. text = document.createTextNode("child number 2");
  1631. child2.appendChild(text);
  1632. domTestHelper.arrayEqual(test, [sub1, sub2],
  1633. xpath.evaluate("child::sub1|child::sub2", document, doc).nodes);
  1634. test.done();
  1635. };
  1636. exports.tests.NIST_expression008 = function(test) {
  1637. var document = new level3.Document();
  1638. var doc = document.createElement("doc");
  1639. document.appendChild(doc);
  1640. var book1 = document.createElement("book");
  1641. doc.appendChild(book1);
  1642. var author1 = document.createElement("author");
  1643. book1.appendChild(author1);
  1644. var name = document.createElement("name");
  1645. author1.appendChild(name);
  1646. name.setAttribute("real", "no");
  1647. var text = document.createTextNode("Carmelo Montanez");
  1648. name.appendChild(text);
  1649. var chapters = document.createElement("chapters");
  1650. author1.appendChild(chapters);
  1651. text = document.createTextNode("Nine");
  1652. chapters.appendChild(text);
  1653. var bibliography = document.createElement("bibliography");
  1654. author1.appendChild(bibliography);
  1655. var book2 = document.createElement("book");
  1656. doc.appendChild(book2);
  1657. var author2 = document.createElement("author");
  1658. book2.appendChild(author2);
  1659. name = document.createElement("name");
  1660. author2.appendChild(name);
  1661. name.setAttribute("real", "na");
  1662. text = document.createTextNode("David Marston");
  1663. name.appendChild(text);
  1664. chapters = document.createElement("chapters");
  1665. author2.appendChild(chapters);
  1666. text = document.createTextNode("Seven");
  1667. chapters.appendChild(text);
  1668. bibliography = document.createElement("bibliography");
  1669. author2.appendChild(bibliography);
  1670. var book3 = document.createElement("book");
  1671. doc.appendChild(book3);
  1672. var author3 = document.createElement("author");
  1673. book3.appendChild(author3);
  1674. name = document.createElement("name");
  1675. author3.appendChild(name);
  1676. name.setAttribute("real", "yes");
  1677. text = document.createTextNode("Mary Brady");
  1678. name.appendChild(text);
  1679. chapters = document.createElement("chapters");
  1680. author3.appendChild(chapters);
  1681. text = document.createTextNode("Ten");
  1682. bibliography = document.createElement("bibliography");
  1683. author3.appendChild(bibliography);
  1684. domTestHelper.arrayEqual(test, [author1],
  1685. xpath.evaluate("author[(name/@real='no' and position()=1)]|author[(name/@real='yes' and position()=last())]", document, book1).nodes);
  1686. domTestHelper.arrayEqual(test, [],
  1687. xpath.evaluate("author[(name/@real='no' and position()=1)]|author[(name/@real='yes' and position()=last())]", document, book2).nodes);
  1688. domTestHelper.arrayEqual(test, [author3],
  1689. xpath.evaluate("author[(name/@real='no' and position()=1)]|author[(name/@real='yes' and position()=last())]", document, book3).nodes);
  1690. test.done();
  1691. };
  1692. exports.tests.NIST_expression009 = function(test) {
  1693. var document = new level3.Document();
  1694. var doc = document.createElement("doc");
  1695. document.appendChild(doc);
  1696. var sub1 = document.createElement("sub1");
  1697. doc.appendChild(sub1);
  1698. var child1 = document.createElement("child1");
  1699. sub1.appendChild(child1);
  1700. var text = document.createTextNode("descendant number 1");
  1701. child1.appendChild(text);
  1702. var sub2 = document.createElement("sub2");
  1703. doc.appendChild(sub2);
  1704. var child2 = document.createElement("child2");
  1705. sub2.appendChild(child2);
  1706. text = document.createTextNode("descendant number 2");
  1707. child2.appendChild(text);
  1708. domTestHelper.arrayEqual(test, [child1,child2],
  1709. xpath.evaluate("descendant::child1|descendant::child2", document, doc).nodes);
  1710. test.done();
  1711. };
  1712. exports.tests.NIST_expression010 = function(test) {
  1713. var document = new level3.Document();
  1714. var doc = document.createElement("doc");
  1715. document.appendChild(doc);
  1716. var sub1 = document.createElement("sub1");
  1717. doc.appendChild(sub1);
  1718. var child1 = document.createElement("child1");
  1719. sub1.appendChild(child1);
  1720. var text = document.createTextNode("descendant number 1");
  1721. child1.appendChild(text);
  1722. var sub2 = document.createElement("sub2");
  1723. doc.appendChild(sub2);
  1724. var child2 = document.createElement("child2");
  1725. sub2.appendChild(child2);
  1726. text = document.createTextNode("descendant number 2");
  1727. child2.appendChild(text);
  1728. domTestHelper.arrayEqual(test, [doc],
  1729. xpath.evaluate("descendant-or-self::doc|descendant-or-self::doc", document, doc).nodes);
  1730. test.done();
  1731. };
  1732. exports.tests.NIST_expression011 = function(test) {
  1733. var document = new level3.Document();
  1734. var doc = document.createElement("doc");
  1735. document.appendChild(doc);
  1736. var book1 = document.createElement("book");
  1737. doc.appendChild(book1);
  1738. var author1 = document.createElement("author");
  1739. book1.appendChild(author1);
  1740. var name = document.createElement("name");
  1741. author1.appendChild(name);
  1742. name.setAttribute("real", "no");
  1743. var text = document.createTextNode("Carmelo Montanez");
  1744. name.appendChild(text);
  1745. var chapters = document.createElement("chapters");
  1746. author1.appendChild(chapters);
  1747. text = document.createTextNode("Nine");
  1748. chapters.appendChild(text);
  1749. var bibliography = document.createElement("bibliography");
  1750. author1.appendChild(bibliography);
  1751. var book2 = document.createElement("book");
  1752. doc.appendChild(book2);
  1753. var author2 = document.createElement("author");
  1754. book2.appendChild(author2);
  1755. name = document.createElement("name");
  1756. author2.appendChild(name);
  1757. name.setAttribute("real", "na");
  1758. text = document.createTextNode("David Marston");
  1759. name.appendChild(text);
  1760. chapters = document.createElement("chapters");
  1761. author2.appendChild(chapters);
  1762. text = document.createTextNode("Seven");
  1763. chapters.appendChild(text);
  1764. bibliography = document.createElement("bibliography");
  1765. author2.appendChild(bibliography);
  1766. var book3 = document.createElement("book");
  1767. doc.appendChild(book3);
  1768. var author3 = document.createElement("author");
  1769. book3.appendChild(author3);
  1770. name = document.createElement("name");
  1771. author3.appendChild(name);
  1772. name.setAttribute("real", "yes");
  1773. text = document.createTextNode("Mary Brady");
  1774. name.appendChild(text);
  1775. chapters = document.createElement("chapters");
  1776. author3.appendChild(chapters);
  1777. text = document.createTextNode("Ten");
  1778. bibliography = document.createElement("bibliography");
  1779. author3.appendChild(bibliography);
  1780. domTestHelper.arrayEqual(test, [author1],
  1781. xpath.evaluate("author[name='Mary Brady']|author[name/@real='no']", document, book1).nodes);
  1782. domTestHelper.arrayEqual(test, [],
  1783. xpath.evaluate("author[name='Mary Brady']|author[name/@real='no']", document, book2).nodes);
  1784. domTestHelper.arrayEqual(test, [author3],
  1785. xpath.evaluate("author[name='Mary Brady']|author[name/@real='no']", document, book3).nodes);
  1786. test.done();
  1787. };
  1788. // expression012 tests XPath variables, amongst other features, and is
  1789. // omitted as above for other tests. A modified version that does not test
  1790. // variables is included below.
  1791. //
  1792. // exports.tests.NIST_expression012 = function(test) {
  1793. // var document = new level3.Document();
  1794. // var doc = document.createElement("doc");
  1795. // document.appendChild(doc);
  1796. // var child1 = document.createElement("child1");
  1797. // doc.appendChild(child1);
  1798. // var text = document.createTextNode("child number 1");
  1799. // child.appendChild(text);
  1800. // var child2 = document.createElement("child2");
  1801. // doc.appendChild(child2);
  1802. // text = document.createTextNode("child number 2");
  1803. // child2.appendChild(text);
  1804. // var child3 = document.createElement("child3");
  1805. // doc.appendChild(child3);
  1806. // text = document.createTextNode("Selection of this child is an error.");
  1807. // child3.appendChild(text);
  1808. //
  1809. // var result1 = xpath.evaluate("//noChild1", document, doc);
  1810. // domTestHelper.arrayEqual(test, [], result1.nodes);
  1811. //
  1812. // var result2 = xpath.evaluate("//noChild2", document, doc);
  1813. // domTestHelper.arrayEqual(test, [], result2.nodes);
  1814. //
  1815. // // set $var1 = result1.nodes
  1816. // // set $var2 = result2.nodes
  1817. //
  1818. // domTestHelper.arrayEqual(test, [],
  1819. // xpath.evaluate("$var1|$var2", document, docu));
  1820. // };
  1821. exports.tests.NIST_expression012_noVariables = function(test) {
  1822. var document = new level3.Document();
  1823. var doc = document.createElement("doc");
  1824. document.appendChild(doc);
  1825. var child1 = document.createElement("child1");
  1826. doc.appendChild(child1);
  1827. var text = document.createTextNode("child number 1");
  1828. child1.appendChild(text);
  1829. var child2 = document.createElement("child2");
  1830. doc.appendChild(child2);
  1831. text = document.createTextNode("child number 2");
  1832. child2.appendChild(text);
  1833. var child3 = document.createElement("child3");
  1834. doc.appendChild(child3);
  1835. text = document.createTextNode("Selection of this child is an error.");
  1836. child3.appendChild(text);
  1837. domTestHelper.arrayEqual(test, [],
  1838. xpath.evaluate("//noChild1", document, doc).nodes);
  1839. domTestHelper.arrayEqual(test, [],
  1840. xpath.evaluate("//noChild2", document, doc).nodes);
  1841. domTestHelper.arrayEqual(test, [],
  1842. xpath.evaluate("//noChild1|//noChild2", document, doc).nodes);
  1843. test.done();
  1844. };
  1845. exports.tests.NIST_expression013 = function(test) {
  1846. var document = new level3.Document();
  1847. var doc = document.createElement("doc");
  1848. document.appendChild(doc);
  1849. var sub1 = document.createElement("sub1");
  1850. doc.appendChild(sub1);
  1851. var child1 = document.createElement("child1");
  1852. sub1.appendChild(child1);
  1853. var text = document.createTextNode("preceding sibling number 1");
  1854. child1.appendChild(text);
  1855. var child2 = document.createElement("child2");
  1856. sub1.appendChild(child2);
  1857. text = document.createTextNode("current node");
  1858. child2.appendChild(text);
  1859. var child3 = document.createElement("child3");
  1860. sub1.appendChild(child3);
  1861. text = document.createTextNode("following sibling number 3");
  1862. child3.appendChild(text);
  1863. domTestHelper.arrayEqual(test, [child2],
  1864. xpath.evaluate("//child2", document, doc).nodes);
  1865. domTestHelper.arrayEqual(test, [child1, child3],
  1866. xpath.evaluate("preceding-sibling::child1|following-sibling::child3", document, child2).nodes);
  1867. test.done();
  1868. };
  1869. // expression014 and expression015 are omitted because they test the XSLT
  1870. // key() function.
  1871. exports.tests.NIST_expression016 = function(test) {
  1872. var document = new level3.Document();
  1873. var doc = document.createElement("doc");
  1874. document.appendChild(doc);
  1875. var book1 = document.createElement("book");
  1876. doc.appendChild(book1);
  1877. var author1 = document.createElement("author");
  1878. book1.appendChild(author1);
  1879. var name1 = document.createElement("name");
  1880. author1.appendChild(name1);
  1881. name1.setAttribute("real", "no");
  1882. var text = document.createTextNode("Carmelo Montanez");
  1883. name1.appendChild(text);
  1884. var chapters = document.createElement("chapters");
  1885. author1.appendChild(chapters);
  1886. text = document.createTextNode("Nine");
  1887. chapters.appendChild(text);
  1888. var bibliography = document.createElement("bibliography");
  1889. author1.appendChild(bibliography);
  1890. var book2 = document.createElement("book");
  1891. doc.appendChild(book2);
  1892. var author2 = document.createElement("author");
  1893. book2.appendChild(author2);
  1894. var name2 = document.createElement("name");
  1895. author2.appendChild(name2);
  1896. name2.setAttribute("real", "na");
  1897. text = document.createTextNode("David Marston");
  1898. name2.appendChild(text);
  1899. chapters = document.createElement("chapters");
  1900. author2.appendChild(chapters);
  1901. text = document.createTextNode("Seven");
  1902. chapters.appendChild(text);
  1903. bibliography = document.createElement("bibliography");
  1904. author2.appendChild(bibliography);
  1905. var book3 = document.createElement("book");
  1906. doc.appendChild(book3);
  1907. var author3 = document.createElement("author");
  1908. book3.appendChild(author3);
  1909. var name3 = document.createElement("name");
  1910. author3.appendChild(name3);
  1911. name3.setAttribute("real", "yes");
  1912. text = document.createTextNode("Mary Brady");
  1913. name3.appendChild(text);
  1914. chapters = document.createElement("chapters");
  1915. author3.appendChild(chapters);
  1916. text = document.createTextNode("Ten");
  1917. bibliography = document.createElement("bibliography");
  1918. author3.appendChild(bibliography);
  1919. var author4 = document.createElement("author");
  1920. bibliography.appendChild(author4);
  1921. var name4 = document.createElement("name");
  1922. author4.appendChild(name4);
  1923. text = document.createTextNode("Lynne Rosenthal");
  1924. name4.appendChild(text);
  1925. chapters = document.createElement("chapters");
  1926. author4.appendChild(chapters);
  1927. text = document.createTextNode("Five");
  1928. chapters.appendChild(text);
  1929. domTestHelper.arrayEqual(test, [name1],
  1930. xpath.evaluate("author/name|author/bibliography/author/name", document, book1).nodes);
  1931. domTestHelper.arrayEqual(test, [name2],
  1932. xpath.evaluate("author/name|author/bibliography/author/name", document, book2).nodes);
  1933. domTestHelper.arrayEqual(test, [name3, name4],
  1934. xpath.evaluate("author/name|author/bibliography/author/name", document, book3).nodes);
  1935. test.done();
  1936. };
  1937. exports.tests.NIST_expression017 = function(test) {
  1938. var document = new level3.Document();
  1939. var doc = document.createElement("doc");
  1940. document.appendChild(doc);
  1941. var book1 = document.createElement("book");
  1942. doc.appendChild(book1);
  1943. var author1 = document.createElement("author");
  1944. book1.appendChild(author1);
  1945. var name1 = document.createElement("name");
  1946. author1.appendChild(name1);
  1947. name1.setAttribute("real", "no");
  1948. var text = document.createTextNode("Carmelo Montanez");
  1949. name1.appendChild(text);
  1950. var chapters = document.createElement("chapters");
  1951. author1.appendChild(chapters);
  1952. text = document.createTextNode("Nine");
  1953. chapters.appendChild(text);
  1954. var bibliography = document.createElement("bibliography");
  1955. author1.appendChild(bibliography);
  1956. var book2 = document.createElement("book");
  1957. doc.appendChild(book2);
  1958. var author2 = document.createElement("author");
  1959. book2.appendChild(author2);
  1960. var name2 = document.createElement("name");
  1961. author2.appendChild(name2);
  1962. name2.setAttribute("real", "na");
  1963. text = document.createTextNode("David Marston");
  1964. name2.appendChild(text);
  1965. chapters = document.createElement("chapters");
  1966. author2.appendChild(chapters);
  1967. text = document.createTextNode("Seven");
  1968. chapters.appendChild(text);
  1969. bibliography = document.createElement("bibliography");
  1970. author2.appendChild(bibliography);
  1971. var book3 = document.createElement("book");
  1972. doc.appendChild(book3);
  1973. var author3 = document.createElement("author");
  1974. book3.appendChild(author3);
  1975. var name3 = document.createElement("name");
  1976. author3.appendChild(name3);
  1977. name3.setAttribute("real", "yes");
  1978. text = document.createTextNode("Mary Brady");
  1979. name3.appendChild(text);
  1980. chapters = document.createElement("chapters");
  1981. author3.appendChild(chapters);
  1982. text = document.createTextNode("Ten");
  1983. bibliography = document.createElement("bibliography");
  1984. author3.appendChild(bibliography);
  1985. var author4 = document.createElement("author");
  1986. bibliography.appendChild(author4);
  1987. var name4 = document.createElement("name");
  1988. author4.appendChild(name4);
  1989. text = document.createTextNode("Lynne Rosenthal");
  1990. name4.appendChild(text);
  1991. chapters = document.createElement("chapters");
  1992. author4.appendChild(chapters);
  1993. text = document.createTextNode("Five");
  1994. chapters.appendChild(text);
  1995. domTestHelper.arrayEqual(test, [name1],
  1996. xpath.evaluate("author/name|author/bibliography/author/chapters", document, book1).nodes);
  1997. domTestHelper.arrayEqual(test, [name2],
  1998. xpath.evaluate("author/name|author/bibliography/author/chapters", document, book2).nodes);
  1999. domTestHelper.arrayEqual(test, [name3, chapters],
  2000. xpath.evaluate("author/name|author/bibliography/author/chapters", document, book3).nodes);
  2001. test.done();
  2002. };
  2003. exports.tests.NIST_expression018 = function(test) {
  2004. var document = new level3.Document();
  2005. var doc = document.createElement("doc");
  2006. document.appendChild(doc);
  2007. var book1 = document.createElement("book");
  2008. doc.appendChild(book1);
  2009. var author1 = document.createElement("author");
  2010. book1.appendChild(author1);
  2011. var name1 = document.createElement("name");
  2012. author1.appendChild(name1);
  2013. name1.setAttribute("real", "na");
  2014. var text = document.createTextNode("David Marston");
  2015. name1.appendChild(text);
  2016. var chapters1 = document.createElement("chapters");
  2017. author1.appendChild(chapters1);
  2018. text = document.createTextNode("Seven");
  2019. chapters1.appendChild(text);
  2020. var bibliography1 = document.createElement("bibliography");
  2021. author1.appendChild(bibliography1);
  2022. var book2 = document.createElement("book");
  2023. doc.appendChild(book2);
  2024. var author2 = document.createElement("author");
  2025. book2.appendChild(author2);
  2026. var name2 = document.createElement("name");
  2027. author2.appendChild(name2);
  2028. name2.setAttribute("real", "yes");
  2029. text = document.createTextNode("Mary Brady");
  2030. name2.appendChild(text);
  2031. var chapters2 = document.createElement("chapters");
  2032. author2.appendChild(chapters2);
  2033. text = document.createTextNode("Ten");
  2034. chapters2.appendChild(text);
  2035. var bibliography2 = document.createElement("bibliography");
  2036. author2.appendChild(bibliography2);
  2037. domTestHelper.arrayEqual(test, [name1],
  2038. xpath.evaluate("author/name|author/noElement", document, book1).nodes);
  2039. domTestHelper.arrayEqual(test, [name2],
  2040. xpath.evaluate("author/name|author/noElement", document, book2).nodes);
  2041. test.done();
  2042. };
  2043. // expression019 tests XPath variables, amongst other features, and is
  2044. // omitted as above for other tests. A modified version that does not test
  2045. // variables is included below.
  2046. //
  2047. // exports.tests.NIST_expression019 = function(test) {
  2048. // var document = new level3.Document();
  2049. // var doc = document.createElement("doc");
  2050. // document.appendChild(doc);
  2051. // var child1 = document.createElement("child1");
  2052. // doc.appendChild(child1);
  2053. // var text = document.createTextNode("Text for variable 1.");
  2054. // child1.appendChild(text);
  2055. // var child2 = document.createElement("child2");
  2056. // doc.appendChild(child2);
  2057. // text = document.createTextNode("Selection of this child is an error.");
  2058. // child2.appendChild(text);
  2059. // var child3 = document.createElement("child3");
  2060. // doc.appendChild(child3);
  2061. // text = document.createTextNode("Selection of this child is an error.");
  2062. // child3.appendChild(text);
  2063. //
  2064. // var result = xpath.evaluate("//child1", document, doc);
  2065. // domTestHelper.arrayEqual(test, [child1], result.nodes);
  2066. //
  2067. // // set $var1 = result.nodes
  2068. // // set $var2 = result.nodes
  2069. //
  2070. // domTestHelper.arrayEqual(test, [child1],
  2071. // xpath.evaluate("$var1|$var2", document, doc));
  2072. // };
  2073. exports.tests.NIST_expression019_noVariables = function(test) {
  2074. var document = new level3.Document();
  2075. var doc = document.createElement("doc");
  2076. document.appendChild(doc);
  2077. var child1 = document.createElement("child1");
  2078. doc.appendChild(child1);
  2079. var text = document.createTextNode("Text for variable 1.");
  2080. child1.appendChild(text);
  2081. var child2 = document.createElement("child2");
  2082. doc.appendChild(child2);
  2083. text = document.createTextNode("Selection of this child is an error.");
  2084. child2.appendChild(text);
  2085. var child3 = document.createElement("child3");
  2086. doc.appendChild(child3);
  2087. text = document.createTextNode("Selection of this child is an error.");
  2088. child3.appendChild(text);
  2089. var result = xpath.evaluate("//child1", document, doc);
  2090. domTestHelper.arrayEqual(test, [child1], result.nodes);
  2091. domTestHelper.arrayEqual(test, [child1],
  2092. xpath.evaluate("//child1|//child1", document, doc).nodes);
  2093. test.done();
  2094. };
  2095. exports.tests.NIST_expression020 = function(test) {
  2096. var document = new level3.Document();
  2097. var doc = document.createElement("doc");
  2098. document.appendChild(doc);
  2099. var sub1 = document.createElement("sub1");
  2100. doc.appendChild(sub1);
  2101. var child1 = document.createElement("child1");
  2102. sub1.appendChild(child1);
  2103. var text = document.createTextNode("child1");
  2104. child1.appendChild(text);
  2105. var sub2 = document.createElement("sub2");
  2106. doc.appendChild(sub2);
  2107. var child2 = document.createElement("child2");
  2108. sub2.appendChild(child2);
  2109. text = document.createTextNode("child2");
  2110. child2.appendChild(text);
  2111. domTestHelper.arrayEqual(test, [child1, child2],
  2112. xpath.evaluate("sub1/child1|sub2/child2", document, doc).nodes);
  2113. test.done();
  2114. };
  2115. exports.tests.NIST_expression021 = function(test) {
  2116. var document = new level3.Document();
  2117. var doc = document.createElement("doc");
  2118. document.appendChild(doc);
  2119. var sub1 = document.createElement("sub1");
  2120. doc.appendChild(sub1);
  2121. var child1 = document.createElement("child1");
  2122. sub1.appendChild(child1);
  2123. var text = document.createTextNode("self content number 1");
  2124. child1.appendChild(text);
  2125. var sub2 = document.createElement("sub2");
  2126. doc.appendChild(sub2);
  2127. var child2 = document.createElement("child2");
  2128. sub2.appendChild(child2);
  2129. text = document.createTextNode("self content number 2");
  2130. child2.appendChild(text);
  2131. domTestHelper.arrayEqual(test, [child1, child2],
  2132. xpath.evaluate("//child1|//child2", document, doc).nodes);
  2133. domTestHelper.arrayEqual(test, [child1],
  2134. xpath.evaluate("self::child1|self::child2", document, child1).nodes);
  2135. domTestHelper.arrayEqual(test, [child2],
  2136. xpath.evaluate("self::child1|self::child2", document, child2).nodes);
  2137. test.done();
  2138. };
  2139. exports.tests.NIST_expression022 = function(test) {
  2140. var document = new level3.Document();
  2141. var doc = document.createElement("doc");
  2142. document.appendChild(doc);
  2143. var child1 = document.createElement("child1");
  2144. doc.appendChild(child1);
  2145. var text = document.createTextNode("1");
  2146. child1.appendChild(text);
  2147. var child2 = document.createElement("child2");
  2148. doc.appendChild(child2);
  2149. text = document.createTextNode("2");
  2150. child2.appendChild(text);
  2151. domTestHelper.arrayEqual(test, [child1, child2],
  2152. xpath.evaluate("//child1|//child2", document, doc).nodes);
  2153. test.done();
  2154. };
  2155. exports.tests.NIST_expression023 = function(test) {
  2156. var document = new level3.Document();
  2157. var doc = document.createElement("doc");
  2158. document.appendChild(doc);
  2159. var child1 = document.createElement("child1");
  2160. doc.appendChild(child1);
  2161. var text = document.createTextNode("1");
  2162. child1.appendChild(text);
  2163. var child2 = document.createElement("child2");
  2164. doc.appendChild(child2);
  2165. text = document.createTextNode("2");
  2166. child2.appendChild(text);
  2167. var child3 = document.createElement("child3");
  2168. doc.appendChild(child3);
  2169. text = document.createTextNode("3");
  2170. child3.appendChild(text);
  2171. domTestHelper.arrayEqual(test, [child1, child2, child3],
  2172. xpath.evaluate("//child1|//child2|//child3", document, doc).nodes);
  2173. test.done();
  2174. };
  2175. // expression024 is omitted because it tests the XSLT key() function.
  2176. // expression025 tests XPath variables, amongst other features, and is
  2177. // omitted as above for other tests. A modified version that does not test
  2178. // variables is included below.
  2179. //
  2180. // exports.tests.NIST_expression025 = function(test) {
  2181. // var document = new level3.Document();
  2182. // var doc = document.createElement("doc");
  2183. // document.appendChild(doc);
  2184. // var child1 = document.createElement("child1");
  2185. // doc.appendChild(child1);
  2186. // var text = document.createTextNode("Text for variable");
  2187. // child1.appendChild(text);
  2188. // var child2 = document.createElement("child2");
  2189. // doc.appendChild(child2);
  2190. // text = document.createTextNode("Text for location Path");
  2191. // child2.appendChild(text);
  2192. // var child3 = document.createElement("child3");
  2193. // text = document.createTextNode("Selection of this child is an error");
  2194. // child3.appendChild(text);
  2195. //
  2196. // var result = xpath.evaluate("//child1", document, doc);
  2197. // domTestHelper.arrayEqual(test, [child1], result.nodes);
  2198. //
  2199. // // set $var1 = result.nodes
  2200. //
  2201. // domTestHelper.arrayEqual(test, [child1, child2],
  2202. // xpath.evaluate("$var1|child::child2", document, doc).nodes);
  2203. // };
  2204. exports.tests.NIST_expression025_noVariables = function(test) {
  2205. var document = new level3.Document();
  2206. var doc = document.createElement("doc");
  2207. document.appendChild(doc);
  2208. var child1 = document.createElement("child1");
  2209. doc.appendChild(child1);
  2210. var text = document.createTextNode("Text for variable");
  2211. child1.appendChild(text);
  2212. var child2 = document.createElement("child2");
  2213. doc.appendChild(child2);
  2214. text = document.createTextNode("Text for location Path");
  2215. child2.appendChild(text);
  2216. var child3 = document.createElement("child3");
  2217. text = document.createTextNode("Selection of this child is an error");
  2218. child3.appendChild(text);
  2219. var result = xpath.evaluate("//child1", document, doc);
  2220. domTestHelper.arrayEqual(test, [child1], result.nodes);
  2221. domTestHelper.arrayEqual(test, [child1, child2],
  2222. xpath.evaluate("//child1|child::child2", document, doc).nodes);
  2223. test.done();
  2224. };
  2225. // expression026 tests XPath variables, so it is omitted as above. There is no
  2226. // modified version of this test that does not use variables, because it would
  2227. // be redundant with other tests.
  2228. //
  2229. // exports.tests.NIST_expression026 = function(test) {
  2230. // var document = new level3.Document();
  2231. // var doc = document.createElement("doc");
  2232. // document.appendChild(doc);
  2233. // var child1 = document.createElement("child1");
  2234. // doc.appendChild(child1);
  2235. // var text = document.createTextNode("child number 1");
  2236. // child1.appendChild(text);
  2237. // var child2 = document.createElement("child2");
  2238. // doc.appendChild(child2);
  2239. // text = document.createTextNode("child number 2");
  2240. // child2.appendChild(text);
  2241. // var child3 = document.createElement("child3");
  2242. // text = document.createTextNode("Selection of this child is an error");
  2243. // child3.appendChild(text);
  2244. //
  2245. // var result1 = xpath.evaluate("//child1", document, doc);
  2246. // domTestHelper.arrayEqual(test, [child1], result1.nodes);
  2247. //
  2248. // var result2 = xpath.evaluate("//child2", document, doc);
  2249. // domTestHelper.arrayEqual(test, [child2], result2.nodes);
  2250. //
  2251. // // set $var1 = result1.nodes
  2252. // // set $var2 = result2.nodes
  2253. //
  2254. // domTestHelper.arrayEqual(test, [child1, child2],
  2255. // xpath.evaluate("$var1|$var2", document, doc).nodes);
  2256. // };
  2257. exports.tests.NIST_expression027 = function(test) {
  2258. var document = new level3.Document();
  2259. test.equal(true, xpath.evaluate("(-0 = 0)", document, document));
  2260. test.done();
  2261. };
  2262. exports.tests.NIST_expression028 = function(test) {
  2263. var document = new level3.Document();
  2264. test.equal(false, xpath.evaluate("(-0 < 0)", document, document));
  2265. test.done();
  2266. };
  2267. exports.tests.NIST_expression029 = function(test) {
  2268. var document = new level3.Document();
  2269. test.equal(false, xpath.evaluate("(-0 > 0)", document, document));
  2270. test.done();
  2271. };
  2272. exports.tests.NIST_expression030 = function(test) {
  2273. var document = new level3.Document();
  2274. test.equal(true, xpath.evaluate("(-0 >= 0)", document, document));
  2275. test.done();
  2276. };
  2277. exports.tests.NIST_expression031 = function(test) {
  2278. var document = new level3.Document();
  2279. test.equal(true, xpath.evaluate("(-0 <= 0)", document, document));
  2280. test.done();
  2281. };
  2282. exports.tests.NIST_expression032 = function(test) {
  2283. var document = new level3.Document();
  2284. test.equal(false, xpath.evaluate("(-0 != 0)", document, document));
  2285. test.done();
  2286. };
  2287. exports.tests.NIST_expression033 = function(test) {
  2288. var document = new level3.Document();
  2289. test.equal(true, xpath.evaluate("2.1 > 2.0", document, document));
  2290. test.equal(false, xpath.evaluate("2.1 < 2.0", document, document));
  2291. test.equal(false, xpath.evaluate("2.1 = 2.0", document, document));
  2292. test.equal(false, xpath.evaluate("2.1 > NaN", document, document));
  2293. test.done();
  2294. };
  2295. exports.tests.NIST_expression034 = function(test) {
  2296. var document = new level3.Document();
  2297. test.equal(true, xpath.evaluate("2.0 < 2.1", document, document));
  2298. test.equal(false, xpath.evaluate("2.0 > 2.1", document, document));
  2299. test.equal(false, xpath.evaluate("2.0 = 2.1", document, document));
  2300. test.equal(false, xpath.evaluate("2.0 < NaN", document, document));
  2301. test.done();
  2302. };
  2303. exports.tests.NIST_expression035 = function(test) {
  2304. var document = new level3.Document();
  2305. test.equal(true, xpath.evaluate("2.0 <= 2.0", document, document));
  2306. test.equal(false, xpath.evaluate("2.0 > 2.0", document, document));
  2307. test.equal(true, xpath.evaluate("2.0 = 2.0", document, document));
  2308. test.equal(false, xpath.evaluate("2.0 <= NaN", document, document));
  2309. test.done();
  2310. };
  2311. exports.tests.NIST_expression036 = function(test) {
  2312. var document = new level3.Document();
  2313. test.equal(true, xpath.evaluate("2.0 >= 2.0", document, document));
  2314. test.equal(false, xpath.evaluate("2.0 < 2.0", document, document));
  2315. test.equal(true, xpath.evaluate("2.0 = 2.0", document, document));
  2316. test.equal(false, xpath.evaluate("2.0 <= NaN", document, document));
  2317. test.done();
  2318. };
  2319. exports.tests.NIST_locationPath001 = function(test) {
  2320. var document = new level3.Document();
  2321. var doc = document.createElement("doc");
  2322. document.appendChild(doc);
  2323. var child1 = document.createElement("child1");
  2324. doc.appendChild(child1);
  2325. var text = document.createTextNode("Text from child1");
  2326. child1.appendChild(text);
  2327. var child2 = document.createElement("child2");
  2328. child1.appendChild(child2);
  2329. domTestHelper.arrayEqual(test, [child1],
  2330. xpath.evaluate("child1[child::child2]", document, doc).nodes);
  2331. test.done();
  2332. };
  2333. exports.tests.NIST_locationPath002 = function(test) {
  2334. var document = new level3.Document();
  2335. var doc = document.createElement("doc");
  2336. document.appendChild(doc);
  2337. var element1 = document.createElement("element1");
  2338. var text = document.createTextNode("Text from first element");
  2339. element1.appendChild(text);
  2340. var child1a = document.createElement("child1");
  2341. element1.appendChild(child1a);
  2342. text = document.createTextNode("Text from child1 of first element");
  2343. child1a.appendChild(text);
  2344. var child2a = document.createElement("child2");
  2345. element1.appendChild(child2a);
  2346. text = document.createTextNode("Text from child2 of first element");
  2347. child2a.appendChild(text);
  2348. var element2 = document.createElement("element2");
  2349. doc.appendChild(element2);
  2350. text = document.createTextNode("Text from second element");
  2351. element2.appendChild(text);
  2352. var child1b = document.createElement("child1");
  2353. element2.appendChild(child1b);
  2354. text = document.createTextNode("Text from child1 of second element");
  2355. child1b.appendChild(text);
  2356. var child2b = document.createElement("child2");
  2357. element2.appendChild(child2b);
  2358. text = document.createTextNode("Text from child2 of second element (corect execution!!)");
  2359. child2b.appendChild(text);
  2360. domTestHelper.arrayEqual(test, [child2b],
  2361. xpath.evaluate("//child2[ancestor::element2]", document, doc).nodes);
  2362. test.done();
  2363. };
  2364. exports.tests.NIST_locationPath003 = function(test) {
  2365. var document = new level3.Document();
  2366. var doc = document.createElement("doc");
  2367. document.appendChild(doc);
  2368. var element1 = document.createElement("element1");
  2369. var text = document.createTextNode("Text from first element");
  2370. element1.appendChild(text);
  2371. var child1a = document.createElement("child1");
  2372. element1.appendChild(child1a);
  2373. text = document.createTextNode("Text from child1 of first element");
  2374. child1a.appendChild(text);
  2375. var child2a = document.createElement("child2");
  2376. element1.appendChild(child2a);
  2377. text = document.createTextNode("Text from child2 of first element");
  2378. child2a.appendChild(text);
  2379. var element2 = document.createElement("element2");
  2380. doc.appendChild(element2);
  2381. text = document.createTextNode("Text from second element");
  2382. element2.appendChild(text);
  2383. var child1b = document.createElement("child1");
  2384. element2.appendChild(child1b);
  2385. text = document.createTextNode("Text from child1 of second element");
  2386. child1b.appendChild(text);
  2387. var child2b = document.createElement("child2");
  2388. element2.appendChild(child2b);
  2389. text = document.createTextNode("Text from child2 of second element (corect execution!!)");
  2390. child2b.appendChild(text);
  2391. domTestHelper.arrayEqual(test, [child2b],
  2392. xpath.evaluate("//child2[ancestor-or-self::element2]", document, doc).nodes);
  2393. test.done();
  2394. };
  2395. exports.tests.NIST_locationPath004 = function(test) {
  2396. var document = new level3.Document();
  2397. var doc = document.createElement("doc");
  2398. document.appendChild(doc);
  2399. var element1 = document.createElement("element1");
  2400. var text = document.createTextNode("Text from first element");
  2401. element1.appendChild(text);
  2402. var child1a = document.createElement("child1");
  2403. element1.appendChild(child1a);
  2404. text = document.createTextNode("Text from child1 of first element");
  2405. child1a.appendChild(text);
  2406. var child2a = document.createElement("child2");
  2407. element1.appendChild(child2a);
  2408. text = document.createTextNode("Text from child2 of first element");
  2409. child2a.appendChild(text);
  2410. var element2 = document.createElement("element2");
  2411. doc.appendChild(element2);
  2412. text = document.createTextNode("Text from second element");
  2413. element2.appendChild(text);
  2414. var child1b = document.createElement("child1");
  2415. element2.appendChild(child1b);
  2416. text = document.createTextNode("Text from child1 of second element");
  2417. child1b.appendChild(text);
  2418. var child2b = document.createElement("child2");
  2419. child2b.setAttribute("attr1", "yes");
  2420. element2.appendChild(child2b);
  2421. text = document.createTextNode("Text from child2 of second element (corect execution!!)");
  2422. child2b.appendChild(text);
  2423. domTestHelper.arrayEqual(test, [child2b],
  2424. xpath.evaluate("//child2[attribute::attr1]", document, doc).nodes);
  2425. test.done();
  2426. };
  2427. exports.tests.NIST_locationPath005 = function(test) {
  2428. var document = new level3.Document();
  2429. var doc = document.createElement("doc");
  2430. document.appendChild(doc);
  2431. var element1a = document.createElement("element1");
  2432. doc.appendChild(element1a);
  2433. var text = document.createTextNode("Text from first element (correct execution)!!!");
  2434. element1a.appendChild(text);
  2435. var child1 = document.createElement("child1");
  2436. element1a.appendChild(child1);
  2437. var child2 = document.createElement("child2");
  2438. element1a.appendChild(child2);
  2439. var element1b = document.createElement("element1");
  2440. doc.appendChild(element1b);
  2441. text = document.createTextNode("Text from second element");
  2442. element1b.appendChild(text);
  2443. child1 = document.createElement("child1");
  2444. element1b.appendChild(child1);
  2445. text = document.createTextNode("Text from child1 of second element");
  2446. child1.appendChild(text);
  2447. domTestHelper.arrayEqual(test, [element1a],
  2448. xpath.evaluate("element1[descendant-or-self::child2]", document, doc).nodes);
  2449. test.done();
  2450. };
  2451. exports.tests.NIST_locationPath006 = function(test) {
  2452. var document = new level3.Document();
  2453. var doc = document.createElement("doc");
  2454. document.appendChild(doc);
  2455. var element1 = document.createElement("element1");
  2456. doc.appendChild(element1);
  2457. var child1a = document.createElement("child1");
  2458. element1.appendChild(child1a);
  2459. var text = document.createTextNode("Test executed successfully!!")
  2460. child1a.appendChild(text);
  2461. var child2 = document.createElement("child2");
  2462. element1.appendChild(child2);
  2463. text = document.createTextNode("child2");
  2464. child2.appendChild(text);
  2465. var element2 = document.createElement("element2");
  2466. doc.appendChild(element2);
  2467. var child1b = document.createElement("child1");
  2468. element2.appendChild(child1b);
  2469. text = document.createTextNode("Wrong node selected!!");
  2470. child1b.appendChild(text);
  2471. var element3 = document.createElement("element3");
  2472. doc.appendChild(element3);
  2473. var child1c = document.createElement("child1");
  2474. element3.appendChild(child1c);
  2475. text = document.createTextNode("Wrong node selected!!");
  2476. child1c.appendChild(text);
  2477. domTestHelper.arrayEqual(test, [child1a],
  2478. xpath.evaluate("//child1[parent::element1]", document, doc).nodes);
  2479. test.done();
  2480. };
  2481. exports.tests.NIST_locationPath007 = function(test) {
  2482. var document = new level3.Document();
  2483. var doc = document.createElement("doc");
  2484. document.appendChild(doc);
  2485. var element1a = document.createElement("element1");
  2486. doc.appendChild(element1a);
  2487. var text = document.createTextNode("Wrong node selected!!");
  2488. element1a.appendChild(text);
  2489. var element1b = document.createElement("element1");
  2490. doc.appendChild(element1b);
  2491. text = document.createTextNode("Test executed successfully!!");
  2492. element1b.appendChild(text);
  2493. var element1c = document.createElement("element1");
  2494. doc.appendChild(element1c);
  2495. text = document.createTextNode("Wrong node selected!!");
  2496. element1c.appendChild(text);
  2497. domTestHelper.arrayEqual(test, [element1b],
  2498. xpath.evaluate("element1[(((((2*10)-4)+9) div 5) mod 3 )]", document, doc).nodes);
  2499. test.done();
  2500. };
  2501. exports.tests.NIST_locationPath008 = function(test) {
  2502. var document = new level3.Document();
  2503. var doc = document.createElement("doc");
  2504. document.appendChild(doc);
  2505. var element1a = document.createElement("element1");
  2506. doc.appendChild(element1a);
  2507. var text = document.createTextNode("Wrong node selected!!");
  2508. element1a.appendChild(text);
  2509. var element1b = document.createElement("element1");
  2510. doc.appendChild(element1b);
  2511. text = document.createTextNode("Test executed successfully!!");
  2512. element1b.appendChild(text);
  2513. var element1c = document.createElement("element1");
  2514. doc.appendChild(element1c);
  2515. text = document.createTextNode("Wrong node selected!!");
  2516. element1c.appendChild(text);
  2517. domTestHelper.arrayEqual(test, [element1b],
  2518. xpath.evaluate("element1[(((((2*10)-4)+9) div 5) mod floor(3))]", document, doc).nodes);
  2519. test.done();
  2520. };
  2521. exports.tests.NIST_locationPath009 = function(test) {
  2522. var document = new level3.Document();
  2523. var doc = document.createElement("doc");
  2524. document.appendChild(doc);
  2525. var element1a = document.createElement("element1");
  2526. doc.appendChild(element1a);
  2527. var text = document.createTextNode("Wrong node selected!!");
  2528. element1a.appendChild(text);
  2529. var element1b = document.createElement("element1");
  2530. doc.appendChild(element1b);
  2531. text = document.createTextNode("Test executed successfully!!");
  2532. element1b.appendChild(text);
  2533. var element1c = document.createElement("element1");
  2534. doc.appendChild(element1c);
  2535. text = document.createTextNode("Wrong node selected!!");
  2536. element1c.appendChild(text);
  2537. domTestHelper.arrayEqual(test, [element1b],
  2538. xpath.evaluate("element1[floor(2)]", document, doc).nodes);
  2539. test.done();
  2540. };
  2541. exports.tests.NIST_locationPath010 = function(test) {
  2542. var document = new level3.Document();
  2543. var doc = document.createElement("doc");
  2544. document.appendChild(doc);
  2545. var element1a = document.createElement("element1");
  2546. doc.appendChild(element1a);
  2547. var text = document.createTextNode("Wrong Node Selected!!");
  2548. element1a.appendChild(text);
  2549. var element1b = document.createElement("element1");
  2550. doc.appendChild(element1b);
  2551. var child1a = document.createElement("child1");
  2552. element1b.appendChild(child1a);
  2553. text = document.createTextNode("Wrong Node Selected!!");
  2554. child1a.appendChild(text);
  2555. var child1b = document.createElement("child1");
  2556. element1b.appendChild(child1b);
  2557. text = document.createTextNode("Wrong Node Selected!!");
  2558. child1b.appendChild(text);
  2559. var child1c = document.createElement("child1");
  2560. element1b.appendChild(child1c);
  2561. text = document.createTextNode("Test Executed Successfully!!");
  2562. child1c.appendChild(text);
  2563. var element1c = document.createElement("element1");
  2564. doc.appendChild(element1c);
  2565. text = document.createTextNode("Wrong Node Selected!!");
  2566. element1c.appendChild(text);
  2567. domTestHelper.arrayEqual(test, [child1c],
  2568. xpath.evaluate("doc/element1[(((((2*10)-4)+9) div 5) mod 3)]/child1[last()]", document, document).nodes);
  2569. test.done();
  2570. };