Skip to content

Commit

Permalink
Add tests for exotic boolean attributes (download, spellcheck, transl…
Browse files Browse the repository at this point in the history
…ate), with matching mocks
  • Loading branch information
pygy committed Dec 25, 2022
1 parent 06cb40e commit 600a5b8
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
63 changes: 63 additions & 0 deletions render/tests/test-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,69 @@ o.spec("attributes", function() {
o(d.dom.value).equals("2")
})
})
o.spec("unusual bool attributes", function() {
o.spec("download", function() {
o("a.download created through hyperscript as a boolean attr", function() {
var a = m('a[href=#][download]')
render(root, a)

o(a.dom.getAttribute("download")).equals("")
})
})

o("spellcheck", function(){
var div = m("[spellcheck=false]")
render(root, div)

o(div.dom.spellcheck).equals(false)

div = m("[spellcheck]")
render(root, div)

o(div.dom.spellcheck).equals(true)

div = m("[spellcheck=true]")
render(root, div)

o(div.dom.spellcheck).equals(true)

div = m("div", {spellcheck: true})
render(root, div)

o(div.dom.spellcheck).equals(true)

div = m("div", {spellcheck: false})
render(root, div)

o(div.dom.spellcheck).equals(false)
})
o("translate", function(){
var div = m("[translate=no]")
render(root, div)

o(div.dom.translate).equals(false)

div = m("[translate]")
render(root, div)

o(div.dom.translate).equals(true)

div = m("[translate=yes]")
render(root, div)

o(div.dom.translate).equals(true)

div = m("div", {translate: true})
render(root, div)

o(div.dom.translate).equals(true)

div = m("div", {translate: false})
render(root, div)

o(div.dom.translate).equals(false)
})
})
o.spec("contenteditable throws on untrusted children", function() {
o("including elements", function() {
var div = m("div", {contenteditable: true}, m("script", {src: "http://evil.com"}))
Expand Down
21 changes: 21 additions & 0 deletions test-utils/domMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,27 @@ module.exports = function(options) {
get className() {
return this.attributes["class"] ? this.attributes["class"].value : ""
},
get download() {
return this.hasAttribute("download") ? this.getAttribute("download") : ""
},
set download(value) {
/*eslint-disable-next-line no-implicit-coercion*/
this.setAttribute("download", ""+value)
},
get spellcheck() {
return this.getAttribute("spellcheck") !== "false"
},
set spellcheck(value) {
// we can rely on implicit bool conversion
this.setAttribute("spellcheck", value ? "true" : "false")
},
get translate() {
return this.getAttribute("translate") !== "no"
},
set translate(value) {
// we can rely on implicit bool conversion
this.setAttribute("translate", value ? "yes" : "no")
},
set className(value) {
if (this.namespaceURI === "http://www.w3.org/2000/svg") throw new Error("Cannot set property className of SVGElement")
else this.setAttribute("class", value)
Expand Down
112 changes: 112 additions & 0 deletions test-utils/tests/test-domMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,119 @@ o.spec("domMock", function() {

o(div.style.background).equals("red")
o(div.style.cssText).equals("background: red;")
})
})
o.spec("odd exotic bool attributes", function() {
o("download when not specified as attribute beforehand", function() {
var a = $document.createElement("a")
a.setAttribute("href", "#")

o(a.hasAttribute("download")).equals(false)
o(a.download).equals("")

a.download = ""

o(a.hasAttribute("download")).equals(true)
o(a.getAttribute("download")).equals("")
o(a.download).equals("")

a.download = false

o(a.getAttribute("download")).equals("false")
})
o("download when specified as boolean attribute beforehand", function() {
var a = $document.createElement("a")
a.setAttribute("href", "#")
a.setAttribute("download", "")

o(a.hasAttribute("download")).equals(true)
o(a.download).equals("")
})
o("download when specified as string attribute beforehand", function() {
var a = $document.createElement("a")
a.setAttribute("href", "#")
a.setAttribute("download", "file-name.extension")

o(a.hasAttribute("download")).equals(true)
o(a.download).equals("file-name.extension")
})
o("spellcheck when not set as attribute", function() {
var div = $document.createElement("div")

o(div.spellcheck).equals(true)

div.spellcheck = false

o(div.getAttribute("spellcheck")).equals("false")

div.spellcheck = ""

o(div.getAttribute("spellcheck")).equals("false")

div.spellcheck = "false"

o(div.getAttribute("spellcheck")).equals("true")

div.spellcheck = true

o(div.getAttribute("spellcheck")).equals("true")
})
o("spellcheck when set as boolean attribute", function() {
var div = $document.createElement("div")
div.setAttribute("spellcheck", "")

o(div.spellcheck).equals(true)
})
o("spellcheck when set to 'true' as attribute", function() {
var div = $document.createElement("div")
div.setAttribute("spellcheck", "true")

o(div.spellcheck).equals(true)
})
o("spellcheck when set to 'false' as attribute", function() {
var div = $document.createElement("div")
div.setAttribute("spellcheck", "false")

o(div.spellcheck).equals(false)
})
o("translate when not set as attribute", function() {
var div = $document.createElement("div")

o(div.translate).equals(true)

div.translate = false

o(div.getAttribute("translate")).equals("no")

div.translate = ""

o(div.getAttribute("translate")).equals("no")

div.translate = "false"

o(div.getAttribute("translate")).equals("yes")

div.translate = true

o(div.getAttribute("translate")).equals("yes")
})
o("translate when set as boolean attribute", function() {
var div = $document.createElement("div")
div.setAttribute("translate", "")

o(div.translate).equals(true)
})
o("translate when set to 'true' as attribute", function() {
var div = $document.createElement("div")
div.setAttribute("translate", "yes")

o(div.translate).equals(true)
})
o("translate when set to 'false' as attribute", function() {
var div = $document.createElement("div")
div.setAttribute("translate", "no")

o(div.translate).equals(false)
})
})
o.spec("events", function() {
Expand Down

0 comments on commit 600a5b8

Please sign in to comment.