Repository navigation
Expand file tree
/
Copy pathstringify.js
More file actions
125 lines (102 loc) · 2.99 KB
/
Copy pathstringify.js
File metadata and controls
125 lines (102 loc) · 2.99 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
'use strict';
const isObject = o => o && Object.prototype.toString.call(o) === '[object Object]';
function indenter (indentation) {
if (!(indentation > 0)) {
return txt => txt;
}
var space = ' '.repeat(indentation);
return txt => {
if (typeof txt !== 'string') {
return txt;
}
const arr = txt.split('\n');
if (arr.length === 1) {
return space + txt;
}
return arr
.map(e => (e.trim() === '') ? e : space + e)
.join('\n');
};
}
const clean = txt => txt
.split('\n')
.filter(e => e.trim() !== '')
.join('\n');
// XML Name subset accepted for element and attribute names.
// Anything else (spaces, quotes, `>`, `=`, `/`) would break out of the tag.
const nameRe = /^[A-Za-z_][A-Za-z0-9_.:-]*$/;
// Escape `&` unless it already starts a character / entity reference,
// so pre-escaped input (e.g. from `tspan`) and intentional entities
// (`→`, `&`) keep working (idempotent escaping).
const ampRe = /&(?![A-Za-z][A-Za-z0-9]*;|#[0-9]+;|#x[0-9A-Fa-f]+;)/g;
const escText = txt => txt
.replace(ampRe, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
const escAttr = txt => escText(txt)
.replace(/"/g, '"');
// Raw text elements: keep content verbatim (CSS / JS), only prevent closing-tag breakout.
const rawText = {style: true, script: true};
const escRaw = txt => txt.replace(/<\//g, '<\\/');
function stringify (a, indentation) {
const cr = (indentation > 0) ? '\n' : '';
const indent = indenter(indentation);
function rec(a) {
let body = '';
let isFlat = true;
const esc = rawText[a[0]] ? escRaw : escText;
let res;
const isEmpty = a.some((e, i, arr) => {
if (i === 0) {
if (typeof e !== 'string' || !nameRe.test(e)) {
throw new TypeError('onml.stringify: invalid element name: ' + JSON.stringify(e));
}
res = '<' + e;
return (arr.length === 1);
}
if (i === 1) {
if (isObject(e)) {
Object.keys(e).map(key => {
if (!nameRe.test(key)) {
return; // drop attributes with invalid names
}
let val = e[key];
if (Array.isArray(val)) {
val = val.join(' ');
}
res += ' ' + key + '="' + escAttr(String(val)) + '"';
});
if (arr.length === 2) {
return true;
}
res += '>';
return;
}
res += '>';
}
if (!e && e !== 0 && e !== '') {
return;
}
switch (typeof e) {
case 'string':
body += esc(e) + cr;
return;
case 'number':
case 'boolean':
case 'undefined':
body += e + cr;
return;
}
isFlat = false;
body += rec(e);
});
if (isEmpty) {
return res + '/>' + cr; // short form
}
return isFlat
? res + clean(body) + '</' + a[0] + '>' + cr
: res + cr + indent(body) + '</' + a[0] + '>' + cr;
}
return rec(a);
}
module.exports = stringify;