summaryrefslogtreecommitdiffstats
path: root/parser/htmlparser/tests/mochitest/parser_web_testrunner.js
blob: 45df6ea2c645af91ad8c4478b3223a53b806363a (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Runs html5lib-formatted test cases in the browser. Requires SimpleTest.
 *
 * Define an array named parserDatFiles before loading this script,
 * and it will load each of those dat files into an array, then run
 * the test parser on each and run the tests by assigning the input
 * data to an iframe's url.
 *
 * Your test document should have an element with id "display" and
 * an iframe with id "testframe".
 */

/* import-globals-from ./parser_datreader.js */
/* import-globals-from ./html5_tree_construction_exceptions.js */
/* globals parserDatFiles */

var functionsToRunAsync = [];

window.addEventListener(
  "message",
  function (event) {
    if (event.source == window && event.data == "async-run") {
      event.stopPropagation();
      var fn = functionsToRunAsync.shift();
      fn();
    }
  },
  true
);

function asyncRun(fn) {
  functionsToRunAsync.push(fn);
  window.postMessage("async-run", "*");
}

function writeErrorSummary(input, expected, got, isTodo) {
  if (isTodo) {
    $("display").appendChild(createEl("h2", null, "Unexpected Success:"));
  } else {
    $("display").appendChild(createEl("h2", null, "Unexpected Failure:"));
  }
  $("display").appendChild(createEl("br"));
  $("display").appendChild(createEl("span", null, "Matched: "));
  $("display").appendChild(document.createTextNode("" + (expected == got)));
  var pre = createEl("pre");
  pre.appendChild(document.createTextNode("Input: \n" + input, "\n-\n"));
  pre.appendChild(document.createTextNode("Expected:\n" + expected, "\n-\n"));
  pre.appendChild(document.createTextNode("Output:\n" + got + "\n-\n"));
  $("display").appendChild(pre);
  $("display").appendChild(createEl("hr"));
}

/**
 * Control will bounce back and forth between nextTest() and the
 * event handler returned by makeTestChecker() or the callback returned by
 * makeFragmentTestChecker() until the 'testcases' iterator is spent.
 */
function makeTestChecker(input, expected, errors) {
  return function (e) {
    var domAsString = docToTestOutput(e.target.contentDocument);
    if (html5Exceptions[input]) {
      todo_is(domAsString, expected, "HTML5 expected success.");
      if (domAsString == expected) {
        writeErrorSummary(input, expected, domAsString, true);
      }
    } else {
      is(domAsString, expected, "HTML5 expected success.");
      if (domAsString != expected) {
        writeErrorSummary(input, expected, domAsString, false);
      }
    }
    nextTest(e.target);
  };
}

function makeFragmentTestChecker(input, expected, errors, fragment, testframe) {
  return function () {
    var context;
    if (fragment.startsWith("svg ")) {
      context = document.createElementNS(
        "http://www.w3.org/2000/svg",
        fragment.substring(4)
      );
    } else if (fragment.startsWith("math ")) {
      context = document.createElementNS(
        "http://www.w3.org/1998/Math/MathML",
        fragment.substring(5)
      );
    } else {
      context = document.createElementNS(
        "http://www.w3.org/1999/xhtml",
        fragment
      );
    }
    // eslint-disable-next-line no-unsanitized/property
    context.innerHTML = input;
    var domAsString = fragmentToTestOutput(context);
    is(domAsString, expected, "HTML5 expected success. " + new Date());
    if (domAsString != expected) {
      writeErrorSummary(input, expected, domAsString, false);
    }
    nextTest(testframe);
  };
}

var testcases;
function nextTest(testframe) {
  var { done, value } = testcases.next();
  if (done) {
    SimpleTest.finish();
    return;
  }
  var [input, output, errors, fragment] = value;
  if (fragment) {
    asyncRun(
      makeFragmentTestChecker(input, output, errors, fragment, testframe)
    );
  } else {
    testframe.onload = makeTestChecker(input, output, errors);
    testframe.srcdoc = input;
  }
}

var testFileContents = [];
function loadNextTestFile() {
  var datFile = parserDatFiles.shift();
  if (datFile) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
      if (this.readyState == 4) {
        testFileContents.push(this.responseText);
        loadNextTestFile();
      }
    };
    xhr.open("GET", "html5lib_tree_construction/" + datFile);
    xhr.send();
  } else {
    testcases = test_parser(testFileContents);
    nextTest($("testframe"));
  }
}

addLoadEvent(loadNextTestFile);
SimpleTest.waitForExplicitFinish();