Permalink
Browse files

correct namespaced serialization to respect case of tag names

  • Loading branch information...
Matt-Esch committed Apr 7, 2014
1 parent 97b6542 commit a4df2c96fe3751bcc50d1e351a5e73a3b215fb7d
Showing with 26 additions and 9 deletions.
  1. +1 −1 dom-element.js
  2. +8 −2 serialize.js
  3. +17 −6 test/test-document.js
View
@@ -11,7 +11,7 @@ function DOMElement(tagName, owner, namespace) {
return new DOMElement(tagName)
}
- var ns = namespace === undefined ? htmlns : namespace || null
+ var ns = namespace === undefined ? htmlns : (namespace || null)
this.tagName = ns === htmlns ? String(tagName).toUpperCase() : tagName
this.className = ""
View
@@ -3,7 +3,13 @@ module.exports = serializeElement
function serializeElement(elem) {
var strings = []
- strings.push("<" + elem.tagName.toLowerCase() +
+ var tagname = elem.tagName
+
+ if (elem.namespaceURI === "http://www.w3.org/1999/xhtml") {
+ tagname = tagname.toLowerCase()
+ }
+
+ strings.push("<" + tagname +
properties(elem) + datasetify(elem) + ">")
if (elem.textContent) {
@@ -14,7 +20,7 @@ function serializeElement(elem) {
strings.push(node.toString())
})
- strings.push("</" + elem.tagName.toLowerCase() + ">")
+ strings.push("</" + tagname + ">")
return strings.join("")
}
View
@@ -125,28 +125,39 @@ function testDocument(document) {
var noNS = document.createElement("div")
var svgNS = document.createElementNS(svgURI, "svg")
- var emptyStringNS = document.createElementNS("", "div")
+ var emptyNS = document.createElementNS("", "div")
var nullNS = document.createElementNS(null, "div")
- var undefinedNS = document.createElementNS(undefined, "div")
+ var undefNS = document.createElementNS(undefined, "div")
+ var caseNS = document.createElementNS("Oops", "AbC")
var htmlNS = document.createElement("div")
assert.equal(noNS.tagName, "DIV")
assert.equal(noNS.namespaceURI, htmlURI)
+ assert.equal(String(noNS.outerHTML || noNS), "<div></div>")
assert.equal(svgNS.tagName, "svg")
assert.equal(svgNS.namespaceURI, svgURI)
+ assert.equal(String(svgNS.outerHTML || svgNS), "<svg></svg>")
- assert.equal(emptyStringNS.tagName, "div")
- assert.equal(emptyStringNS.namespaceURI, null)
+ assert.equal(emptyNS.tagName, "div")
+ assert.equal(emptyNS.namespaceURI, null)
+ assert.equal(String(emptyNS.outerHTML || emptyNS), "<div></div>")
assert.equal(nullNS.tagName, "div")
assert.equal(nullNS.namespaceURI, null)
+ assert.equal(String(nullNS.outerHTML || nullNS), "<div></div>")
- assert.equal(undefinedNS.tagName, "div")
- assert.equal(undefinedNS.namespaceURI, "undefined")
+ assert.equal(undefNS.tagName, "div")
+ assert.equal(undefNS.namespaceURI, "undefined")
+ assert.equal(String(undefNS.outerHTML || undefNS), "<div></div>")
+
+ assert.equal(caseNS.tagName, "AbC")
+ assert.equal(caseNS.namespaceURI, "Oops")
+ assert.equal(String(caseNS.outerHTML || caseNS), "<AbC></AbC>")
assert.equal(htmlNS.tagName, "DIV")
assert.equal(htmlNS.namespaceURI, htmlURI)
+ assert.equal(String(htmlNS.outerHTML || undefNS), "<div></div>")
assert.end()
})

0 comments on commit a4df2c9

Please sign in to comment.