CETEIcean 1.9.0 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/defaultBehaviors.js +3 -1
- package/src/utilities.js +72 -0
package/package.json
CHANGED
package/src/defaultBehaviors.js
CHANGED
@@ -97,12 +97,14 @@ export default {
|
|
97
97
|
"egXML": function(elt) {
|
98
98
|
const doc = elt.ownerDocument;
|
99
99
|
let pre = doc.createElement("pre");
|
100
|
+
let code = doc.createElement("code");
|
101
|
+
pre.appendChild(code);
|
100
102
|
let content = this.serialize(elt, true).replace(/</g, "<");
|
101
103
|
let ws = content.match(/^[\t ]+/);
|
102
104
|
if (ws) {
|
103
105
|
content = content.replace(new RegExp("^" + ws[0], "mg"), "");
|
104
106
|
}
|
105
|
-
|
107
|
+
code.innerHTML = content;
|
106
108
|
return pre;
|
107
109
|
}
|
108
110
|
}
|
package/src/utilities.js
CHANGED
@@ -245,6 +245,78 @@ export function serialize(el, stripElt, ws) {
|
|
245
245
|
return str;
|
246
246
|
}
|
247
247
|
|
248
|
+
/*
|
249
|
+
Write out the HTML markup to a string, using HTML conventions.
|
250
|
+
*/
|
251
|
+
export function serializeHTML(el, stripElt, ws) {
|
252
|
+
const EMPTY_ELEMENTS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
|
253
|
+
let str = "";
|
254
|
+
const ignorable = (txt) => {
|
255
|
+
return !(/[^\t\n\r ]/.test(txt));
|
256
|
+
}
|
257
|
+
// nodeType 1 is Node.ELEMENT_NODE
|
258
|
+
if (!stripElt && el.nodeType == 1) {
|
259
|
+
if ((typeof ws === "string") && ws !== "") {
|
260
|
+
str += "\n" + ws + "<";
|
261
|
+
} else {
|
262
|
+
str += "<";
|
263
|
+
}
|
264
|
+
str += el.nodeName;
|
265
|
+
for (let attr of Array.from(el.attributes)) {
|
266
|
+
str += " " + attr.name + "=\"" + attr.value + "\"";
|
267
|
+
}
|
268
|
+
str += ">";
|
269
|
+
}
|
270
|
+
for (let node of Array.from(el.childNodes)) {
|
271
|
+
// nodeType 1 is Node.ELEMENT_NODE
|
272
|
+
// nodeType 7 is Node.PROCESSING_INSTRUCTION_NODE
|
273
|
+
// nodeType 8 is Node.COMMENT_NODE
|
274
|
+
switch (node.nodeType) {
|
275
|
+
case 1:
|
276
|
+
if (typeof ws === "string") {
|
277
|
+
str += serializeHTML(node, false, ws + " ");
|
278
|
+
} else {
|
279
|
+
str += serializeHTML(node, false, ws);
|
280
|
+
}
|
281
|
+
break;
|
282
|
+
case 7:
|
283
|
+
str += `<?${node.nodeName} ${node.nodeValue}?>`;
|
284
|
+
if (el.nodeType === 9 || el.nodeType === 11) {
|
285
|
+
str += "\n";
|
286
|
+
}
|
287
|
+
break;
|
288
|
+
case 8:
|
289
|
+
str += `<!--${node.nodeValue}-->`;
|
290
|
+
if (el.nodeType === 9 || el.nodeType === 11) {
|
291
|
+
str += "\n";
|
292
|
+
}
|
293
|
+
break;
|
294
|
+
default:
|
295
|
+
if (stripElt && ignorable(node.nodeValue)) {
|
296
|
+
str += node.nodeValue.replace(/^\s*\n/, "");
|
297
|
+
}
|
298
|
+
if ((typeof ws === "string") && ignorable(node.nodeValue)) {
|
299
|
+
break;
|
300
|
+
}
|
301
|
+
str += node.nodeValue.replace(/</g, "<");
|
302
|
+
}
|
303
|
+
}
|
304
|
+
if (!EMPTY_ELEMENTS.includes(el.nodeName)) {
|
305
|
+
if (!stripElt && el.nodeType == 1) {
|
306
|
+
if (typeof ws === "string") {
|
307
|
+
str += `\n${ws}</`;
|
308
|
+
} else {
|
309
|
+
str += "</";
|
310
|
+
}
|
311
|
+
str += `${el.nodeName}>`;
|
312
|
+
}
|
313
|
+
}
|
314
|
+
if (el.nodeType === 9 || el.nodeType === 11) {
|
315
|
+
str += "\n";
|
316
|
+
}
|
317
|
+
return str;
|
318
|
+
}
|
319
|
+
|
248
320
|
export function unEscapeEntities(str) {
|
249
321
|
return str.replace(/>/, ">")
|
250
322
|
.replace(/"/, "\"")
|