Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XmlPrefixName.namespaceUri is null in copied XmlNode #136

Open
scribetw opened this issue Feb 14, 2022 · 3 comments
Open

XmlPrefixName.namespaceUri is null in copied XmlNode #136

scribetw opened this issue Feb 14, 2022 · 3 comments

Comments

@scribetw
Copy link

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
  });
}

String? get namespaceUri => lookupAttribute(parent, xmlns, prefix)?.value;

The resolving of a namespace URI is depending on the ancestors.

Consider storing the namespace URI property directly.

@renggli
Copy link
Owner

renggli commented Feb 14, 2022

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
Copy link
Author

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
Copy link
Owner

renggli commented Feb 14, 2022

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 :-/

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

No branches or pull requests

2 participants