File: attributes-namednodemap.html

package info (click to toggle)
firefox 144.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,637,504 kB
  • sloc: cpp: 7,576,692; javascript: 6,430,831; ansic: 3,748,119; python: 1,398,978; xml: 628,810; asm: 438,679; java: 186,194; sh: 63,212; makefile: 19,159; objc: 13,086; perl: 12,986; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (120 lines) | stat: -rw-r--r-- 3,908 bytes parent folder | download | duplicates (10)
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 length property of an `NamedNodeMap` object");

</script>