/
xml.js
191 lines (157 loc) · 5.8 KB
/
xml.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
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
"use strict";
const { SaxesParser } = require("saxes");
const DOMException = require("domexception");
const { createElement } = require("../../living/create-element");
const DocumentFragment = require("../../living/generated/DocumentFragment");
const DocumentType = require("../../living/generated/DocumentType");
const CDATASection = require("../../living/generated/CDATASection");
const Comment = require("../../living/generated/Comment");
const ProcessingInstruction = require("../../living/generated/ProcessingInstruction");
const Text = require("../../living/generated/Text");
const attributes = require("../../living/attributes");
const { HTML_NS } = require("../../living/helpers/namespaces");
const HTML5_DOCTYPE = /<!doctype html>/i;
const PUBLIC_DOCTYPE = /<!doctype\s+([^\s]+)\s+public\s+"([^"]+)"\s+"([^"]+)"/i;
const SYSTEM_DOCTYPE = /<!doctype\s+([^\s]+)\s+system\s+"([^"]+)"/i;
const CUSTOM_NAME_DOCTYPE = /<!doctype\s+([^\s>]+)/i;
function parseDocType(doc, html) {
if (HTML5_DOCTYPE.test(html)) {
return createDocumentType(doc, "html", "", "");
}
const publicPieces = PUBLIC_DOCTYPE.exec(html);
if (publicPieces) {
return createDocumentType(doc, publicPieces[1], publicPieces[2], publicPieces[3]);
}
const systemPieces = SYSTEM_DOCTYPE.exec(html);
if (systemPieces) {
return createDocumentType(doc, systemPieces[1], "", systemPieces[2]);
}
const namePiece = CUSTOM_NAME_DOCTYPE.exec(html)[1] || "html";
return createDocumentType(doc, namePiece, "", "");
}
function createDocumentType(ownerDocument, name, publicId, systemId) {
return DocumentType.createImpl([], { ownerDocument, name, publicId, systemId });
}
function appendChild(parent, child) {
// Template elements do not have children but instead store their content in a separate hierarchy.
if (parent.tagName === "template" && parent.namespaceURI === HTML_NS) {
parent._templateContents._insert(child, null);
} else {
parent._insert(child, null);
}
}
function createParser(rootNode, ownerDocument, saxesOptions) {
const parser = new SaxesParser(saxesOptions);
const openStack = [rootNode];
parser.ontext = saxesOptions.fragment ?
// In a fragment, all text events produced by saxes must result in a text
// node.
data => {
appendChild(
openStack[openStack.length - 1],
Text.createImpl([], { data, ownerDocument })
);
} :
// When parsing a whole document, we must ignore those text nodes that are
// produced outside the root element. Saxes produces events for them,
// but DOM trees do not record text outside the root element.
data => {
if (openStack.length > 1) {
appendChild(
openStack[openStack.length - 1],
Text.createImpl([], { data, ownerDocument })
);
}
};
parser.oncdata = data => {
appendChild(
openStack[openStack.length - 1],
CDATASection.createImpl([], { data, ownerDocument })
);
};
parser.onopentag = tag => {
const { local: tagLocal, attributes: tagAttributes } = tag;
const tagNamespace = tag.uri === "" ? null : tag.uri;
const tagPrefix = tag.prefix === "" ? null : tag.prefix;
const elem = createElement(ownerDocument, tagLocal, tagNamespace, tagPrefix, null, true);
// We mark a script element as "parser-inserted", which prevents it from
// being immediately executed.
if (tagLocal === "script" && tagNamespace === HTML_NS) {
elem._parserInserted = true;
}
for (const key of Object.keys(tagAttributes)) {
const { prefix, local, uri, value } = tagAttributes[key];
attributes.setAttributeValue(
elem, local, value, prefix === "" ? null : prefix,
uri === "" ? null : uri
);
}
appendChild(openStack[openStack.length - 1], elem);
openStack.push(elem);
};
parser.onclosetag = () => {
const elem = openStack.pop();
// Once a script is populated, we can execute it.
if (elem.localName === "script" && elem.namespaceURI === HTML_NS) {
elem._eval();
}
};
parser.oncomment = data => {
appendChild(
openStack[openStack.length - 1],
Comment.createImpl([], { data, ownerDocument })
);
};
parser.onprocessinginstruction = ({ target, body }) => {
appendChild(
openStack[openStack.length - 1],
ProcessingInstruction.createImpl([], { target, data: body, ownerDocument })
);
};
parser.ondoctype = dt => {
appendChild(
openStack[openStack.length - 1],
parseDocType(ownerDocument, `<!doctype ${dt}>`)
);
const entityMatcher = /<!ENTITY ([^ ]+) "([^"]+)">/g;
let result;
while ((result = entityMatcher.exec(dt))) {
const [, name, value] = result;
if (!(name in parser.ENTITIES)) {
parser.ENTITIES[name] = value;
}
}
};
parser.onerror = err => {
throw new DOMException(err.message, "SyntaxError");
};
return parser;
}
function parseFragment(markup, contextElement) {
const ownerDocument = contextElement._ownerDocument;
const fragment = DocumentFragment.createImpl([], { ownerDocument });
// Only parseFragment needs resolvePrefix per the saxes documentation:
// https://github.com/lddubeau/saxes#parsing-xml-fragments
const parser = createParser(fragment, ownerDocument, {
xmlns: true,
fragment: true,
resolvePrefix(prefix) {
// saxes wants undefined as the return value if the prefix is not defined, not null.
return contextElement.lookupNamespaceURI(prefix) || undefined;
}
});
parser.write(markup).close();
return fragment;
}
function parseIntoDocument(markup, ownerDocument) {
const parser = createParser(ownerDocument, ownerDocument, {
xmlns: true,
fileName: ownerDocument.location && ownerDocument.location.href
});
parser.write(markup).close();
return ownerDocument;
}
module.exports = {
parseFragment,
parseIntoDocument
};