summaryrefslogtreecommitdiffstats
path: root/parser/htmlparser/tests/mochitest/test_xml_parse_error.html
blob: 5c023ecfbda136bfd7964d63cf3295afd20e5850 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title><!-- TODO: insert title here --></title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    function getExpectedError(string, filename=location.href) {
      let lines = string.split(/\r\n|\r|\n/);
      let line, column;
      let errorLine;
      for (line = 0; line < lines.length; ++line) {
        errorLine = lines[line];
        // The error starts at the opening '<' of '<b>'.
        column = errorLine.search("<<b>") + 1;
        if (column > 0) {
          // Line and column are 1-based.
          line += 1;
          column += 1;
          break;
        }
      }

      let expectedError = `XML Parsing Error: not well-formed
Location: ${filename}
Line Number ${line}, Column ${column}:${errorLine}
${"^".padStart(column, "-")}`;
      return expectedError;
    }

    function getParseError(string) {
      let error = new window.DOMParser()
        .parseFromString(string, "text/xml")
        .getElementsByTagName("parsererror")[0].textContent;
      return [error, getExpectedError(string)];
    }

    SimpleTest.waitForExplicitFinish();

    function runTest() {
      let [error, expectedError] = getParseError("<p>Not a <<b>well-formed</b> xml string</p>");
      is(error, expectedError, "Check that parsererror contains the right data.");

      [error, expectedError] = getParseError("<p>Not \na <<b>well-formed</b> xml string</p>");
      is(error, expectedError, "Check that parsererror contains the right data.");

      [error, expectedError] = getParseError("<p>Not \na <<b>well-formed</b> xml string</p>");
      is(error, expectedError, "Check that parsererror contains the right data.");

      [error, expectedError] = getParseError("<p>Not a <<b>well-fo\nrmed</b> xml string</p>");
      is(error, expectedError, "Check that parsererror contains the right data.");

      [error, expectedError] = getParseError(`<p>Not ${' '.repeat(512)} a <<b>well-formed</b> xml string</p>`);
      is(error, expectedError, "Check that parsererror contains the right data.");

      [error, expectedError] = getParseError(`<p>${' '.repeat(505)}<br>${' '.repeat(512)}<<b>Not a well-formed</b> xml string</p>`);
      is(error, expectedError, "Check that parsererror contains the right data.");

      [error, expectedError] = getParseError(`<p>${' '.repeat(2048)}<br>${' '.repeat(512)}<<b>Not a well-formed</b> xml string</p>`);
      is(error, expectedError, "Check that parsererror contains the right data.");

      fetch("file_xml_parse_error.xml").then(response => response.text()).then(string => {
        let doc = document.getElementById("frame").contentDocument;
        error = doc.documentElement.textContent;
        expectedError = getExpectedError(string, doc.location);
        is(error, expectedError, "Check that parsererror contains the right data.");

        SimpleTest.finish();
      });
    }
  </script>
</head>
<body onload="runTest()">
<p id="display"><iframe id="frame" src="file_xml_parse_error.xml"></iframe></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>