Skip to content

Commit

Permalink
Do not apply default namespace if it was declared in an ancestor
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Mar 29, 2020
1 parent ed3014e commit 73272b3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"types": "./lib/index.d.ts",
"dependencies": {
"@oozcitak/util": "8.3.3",
"@oozcitak/dom": "1.15.4",
"@oozcitak/dom": "1.15.5",
"@oozcitak/infra": "1.0.5"
},
"devDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/callback/XMLBuilderCBImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,10 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
this._push(this._writer.openTagBegin(qualifiedName))
} else {
let prefix = node.prefix
let candidatePrefix = map.get(prefix, ns)
let candidatePrefix: string | null = null
if (prefix !== null || ns !== localDefaultNamespace) {
candidatePrefix = map.get(prefix, ns)
}
if (prefix === "xmlns") {
if (this._options.wellFormed) {
this.emit("error", new Error("An element cannot have the 'xmlns' prefix (well-formed required)."))
Expand Down
11 changes: 10 additions & 1 deletion src/writers/BaseWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,16 @@ export abstract class BaseWriter<T extends BaseWriterOptions, U extends XMLSeria
* null if no namespace key ns exists in map.
*/
let prefix = node.prefix
let candidatePrefix = map.get(prefix, ns)
/**
* We don't need to run "retrieving a preferred prefix string" algorithm if
* the element has no prefix and its namespace matches to the default
* namespace.
* See: https://github.com/web-platform-tests/wpt/pull/16703
*/
let candidatePrefix: string | null = null
if (prefix !== null || ns !== localDefaultNamespace) {
candidatePrefix = map.get(prefix, ns)
}
/**
* 12.3. If the value of prefix matches "xmlns", then run the following
* steps:
Expand Down
12 changes: 12 additions & 0 deletions test/basic/namespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,16 @@ describe('namespaces', () => {
expect(() => doc.ele({ 'root@@ns1': {} })).toThrow()
})

test('default namespace does not apply if was declared in an ancestor', () => {
const doc = $$.create()
.ele('root').att('http://www.w3.org/2000/xmlns/', 'xmlns:x', 'uri1')
.ele('uri1', 'table').att('http://www.w3.org/2000/xmlns/', 'xmlns', 'uri1')
.doc().node as any
expect($$.serialize(doc)).toBe(
'<root xmlns:x="uri1">' +
'<table xmlns="uri1"/>' +
'</root>'
)
})

})

0 comments on commit 73272b3

Please sign in to comment.