-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathparse-from-string.js
More file actions
132 lines (111 loc) · 3.9 KB
/
parse-from-string.js
File metadata and controls
132 lines (111 loc) · 3.9 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
'use strict';
const HTMLParser2 = require('htmlparser2');
const {ELEMENT_NODE, SVG_NAMESPACE} = require('./constants.js');
const {CUSTOM_ELEMENTS, PREV, END, VALUE, CONTENT, PRIVATE} = require('./symbols.js');
const {keys} = require('./object.js');
const {knownBoundaries, knownSiblings} = require('./utils.js');
const {attributeChangedCallback, connectedCallback} = require('../interface/custom-element-registry.js');
const {Parser} = HTMLParser2;
// import {Mime} from './mime.js';
// const VOID_SOURCE = Mime['text/html'].voidElements.source.slice(4, -2);
// const VOID_ELEMENTS = new RegExp(`<(${VOID_SOURCE})([^>]*?)>`, 'gi');
// const VOID_SANITIZER = (_, $1, $2) => `<${$1}${$2}${/\/$/.test($2) ? '' : ' /'}>`;
// const voidSanitizer = html => html.replace(VOID_ELEMENTS, VOID_SANITIZER);
let notParsing = true;
const append = (self, node, active) => {
if (self && self.localName === 'template' && self[CONTENT]) {
self = self[CONTENT];
}
if (!self) return node; // null 체크 추가
const end = self[END];
node.parentNode = self;
knownBoundaries(end[PREV], node, end);
if (active && node.nodeType === ELEMENT_NODE)
connectedCallback(node);
return node;
};
const attribute = (element, end, attribute, value, active) => {
attribute[VALUE] = value;
attribute.ownerElement = element;
knownSiblings(end[PREV], attribute, end);
if (attribute.name === 'class')
element.className = value;
if (active)
attributeChangedCallback(element, attribute.name, null, value);
};
const isNotParsing = () => notParsing;
exports.isNotParsing = isNotParsing;
const parseFromString = (document, isHTML, markupLanguage) => {
const {active, registry} = document[CUSTOM_ELEMENTS];
let node = document;
let ownerSVGElement = null;
let parsingCData = false;
notParsing = false;
const content = new Parser({
// <!DOCTYPE ...>
onprocessinginstruction(name, data) {
if (name.toLowerCase() === '!doctype')
document.doctype = data.slice(name.length).trim();
},
// <tagName>
onopentag(name, attributes) {
let create = true;
if (isHTML) {
if (ownerSVGElement) {
node = append(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
create = false;
}
else if (name === 'svg' || name === 'SVG') {
ownerSVGElement = document.createElementNS(SVG_NAMESPACE, name);
node = append(node, ownerSVGElement, active);
create = false;
}
else if (active) {
const ce = name.includes('-') ? name : (attributes.is || '');
if (ce && registry.has(ce)) {
const {Class} = registry.get(ce);
node = append(node, new Class, active);
delete attributes.is;
create = false;
}
}
}
if (create)
node = append(node, document.createElement(name), false);
let end = node[END];
for (const name of keys(attributes))
attribute(node, end, document.createAttribute(name), attributes[name], active);
},
// #text, #comment
oncomment(data) { append(node, document.createComment(data), active); },
ontext(text) {
if (parsingCData) {
append(node, document.createCDATASection(text), active);
} else {
append(node, document.createTextNode(text), active);
}
},
// #cdata
oncdatastart() { parsingCData = true; },
oncdataend() { parsingCData = false; },
// </tagName>
onclosetag() {
if (isHTML && node === ownerSVGElement)
ownerSVGElement = null;
if (node && node[PRIVATE]) {
node = node[PRIVATE];
}
node = node.parentNode;
}
}, {
lowerCaseAttributeNames: false,
decodeEntities: true,
xmlMode: !isHTML
});
content.write(markupLanguage);
content.end();
notParsing = true;
return document;
};
exports.parseFromString = parseFromString;