summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/case.js
blob: 8c2da4a44af345dddca47d18f5bfb89d0686dfb0 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * document.createElement(NS)
 *
 * document.getElementsByTagName(NS)
 *
 * Element.setAttribute(NS)
 *
 * Element.getAttribute(NS)
 * Element.hasAttribute(NS)
 * Element.getElementsByTagName(NS)
 */

var tests = [];
setup(function() {
        var name_inputs = ["abc", "Abc", "ABC", "ä", "Ä"];
        var namespaces = ["http://www.w3.org/1999/xhtml", "http://www.w3.org/2000/svg", "http://FOO"];
        name_inputs.forEach(function(x) {
                              tests.push(["createElement " + x, test_create_element, [x]]);
                              tests.push(["setAttribute " +x, test_set_attribute, [x]]);
                              tests.push(["getAttribute " +x, test_get_attribute, [x]]);
                              tests.push(["getElementsByTagName a:" +x, test_get_elements_tag_name,
                                          [outer_product(namespaces, ["a"], name_inputs),
                                           x]]);
                              tests.push(["getElementsByTagName " +x, test_get_elements_tag_name,
                                          [outer_product(namespaces, [null], name_inputs),
                                           x]]);
                            });
        outer_product(namespaces, name_inputs, name_inputs).forEach(function(x) {
                                                                      tests.push(["createElementNS " + x, test_create_element_ns, x]);
                                                                      tests.push(["setAttributeNS " + x, test_set_attribute_ns, x]);
                                                                      tests.push(["getAttributeNS " + x, test_get_attribute_ns, x]);
                                                                    });
        outer_product([null].concat(namespaces), name_inputs).forEach(function(x) {
                                                                        tests.push(["getElementsByTagNameNS " + x, test_get_elements_tag_name_ns,
                                                                        outer_product(namespaces, name_inputs), x]);
                                                                      });
        name_inputs.forEach(function(x) {
                              tests.push(["createElementNS " + x, test_create_element_ns, [null, null, x]]);
                              tests.push(["setAttributeNS " + x, test_set_attribute_ns, [null, null, x]]);
                              tests.push(["getAttributeNS " + x, test_get_attribute_ns, [null, null, x]]);
                            });

      });
function outer_product() {
  var rv = [];
  function compute_outer_product() {
    var args = Array.prototype.slice.call(arguments);
    var index = args[0];
    if (index < args.length) {
      args[index].forEach(function(x) {
                           compute_outer_product.apply(this, [index+1].concat(args.slice(1, index), x, args.slice(index+1)));
                          });
    } else {
      rv.push(args.slice(1));
    }
  }
  compute_outer_product.apply(this, [1].concat(Array.prototype.slice.call(arguments)));
  return rv;
}

function expected_case(input) {
  //is_html gets set by a global on the page loading the tests
  if (is_html) {
    return ascii_lowercase(input);
  } else {
    return input;
  }
}

function ascii_lowercase(input) {
  return input.replace(/[A-Z]/g, function(x) {
                         return x.toLowerCase();
                       });
}

function get_qualified_name(el) {
  if (el.prefix) {
    return el.prefix + ":" + el.localName;
  }
  return el.localName;
}

function test_create_element(name) {
  var node = document.createElement(name);
  assert_equals(node.localName, expected_case(name));
}

function test_create_element_ns(namespace, prefix, local_name) {
  var qualified_name = prefix ? prefix + ":" + local_name : local_name;
  var node = document.createElementNS(namespace, qualified_name);
  assert_equals(node.prefix, prefix, "prefix");
  assert_equals(node.localName, local_name, "localName");
}

function test_set_attribute(name) {
  var node = document.createElement("div");
  node.setAttribute(name, "test");
  assert_equals(node.attributes[0].localName, expected_case(name));
}

function test_set_attribute_ns(namespace, prefix, local_name) {
  var qualified_name = prefix ? prefix + ":" + local_name : local_name;
  var node = document.createElement("div");
  node.setAttributeNS(namespace, qualified_name, "test");
  var attr = node.attributes[0];
  assert_equals(attr.prefix, prefix, "prefix");
  assert_equals(attr.localName, local_name, "localName");
}

function test_get_attribute(name) {
  var node = document.createElement("div");
  node.setAttribute(name, "test");
  var expected_name = expected_case(name);
  assert_equals(node.getAttribute(expected_name), "test");
  if (expected_name != name) {
    assert_equals(node.getAttribute(expected_name), "test");
  } else if (name !== ascii_lowercase(name)) {
    assert_equals(node.getAttribute(ascii_lowercase(name)), null);
  }
}

function test_get_attribute_ns(namespace, prefix, local_name) {
  var qualified_name = prefix ? prefix + ":" + local_name : local_name;
  var node = document.createElement("div");
  node.setAttributeNS(namespace, qualified_name, "test");
  var expected_name = local_name;
  assert_equals(node.getAttributeNS(namespace, expected_name), "test");
  if (local_name !== ascii_lowercase(local_name)) {
    assert_equals(node.getAttributeNS(namespace, ascii_lowercase(local_name)), null);
  }
}

function test_get_elements_tag_name(elements_to_create, search_string) {
  var container = document.createElement("div");
  elements_to_create.forEach(function(x) {
                               var qualified_name = x[1] ? x[1] + ":" + x[2] : x[2];
                               var element = document.createElementNS(x[0], qualified_name);
                               container.appendChild(element);
                             });
  var expected = Array.prototype.filter.call(container.childNodes,
                                            function(node) {
                                              if (is_html && node.namespaceURI === "http://www.w3.org/1999/xhtml") {
                                                return get_qualified_name(node) === expected_case(search_string);
                                              } else {
                                                return get_qualified_name(node) === search_string;
                                              }
                                            });
  document.documentElement.appendChild(container);
  try {
    assert_array_equals(document.getElementsByTagName(search_string), expected);
  } finally {
    document.documentElement.removeChild(container);
  }
}

function test_get_elements_tag_name_ns(elements_to_create, search_input) {
  var search_uri = search_input[0];
  var search_name = search_input[1];
  var container = document.createElement("div");
  elements_to_create.forEach(function(x) {
                               var qualified_name = x[1] ? x[1] + ":" + x[2] : x[2];
                               var element = document.createElementNS(x[0], qualified_name);
                               container.appendChild(element);
                             });
  var expected = Array.prototype.filter.call(container.childNodes,
                                            function(node) {
                                              return node.namespaceURI === search_uri;
                                              return node.localName === search_name;
                                            });
  document.documentElement.appendChild(container);
  try {
    assert_array_equals(document.getElementsByTagNameNS(search_uri, search_name), expected);
  } catch(e) {
    throw e;
  } finally {
    document.documentElement.removeChild(container);
  }
}

function test_func() {
  var func = arguments[0];
  var rest = arguments[1];
  func.apply(this, rest);
}

generate_tests(test_func, tests);