Bug 1942639 - Propagate --filter argument from desktop_unittest.py to runreftest...
[gecko.git] / dom / xslt / tests / mochitest / test_parameter.html
blob32acd72fb28c11b76779f275f33595fa2b1697ae
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <title>Test for setParameter/getParameter</title>
5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
7 </head>
8 <body>
9 <p id="display"></p>
10 <div id="content" style="display: none"><p id="number">123</p><p id="string">abc</p></div>
11 <pre id="test">
12 <script>
13 let processor = new XSLTProcessor();
15 processor.setParameter(null, "test", "hello");
16 is(processor.getParameter(null, "test"), "hello", "null namespace works");
18 processor.setParameter("foo", "bar", "foobar");
19 is(processor.getParameter("foo", "bar"), "foobar", "non-null namespace works");
21 processor.setParameter(null, "test", 123);
22 is(processor.getParameter(null, "test"), 123, "number value works");
24 processor.removeParameter(null, "test");
25 is(processor.getParameter(null, "test"), null, "removeParameter works");
27 is(processor.getParameter(null, "not-here"), null, "nonexistant parameter");
29 let parser = new DOMParser();
30 const style =
31 `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
32 xmlns:exslt="http://exslt.org/common">
33 <xsl:output method="html" />
34 <xsl:param name="test" />
35 <xsl:template match="/">
36 <p id="result"><xsl:value-of select="exslt:object-type($test)" /></p>
37 </xsl:template>
38 </xsl:stylesheet>`;
39 const styleDoc = parser.parseFromString(style, "text/xml");
41 const xml = `<root />`;
42 const sourceDoc = parser.parseFromString(xml, "text/xml");
44 function checkParameter(value, parameter) {
45 let valueType = typeof value;
46 switch (valueType) {
47 case "number":
48 case "boolean":
49 case "string":
50 is(typeof parameter, valueType, "Parameter has right type");
51 is(parameter, value, "Parameter has right value");
52 return;
53 case "object":
54 if (value instanceof Array || value instanceof NodeList) {
55 ok(parameter instanceof Array, "Parameter has right type");
56 ok(parameter.length == value.length &&
57 parameter.every((p, i) => value[i] === p),
58 "Parameter has right value");
59 return;
62 if (value instanceof Node) {
63 is(parameter, value, "Parameter has right value");
64 return;
67 if (value instanceof XPathResult) {
68 ok(parameter instanceof XPathResult, "Parameter has right type");
69 is(parameter.resultType, value.resultType, "Parameter has right inner type");
70 let valueProperty;
71 switch (value.resultType) {
72 case XPathResult.NUMBER_TYPE:
73 valueProperty = "numberValue";
74 break;
75 case XPathResult.BOOLEAN_TYPE:
76 valueProperty = "booleanValue";
77 break;
78 case XPathResult.STRING_TYPE:
79 valueProperty = "stringValue";
80 break;
81 case XPathResult.FIRST_ORDERED_NODE_TYPE:
82 valueProperty = "singleNodeValue";
83 break;
84 default:
85 ok(false, "Unexpected inner type");
86 return;
88 is(parameter[valueProperty], value[valueProperty], "Parameter has right value");
89 return;
92 ok(false, "Unexpected value");
95 function getXSLTType(value) {
96 let valueType = typeof value;
97 switch (valueType) {
98 case "number":
99 case "boolean":
100 case "string":
101 return valueType;
102 case "object":
103 if (value instanceof Array || value instanceof Node || value instanceof NodeList) {
104 return "node-set";
106 if (value instanceof XPathResult) {
107 switch (value.resultType) {
108 case XPathResult.NUMBER_TYPE:
109 return "number";
110 case XPathResult.BOOLEAN_TYPE:
111 return "boolean";
112 case XPathResult.STRING_TYPE:
113 return "string";
114 case XPathResult.FIRST_ORDERED_NODE_TYPE:
115 return "node-set";
119 ok(false, "Unexpected value");
120 throw new Error("Unexpected value");
123 function testParameter(value) {
124 let testProcessor = new XSLTProcessor();
126 testProcessor.setParameter(null, "test", value);
127 let parameter = testProcessor.getParameter(null, "test");
129 checkParameter(value, parameter);
131 testProcessor.importStylesheet(styleDoc);
133 let fragment = testProcessor.transformToFragment(sourceDoc, document);
136 fragment.getElementById("result").textContent,
137 getXSLTType(value),
138 "Global parameter has right type in XSLT."
142 testParameter(123);
143 testParameter(true);
144 testParameter("abcd");
145 testParameter([document]);
146 testParameter(document.documentElement.childNodes);
147 testParameter(document);
148 testParameter(document.evaluate("id('number')", document, null, XPathResult.NUMBER_TYPE));
149 testParameter(document.evaluate("/", document, null, XPathResult.BOOLEAN_TYPE));
150 testParameter(document.evaluate("id('string')", document, null, XPathResult.STRING_TYPE));
151 testParameter(
152 document.evaluate("/", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE)
154 </script>
155 </pre>
156 </body>
157 </html>