Skip to content

Reader does not normalize \r\n/\r line endings or literal whitespace in attribute values (XML 1.0 §2.11, §3.3.3) #9

Closed

Description

@trackoor

stax-xml readers and stax-xml/converter return raw \r\n and \r in parsed content and raw literal \t, \n, and \r\n in attribute values instead of applying XML 1.0 end-of-line handling (§2.11) and attribute-value whitespace normalization (§3.3.3).

Reproduction (npm init -y && npm pkg set type=module && npm i stax-xml@1.1.2 && node repro.mjs):

// repro.mjs
import { EventReaderSync } from 'stax-xml';
import { x } from 'stax-xml/converter';

const events = (xml) => [...new EventReaderSync(xml)];

console.log(JSON.stringify({
  textCRLF: events('<a>x\r\ny</a>')[2].value,
  textCR: events('<a>x\ry</a>')[2].value,
  cdataCRLF: events('<a><![CDATA[x\r\ny]]></a>')[2].value,
  attrWhitespace: events('<a b="1\t2\n3\r\n4"/>')[1].attributes.get('b').value,
  attrCharRefs: events('<a b="1&#9;2&#10;3&#13;4"/>')[1].attributes.get('b').value,
  converter: x.object({
    text: x.string().xpath('/a/t'),
    attr: x.string().xpath('/a/@b'),
  }).parseSync('<a b="1\t2\n3\r\n4"><t>x\r\ny</t></a>'),
}, null, 2));

Actual output:

{
  "textCRLF": "x\r\ny",
  "textCR": "x\ry",
  "cdataCRLF": "x\r\ny",
  "attrWhitespace": "1\t2\n3\r\n4",
  "attrCharRefs": "1\t2\n3\r4",
  "converter": {
    "text": "x\r\ny",
    "attr": "1\t2\n3\r\n4"
  }
}

Expected output (\r\n and lone \r normalized to \n before parsing per §2.11, and literal \t, \r, \n in attribute values replaced with ' ' while character references &#9;, &#10;, &#13; are preserved per §3.3.3):

{
  "textCRLF": "x\ny",
  "textCR": "x\ny",
  "cdataCRLF": "x\ny",
  "attrWhitespace": "1 2 3 4",
  "attrCharRefs": "1\t2\n3\r4",
  "converter": {
    "text": "x\ny",
    "attr": "1 2 3 4"
  }
}

In packages/stax-xml/src/core/TokenCursor.ts, text() and attributeValue() slice raw buffer spans and pass them straight to decodeEntities() without normalizing line endings or replacing literal whitespace in attribute values (the same happens for Uint8Array and async EventReader/StreamReader inputs).

Tested on stax-xml@1.1.2 (npm latest) and master at 07cca8a0f53f83a47de4149f84749e4259dc9360 with Node.js v26.13.0.

Credit: https://ucsbplse.github.io/

Activity

  1. Clickin commented on Nov 16, 2026

    @Clickin
    Owner

    Thanks for precise issue. CRLF normalization issue is resolved in #10 . At that PR, I added w3c expected result tests also(previously chekcs only pass or fail).
    Patch version will be published to npm soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions