CETEIcean 1.9.0 → 1.9.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/utilities.js +72 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "CETEIcean",
3
- "version": "1.9.0",
3
+ "version": "1.9.1",
4
4
  "description": "JavaScript library to load a TEI XML document and register it as HTML5 custom elements.",
5
5
  "main": "src/CETEI.js",
6
6
  "type": "module",
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;
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(/&gt;/, ">")
250
322
  .replace(/&quot;/, "\"")