xmldom
xmldom copied to clipboard
XMLSerializer: encode newlines in attributes
It appears that xmldom suffers from the bad implementation described here: http://stackoverflow.com/a/2012277
That is, newline and CR characters should be encoded as etc.
I think that's easy to fix by simply adding CR and LF characters to the regex in https://github.com/jindw/xmldom/blob/master/dom.js#L961
PS: xmldom is great! Keep up the good work...
I have created a pull request which adds an extra argument to XMLSerializer.serializeToString. In this extra argument you can pass a function which encodes the entities for TEXT and ATTRIBUTE nodes.
https://github.com/jindw/xmldom/pull/254
Example implementation:
const NodeType = {
ATTRIBUTE_NODE: 2,
TEXT_NODE: 3
}
const xml = xmlSerializer.serializeToString(doc, isHtml, null, (nodeType, value) => {
const xmlEncoder = value => {
switch (value) {
case '<': return '<'
case '>': return '>'
case '&': return '&'
case '"': return '"'
case "'": return '''
default:
return '&#' + value.charCodeAt().toString().padStart(2, '0') + ';'
}
}
switch (nodeType) {
case NodeType.ATTRIBUTE_NODE:
return value.replace(/[\t\n\r'"&<>]/g, xmlEncoder)
case NodeType.TEXT_NODE:
return value.replace(/[&<>]/g, xmlEncoder)
default:
return value
}
})