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