<html>
<head>
<script src="../../../resources/js-test-pre.js"></script>
<script src="../xpath-test-pre.js"></script>
</head>
<body>
<div id="console"></div>

<script>
var doc = (new DOMParser).parseFromString(
    '<doc>' +
        '<item id="1" color="red" />' +
        '<chapter id="c1">' +
            '<item id="2" color="blue" />' +
        '</chapter>' +
   '</doc>',
    'application/xml');

var ROOT = doc.documentElement;
var DI1 = ROOT.firstChild;
var DC1 = DI1.nextSibling;
var DI2 = DC1.firstChild;

var docns = (new DOMParser).parseFromString(
    '<doc xmlns="http://a.example.com" xmlns:b="http://b.example.com">' +
        '<item id="1" color="red"/>' +
        '<a:item id="2" xmlns:a="http://a.example.com" a:color="orange"/>' +
        '<b:item id="3" color="yellow" />' +
        '<item id="4" xmlns="http://a.example.com" color="green"/>' +
        '<chapter id="c1" xmlns="http://b.example.com">' +
            '<item id="5" color="blue" />' +
            '<b:item id="6" b:color="indigo"/>' +
        '</chapter>' +
        '<chapter id="c2" xmlns="http://b.example.com" xmlns:b="http://a.example.com">' +
            '<item id="7" b:color="violet"/>' +
            '<b:item id="8" a:color="brown" xmlns:a="http://b.example.com"/>' +
        '</chapter>' +
   '</doc>',
    'application/xml');

var XROOT = docns.firstChild;
var XI1 = XROOT.firstChild;
var XI2 = XI1.nextSibling;
var XI3 = XI2.nextSibling;
var XI4 = XI3.nextSibling;
var XC1 = XI4.nextSibling;
var XI5 = XC1.firstChild;
var XI6 = XI5.nextSibling;
var XC2 = XC1.nextSibling;
var XI7 = XC2.firstChild;
var XI8 = XI7.nextSibling;

function nsResolver(prefix)
{
    if (prefix == "b")
        return "http://b.example.com";
    if (prefix == "a")
        return "http://a.example.com";
    return null;
}

// Some of these tests originally used a default namespace, which is not available in JavaScript XPathEvaluator.
test(doc, doc, '/descendant::item', [DI1, DI2]);
test(docns, docns, '/descendant::a:item', [XI1, XI2, XI4, XI8], nsResolver);
test(docns, docns, '/descendant::b:*', [XI3, XC1, XI5, XI6, XC2, XI7], nsResolver);
shouldThrow('docns.evaluate("//x:*", docns, nsResolver, XPathResult.ANY_TYPE, null)');
test(doc, doc, 'doc/child::*', [DI1, DC1]);
test(docns, docns, 'a:doc/child::*', [XI1, XI2, XI3, XI4, XC1, XC2], nsResolver);
test(doc, doc, '//attribute::color', [DI1.getAttributeNode("color"), DI2.getAttributeNode("color")]);
test(docns, docns, '//attribute::color', [XI1.getAttributeNode("color"), XI3.getAttributeNode("color"), XI4.getAttributeNode("color"), XI5.getAttributeNode("color")], nsResolver);
test(docns, docns, '//attribute::b:*', [XI6.getAttributeNodeNS("http://b.example.com", "color"), XI8.getAttributeNodeNS("http://b.example.com", "color")], nsResolver);
test(doc, doc, '//attribute::*', [DI1.getAttributeNode("id"), DI1.getAttributeNode("color"), DC1.getAttributeNode("id"), DI2.getAttributeNode("id"), DI2.getAttributeNode("color")]);
test(docns, docns, 'count(//attribute::*)', 18, nsResolver);


var doc = (new DOMParser).parseFromString(
    '<doc><element />text<?one pi?><?two pi?><!--comment--></doc>',
    'application/xml');

var ROOT = doc.documentElement;
var TEXT = ROOT.firstChild.nextSibling;
var COMMENT = ROOT.lastChild;
var PI1 = TEXT.nextSibling;
var PI2 = PI1.nextSibling;

test(doc, doc, 'doc/child::text()', [TEXT]);
test(doc, doc, 'doc/child::comment()', [COMMENT]);
test(doc, doc, 'doc/child::processing-instruction()', [PI1, PI2]);
test(doc, doc, 'doc/child::processing-instruction("one")', [PI1]);

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