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

XmlPrefixName.namespaceUri is null in copied XmlNode #136

Open scribetw opened 2 years ago

scribetw commented 2 years ago

I'm using xml to process SAML responses.

dependencies:
  xml: ^5.3.1
import 'package:test/test.dart';
import 'package:xml/xml.dart';

void main() {
  test('clone namespaceUri', () {
    final xml = '<samlp:Response xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">'
        '<saml:Assertion></saml:Assertion>'
        '</samlp:Response>';
    final doc = XmlDocument.parse(xml);
    final assertion = doc.rootElement.firstElementChild;
    final assertion2 = assertion!.copy();

    expect(assertion.name.namespaceUri, 'urn:oasis:names:tc:SAML:2.0:assertion');
    expect(assertion2.name.namespaceUri, 'urn:oasis:names:tc:SAML:2.0:assertion'); // failed
  });
}

https://github.com/renggli/dart-xml/blob/3c587e18c3b507eb737a2aec864f0bc8413f8aed/lib/src/xml/utils/prefix_name.dart#L18 The resolving of a namespace URI is depending on the ancestors.

Consider storing the namespace URI property directly.

renggli commented 2 years ago

I don't know how other libraries handle this situation?

Resolving the property early might be possible, but it breaks the general mutability of the DOM (or require complicated invalidation logic).

Storing the property on copy might be possible, but changes the DOM in unexpected ways.

scribetw commented 2 years ago

In xmldom (a Node.js library) it is stored while parsing and creating a new element or attribute node. https://github.com/xmldom/xmldom/blob/e31e25d9b0ce79e8545d23771181a79d96b80c73/lib/dom.js#L894

https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-NodeNSname

namespaceURI of type DOMString, readonly, introduced in DOM Level 2 The namespace URI of this node, or null if it is unspecified. This is not a computed value that is the result of a namespace lookup based on an examination of the namespace declarations in scope. It is merely the namespace URI given at creation time.

I think it's safe to store it at creation time.

renggli commented 2 years ago

Interesting: https://www.w3.org/TR/DOM-Level-2-Core/core.html#Namespaces-Considerations. This basically breaks mutability and serialization of the DOM by design :-/