Skip to content

Commit

Permalink
refactor!: Avoid empty namespace value like xmlns:ds="" (#168)
Browse files Browse the repository at this point in the history
* X3-88886: Signed Facturae XML not validated

* avoid empty namespace url

* avoid empty namespace url

* avoid empty namespace url

* Add test for prefixed xml tag without namespace

Co-authored-by: Franck Depoortere <franck.depoortere@sage.com>
Co-authored-by: Eric Boureau <eric.boureau@sage.com>
Co-authored-by: Christopher J. Brody <chris.brody+brodybits@gmail.com>
  • Loading branch information
4 people committed Mar 10, 2021
1 parent fa67fcf commit 82b0481
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/dom.js
Expand Up @@ -1024,9 +1024,13 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
if (needNamespaceDefine(node,isHTML, visibleNamespaces)) {
var prefix = node.prefix||'';
var uri = node.namespaceURI;
var ns = prefix ? ' xmlns:' + prefix : " xmlns";
buf.push(ns, '="' , uri , '"');
visibleNamespaces.push({ prefix: prefix, namespace:uri });
if (uri) {
// Avoid empty namespace value like xmlns:ds=""
// Empty namespace URL will we produce an invalid XML document
var ns = prefix ? ' xmlns:' + prefix : " xmlns";
buf.push(ns, '="' , uri , '"');
visibleNamespaces.push({ prefix: prefix, namespace:uri });
}
}

if(child || isHTML && !/^(?:meta|link|img|br|hr|input)$/i.test(nodeName)){
Expand Down
14 changes: 14 additions & 0 deletions test/parse/node.test.js
Expand Up @@ -87,6 +87,20 @@ 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 { documentElement } = new DOMParser().parseFromString(source)

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

expect(documentElement.toString()).toStrictEqual(source)
})

it('cdata comment', () => {
const { documentElement } = new DOMParser().parseFromString(
'<xml>start <![CDATA[<encoded>]]> <!-- comment -->end</xml>'
Expand Down

0 comments on commit 82b0481

Please sign in to comment.