-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2efml.cjs
242 lines (234 loc) · 6.63 KB
/
xml2efml.cjs
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
const nodeTypeMap = {
1: 'ELEMENT_NODE',
2: 'ATTRIBUTE_NODE',
3: 'TEXT_NODE',
4: 'CDATA_SECTION_NODE',
5: 'ENTITY_REFERENCE_NODE',
6: 'ENTITY_NODE',
7: 'PROCESSING_INSTRUCTION_NODE',
8: 'COMMENT_NODE',
9: 'DOCUMENT_NODE',
10: 'DOCUMENT_TYPE_NODE',
11: 'DOCUMENT_FRAGMENT_NODE',
12: 'NOTATION_NODE',
};
function findWindow(win) {
if (typeof win === 'object') {
return win;
}
if (typeof window !== 'undefined') {
return window;
}
return null;
}
function dom2efml(elem, {
spaces = ' ',
ignoreEmptyTextNode = true,
indentOffset = 0,
} = {}) {
let output = '';
/**
* @param {Element} element
* @param {number} indent
*/
function recursive(element, indent) {
if (indent === undefined) {
indent = 0;
}
output += `${spaces.repeat(indent)}>${element.tagName}`;
Array.prototype.forEach.call(element.classList, (e) => {
output += `.${e}`;
});
output += '\n';
Array.prototype.forEach.call(element.attributes, (e) => {
if (e.name === 'class') {
return;
}
output += `${spaces.repeat(indent + 1)}#${e.name}`;
if (e.value !== '') {
output += ` = ${e.value}`;
}
output += '\n';
});
Array.prototype.forEach.call(element.childNodes, (e) => {
switch (e.nodeType) {
case 1: {
recursive(e, indent + 1);
break;
}
case 3: {
if (!ignoreEmptyTextNode || e.textContent.trim() !== '') {
output += `${spaces.repeat(indent + 1)}.${e.textContent.replace(/&/g, '&&').replace(/\n/g, '&n')}\n`;
}
break;
}
default: {
console.warn(`The nodeType value is ${e.nodeType} (${nodeTypeMap[e.nodeType]}), which didn't handled by dom2efml`);
break;
}
}
});
}
recursive(elem, indentOffset);
return output;
}
/**
* @param {Element} elem
*/
function dom2ast(elem, {
ignoreEmptyTextNode = true,
} = {}) {
const myRoot = [];
const myTag = {
t: elem.tagName,
};
const myAttr = {};
Array.prototype.forEach.call(elem.attributes, (e) => {
myAttr[e.name] = e.value;
});
if (Object.keys(myAttr).length > 0) {
myTag.a = myAttr;
}
myRoot.push(myTag);
Array.prototype.forEach.call(elem.childNodes, (e) => {
switch (e.nodeType) {
case 1: {
myRoot.push(dom2ast(e, {
ignoreEmptyTextNode,
}));
break;
}
case 3: {
if (!ignoreEmptyTextNode || e.textContent.trim() !== '') {
myRoot.push(e.textContent);
}
break;
}
default: {
console.warn(`The nodeType value is ${e.nodeType} (${nodeTypeMap[e.nodeType]}), which didn't handled by dom2efml`);
break;
}
}
});
return myRoot;
}
function htmlSnippet2efml(str, {
spaces = ' ',
ignoreEmptyTextNode = true,
win,
} = {}) {
const myWindow = findWindow(win);
if (!myWindow) {
throw new ReferenceError('htmlSnippet2efml requires a window');
}
let result = '';
const parser = new myWindow.DOMParser();
const data = parser.parseFromString(str, 'text/html');
function loop(e) {
switch (e.nodeType) {
case 1: {
result += dom2efml(e, {
spaces,
ignoreEmptyTextNode,
});
break;
}
case 3: {
if (!ignoreEmptyTextNode || e.textContent.trim() !== '') {
result += `.${e.textContent.replace(/&/g, '&&').replace(/\n/g, '&n')}\n`;
}
break;
}
default: {
console.warn(`The nodeType value is ${e.nodeType} (${nodeTypeMap[e.nodeType]}), which didn't handled by dom2efml`);
break;
}
}
}
Array.prototype.forEach.call(data.head.childNodes, loop);
Array.prototype.forEach.call(data.body.childNodes, loop);
return result;
}
function htmlSnippet2ast(str, {
ignoreEmptyTextNode = true,
win,
} = {}) {
const myWindow = findWindow(win);
if (!myWindow) {
throw new ReferenceError('htmlSnippet2ast requires a window');
}
const parser = new myWindow.DOMParser();
const data = parser.parseFromString(str, 'text/html');
const childs = Array.from(data.head.childNodes);
Array.prototype.forEach.call(data.body.childNodes, (e) => {
childs.push(e);
});
if (childs.length === 1) {
return dom2ast(childs[0], { ignoreEmptyTextNode });
}
// ========== if has more than 1 childs ==========
const root = [{
t: 0,
}];
childs.forEach((e) => {
switch (e.nodeType) {
case 1: {
root.push(dom2ast(e, { ignoreEmptyTextNode }));
break;
}
case 3: {
if (!ignoreEmptyTextNode || e.textContent.trim() !== '') {
root.push(e.textContent);
}
break;
}
default: {
console.warn(`The nodeType value is ${e.nodeType} (${nodeTypeMap[e.nodeType]}), which didn't handled by dom2efml`);
break;
}
}
});
return root;
}
function xml2efml(str, {
spaces = ' ',
ignoreEmptyTextNode = true,
win,
type = 'text/xml',
} = {}) {
const myWindow = findWindow(win);
if (!myWindow) {
throw new ReferenceError('xml2efml requires a window');
}
const parser = new myWindow.DOMParser();
const data = parser.parseFromString(str, type);
const result = dom2efml(data.documentElement, {
spaces,
ignoreEmptyTextNode,
});
return result;
}
function xml2ast(str, {
ignoreEmptyTextNode = true,
win,
type = 'text/xml',
} = {}) {
const myWindow = findWindow(win);
if (!myWindow) {
throw new ReferenceError('xml2ast requires a window');
}
const parser = new myWindow.DOMParser();
const data = parser.parseFromString(str, type);
const result = dom2ast(data.documentElement, {
ignoreEmptyTextNode,
});
return result;
}
module.exports = {
dom2efml,
dom2ast,
htmlSnippet2efml,
htmlSnippet2ast,
xml2efml,
xml2ast,
};