nodetests.html revision 5f90462bbf4efb0ac7bb65a852d5559d0ab30f0b
1<html>
2<head>
3<link rel="stylesheet" href="/js/resources/js-test-style.css">
4<script src="/js/resources/js-test-pre.js"></script>
5<script src="/xpath-test-pre.js"></script>
6</head>
7<body>
8<div id="console"></div>
9
10<script>
11var doc = (new DOMParser).parseFromString(
12    '<doc>' +
13        '<item id="1" color="red" />' +
14        '<chapter id="c1">' +
15            '<item id="2" color="blue" />' +
16        '</chapter>' +
17   '</doc>',
18    'application/xml');
19
20var ROOT = doc.documentElement;
21var DI1 = ROOT.firstChild;
22var DC1 = DI1.nextSibling;
23var DI2 = DC1.firstChild;
24
25var docns = (new DOMParser).parseFromString(
26    '<doc xmlns="http://a.example.com" xmlns:b="http://b.example.com">' +
27        '<item id="1" color="red"/>' +
28        '<a:item id="2" xmlns:a="http://a.example.com" a:color="orange"/>' +
29        '<b:item id="3" color="yellow" />' +
30        '<item id="4" xmlns="http://a.example.com" color="green"/>' +
31        '<chapter id="c1" xmlns="http://b.example.com">' +
32            '<item id="5" color="blue" />' +
33            '<b:item id="6" b:color="indigo"/>' +
34        '</chapter>' +
35        '<chapter id="c2" xmlns="http://b.example.com" xmlns:b="http://a.example.com">' +
36            '<item id="7" b:color="violet"/>' +
37            '<b:item id="8" a:color="brown" xmlns:a="http://b.example.com"/>' +
38        '</chapter>' +
39   '</doc>',
40    'application/xml');
41
42var XROOT = docns.firstChild;
43var XI1 = XROOT.firstChild;
44var XI2 = XI1.nextSibling;
45var XI3 = XI2.nextSibling;
46var XI4 = XI3.nextSibling;
47var XC1 = XI4.nextSibling;
48var XI5 = XC1.firstChild;
49var XI6 = XI5.nextSibling;
50var XC2 = XC1.nextSibling;
51var XI7 = XC2.firstChild;
52var XI8 = XI7.nextSibling;
53
54function nsResolver(prefix)
55{
56    if (prefix == "b")
57        return "http://b.example.com";
58    if (prefix == "a")
59        return "http://a.example.com";
60    return null;
61}
62
63// Some of these tests originally used a default namespace, which is not available in JavaScript XPathEvaluator.
64test(doc, doc, '/descendant::item', [DI1, DI2]);
65test(docns, docns, '/descendant::a:item', [XI1, XI2, XI4, XI8], nsResolver);
66test(docns, docns, '/descendant::b:*', [XI3, XC1, XI5, XI6, XC2, XI7], nsResolver);
67shouldThrow('docns.evaluate("//x:*", docns, nsResolver, XPathResult.ANY_TYPE, null)');
68test(doc, doc, 'doc/child::*', [DI1, DC1]);
69test(docns, docns, 'a:doc/child::*', [XI1, XI2, XI3, XI4, XC1, XC2], nsResolver);
70test(doc, doc, '//attribute::color', [DI1.getAttributeNode("color"), DI2.getAttributeNode("color")]);
71test(docns, docns, '//attribute::color', [XI1.getAttributeNode("color"), XI3.getAttributeNode("color"), XI4.getAttributeNode("color"), XI5.getAttributeNode("color")], nsResolver);
72test(docns, docns, '//attribute::b:*', [XI6.getAttributeNodeNS("http://b.example.com", "color"), XI8.getAttributeNodeNS("http://b.example.com", "color")], nsResolver);
73test(doc, doc, '//attribute::*', [DI1.getAttributeNode("id"), DI1.getAttributeNode("color"), DC1.getAttributeNode("id"), DI2.getAttributeNode("id"), DI2.getAttributeNode("color")]);
74test(docns, docns, 'count(//attribute::*)', 18, nsResolver);
75
76
77var doc = (new DOMParser).parseFromString(
78    '<doc><element />text<?one pi?><?two pi?><!--comment--></doc>',
79    'application/xml');
80
81var ROOT = doc.documentElement;
82var TEXT = ROOT.firstChild.nextSibling;
83var COMMENT = ROOT.lastChild;
84var PI1 = TEXT.nextSibling;
85var PI2 = PI1.nextSibling;
86
87test(doc, doc, 'doc/child::text()', [TEXT]);
88test(doc, doc, 'doc/child::comment()', [COMMENT]);
89test(doc, doc, 'doc/child::processing-instruction()', [PI1, PI2]);
90test(doc, doc, 'doc/child::processing-instruction("one")', [PI1]);
91
92
93var successfullyParsed = true;
94
95</script>
96<script src="/js/resources/js-test-post.js"></script>
97</body>
98</html>
99