Debian Codegraph @

mixmark-io-domino/lib/NodeUtils.js

raw symbols SCIP
1 "use strict";
2 module.exports = {
3 // NOTE: The `serializeOne()` function used to live on the `Node.prototype`
4 // as a private method `Node#_serializeOne(child)`, however that requires
5 // a megamorphic property access `this._serializeOne` just to get to the
6 // method, and this is being done on lots of different `Node` subclasses,
7 // which puts a lot of pressure on V8's megamorphic stub cache. So by
8 // moving the helper off of the `Node.prototype` and into a separate
9 // function in this helper module, we get a monomorphic property access
10 // `NodeUtils.serializeOne` to get to the function and reduce pressure
11 // on the megamorphic stub cache.
12 // See https://github.com/fgnass/domino/pull/142 for more information.
13 serializeOne: serializeOne,
14
15 // Export util functions so that we can run extra test for them.
16 // Note: we prefix function names with `ɵ`, similar to what we do
17 // with internal functions in Angular packages.
18 ɵescapeMatchingClosingTag: escapeMatchingClosingTag,
19 ɵescapeClosingCommentTag: escapeClosingCommentTag,
20 ɵescapeProcessingInstructionContent: escapeProcessingInstructionContent
21 };
22
23 var utils = require('./utils');
24 var NAMESPACE = utils.NAMESPACE;
25
26 var hasRawContent = {
27 STYLE: true,
28 SCRIPT: true,
29 XMP: true,
30 IFRAME: true,
31 NOEMBED: true,
32 NOFRAMES: true,
33 PLAINTEXT: true
34 };
35
36 var emptyElements = {
37 area: true,
38 base: true,
39 basefont: true,
40 bgsound: true,
41 br: true,
42 col: true,
43 embed: true,
44 frame: true,
45 hr: true,
46 img: true,
47 input: true,
48 keygen: true,
49 link: true,
50 meta: true,
51 param: true,
52 source: true,
53 track: true,
54 wbr: true
55 };
56
57 var extraNewLine = {
58 /* Removed in https://github.com/whatwg/html/issues/944
59 pre: true,
60 textarea: true,
61 listing: true
62 */
63 };
64
65 const ESCAPE_REGEXP = /[&<>\u00A0]/g;
66 const ESCAPE_ATTR_REGEXP = /[&"<>\u00A0]/g;
67
68 function escape(s) {
69 if (!ESCAPE_REGEXP.test(s)) {
70 // nothing to do, fast path
71 return s;
72 }
73
74 return s.replace(ESCAPE_REGEXP, (c) => {
75 switch (c) {
76 case "&":
77 return "&amp;";
78 case "<":
79 return "&lt;";
80 case ">":
81 return "&gt;";
82 case "\u00A0":
83 return "&nbsp;";
84 }
85 });
86 }
87
88 function escapeAttr(s) {
89 if (!ESCAPE_ATTR_REGEXP.test(s)) {
90 // nothing to do, fast path
91 return s;
92 }
93
94 return s.replace(ESCAPE_ATTR_REGEXP, (c) => {
95 switch (c) {
96 case "<":
97 return "&lt;";
98 case ">":
99 return "&gt;";
100 case "&":
101 return "&amp;";
102 case '"':
103 return "&quot;";
104 case "\u00A0":
105 return "&nbsp;";
106 }
107 });
108 }
109
110 function attrname(a) {
111 var ns = a.namespaceURI;
112 if (!ns)
113 return a.localName;
114 if (ns === NAMESPACE.XML)
115 return 'xml:' + a.localName;
116 if (ns === NAMESPACE.XLINK)
117 return 'xlink:' + a.localName;
118
119 if (ns === NAMESPACE.XMLNS) {
120 if (a.localName === 'xmlns') return 'xmlns';
121 else return 'xmlns:' + a.localName;
122 }
123 return a.name;
124 }
125
126 /**
127 * Escapes matching closing tag in a raw text.
128 *
129 * For example, given `<style>#text(</style><script></script>)</style>`,
130 * the parent tag would by "style" and the raw text is
131 * "</style><script></script>". If we come across a matching closing tag
132 * (in out case `</style>`) - replace `<` with `&lt;` to avoid unexpected
133 * and unsafe behavior after de-serialization.
134 */
135 function escapeMatchingClosingTag(rawText, parentTag) {
136 const parentClosingTag = '</' + parentTag;
137 if (!rawText.toLowerCase().includes(parentClosingTag)) {
138 return rawText; // fast path
139 }
140 const result = [...rawText];
141 const matches = rawText.matchAll(new RegExp(parentClosingTag, 'ig'));
142 for (const match of matches) {
143 result[match.index] = '&lt;';
144 }
145 return result.join('');
146 }
147
148 const CLOSING_COMMENT_REGEXP = /--!?>/;
149
150 /**
151 * Escapes closing comment tag in a comment content.
152 *
153 * For example, given `#comment('-->')`, the content of a comment would be
154 * updated to `--&gt;` to avoid unexpected and unsafe behavior after
155 * de-serialization.
156 */
157 function escapeClosingCommentTag(rawContent) {
158 if (!CLOSING_COMMENT_REGEXP.test(rawContent)) {
159 return rawContent; // fast path
160 }
161 return rawContent.replace(/(--\!?)>/g, '$1&gt;');
162 }
163
164 /**
165 * Escapes processing instruction content by replacing `>` with `&gt`.
166 */
167 function escapeProcessingInstructionContent(rawContent) {
168 return rawContent.includes('>')
169 ? rawContent.replaceAll('>', '&gt;')
170 : rawContent;
171 }
172
173 function serializeOne(kid, parent) {
174 var s = '';
175 switch(kid.nodeType) {
176 case 1: //ELEMENT_NODE
177 var ns = kid.namespaceURI;
178 var html = ns === NAMESPACE.HTML;
179 var tagname = (html || ns === NAMESPACE.SVG || ns === NAMESPACE.MATHML) ? kid.localName : kid.tagName;
180
181 s += '<' + tagname;
182
183 for(var j = 0, k = kid._numattrs; j < k; j++) {
184 var a = kid._attr(j);
185 s += ' ' + attrname(a);
186 if (a.value !== undefined) s += '="' + escapeAttr(a.value) + '"';
187 }
188 s += '>';
189
190 if (!(html && emptyElements[tagname])) {
191 var ss = kid.serialize();
192 // If an element can have raw content, this content may
193 // potentially require escaping to avoid XSS.
194 if (hasRawContent[tagname.toUpperCase()]) {
195 ss = escapeMatchingClosingTag(ss, tagname);
196 }
197 if (html && extraNewLine[tagname] && ss.charAt(0)==='\n') s += '\n';
198 // Serialize children and add end tag for all others
199 s += ss;
200 s += '</' + tagname + '>';
201 }
202 break;
203 case 3: //TEXT_NODE
204 case 4: //CDATA_SECTION_NODE
205 var parenttag;
206 if (parent.nodeType === 1 /*ELEMENT_NODE*/ &&
207 parent.namespaceURI === NAMESPACE.HTML)
208 parenttag = parent.tagName;
209 else
210 parenttag = '';
211
212 if (hasRawContent[parenttag] ||
213 (parenttag==='NOSCRIPT' && parent.ownerDocument._scripting_enabled)) {
214 s += kid.data;
215 } else {
216 s += escape(kid.data);
217 }
218 break;
219 case 8: //COMMENT_NODE
220 s += '<!--' + escapeClosingCommentTag(kid.data) + '-->';
221 break;
222 case 7: //PROCESSING_INSTRUCTION_NODE
223 const content = escapeProcessingInstructionContent(kid.data);
224 s += '<?' + kid.target + ' ' + content + '?>';
225 break;
226 case 10: //DOCUMENT_TYPE_NODE
227 s += '<!DOCTYPE ' + kid.name;
228
229 if (false) {
230 // Latest HTML serialization spec omits the public/system ID
231 if (kid.publicID) {
232 s += ' PUBLIC "' + kid.publicId + '"';
233 }
234
235 if (kid.systemId) {
236 s += ' "' + kid.systemId + '"';
237 }
238 }
239
240 s += '>';
241 break;
242 default:
243 utils.InvalidStateError();
244 }
245 return s;
246 }