<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="xpath-test-pre.js"></script>
</head>
<body>
<div id="console"></div>

<script>
doc = (new DOMParser).parseFromString(
    '<doc>' +
    '<CHILD attr1="val1" attr31="31">' +
    '<GCHILD name="GCHILD11"/>' +
    '<GCHILD name="GCHILD12"/>' +
    'TEXT1' +
    '</CHILD>' +
    '<CHILD attr4="4">' +
    '<GCHILD name="GCHILD21"/>' +
    '</CHILD>' +
    '</doc>',
    'application/xml');

CHILD1 = doc.getElementsByTagName("CHILD")[0];
CHILD2 = doc.getElementsByTagName("CHILD")[1];
attr1 = CHILD1.getAttributeNode("attr1");
attr31 = CHILD1.getAttributeNode("attr31");
attr4 = CHILD2.getAttributeNode("attr4");
GCHILD11 = doc.getElementsByTagName("GCHILD")[0];
GCHILD12 = doc.getElementsByTagName("GCHILD")[1];
GCHILD21 = doc.getElementsByTagName("GCHILD")[2];
TEXT1 = GCHILD12.nextSibling;


result = doc.evaluate("ancestor::*", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("ancestor::* (context = attr1)", 
    result, 
    [doc.documentElement, CHILD1]);

result = doc.evaluate("ancestor::*", GCHILD11, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("ancestor::* (context = GCHILD11)", 
    result, 
    [doc.documentElement, CHILD1]);

result = doc.evaluate("ancestor::node()", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("ancestor::node() (context = attr1)", 
    result, 
    [doc, doc.documentElement, CHILD1]);

result = doc.evaluate("ancestor::node()", GCHILD11, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("ancestor::node() (context = GCHILD11)", 
    result, 
    [doc, doc.documentElement, CHILD1]);

result = doc.evaluate("preceding::node()", attr4, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("preceding::node() (context = attr4)", 
    result, 
    [CHILD1, GCHILD11, GCHILD12, TEXT1]);

result = doc.evaluate("preceding::node()", CHILD2, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("preceding::node() (context = CHILD2)", 
    result, 
    [CHILD1, GCHILD11, GCHILD12, TEXT1]);

result = doc.evaluate("preceding::node()", GCHILD12, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("preceding::node() (context = GCHILD12)", 
    result, 
    [GCHILD11]);

result = doc.evaluate("following::node()", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("following::node() (context = attr1)", 
    result, 
    [GCHILD11, GCHILD12, TEXT1, CHILD2, GCHILD21]);

result = doc.evaluate("following::node()", CHILD1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("following::node() (context = CHILD1)", 
    result, 
    [CHILD2, GCHILD21]);

result = doc.evaluate("following::node()", GCHILD11, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("following::node() (context = GCHILD11)", 
    result, 
    [GCHILD12, TEXT1, CHILD2, GCHILD21]);

result = doc.evaluate("following::node()", CHILD2, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("following::node() (context = CHILD2)", 
    result, 
    []);

result = doc.evaluate("//CHILD | //@attr1", doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("//CHILD | //@attr1", 
    result, 
    [CHILD1, attr1, CHILD2]);

result = doc.evaluate("//CHILD | //@attr1 | //*[@name='GCHILD12'] | //@attr31", doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("//CHILD | //@attr1 | //*[@name='GCHILD12'] | //@attr31", 
    result, 
    [CHILD1, attr1, attr31, GCHILD12, CHILD2]);

result = doc.evaluate("(//CHILD | //@attr1 | //*[@name='GCHILD12'] | //@attr31)[3]", doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("(//CHILD | //@attr1 | //*[@name='GCHILD12'] | //@attr31)[3]", 
    result, 
    [attr31]);

result = doc.evaluate("(//CHILD | //@attr1 | //*[@name='GCHILD12'] | //@attr31)[position() = last()]", doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("(//CHILD | //@attr1 | //*[@name='GCHILD12'] | //@attr31)[position() = last()]", 
    result, 
    [CHILD2]);

result = doc.evaluate("//CHILD[2]/GCHILD | //CHILD[1]/GCHILD", doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("//CHILD[2]/GCHILD | //CHILD[1]/GCHILD", 
    result, 
    [GCHILD11, GCHILD12, GCHILD21]);

shouldBe('doc.evaluate("string(//*[@name=\'GCHILD12\'] | //CHILD | //@attr1 | //@attr31)", doc, null, XPathResult.STRING_TYPE, null).stringValue', '"TEXT1"');

result = doc.evaluate("descendant::node()", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("descendant::node() (context = attr1)", 
    result, 
    []);

result = doc.evaluate("child::node()", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("child::node() (context = attr1)", 
    result, 
    []);

result = doc.evaluate("parent::node()", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("parent::node() (context = attr1)", 
    result, 
    [CHILD1]);

result = doc.evaluate("following-sibling::node()", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("following-sibling::node() (context = attr1)", 
    result, 
    []);

result = doc.evaluate("preceding-sibling::node()", attr4, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("preceding-sibling::node() (context = attr4)", 
    result, 
    []);

result = doc.evaluate("attribute::node()", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("attribute::node() (context = attr1)", 
    result, 
    []);

result = doc.evaluate("self::node()", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("self::node() (context = attr1)", 
    result, 
    [attr1]);

result = doc.evaluate("self::*", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("self::* (context = attr1)", 
    result, 
    []);

result = doc.evaluate("descendant-or-self::node()", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("descendant-or-self::node() (context = attr1)", 
    result, 
    [attr1]);
    
result = doc.evaluate("ancestor-or-self::node()", attr1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
checkSnapshot("ancestor-or-self::node() (context = attr1)", 
    result, 
    [doc, doc.documentElement, CHILD1, attr1]);

</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
