renggli / dart-xml

Lightweight library for parsing, traversing, and transforming XML in Dart.
http://pub.dartlang.org/packages/xml
MIT License
223 stars 52 forks source link

2.11 End-of-Line Handling #160

Closed scribetw closed 1 year ago

scribetw commented 1 year ago

Hi, I want to report a spec issue here.

Issue

https://www.w3.org/TR/xml/#sec-line-ends

XML parsed entities are often stored in computer files which, for editing convenience, are organized into lines. These lines are typically separated by some combination of the characters CARRIAGE RETURN (#xD) and LINE FEED (#xA).

To simplify the tasks of applications, the XML processor MUST behave as if it normalized all line breaks in external parsed entities (including the document entity) on input, before parsing, by translating both the two-character sequence #xD #xA and any #xD that is not followed by #xA to a single #xA character.

See also: https://github.com/xmldom/xmldom/issues/303

Test case

import 'package:xml/xml.dart';
import 'package:test/test.dart';

void main() {
  test('End-of-Line Handling', () {
    final xml = '<?xml version="1.0"?>\r\n'
      '<aaa>\r\n'
      '  <bbb ccc="ddd"/>\r\n'
      '</aaa>';
    final doc = XmlDocument.parse(xml);
    final textNode = doc.rootElement.firstChild!;
    expect(textNode.nodeType, XmlNodeType.TEXT);
    expect(textNode.text, equals('\n  '));
  });
}

Expect: PASS Actual: FAIL

Workaround

Replace '\r\n' into '\n' before passing to XmlDocument.parse.

renggli commented 1 year ago

This is an interesting issue, but possibly contradicting with the requirement of some users to be able to serialize the parsed XML back to an "identical" byte-string.

scribetw commented 1 year ago

Yes, it could be a breaking feature for the current users. An option to turn it on/off might be feasible. See: https://github.com/xmldom/xmldom/blob/0.8.6/lib/dom-parser.js#L47-L55

renggli commented 1 year ago

I think this can be closed as completed.