| 1 |
"use strict"; |
| 2 |
module.exports = { |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
serializeOne: serializeOne, |
| 14 |
|
| 15 |
|
| 16 |
o |
| 17 |
|
| 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 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
|
| 71 |
return s; |
| 72 |
} |
| 73 |
|
| 74 |
return s.replace(ESCAPE_REGEXP, (c) => { |
| 75 |
switch (c) { |
| 76 |
case "&": |
| 77 |
return "&"; |
| 78 |
case "<": |
| 79 |
return "<"; |
| 80 |
case ">": |
| 81 |
return ">"; |
| 82 |
case "\u00A0": |
| 83 |
return " "; |
| 84 |
} |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
function escapeAttr(s) { |
| 89 |
if (!ESCAPE_ATTR_REGEXP.test(s)) { |
| 90 |
|
| 91 |
return s; |
| 92 |
} |
| 93 |
|
| 94 |
return s.replace(ESCAPE_ATTR_REGEXP, (c) => { |
| 95 |
switch (c) { |
| 96 |
case "<": |
| 97 |
return "<"; |
| 98 |
case ">": |
| 99 |
return ">"; |
| 100 |
case "&": |
| 101 |
return "&"; |
| 102 |
case '"': |
| 103 |
return """; |
| 104 |
case "\u00A0": |
| 105 |
return " "; |
| 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 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
function escapeMatchingClosingTag(rawText, parentTag) { |
| 136 |
const parentClosingTag = '</' + parentTag; |
| 137 |
if (!rawText.toLowerCase().includes(parentClosingTag)) { |
| 138 |
return rawText; |
| 139 |
} |
| 140 |
const result = [...rawText]; |
| 141 |
const matches = rawText.matchAll(new RegExp(parentClosingTag, 'ig')); |
| 142 |
for (const match of matches) { |
| 143 |
result[match.index] = '<'; |
| 144 |
} |
| 145 |
return result.join(''); |
| 146 |
} |
| 147 |
|
| 148 |
const CLOSING_COMMENT_REGEXP = /--!?>/; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
function escapeClosingCommentTag(rawContent) { |
| 158 |
if (!CLOSING_COMMENT_REGEXP.test(rawContent)) { |
| 159 |
return rawContent; |
| 160 |
} |
| 161 |
return rawContent.replace(/(--\!?)>/g, '$1>'); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
function escapeProcessingInstructionContent(rawContent) { |
| 168 |
return rawContent.includes('>') |
| 169 |
? rawContent.replaceAll('>', '>') |
| 170 |
: rawContent; |
| 171 |
} |
| 172 |
|
| 173 |
function serializeOne(kid, parent) { |
| 174 |
var s = ''; |
| 175 |
switch(kid.nodeType) { |
| 176 |
case 1: |
| 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 |
|
| 193 |
|
| 194 |
if (hasRawContent[tagname.toUpperCase()]) { |
| 195 |
ss = escapeMatchingClosingTag(ss, tagname); |
| 196 |
} |
| 197 |
if (html && extraNewLine[tagname] && ss.charAt(0)==='\n') s += '\n'; |
| 198 |
|
| 199 |
s += ss; |
| 200 |
s += '</' + tagname + '>'; |
| 201 |
} |
| 202 |
break; |
| 203 |
case 3: |
| 204 |
case 4: |
| 205 |
var parenttag; |
| 206 |
if (parent.nodeType === 1 && |
| 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: |
| 220 |
s += '<!--' + escapeClosingCommentTag(kid.data) + '-->'; |
| 221 |
break; |
| 222 |
case 7: |
| 223 |
const content = escapeProcessingInstructionContent(kid.data); |
| 224 |
s += '<?' + kid.target + ' ' + content + '?>'; |
| 225 |
break; |
| 226 |
case 10: |
| 227 |
s += '<!DOCTYPE ' + kid.name; |
| 228 |
|
| 229 |
if (false) { |
| 230 |
|
| 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 |
} |