xmldom icon indicating copy to clipboard operation
xmldom copied to clipboard

XMLSerializer: encode newlines in attributes

Open jampy opened this issue 9 years ago • 2 comments

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...

jampy avatar Apr 25 '16 18:04 jampy

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

PieterScheffers avatar Jun 13 '19 09:06 PieterScheffers

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 '&lt;'
      case '>': return '&gt;'
      case '&': return '&amp;'
      case '"': return '&quot;'
      case "'": return '&apos;'
      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
  }
})

PieterScheffers avatar Jun 13 '19 09:06 PieterScheffers