https://github.com/web-platform-tests/wpt
Tip revision: 5de28d46eab8eb2f610a77aca0bb004e872a3e3d authored by Hallvord R. M. Steen on 28 February 2014, 09:55:18 UTC
dropping superfluous variable declaration
dropping superfluous variable declaration
Tip revision: 5de28d4
attributes.html
<!DOCTYPE html>
<meta charset=utf-8>
<title>Attributes tests</title>
<link rel=help href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#attr">
<link rel=help href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-element-setattribute">
<link rel=help href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-element-setattributens">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="attributes.js"></script>
<script src="productions.js"></script>
<div id="log"></div>
<span id="test1"></span>
<span class="&<>foo"></span>
<script>
var XML = "http://www.w3.org/XML/1998/namespace"
var XMLNS = "http://www.w3.org/2000/xmlns/"
// AttrExodus
test(function() {
document.body.setAttribute("abc", "pass")
var attr = document.body.attributes[0]
assert_true(attr instanceof Attr)
assert_false(attr instanceof Node)
assert_throws(new TypeError(), function() { attr.appendChild(document.createTextNode("fail")) })
assert_throws(new TypeError(), function() { attr.appendChild(null) })
assert_equals(attr.value, "pass")
assert_false("childNodes" in attr)
}, "AttrExodus")
// setAttribute exhaustive tests
// Step 1
test(function() {
var el = document.createElement("foo")
for (var i = 0; i < invalid_names.length; i++) {
assert_throws("INVALID_CHARACTER_ERR", function() { el.setAttribute(invalid_names[i], "test") })
}
}, "When qualifiedName does not match the Name production, an " +
"INVALID_CHARACTER_ERR exception is to be thrown. (setAttribute)")
// Step 2
test(function() {
var el = document.createElement("div")
el.setAttribute("ALIGN", "left")
assert_equals(el.getAttributeNS("", "ALIGN"), null)
assert_equals(el.getAttributeNS("", "align"), "left")
assert_equals(el.getAttribute("align"), "left")
}, "setAttribute should lowercase its name argument (upper case attribute)")
test(function() {
var el = document.createElement("div")
el.setAttribute("CHEEseCaKe", "tasty")
assert_equals(el.getAttributeNS("", "CHEEseCaKe"), null)
assert_equals(el.getAttributeNS("", "cheesecake"), "tasty")
assert_equals(el.getAttribute("cheesecake"), "tasty")
}, "setAttribute should lowercase its name argument (mixed case attribute)")
// Step 3
test(function() {
var el = document.createElement("foo")
var tests = ["xmlns", "xmlns:a", "xmlnsx", "xmlns0"]
for (var i = 0; i < tests.length; i++) {
assert_throws("NAMESPACE_ERR",
function() { el.setAttribute(tests[i], "fail") },
"Expected exception for " + tests[i] + ".")
}
}, "setAttribute should throw a NAMESPACE_ERR when qualifiedName starts with 'xmlns'")
// Step 4
test(function() {
var el = document.createElement("foo")
for (var i = 0; i < valid_names.length; i++) {
el.setAttribute(valid_names[i], "test")
assert_equals(el.getAttribute(valid_names[i]), "test")
}
}, "Basic functionality should be intact.")
// Step 5
test(function() {
var el = document.createElement("foo")
el.setAttribute("a", "1")
el.setAttribute("b", "2")
el.setAttribute("a", "3")
el.setAttribute("c", "4")
attributes_are(el, [["a", "3"],
["b", "2"],
["c", "4"]])
}, "setAttribute should not change the order of previously set attributes.")
test(function() {
var el = document.createElement("baz")
el.setAttributeNS("ab", "attr", "fail")
el.setAttributeNS("kl", "attr", "pass")
el.setAttribute("attr", "pass")
attributes_are(el, [["attr", "pass", "ab"],
["attr", "pass", "kl"]])
}, "setAttribute should set the first attribute with the given name")
test(function() {
// Based on a test by David Flanagan.
var el = document.createElement("baz")
el.setAttributeNS("foo", "foo:bar", "1");
assert_equals(el.getAttribute("foo:bar"), "1")
attr_is(el.attributes[0], "1", "bar", "foo", "foo", "foo:bar")
el.setAttribute("foo:bar", "2");
assert_equals(el.getAttribute("foo:bar"), "2")
attr_is(el.attributes[0], "2", "bar", "foo", "foo", "foo:bar")
}, "setAttribute should set the attribute with the given qualified name")
// setAttributeNS exhaustive tests
// Step 1
test(function() {
var el = document.createElement("foo")
for (var i = 0, il = invalid_names.length; i < il; ++i) {
assert_throws("INVALID_CHARACTER_ERR",
function() { el.setAttributeNS("a", invalid_names[i], "fail") })
}
}, "When qualifiedName does not match the Name production, an " +
"INVALID_CHARACTER_ERR exception is to be thrown. (setAttributeNS)")
// Step 2
test(function() {
var el = document.createElement("foo")
for (var i = 0, il = invalid_qnames.length; i < il; ++i) {
assert_throws("NAMESPACE_ERR",
function() { el.setAttributeNS("a", invalid_qnames[i], "fail") },
"Expected exception for " + invalid_qnames[i] + ".")
}
}, "When qualifiedName does not match the QName production, an " +
"NAMESPACE_ERR exception is to be thrown.")
// Step 3
test(function() {
var el = document.createElement("foo")
el.setAttributeNS(null, "aa", "bb")
el.setAttributeNS("", "xx", "bb")
attributes_are(el, [["aa", "bb"],
["xx", "bb"]])
}, "null and the empty string should result in a null namespace.")
// Step 4
test(function() {
var el = document.createElement("foo")
assert_throws("NAMESPACE_ERR",
function() { el.setAttributeNS("", "aa:bb", "fail") })
assert_throws("NAMESPACE_ERR",
function() { el.setAttributeNS(null, "aa:bb", "fail") })
}, "A namespace is required to use a prefix.")
// Step 5
test(function() {
var el = document.createElement("foo")
assert_throws("NAMESPACE_ERR",
function() { el.setAttributeNS("a", "xml:bb", "fail") })
}, "The xml prefix should not be allowed for arbitrary namespaces")
test(function() {
var el = document.createElement("foo")
el.setAttributeNS(XML, "a:bb", "pass")
assert_equals(el.attributes.length, 1)
attr_is(el.attributes[0], "pass", "bb", XML, "a", "a:bb")
}, "XML-namespaced attributes don't need an xml prefix")
// Step 6
test(function() {
var el = document.createElement("foo")
assert_throws("NAMESPACE_ERR",
function() { el.setAttributeNS("a", "xmlns:bb", "fail") })
}, "The xmlns prefix should not be allowed for arbitrary namespaces")
test(function() {
var el = document.createElement("foo")
assert_throws("NAMESPACE_ERR",
function() { el.setAttributeNS("a", "xmlns", "fail") })
}, "The xmlns qualified name should not be allowed for arbitrary namespaces")
test(function() {
var el = document.createElement("foo")
el.setAttributeNS("ns", "a:xmlns", "pass")
assert_equals(el.attributes.length, 1)
attr_is(el.attributes[0], "pass", "xmlns", "ns", "a", "a:xmlns")
}, "xmlns should be allowed as local name")
// Step 7
test(function() {
var el = document.createElement("foo")
assert_throws("NAMESPACE_ERR",
function() { el.setAttributeNS(XMLNS, "a:xmlns", "fail") })
assert_throws("NAMESPACE_ERR",
function() { el.setAttributeNS(XMLNS, "b:foo", "fail") })
}, "The XMLNS namespace should require xmlns as prefix or qualified name")
test(function() {
var el = document.createElement("foo")
el.setAttributeNS(XMLNS, "xmlns:a", "pass")
assert_equals(el.attributes.length, 1)
attr_is(el.attributes[0], "pass", "a", XMLNS, "xmlns", "xmlns:a")
}, "xmlns should be allowed as prefix in the XMLNS namespace")
test(function() {
var el = document.createElement("foo")
el.setAttributeNS(XMLNS, "xmlns", "pass")
assert_equals(el.attributes.length, 1)
attr_is(el.attributes[0], "pass", "xmlns", XMLNS, null, "xmlns")
}, "xmlns should be allowed as qualified name in the XMLNS namespace")
// Step 8-9
test(function() {
var el = document.createElement("foo")
el.setAttributeNS("a", "foo:bar", "X")
assert_equals(el.attributes.length, 1)
attr_is(el.attributes[0], "X", "bar", "a", "foo", "foo:bar")
el.setAttributeNS("a", "quux:bar", "Y")
assert_equals(el.attributes.length, 1)
attr_is(el.attributes[0], "Y", "bar", "a", "foo", "foo:bar")
el.removeAttributeNS("a", "bar")
}, "Setting the same attribute with another prefix should not change the prefix")
// Miscellaneous tests
test(function() {
var el = document.createElement("iframe")
el.setAttribute("src", "file:///home")
assert_equals(el.getAttribute("src"), "file:///home")
}, "setAttribute should not throw even if a load is not allowed")
test(function() {
var docFragment = document.createDocumentFragment()
var newOne = document.createElement("newElement")
newOne.setAttribute("newdomestic", "Yes")
docFragment.appendChild(newOne)
var domesticNode = docFragment.firstChild
var attr = domesticNode.attributes.item(0)
attr_is(attr, "Yes", "newdomestic", null, null, "newdomestic")
}, "Attributes should work in document fragments.")
test(function() {
var el = document.createElement("foo")
el.setAttribute("x", "y")
var attr = el.attributes[0]
attr.value = "Y<"
attr_is(attr, "Y<", "x", null, null, "x")
assert_equals(el.getAttribute("x"), "Y<")
}, "Attribute values should not be parsed.")
test(function() {
var el = document.getElementsByTagName("span")[0]
attr_is(el.attributes[0], "test1", "id", null, null, "id")
}, "Specified attributes should be accessible.")
test(function() {
var el = document.getElementsByTagName("span")[1]
attr_is(el.attributes[0], "&<>foo", "class", null, null, "class")
}, "Entities in attributes should have been expanded while parsing.")
test(function() {
var el = document.createElement("div")
assert_equals(el.hasAttribute("bar"), false)
assert_equals(el.hasAttributeNS(null, "bar"), false)
assert_equals(el.hasAttributeNS("", "bar"), false)
assert_equals(el.getAttribute("bar"), null)
assert_equals(el.getAttributeNS(null, "bar"), null)
assert_equals(el.getAttributeNS("", "bar"), null)
}, "Unset attributes return null")
test(function() {
var el = document.createElement("div")
el.setAttributeNS("ab", "attr", "t1")
el.setAttributeNS("kl", "attr", "t2")
assert_equals(el.hasAttribute("attr"), true)
assert_equals(el.hasAttributeNS("ab", "attr"), true)
assert_equals(el.hasAttributeNS("kl", "attr"), true)
assert_equals(el.getAttribute("attr"), "t2")
assert_equals(el.getAttributeNS("ab", "attr"), "t1")
assert_equals(el.getAttributeNS("kl", "attr"), "t2")
assert_equals(el.getAttributeNS(null, "attr"), null)
assert_equals(el.getAttributeNS("", "attr"), null)
}, "First set attribute is returned by setAttribute")
test(function() {
var el = document.createElement("div")
el.setAttribute("style", "color:#fff;")
assert_equals(el.hasAttribute("style"), true)
assert_equals(el.hasAttributeNS(null, "style"), true)
assert_equals(el.hasAttributeNS("", "style"), true)
assert_equals(el.getAttribute("style"), "color:#fff;")
assert_equals(el.getAttributeNS(null, "style"), "color:#fff;")
assert_equals(el.getAttributeNS("", "style"), "color:#fff;")
}, "Style attributes are not normalized")
test(function() {
var el = document.createElement("div")
el.setAttributeNS("", "ALIGN", "left")
assert_equals(el.hasAttribute("ALIGN"), false)
assert_equals(el.hasAttribute("align"), false)
assert_equals(el.hasAttributeNS(null, "ALIGN"), true)
assert_equals(el.hasAttributeNS(null, "align"), false)
assert_equals(el.hasAttributeNS("", "ALIGN"), true)
assert_equals(el.hasAttributeNS("", "align"), false)
assert_equals(el.getAttribute("ALIGN"), null)
assert_equals(el.getAttribute("align"), null)
assert_equals(el.getAttributeNS(null, "ALIGN"), "left")
assert_equals(el.getAttributeNS("", "ALIGN"), "left")
assert_equals(el.getAttributeNS(null, "align"), null)
assert_equals(el.getAttributeNS("", "align"), null)
el.removeAttributeNS("", "ALIGN")
}, "Only lowercase attributes are returned on HTML elements (upper case attribute)")
test(function() {
var el = document.createElement("div")
el.setAttributeNS("", "CHEEseCaKe", "tasty")
assert_equals(el.hasAttribute("CHEESECAKE"), false)
assert_equals(el.hasAttribute("CHEEseCaKe"), false)
assert_equals(el.hasAttribute("cheesecake"), false)
assert_equals(el.hasAttributeNS("", "CHEESECAKE"), false)
assert_equals(el.hasAttributeNS("", "CHEEseCaKe"), true)
assert_equals(el.hasAttributeNS("", "cheesecake"), false)
assert_equals(el.hasAttributeNS(null, "CHEESECAKE"), false)
assert_equals(el.hasAttributeNS(null, "CHEEseCaKe"), true)
assert_equals(el.hasAttributeNS(null, "cheesecake"), false)
assert_equals(el.getAttribute("CHEESECAKE"), null)
assert_equals(el.getAttribute("CHEEseCaKe"), null)
assert_equals(el.getAttribute("cheesecake"), null)
assert_equals(el.getAttributeNS(null, "CHEESECAKE"), null)
assert_equals(el.getAttributeNS("", "CHEESECAKE"), null)
assert_equals(el.getAttributeNS(null, "CHEEseCaKe"), "tasty")
assert_equals(el.getAttributeNS("", "CHEEseCaKe"), "tasty")
assert_equals(el.getAttributeNS(null, "cheesecake"), null)
assert_equals(el.getAttributeNS("", "cheesecake"), null)
el.removeAttributeNS("", "CHEEseCaKe")
}, "Only lowercase attributes are returned on HTML elements (mixed case attribute)")
test(function() {
var el = document.createElement("div")
document.body.appendChild(el)
el.setAttributeNS("", "align", "left")
el.setAttributeNS("xx", "align", "right")
el.setAttributeNS("", "foo", "left")
el.setAttributeNS("xx", "foo", "right")
assert_equals(el.hasAttribute("align"), true)
assert_equals(el.hasAttribute("foo"), true)
assert_equals(el.hasAttributeNS("xx", "align"), true)
assert_equals(el.hasAttributeNS(null, "foo"), true)
assert_equals(el.getAttribute("align"), "left")
assert_equals(el.getAttribute("foo"), "left")
assert_equals(el.getAttributeNS("xx", "align"), "right")
assert_equals(el.getAttributeNS(null, "foo"), "left")
assert_equals(el.getAttributeNS("", "foo"), "left")
el.removeAttributeNS("", "align")
el.removeAttributeNS("xx", "align")
el.removeAttributeNS("", "foo")
el.removeAttributeNS("xx", "foo")
document.body.removeChild(el)
}, "First set attribute is returned with mapped attribute set first")
test(function() {
var el = document.createElement("div")
el.setAttributeNS("xx", "align", "right")
el.setAttributeNS("", "align", "left")
el.setAttributeNS("xx", "foo", "right")
el.setAttributeNS("", "foo", "left")
assert_equals(el.hasAttribute("align"), true)
assert_equals(el.hasAttribute("foo"), true)
assert_equals(el.hasAttributeNS("xx", "align"), true)
assert_equals(el.hasAttributeNS(null, "foo"), true)
assert_equals(el.getAttribute("align"), "right")
assert_equals(el.getAttribute("foo"), "right")
assert_equals(el.getAttributeNS("xx", "align"), "right")
assert_equals(el.getAttributeNS(null, "foo"), "left")
assert_equals(el.getAttributeNS("", "foo"), "left")
el.removeAttributeNS("", "align")
el.removeAttributeNS("xx", "align")
el.removeAttributeNS("", "foo")
el.removeAttributeNS("xx", "foo")
}, "First set attribute is returned with mapped attribute set later")
</script>