Home | History | Annotate | Download | only in xpath
      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4 <link rel="stylesheet" href="../js/resources/js-test-style.css">
      5 <script src="../js/resources/js-test-pre.js"></script>
      6 <script src="xpath-test-pre.js"></script>
      7 </head>
      8 <body>
      9 <div id="console"></div>
     10 
     11 <script>
     12 
     13 var ROOT = document.createElement('div');
     14 ROOT.innerHTML = '<p>a</p><div><span id="21"></span><span id="22"></span><span id="23"></span></div>';
     15 var CHILD1 = ROOT.firstChild;
     16 var CHILD1TEXT = CHILD1.firstChild;
     17 var CHILD2 = ROOT.lastChild;
     18 var CHILD21 = CHILD2.firstChild;
     19 var CHILD22 = CHILD21.nextSibling;
     20 var CHILD23 = CHILD22.nextSibling;
     21 
     22     result = document.evaluate(".//*", ROOT, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     23     checkSnapshot(".//*", result, [CHILD1, CHILD2, CHILD21, CHILD22, CHILD23]);
     24 
     25     result = document.evaluate("*[position() = 2]", ROOT, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     26     checkSnapshot("*[position() = 2]", result, [CHILD2]);
     27 
     28     result = document.evaluate(".//*[position() = 2]", ROOT, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     29     checkSnapshot(".//*[position() = 2]", result, [CHILD2, CHILD22]);
     30 
     31     result = document.evaluate("./././/*[2]/.", ROOT, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     32     checkSnapshot("./././/*[2]/.", result, [CHILD2, CHILD22]);
     33 
     34     result = document.evaluate(".//*/span[2]", ROOT, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     35     checkSnapshot(".//*/span[2]", result, [CHILD22]);
     36 
     37     result = document.evaluate(".//*[position() = 1]", ROOT, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     38     checkSnapshot(".//*[position() = 1]", result, [CHILD1, CHILD21]);
     39 
     40     result = document.evaluate("./descendant-or-self::node()/child::*[position() = 2]", ROOT, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     41     checkSnapshot("./descendant-or-self::node()/child::*[position() = 2]", result, [CHILD2, CHILD22]);
     42 
     43     result = document.evaluate("descendant-or-self::node()/child::*[position() = 2]", ROOT, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     44     checkSnapshot("descendant-or-self::node()/child::*[position() = 2]", result, [CHILD2, CHILD22]);
     45 
     46     result = document.evaluate(".//*/*[2]", ROOT, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     47     checkSnapshot(".//*/*[2]", result, [CHILD22]);
     48 
     49     result = document.evaluate("preceding::node()[1]", CHILD2, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     50     checkSnapshot("preceding::node()[1]", result, [CHILD1TEXT]);
     51 
     52     result = document.evaluate("preceding::node()[2]", CHILD2, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     53     checkSnapshot("preceding::node()[2]", result, [CHILD1]);
     54 
     55     result = document.evaluate("preceding::node()[3]", CHILD2, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     56     checkSnapshot("preceding::node()[3]", result, []);
     57 
     58     result = document.evaluate("preceding::node()/*[3]", CHILD2, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     59     checkSnapshot("preceding::node()/*[3]", result, []);
     60 
     61     result = document.evaluate("ancestor::node()[2]", CHILD21, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     62     checkSnapshot("ancestor::node()[2]", result, [ROOT]);
     63 
     64     result = document.evaluate("ancestor-or-self::node()[2]", CHILD21, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     65     checkSnapshot("ancestor-or-self::node()[2]", result, [CHILD2]);
     66 
     67     result = document.evaluate("ancestor-or-self::node()[3]", CHILD21, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     68     checkSnapshot("ancestor-or-self::node()[3]", result, [ROOT]);
     69 
     70     result = document.evaluate("preceding::span[1]", CHILD23, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     71     checkSnapshot("preceding::span[1]", result, [CHILD22]);
     72 
     73     result = document.evaluate("(preceding::span)[1]", CHILD23, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
     74     checkSnapshot("(preceding::span)[1]", result, [CHILD21]);
     75 
     76 var ROOT2 = document.createElement('div');
     77 ROOT2.innerHTML = '<p num="1"></p><p num="2" type="warning"></p><p num="3" type="warning"></p><p num="4" type="warning"></p><p num="5" type="warning"></p><p num="6" type="warning"></p>';
     78 
     79     shouldBe("document.evaluate('p[@type=\"warning\"][5]/@num', ROOT2, null, XPathResult.STRING_TYPE, null).stringValue", "'6'");
     80     shouldBe("document.evaluate('p[5][@type=\"warning\"]/@num', ROOT2, null, XPathResult.STRING_TYPE, null).stringValue", "'5'");
     81 
     82     var successfullyParsed = true;
     83 
     84 </script>
     85 <script src="../js/resources/js-test-post.js"></script>
     86 </body>
     87 </html>
     88