[domMock] Better accuracy for namespaced elements

This commit is contained in:
Pierre-Yves Gérardy 2018-05-31 16:52:42 +02:00 committed by Pierre-Yves Gérardy
parent 5b51b682ee
commit b39f4f683d
2 changed files with 57 additions and 12 deletions

View file

@ -18,6 +18,9 @@ module.exports = function(options) {
options = options || {}
var spy = options.spy || function(f){return f}
var spymap = []
var hasOwn = ({}.hasOwnProperty)
function registerSpies(element, spies) {
if(options.spy) {
var i = spymap.indexOf(element)
@ -127,9 +130,8 @@ module.exports = function(options) {
// this is the correct kind of conversion, passing a Symbol throws in browsers too.
var nodeValue = "" + value
/*eslint-enable no-implicit-coercion*/
this.attributes[name] = {
namespaceURI: null,
namespaceURI: hasOwn.call(this.attributes, name) ? this.attributes[name].namespaceURI : null,
get value() {return nodeValue},
set value(value) {
/*eslint-disable no-implicit-coercion*/
@ -404,8 +406,16 @@ module.exports = function(options) {
if (element.nodeName === "A") {
Object.defineProperty(element, "href", {
get: function() {return this.attributes["href"] === undefined ? "" : "[FIXME implement]"},
set: function(value) {this.setAttribute("href", value)},
get: function() {
if (this.namespaceURI === "http://www.w3.org/2000/svg") {
var val = this.hasAttribute("href") ? this.attributes.href.value : ""
return {baseVal: val, animVal: val}
} else return this.attributes["href"] === undefined ? "" : "[FIXME implement]"
},
set: function(value) {
// This is a readonly attribute for SVG, todo investigate MathML which may have yet another IDL
if (this.namespaceURI !== "http://www.w3.org/2000/svg") this.setAttribute("href", value)
},
enumerable: true,
})
}

View file

@ -364,6 +364,12 @@ o.spec("domMock", function() {
o(div.getAttribute("id")).equals("aaa")
})
o("works for attributes with a namespace", function() {
var div = $document.createElement("div")
div.setAttributeNS("http://www.w3.org/1999/xlink", "href", "aaa")
o(div.getAttribute("href")).equals("aaa")
})
})
o.spec("setAttribute", function() {
@ -429,18 +435,40 @@ o.spec("domMock", function() {
o.spec("setAttributeNS", function() {
o("works", function() {
var div = $document.createElement("div")
div.setAttributeNS("http://www.w3.org/1999/xlink", "href", "aaa")
var a = $document.createElementNS("http://www.w3.org/2000/svg", "a")
a.setAttributeNS("http://www.w3.org/1999/xlink", "href", "/aaa")
o(div.attributes["href"].value).equals("aaa")
o(div.attributes["href"].namespaceURI).equals("http://www.w3.org/1999/xlink")
o(a.href).deepEquals({baseVal: "/aaa", animVal: "/aaa"})
o(a.attributes["href"].value).equals("/aaa")
o(a.attributes["href"].namespaceURI).equals("http://www.w3.org/1999/xlink")
})
o("works w/ number", function() {
var div = $document.createElement("div")
div.setAttributeNS("http://www.w3.org/1999/xlink", "href", 123)
var a = $document.createElementNS("http://www.w3.org/2000/svg", "a")
a.setAttributeNS("http://www.w3.org/1999/xlink", "href", 123)
o(div.attributes["href"].value).equals("123")
o(div.attributes["href"].namespaceURI).equals("http://www.w3.org/1999/xlink")
o(a.href).deepEquals({baseVal: "123", animVal: "123"})
o(a.attributes["href"].value).equals("123")
o(a.attributes["href"].namespaceURI).equals("http://www.w3.org/1999/xlink")
})
o("attributes with a namespace can be querried, updated and removed with non-NS functions", function() {
var a = $document.createElementNS("http://www.w3.org/2000/svg", "a")
a.setAttributeNS("http://www.w3.org/1999/xlink", "href", "/aaa")
o(a.hasAttribute("href")).equals(true)
o(a.getAttribute("href")).equals("/aaa")
a.setAttribute("href", "/bbb")
o(a.href).deepEquals({baseVal: "/bbb", animVal: "/bbb"})
o(a.getAttribute("href")).equals("/bbb")
o(a.attributes["href"].value).equals("/bbb")
o(a.attributes["href"].namespaceURI).equals("http://www.w3.org/1999/xlink")
a.removeAttribute("href")
o(a.hasAttribute("href")).equals(false)
o(a.getAttribute("href")).equals(null)
o("href" in a.attributes).equals(false)
})
})
@ -1266,6 +1294,13 @@ o.spec("domMock", function() {
o(a.href).notEquals("")
o(a.attributes["href"].value).equals("")
})
o("property is read-only for SVG elements", function() {
var a = $document.createElementNS("http://www.w3.org/2000/svg", "a")
a.href = "/foo"
o(a.href).deepEquals({baseVal: "", animVal: ""})
o("href" in a.attributes).equals(false)
})
})
o.spec("input[checked]", function() {
o("only exists in input elements", function() {