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

fix!: Avoid empty namespace value like xmlns:ds="" #168

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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