summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/syntax/parsing/test.js
blob: e725107b80d89996bef6cd4d484304130f246170 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var namespaces = {
  "html":"http://www.w3.org/1999/xhtml",
  "math":"http://www.w3.org/1998/Math/MathML",
  "mathml":"http://www.w3.org/1998/Math/MathML",
  "svg":"http://www.w3.org/2000/svg",
  "xlink":"http://www.w3.org/1999/xlink",
  "xml":"http://www.w3.org/XML/1998/namespace",
  "xmlns":"http://www.w3.org/2000/xmlns/"
};

var prefixes = {};
for (var prefix in namespaces) {
  if (namespaces.hasOwnProperty(prefix)) {
    prefixes[namespaces[prefix]] = prefix;
  }
}
prefixes[namespaces["mathml"]] = "math";

function format(format_string) {
  var insertions = Array.prototype.slice.call(arguments, 1);
  var regexp = /%s/g;
  var match_count = 0;
  var rv = format_string.replace(regexp, function(match) {
                                   var rv = insertions[match_count];
                                   match_count++;
                                   return rv;
                                 });
  return rv;
}

function test_serializer(element) {
  element.normalize();
  var lines = [];
  function serialize_element(element, indent) {
    var indent_spaces = (new Array(indent)).join(" ");
    switch(element.nodeType) {
      case Node.DOCUMENT_TYPE_NODE:
        if (element.name) {
          if (element.publicId || element.systemId) {
            var publicId = element.publicId ? element.publicId : "";
            var systemId = element.systemId ? element.systemId : "";
            lines.push(format("|%s<!DOCTYPE %s \"%s\" \"%s\">", indent_spaces,
                                element.name, publicId, systemId));
          } else {
            lines.push(format("|%s<!DOCTYPE %s>", indent_spaces,
                                element.name));
          }
        } else {
          lines.push(format("|%s<!DOCTYPE >", indent_spaces));
        }
        break;
      case Node.DOCUMENT_NODE:
        lines.push("#document");
        break;
      case Node.DOCUMENT_FRAGMENT_NODE:
        lines.push("#document-fragment");
        break;
      case Node.COMMENT_NODE:
        lines.push(format("|%s<!-- %s -->", indent_spaces, element.nodeValue));
      break;
      case Node.TEXT_NODE:
        lines.push(format("|%s\"%s\"", indent_spaces, element.nodeValue));
        break;
      case Node.ELEMENT_NODE:
        if (element.getAttribute("data-skip") !== null) {
          return;
        }
        if (element.namespaceURI !== null && element.namespaceURI !== namespaces.html) {
          var name = format("%s %s", prefixes[element.namespaceURI],
                            element.localName);
        } else {
          var name = element.localName;
        }
        lines.push(format("|%s<%s>", indent_spaces, name));

        var attributes = Array.prototype.map.call(
         element.attributes,
         function(attr) {
           var name = (attr.namespaceURI ? prefixes[attr.namespaceURI] + " " : "") +
            attr.localName;
           return [name, attr.value];
         });
        attributes.sort(function (a, b) {
                          var x = a[0];
                          var y = b[0];
                          if (x === y) {
                            return 0;
                          }
                          return x > y ? 1 : -1;
                        });

        attributes.forEach(
          function(attr) {
            var indent_spaces = (new Array(indent + 2)).join(" ");
            lines.push(format("|%s%s=\"%s\"", indent_spaces, attr[0], attr[1]));
          }
        );
        if ("HTMLTemplateElement" in window &&
            Object.prototype.toString.call(element) === "[object HTMLTemplateElement]") {
          indent += 2;
          indent_spaces = (new Array(indent)).join(" ");
          lines.push(format("|%scontent", indent_spaces));
          indent += 2;
          Array.prototype.forEach.call(element.content.childNodes,
                                       function(node) {
                                         serialize_element(node, indent);
                                       });
          indent -= 4;
        }
        break;
    }
    indent += 2;
    Array.prototype.forEach.call(element.childNodes,
                                 function(node) {
                                   serialize_element(node, indent);
                                 });
  }
  serialize_element(element, 0);
  return lines.join("\n");
}

function parse_query() {
    var query = location.search.slice(1);
    var vars = query.split("&");
    var fields = vars.map(function (x) {
                            var split = x.split("=");
                            return [split[0], split.slice(1).join("=")];
                          });
    return fields;
}

function get_type() {
  var run_type = "uri";
  var fields = parse_query();
  fields.forEach(function(x) {
                   if(x[0] == "run_type") {
                     run_type = x[1];
                   }
                 });
  return run_type;
};

var test_in_blob_uri = get_test_func(function (iframe, uri_encoded_input, t) {
                                       var b = new Blob([decodeURIComponent(uri_encoded_input)], { type: "text/html" });
                                       var blobURL = URL.createObjectURL(b);
                                       iframe.src = blobURL;
                                       t.add_cleanup(function() {
                                         URL.revokeObjectURL(blobURL);
                                       });
                                     });

var test_document_write = get_test_func(function(iframe, uri_encoded_input, t) {
                                          iframe.contentDocument.open();
                                          var input = decodeURIComponent(uri_encoded_input);
                                          iframe.contentDocument.write(input);
                                          iframe.contentDocument.close();
                                        });

var test_document_write_single = get_test_func(function(iframe, uri_encoded_input, t) {
                                                 iframe.contentDocument.open();
                                                 var input = decodeURIComponent(uri_encoded_input);
                                                 for (var i=0; i< input.length; i++) {
                                                   iframe.contentDocument.write(input[i]);
                                                 }
                                                 iframe.contentDocument.close();
                                               });

