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

Serializing DOM that uses undefined namespace prefixes in attributes produces invalid XML #243

Closed
pdecat opened this issue Jun 4, 2021 · 2 comments · Fixed by #244
Closed
Assignees
Labels
bug Something isn't working spec:Namespaces in XML https://www.w3.org/TR/REC-xml-names/ and https://www.w3.org/TR/xml-names11/
Milestone

Comments

@pdecat
Copy link
Contributor

pdecat commented Jun 4, 2021

This is a continuation of #168 which fixed the issue in some cases but not all as revealed in
#168 (comment)

# git diff
diff --git a/test/parse/node.test.js b/test/parse/node.test.js
index 6c10dc8..acde64e 100644
--- a/test/parse/node.test.js
+++ b/test/parse/node.test.js
@@ -88,13 +88,13 @@ describe('XML Node Parse', () => {
        })

        it('prefixed without empty namespace', () => {
-               const source = '<w:p><w:r>test1</w:r><w:r>test2</w:r></w:p>'
+               const source = '<w:p><w:r><w:rPr><w:rFonts w:ascii=\"FreeSans\" w:hAnsi=\"FreeSans\"/><w:color w:val=\"FF0000\"/></w:rPr><w:t>test1</w:t></w:r><w:r><w:t>test2</w:t></w:r></w:p>'
                const { documentElement } = new DOMParser().parseFromString(source)

-               expect(documentElement.firstChild.firstChild).toMatchObject({
+               expect(documentElement.firstChild.lastChild.firstChild).toMatchObject({
                        nodeValue: 'test1',
                })
-               expect(documentElement.lastChild.firstChild).toMatchObject({
+               expect(documentElement.lastChild.firstChild.firstChild).toMatchObject({
                        nodeValue: 'test2',
                })

# ./node_modules/jest/bin/jest.js -t "prefixed without empty namespace"
 FAIL  test/parse/node.test.js
  ● XML Node Parse › prefixed without empty namespace

    expect(received).toStrictEqual(expected) // deep equality

    Expected: "<w:p><w:r><w:rPr><w:rFonts w:ascii=\"FreeSans\" w:hAnsi=\"FreeSans\"/><w:color w:val=\"FF0000\"/></w:rPr><w:t>test1</w:t></w:r><w:r><w:t>test2</w:t></w:r></w:p>"
    Received: "<w:p><w:r><w:rPr><w:rFonts xmlns:w=\"\" w:ascii=\"FreeSans\" w:hAnsi=\"FreeSans\"/><w:color w:val=\"FF0000\"/></w:rPr><w:t>test1</w:t></w:r><w:r><w:t>test2</w:t></w:r></w:p>"

       99 |             })
      100 |
    > 101 |             expect(documentElement.toString()).toStrictEqual(source)
          |                                                ^
      102 |     })
      103 |
      104 |     it('cdata comment', () => {

      at Object.<anonymous> (test/parse/node.test.js:101:38)


Summary of all failing tests
 FAIL  test/parse/node.test.js
  ● XML Node Parse › prefixed without empty namespace

    expect(received).toStrictEqual(expected) // deep equality

    Expected: "<w:p><w:r><w:rPr><w:rFonts w:ascii=\"FreeSans\" w:hAnsi=\"FreeSans\"/><w:color w:val=\"FF0000\"/></w:rPr><w:t>test1</w:t></w:r><w:r><w:t>test2</w:t></w:r></w:p>"
    Received: "<w:p><w:r><w:rPr><w:rFonts xmlns:w=\"\" w:ascii=\"FreeSans\" w:hAnsi=\"FreeSans\"/><w:color w:val=\"FF0000\"/></w:rPr><w:t>test1</w:t></w:r><w:r><w:t>test2</w:t></w:r></w:p>"

       99 |             })
      100 |
    > 101 |             expect(documentElement.toString()).toStrictEqual(source)
          |                                                ^
      102 |     })
      103 |
      104 |     it('cdata comment', () => {

      at Object.<anonymous> (test/parse/node.test.js:101:38)


Test Suites: 1 failed, 28 skipped, 1 of 29 total
Tests:       1 failed, 573 skipped, 574 total
Snapshots:   0 total
Time:        2.227 s, estimated 3 s
Ran all test suites with tests matching "prefixed without empty namespace".
@pdecat pdecat changed the title DOMParser adds DOMParser adds empty namespace Jun 4, 2021
@karfau
Copy link
Member

karfau commented Jun 4, 2021

Thank you for reporting.
From the test output you provided, it looks like the empty namespace is still added when there are any attributes with that namespace involved, so most likely we can reduce the complexity of the example.
I'll have a look into it.

@karfau karfau self-assigned this Jun 4, 2021
@karfau karfau added this to the next release milestone Jun 4, 2021
@karfau karfau added bug Something isn't working spec:Namespaces in XML https://www.w3.org/TR/REC-xml-names/ and https://www.w3.org/TR/xml-names11/ labels Jun 4, 2021
@karfau karfau changed the title DOMParser adds empty namespace Serializing DOM that uses undefined namespace prefixes in attributes produces invalid XML Jun 4, 2021
@karfau
Copy link
Member

karfau commented Jul 26, 2021

See this comment for a reason why this might be a breaking change: #168 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working spec:Namespaces in XML https://www.w3.org/TR/REC-xml-names/ and https://www.w3.org/TR/xml-names11/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants