summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/attributes-namednodemap.html
blob: 96f9d30703b01dfe446a9acb37ff07a5a1da0cb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!DOCTYPE HTML>
<title>Tests of some tricky semantics around NamedNodeMap and the element.attributes collection</title>
<link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me">
<link rel="help" href="https://dom.spec.whatwg.org/#interface-namednodemap">
<link rel="help" href="https://dom.spec.whatwg.org/#dom-element-attributes">

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
"use strict";

test(() => {

  const element = document.createElement("div");
  element.setAttribute("x", "first");

  assert_equals(element.attributes.length, 1, "one attribute");
  assert_equals(element.attributes.x.value, "first");

}, "an attribute set by setAttribute should be accessible as a field on the `attributes` field of an Element");

test(() => {

  const element = document.createElement("div");
  const map = element.attributes;

  assert_equals(map.length, 0);

  const attr1 = document.createAttribute("attr1");
  map.setNamedItem(attr1);
  assert_equals(map.attr1, attr1);
  assert_equals(map.length, 1);

  const attr2 = document.createAttribute("attr2");
  map.setNamedItem(attr2);
  assert_equals(map.attr2, attr2);
  assert_equals(map.length, 2);

  const rm1 = map.removeNamedItem("attr1");
  assert_equals(rm1, attr1);
  assert_equals(map.length, 1);

  const rm2 = map.removeNamedItem("attr2");
  assert_equals(rm2, attr2);
  assert_equals(map.length, 0);

}, "setNamedItem and removeNamedItem on `attributes` should add and remove fields from `attributes`");

test(() => {

  const element = document.createElement("div");
  const map = element.attributes;

  const fooAttribute = document.createAttribute("foo");
  map.setNamedItem(fooAttribute);

  const itemAttribute = document.createAttribute("item");
  map.setNamedItem(itemAttribute);

  assert_equals(map.foo, fooAttribute);
  assert_equals(map.item, NamedNodeMap.prototype.item);
  assert_equals(typeof map.item, "function");

  map.removeNamedItem("item");
  assert_equals(map.item, NamedNodeMap.prototype.item);
  assert_equals(typeof map.item, "function");

}, "setNamedItem and removeNamedItem on `attributes` should not interfere with existing method names");

test(() => {

  const element = document.createElement("div");
  element.setAttributeNS(null, "x", "first");

  assert_equals(element.attributes.length, 1, "one attribute");
  assert_equals(element.attributes.x.value, "first");

}, "an attribute with a null namespace should be accessible as a field on the `attributes` field of an Element");

test(() => {

  const element = document.createElement("div");
  element.setAttributeNS("foo", "x", "first");

  assert_equals(element.attributes.length, 1, "one attribute");
  assert_equals(element.attributes.x.value, "first");

}, "an attribute with a set namespace should be accessible as a field on the `attributes` field of an Element");

test(() => {

  const element = document.createElement("div");
  element.setAttributeNS("foo", "setNamedItem", "first");

  assert_equals(element.attributes.length, 1, "one attribute");
  assert_equals(typeof element.attributes.setNamedItem, "function");

}, "setting an attribute should not overwrite the methods of an `NamedNodeMap` object");

test(() => {

  const element = document.createElement("div");
  element.setAttributeNS("foo", "toString", "first");

  assert_equals(element.attributes.length, 1, "one attribute");
  assert_equals(typeof element.attributes.toString, "function");

}, "setting an attribute should not overwrite the methods defined by prototype ancestors of an `NamedNodeMap` object");

test(() => {

  const element = document.createElement("div");
  element.setAttributeNS("foo", "length", "first");

  assert_equals(element.attributes.length, 1, "one attribute");

}, "setting an attribute should not overwrite the the length property of an `NamedNodeMap` object");

</script>