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

Support namespace prefixes #53

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

nrenner
Copy link

@nrenner nrenner commented Mar 4, 2021

Fixes #52 by using lookupNamespaceURI and supporting changes.

Tested with current Chromium (89) and Firefox (86) on Ubuntu (not on IE or other browsers).

See updated demo that now has the locus: prefix in the XML string also.

Failing tests are in separate commits before fixes, so they can be checked out and seen failing (feel free to squash).

Includes the following changes:

  • Refactoring of duplicate createElement and namespace retrieval code into functions
  • For prefixed elements, the corresponding namespace is searched a) in the attributes of the current value, then b) in the parent element hierarchy by using lookupNamespaceURI.
  • For the lookup to work, the element hierarchy already needs to exist when procesing a value. Therefore appendChild is now done before recursing down (plugging together top down instead of bottom up).
  • Relying on getting xmlns attributes handled automatically with createElementNS doesn't seem to work for all cases. So they are added explicitly now with setAttributeNS and their namespace http://www.w3.org/2000/xmlns/, which is needed for lookup. This way, they can be set anywhere in the parent hierarchy of a prefixed element.
  • Adds attributes first when creating an object tree from XML. This is needed for a round trip stringToJs -> jsToString, as the the namespace lookup needs the xmlns attributes to already exist in the document.

Resources

@nrenner
Copy link
Author

nrenner commented Mar 5, 2021

I also added namespace support for prefixed attributes.

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

Successfully merging this pull request may close these issues.

Namespace prefix removed with jsToString in browsers
1 participant