Repository navigation
Expand file tree
/
Copy pathnamespaces.js
More file actions
112 lines (92 loc) · 5.43 KB
/
Copy pathnamespaces.js
File metadata and controls
112 lines (92 loc) · 5.43 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
"use strict";
const assert = require("chai").assert;
const describe = require("mocha-sugar-free").describe;
const specify = require("mocha-sugar-free").specify;
const jsdom = require("../..");
const readTestFixture = require("../util").readTestFixture;
function testHTMLDocument(document) {
assert.strictEqual(document.getElementsByTagName("body").length, 1);
assert.strictEqual(document.getElementsByTagName("p").length, 1);
assert.strictEqual(document.getElementsByTagName("img").length, 1);
const p = document.getElementsByTagName("p")[0];
assert.strictEqual(p.prefix, null, "tag prefixes in html documents should always be null");
assert.strictEqual(p.localName, "p", "localNames in html documents always equal the tag name");
assert.strictEqual(p.namespaceURI, "http://www.w3.org/1999/xhtml",
"html elements should automatically be assigned the XHTML namespace");
assert.strictEqual(p.getAttribute("xmlns:xlink"), "http://www.w3.org/1999/xlink",
"attributes should be retrievable by their full name");
const xmlnsAttr = p.attributes["xmlns:xlink"];
assert.strictEqual(xmlnsAttr.prefix, null, "attribute prefixes should be detected");
assert.strictEqual(xmlnsAttr.localName, "xmlns:xlink", "attribute localNames should be detected");
assert.strictEqual(xmlnsAttr.namespaceURI, null,
"xmlns: attributes should not automatically be assigned to the xmlns namespace");
const img = document.getElementsByTagName("img")[0];
assert.strictEqual(img.prefix, null, "tag prefixes in html documents should always be null");
assert.strictEqual(img.localName, "img", "localNames in html documents always equal the tag name");
assert.strictEqual(img.namespaceURI, "http://www.w3.org/1999/xhtml",
"html elements should automatically be assigned the XHTML namespace");
assert.strictEqual(img.getAttribute("xlink:href"), "#test",
"attributes should be retrievable by their full name");
const xlinkAttr = img.attributes["xlink:href"];
assert.strictEqual(xlinkAttr.prefix, null, "attribute prefixes should be detected");
assert.strictEqual(xlinkAttr.localName, "xlink:href", "attribute localNames should be detected");
assert.strictEqual(xlinkAttr.namespaceURI, null,
"it shouldn't be possible to create custom namespaces");
}
describe("jsdom/namespaces", () => {
specify("should set namespaces in HTML documents created by jsdom.env()", () => {
return readTestFixture("jsdom/files/ns-html.html").then(content => {
const doc = jsdom.jsdom(content);
testHTMLDocument(doc);
});
});
specify("should set namespace-related properties in HTML documents created by innerHTML", () => {
return readTestFixture("jsdom/files/ns-html.html").then(content => {
const doc = jsdom.jsdom();
doc.body.innerHTML = content;
testHTMLDocument(doc);
});
});
function testDocumentWithSVG(document) {
assert.strictEqual(document.getElementsByTagName("body").length, 1);
assert.strictEqual(document.getElementsByTagName("svg").length, 1);
assert.strictEqual(document.getElementsByTagName("use").length, 1);
const svg = document.getElementsByTagName("svg")[0];
assert.strictEqual(svg.prefix, null, "tag prefixes in html documents should always be null");
assert.strictEqual(svg.localName, "svg", "localNames in html documents always equal the tag name");
assert.strictEqual(svg.namespaceURI, "http://www.w3.org/2000/svg",
"svg elements should automatically be assigned the SVG namespace");
assert.strictEqual(svg.getAttribute("xmlns:xlink"), "http://www.w3.org/1999/xlink",
"attributes should be retrievable by their full name");
const xmlnsAttr = svg.attributes["xmlns:xlink"];
assert.strictEqual(xmlnsAttr.prefix, "xmlns", "attribute prefixes should be detected");
assert.strictEqual(xmlnsAttr.localName, "xlink", "attribute localNames should be detected");
assert.strictEqual(xmlnsAttr.namespaceURI, "http://www.w3.org/2000/xmlns/",
"xmlns: attributes should automatically be assigned to the xmlns namespace");
const use = document.getElementsByTagName("use")[0];
assert.strictEqual(use.prefix, null, "tag prefixes in html documents should always be null");
assert.strictEqual(use.localName, "use", "localNames in html documents always equal the tag name");
assert.strictEqual(use.namespaceURI, "http://www.w3.org/2000/svg",
"svg elements should automatically be assigned the SVG namespace");
assert.strictEqual(use.getAttribute("xlink:href"), "#test",
"attributes should be retrievable by their full name");
const xlinkAttr = use.attributes["xlink:href"];
assert.strictEqual(xlinkAttr.prefix, "xlink", "attribute prefixes should be detected");
assert.strictEqual(xlinkAttr.localName, "href", "attribute localNames should be detected");
assert.strictEqual(xlinkAttr.namespaceURI, "http://www.w3.org/1999/xlink",
"custom defined namespaces should be valid in child elements");
}
specify("should set namespace-related properties in HTML-SVG documents created by jsdom.env()", () => {
return readTestFixture("jsdom/files/ns-svg.html").then(content => {
const doc = jsdom.jsdom(content);
testDocumentWithSVG(doc);
});
});
specify("should set namespace-related properties in HTML-SVG documents created by innerHTML", () => {
return readTestFixture("jsdom/files/ns-svg.html").then(content => {
const doc = jsdom.jsdom();
doc.body.innerHTML = content;
testDocumentWithSVG(doc);
});
});
});