function get_test_func(inject_func) {
  function test_func(iframe, t, test_id, uri_encoded_input, escaped_expected) {
    var expected = decodeURIComponent(escaped_expected);
    current_tests[iframe.id] = {test_id:test_id,
                                uri_encoded_input:uri_encoded_input,
                                expected:expected,
                                actual:null
                               };

    iframe.onload = function() {
      t.step(function() {
               iframe.onload = null;
               var serialized_dom = test_serializer(iframe.contentDocument);
               current_tests[iframe.id].actual = serialized_dom;
               assert_equals(serialized_dom, expected);
               t.done();
             }
            );
    };
    inject_func(iframe, uri_encoded_input, t);
  }
  return test_func;
}

function test_fragment(iframe, t, test_id, uri_encoded_input, escaped_expected, container) {
  var input_string = decodeURIComponent(uri_encoded_input);
  var expected = decodeURIComponent(escaped_expected);
  current_tests[iframe.id] = {
      test_id:test_id,
      input:uri_encoded_input,
      expected:expected,
      actual:null,
      container:container
  };

  var components = container.split(" ");
  var container_elem = null;
  if (components.length > 1) {
    var namespace = namespaces[components[0]];
    container_elem = document.createElementNS(namespace,
                                              components[0] + ":" +
                                              components[1]);
  } else {
     container_elem = document.createElement(container);
  }
  container_elem.innerHTML = input_string;
  var serialized_dom;
  if (container_elem.namespaceURI === namespaces["html"] && container_elem.localName === "template") {
    serialized_dom = test_serializer(container_elem.content);
  } else {
    serialized_dom = test_serializer(container_elem);
  }
  current_tests[iframe.id].actual = serialized_dom;
  serialized_dom = convert_innerHTML(serialized_dom);
  assert_equals(serialized_dom, expected);
  t.done();
}

function convert_innerHTML(serialized_dom) {
  var lines = serialized_dom.split("\n");
  assert_not_equals(lines[0], "<template>", "template is never the innerHTML context object");
  lines[0] = "#document";
  return lines.join("\n");
}

function print_diffs(test_id, uri_encoded_input, expected, actual, container) {
  container = container ? container : null;
  if (actual) {
    var diffs = mark_diffs(expected, actual);
    var expected_text = diffs[0];
    var actual_text = diffs[1];
  } else {
    var expected_text = expected;
    var actual_text = "";
  }

  var tmpl = ["div", {"id":"${test_id}"},
              ["h2", {}, "${test_id}"],
              function(vars) {
                if (vars.container !== null) {
                  return ["div", {"class":"container"},
                  ["h3", {}, "innerHTML Container"],
                  ["pre", {}, vars.container]];
                } else {
                  return null;
                }
              },
              ["div", {"id":"input_${test_id}"}, ["h3", {}, "Input"], ["pre", {},
                                                                       ["code", {}, decodeURIComponent(uri_encoded_input)]]],
              ["div", {"id":"expected_${test_id}"}, ["h3", {}, "Expected"],
               ["pre", {}, ["code", {}, expected_text]]],
              ["div", {"id":"actual_${test_id}"}, ["h3", {}, "Actual"],
               ["pre", {}, ["code", {}, actual_text]]]
             ];

  var diff_dom = template.render(tmpl, {test_id:test_id, container:container});
  document.body.appendChild(diff_dom);
}

var current_tests = {};
var iframe_map = {};

function init_tests(test_type) {
  var test_func = null;
  var test_funcs = {
    "write":test_document_write,
    "write_single":test_document_write_single,
    "uri":test_in_blob_uri,
    "innerHTML":test_fragment
  };
  var tests_started = 0;
  var tests_complete = 0;

  setup(function() {
    test_func = test_funcs[test_type];

    var fails = [];

    add_result_callback(function(test) {
      tests_complete++;
      var iframe = document.getElementById(iframe_map[test.name]);
      if (test.status !== test.PASS) {
        fails.push(current_tests[iframe.id]);
        var new_iframe = document.createElement("iframe");
        new_iframe.style.display = "none";
        new_iframe.id = iframe.id;
        document.body.replaceChild(new_iframe, iframe);
        iframe = new_iframe;
      }
      if (tests_complete === order.length) {
        done();
      } else if (tests_started < order.length) {
        test_next(iframe);
      }
    });

    add_completion_callback(function() {
      fails.forEach(function(t) {
                      print_diffs(t.test_id, t.uri_encoded_input,
                      t.expected, t.actual);
                    });
      });

    //Create the iframes we will use to test
    //in the innerHTML case these are not actually used
    //but it is convenient to reuse the same code
    for (var i=0; i<num_iframes; i++) {
      var iframe = document.createElement("iframe");
      iframe.id = "iframe_" + i;
      iframe.style.display = "none";
      document.body.appendChild(iframe);
    }
  },
  {explicit_done:true});

  function test_next(iframe) {
    var test_id = order[tests_started];
    tests_started++;
    var x = tests[test_id];
    var t = x[0];
    iframe_map[t.name] = iframe.id;
    step_timeout(function() {
                 t.step(function() {
                   var string_uri_encoded_input = x[1];
                   var string_escaped_expected = x[2];
                   if (test_type === "innerHTML") {
                     var container = x[3];
                   }
                   test_func(iframe, t, test_id, string_uri_encoded_input, string_escaped_expected,
                             container);
                 });
         }, 0);
  }

  onload = function() {
    Array.prototype.forEach.call(document.getElementsByTagName("iframe"),
    function(iframe) {
      if (tests_started<order.length) {
        test_next(iframe);
      }
    });
  };
}