Repository navigation
Expand file tree
/
Copy pathparsing.js
More file actions
340 lines (254 loc) · 11.8 KB
/
Copy pathparsing.js
File metadata and controls
340 lines (254 loc) · 11.8 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"use strict";
const assert = require("chai").assert;
const describe = require("mocha-sugar-free").describe;
const specify = require("mocha-sugar-free").specify;
const readTestFixture = require("../util").readTestFixture;
const jsdom = require("../..");
// These tests are mostly random regression tests, not systematic parsing tests. They are compiled from the bug tracker.
describe("jsdom/parsing", () => {
specify("<template> content document fragment (GH-942)", () => {
const document = jsdom.jsdom("<ul><template><div>Hello world!</div></template></ul>");
const tmpl = document.getElementsByTagName("template");
assert.equal(tmpl[0].content.nodeType, 11);
assert.equal(tmpl[0].innerHTML, "<div>Hello world!</div>");
});
specify("unclosed <td> (GH-605)", () => {
const document = jsdom.jsdom("<table><tr><td>first td<td>second td</tr></table>");
const tds = document.getElementsByTagName("td");
assert.equal(tds.length, 2);
assert.equal(tds[0].innerHTML, "first td");
assert.equal(tds[1].innerHTML, "second td");
});
specify("multiline attributes (GH-585)", () => {
const document = jsdom.jsdom("<a data-title='<strong>hello \nworld</strong>' href='example.org</strong>'>link</a>");
const as = document.getElementsByTagName("a");
assert.equal(as.length, 1);
assert.equal(as[0].innerHTML, "link");
assert.equal(as[0].getAttribute("data-title"), "<strong>hello \nworld</strong>");
assert.equal(as[0].getAttribute("href"), "example.org</strong>");
});
specify("innerHTML of <script type='text/html'> (GH-575)", () => {
const document = jsdom.jsdom("<script type='text/html'>script innerHTML</script>");
const scripts = document.getElementsByTagName("script");
assert.equal(scripts.length, 1);
assert.equal(scripts[0].innerHTML, "script innerHTML");
});
specify("attributes containing '<' and '>' (GH-494)", () => {
const document = jsdom.jsdom("<p title='<'>stuff</p><p title='>'>more</p><p>just testing</p>");
const ps = document.getElementsByTagName("p");
assert.equal(ps.length, 3);
assert.equal(ps[0].getAttribute("title"), "<");
assert.equal(ps[1].getAttribute("title"), ">");
});
s
8000
pecify("empty attributes (GH-488)", () => {
const document = jsdom.jsdom("<div ng-app></div>");
const divs = document.getElementsByTagName("div");
assert.equal(divs.length, 1);
assert.equal(divs[0].getAttribute("ng-app"), "");
});
specify("omitting optional closing tags (GH-482)", () => {
const document = jsdom.jsdom("<p>First<p>Second<p>Third");
const ps = document.getElementsByTagName("p");
assert.equal(ps.length, 3);
assert.equal(ps[0].innerHTML, "First");
assert.equal(ps[1].innerHTML, "Second");
assert.equal(ps[2].innerHTML, "Third");
});
specify("crazy attribute names (GH-368)", () => {
const document = jsdom.jsdom("<p <='' FAIL>stuff</p>");
const ps = document.getElementsByTagName("p");
assert.equal(ps.length, 1);
assert.equal(ps[0].innerHTML, "stuff");
assert.equal(ps[0].getAttribute("<"), "");
assert.equal(ps[0].getAttribute("fail"), "");
});
specify("attribute named 'constructor' (GH-625)", () => {
const document = jsdom.jsdom("<element constructor='Hello'></element>");
const els = document.getElementsByTagName("element");
assert.equal(els.length, 1);
assert.equal(els[0].getAttribute("constructor"), "Hello");
assert.equal(els[0].attributes.length, 1);
assert.equal(els[0].outerHTML, "<element constructor=\"Hello\"></element>");
});
specify("CDATA should parse as bogus comments (GH-618)", () => {
const document = jsdom.jsdom("<html><head></head><body><div><![CDATA[test]]></div></body></html>");
const div = document.getElementsByTagName("div")[0];
assert.ok(div);
assert.equal(div.childNodes.length, 1);
const comment = div.childNodes[0];
assert.equal(comment.nodeType, comment.COMMENT_NODE);
assert.equal(comment.nodeValue, "[CDATA[test]]");
assert.equal(
document.documentElement.outerHTML,
"<html><head></head><body><div><!--[CDATA[test]]--></div></body></html>"
);
});
specify("innerHTML behavior in <script> vs. <p> (GH-652)", () => {
const document = jsdom.jsdom();
const script = document.createElement("script");
script.innerHTML = "3 < 5";
assert.equal(script.innerHTML, "3 < 5");
const p = document.createElement("p");
p.innerHTML = "3 < 5";
assert.equal(p.innerHTML, "3 < 5");
});
specify("lower-cases tags in outerHTML and innerHTML", () => {
const document = jsdom.jsdom("<HTML><HEAD></HEAD><BODY><P ALIGN='RIGHT'>test</P></BODY></HTML>");
assert.equal(
document.documentElement.outerHTML,
"<html><head></head><body><p align=\"RIGHT\">test</p></body></html>"
);
document.body.innerHTML = "<DIV>test</DIV>";
assert.equal(document.body.innerHTML, "<div>test</div>");
});
specify("HTML entities without ; (GH-821)", () => {
const document = jsdom.jsdom("<a>a</a>");
assert.strictEqual(document.getElementsByTagName("a")[0].textContent, "a");
});
specify("Added < and > near <script> tags (GH-826)", () => {
const document = jsdom.jsdom("<<script>alert(1);//<</script>");
assert.strictEqual(document.body.innerHTML, "<<script>alert(1);//<</script>");
});
specify("Chinese characters as a 'tag name' (GH-719)", { async: true }, t => {
jsdom.env({
html: "<div>chinese here:<中文></div>",
done(err, window) {
assert.ifError(err);
assert.strictEqual(window.document.body.innerHTML, "<div>chinese here:<中文></div>");
t.done();
}
});
});
specify("A single <html> tag (GH-827)", () => {
const document = jsdom.jsdom("<html>");
assert.strictEqual(document.documentElement.innerHTML, "<head></head><body></body>");
});
specify("Missing <html> tags (GH-555)", () => {
const doctype = "<!DOCTYPE html PUBLIC\"-//W3C//DTD HTML 4.0//EN\">";
const docA = jsdom.jsdom(doctype + "<html><head><title></title></head><p>");
const docB = jsdom.jsdom(doctype + "<head><title></title></head><p>");
assert.strictEqual(docA.querySelectorAll("p").length, 1);
assert.strictEqual(docB.querySelectorAll("p").length, 1);
});
specify("Missing <body> tag with script src (GH-389)", { async: true }, t => {
jsdom.env({
html: "<html></html>",
src: "''",
done(err) {
assert.ifError(err);
t.done();
}
});
});
specify("More missing <html> and <body> tag tests (GH-88)", () => {
const doc1 = jsdom.jsdom("<html><body></body></html>");
assert.strictEqual(doc1.body.innerHTML, "");
const doc2 = jsdom.jsdom("<body></body>");
assert.strictEqual(doc2.body.innerHTML, "");
const doc3 = jsdom.jsdom("gavin!");
assert.strictEqual(doc3.body.innerHTML, "gavin!");
});
specify(".innerHTML with < character (GH-652)", { async: true }, t => {
jsdom.env({
html: "<html>",
done(err, window) {
assert.ifError(err);
const document = window.document;
const script = document.createElement("script");
script.innerHTML = "5 < 3";
assert.strictEqual(script.innerHTML, "5 < 3");
const p = document.createElement("p");
p.innerHTML = "5 < 3";
assert.strictEqual(p.innerHTML, "5 < 3");
t.done();
}
});
});
specify("whitespace after <!DOCTYPE> (GH-160)", () => {
const document = jsdom.jsdom("<!DOCTYPE html>\n<html></html>");
const firstChild = document.firstChild;
assert.strictEqual(firstChild.nodeName, "html");
});
specify("pre tag with < and > characters (GH-755)", () => {
const document = jsdom.jsdom("<pre>[task.h:277] - RunnableMethod<DOMStorageDispatcherHost,void " +
"( DOMStorageDispatcherHost::*)(DOMStorageType,IPC::Message *),Tuple2<DOMStorageType,IPC::Message *>>" +
"::Run()</pre>");
assert.strictEqual(document.body.innerHTML, "<pre>[task.h:277] - RunnableMethod<domstoragedispatcherhost,void " +
"(=\"\" domstoragedispatcherhost::*)(domstoragetype,ipc::message=\"\" *),tuple2" +
"<domstoragetype,ipc::message=\"\" *=\"\">>::Run()</domstoragedispatcherhost,void></pre>");
});
specify("querySelector applied to document fragments (GH-523)", () => {
const document = jsdom.jsdom("<a href='http://foo'></a>");
assert.strictEqual(document.querySelector("[href]").tagName, "A");
});
specify("real-world page with < inside a text node (GH-800)", () => {
return readTestFixture("jsdom/files/steam.html").then(content => {
const doc = jsdom.jsdom(content);
assert.notEqual(doc.querySelector(".badge_card_set_text"), null);
});
});
specify("void tags with innerHTML (GH-863)", () => {
const document = jsdom.jsdom();
document.body.innerHTML = "<p>hello <img src=\"test\"> world</p>";
assert.strictEqual(document.body.firstChild.childNodes.length, 3, "paragraph should contain 3 children");
});
specify("void tags set by innerHTML from createElement (GH-863/872)", () => {
const document = jsdom.jsdom();
const div = document.createElement("div");
div.innerHTML = "first <img src=\"test\"> third";
assert.equal(div.childNodes.length, 3);
assert.equal(div.childNodes[0].textContent, "first ");
assert.equal(div.childNodes[1].nodeName, "IMG");
assert.equal(div.childNodes[2].textContent, " third");
});
specify("<template> with whitespace inside (GH-1004)", () => {
const document = jsdom.jsdom("<template> <div></div> </template>");
assert.equal(
document.documentElement.innerHTML,
"<head><template> <div></div> </template></head><body></body>"
);
});
specify("doctype parsing should work for simple cases (GH-1066)", () => {
const document = jsdom.jsdom("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
assert.strictEqual(document.doctype.name, "html");
assert.strictEqual(document.doctype.systemId, "");
assert.strictEqual(document.doctype.publicId, "-//W3C//DTD HTML 4.01 Transitional//EN");
});
specify("completely empty <!DOCTYPE> (GH-1204)", () => {
const document = jsdom.jsdom("<!DOCTYPE>");
assert.strictEqual(document.doctype.name, "");
assert.strictEqual(document.doctype.systemId, "");
assert.strictEqual(document.doctype.publicId, "");
});
specify("incomplete SVG doctype (GH-259)", () => {
const document = jsdom.jsdom(`<!DOCTYPE svg>\n<svg version="1.1"></svg>`);
assert.strictEqual(document.doctype.name, "svg");
assert.strictEqual(document.doctype.systemId, "");
assert.strictEqual(document.doctype.publicId, "");
});
specify("extra HTML after </html> (GH-319)", () => {
const document = jsdom.jsdom("<!DOCTYPE html><html><head><title>Title</title></head>" +
"<body>My body</body></html><div></div>");
assert.strictEqual(document.body.childNodes.length, 2);
assert.strictEqual(document.querySelector("div").parentNode, document.body);
});
specify("<%= stuff %> inside <script> tags (GH-58)", () => {
const content = "<%= cid %>";
const script = `<script type="text/x-underscore-tmpl">${content}</script>`;
const html = `<html><head>${script}</head><body><p>hello world!</p></body></html>`;
const document = jsdom.jsdom(html);
document.write(html);
document.close();
assert.strictEqual(document.head.childNodes[0].innerHTML, content);
});
specify("xmlns doesn't cause empty prefix", { async: true }, t => {
const html = "<!DOCTYPE html><math xmlns=\"http://www.w3.org/1998/Math/MathML\"></math>";
const expected = "<!DOCTYPE html><html><head></head>" +
"<body><math xmlns=\"http://www.w3.org/1998/Math/MathML\"></math></body></html>";
jsdom.env(html, (err, window) => {
assert.ifError(err);
assert.strictEqual(jsdom.serializeDocument(window.document), expected);
t.done();
});
});
});