-
Notifications
You must be signed in to change notification settings - Fork 0
/
outer-html.js
58 lines (51 loc) · 1.54 KB
/
outer-html.js
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
'use strict';
module.exports = outerHTML;
function outerHTML(node) {
var string;
if (node.nodeType === 1) {
string = "";
string += "<" + node.tagName;
var attributes = node.attributes;
for (var index = 0; index < attributes.length; index++) {
var attribute = attributes.item(index);
string += " " + attribute.name + "=\"" + enquote(attribute.value) + "\"";
}
string += ">";
string += innerHTML(node);
string += "</" + node.tagName + ">";
return string;
} else if (node.nodeType === 3) {
return encode(node.data);
} else if (node.nodeType === 8) {
return "<!--" + node.data + "-->";
} else if (node.nodeType === 9) { // document
string = "";
if (node.doctype) {
string += node.doctype;
}
string += outerHTML(node.documentElement);
return string;
} else {
return "";
}
}
var nonAttributeModeSpecialCharRegExp = /[&<>\xA0]/g;
var attributeModeSpecialCharRegExp = /["&<>\xA0]/g;
var specialCharEntities = {
"&": "&",
"\"": """,
"<": "<",
">": ">",
"\xA0": " "
};
function specialCharToEntity(s) {
var entity = specialCharEntities[s];
return entity ? entity : s;
}
function encode(string) {
return string.replace(nonAttributeModeSpecialCharRegExp, specialCharToEntity);
}
function enquote(string) {
return string.replace(attributeModeSpecialCharRegExp, specialCharToEntity);
}
var innerHTML = require("./inner-html");