summaryrefslogtreecommitdiff
path: root/parser/htmlparser/tests/mochitest/test_xml_mislabeled.html
blob: 821edd42a7720ff9933b610ff24425489193d8c2 (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
<!DOCTYPE html><meta charset=utf-8>
<title>Test for mislabeled or unlabeled XML entities with U+xxD8</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css" />
<body>
<script class="testbody">
'use strict';

SimpleTest.waitForExplicitFinish();

var declaredEncodings = [null, 'utf-8', 'uTf-8', 'UTF-8', 'utf-16', 'uTf-16', 'UTF-16'];
var actualEncodings = ['utf-8', 'utf-16be', 'utf-16le'];
var i = 0, j = 0
testxhr(declaredEncodings[i], actualEncodings[j]);

function testxhr(declaredEncoding, actualEncoding) {
  // utf-16 XML requres the BOM
  var bom = actualEncoding.startsWith('utf-16') ? '\uFEFF' : '';
  var xmlDecl = declaredEncoding ? '<?xml version="1.0" encoding="' + declaredEncoding + '" ?>\n' : '';
  // The test string need to contain U+xxD8 (bug 860180)
  var xmlString = bom + xmlDecl + '<test>testハヒフヘホ</test>';
  var xml = new TextEncoder(actualEncoding).encode(xmlString);
  var description = declaredEncoding ? ' labeled with ' + declaredEncoding : ' without XML declaration';
  if (!declaredEncoding || actualEncoding !== declaredEncoding.toLowerCase()) {
    description += ' but actually encoded with ' + actualEncoding;
  }
  var xhr = new XMLHttpRequest();
  var url = URL.createObjectURL(new Blob([xml], {type: 'text/xml'}));
  xhr.open('GET', url);
  xhr.send();
  xhr.onload = xhr.onerror = function(e) {
    URL.revokeObjectURL(url);
    is(e.type, 'load', 'xhr loading should succeed for XML' + description);
    is(xhr.responseXML.documentElement.textContent, 'testハヒフヘホ',
       'response should be available for XML' + description);
    testiframe(description, xml);
  };
}

function testiframe(description, xml) {
  var iframe = document.createElement('iframe');
  var url = URL.createObjectURL(new Blob([xml], {type: 'text/xml'}))
  iframe.src = url;
  iframe.onload = iframe.onerror = function(e) {
    URL.revokeObjectURL(url);
    is(e.type, 'load', 'iframe loading should succeed for XML' + description);
    is(iframe.contentDocument.documentElement.textContent, 'testハヒフヘホ',
       'iframe content should be available for XML' + description);
    if (++i >= declaredEncodings.length) {
      i = 0;
      if (++j >= actualEncodings.length) {
        SimpleTest.finish();
        return;
      }
    }
    testxhr(declaredEncodings[i], actualEncodings[j]);
  };
  document.body.appendChild(iframe);
}
</script>
<div id="display"></div>
</body>