File size: 866 Bytes
eefe2da | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// String(val)/val.toString() drop the sign of -0 (e.g. String(-0) === '0'), silently
// corrupting a round-tripped negative-zero value. XML has no separate int/float syntax,
// so this is the single place every raw value gets turned into text.
export function valToStr(val) {
return typeof val === 'number' && Object.is(val, -0) ? '-0' : String(val)
}
export function safeComment(val) {
return valToStr(val)
.replace(/--/g, '- -') // -- is illegal anywhere in comment content
.replace(/--/g, '- -') // handle the scenario when 2 consiucative dashes appears
.replace(/-$/, '- '); // trailing - would form -- with the closing -->
}
export function safeCdata(val) {
return valToStr(val).replace(/\]\]>/g, ']]]]><![CDATA[>')
}
export function escapeAttribute(val) {
return valToStr(val).replace(/"/g, '"').replace(/'/g, ''')
} |