OLD | NEW |
1 function encode(charset, unicode) | 1 let uniqueId = 0; |
2 { | 2 function encodeText(charsetName, unicode) { |
3 // Returns a value already encoded, since we can't do it synchronously. | 3 return new Promise((resolve, reject) => { |
4 return results[charset][unicode]; | 4 const frame_id = `subframe${++uniqueId}`; |
| 5 |
| 6 const iframe = document.createElement('iframe'); |
| 7 iframe.style.display = 'none'; |
| 8 // |iframe.name| must be assigned before adding frame to the body or |
| 9 // |form.target| will not find it. |
| 10 iframe.name = frame_id; |
| 11 document.body.appendChild(iframe); |
| 12 |
| 13 const form = document.body.appendChild(document.createElement('form')); |
| 14 form.style.display = 'none'; |
| 15 form.method = 'GET'; |
| 16 form.action = 'resources/dummy.html'; |
| 17 form.acceptCharset = charsetName; |
| 18 form.target = frame_id; |
| 19 |
| 20 const input = form.appendChild(document.createElement('input')); |
| 21 input.type = 'text'; |
| 22 input.name = 'text'; |
| 23 input.value = String.fromCharCode(unicode.replace('U+', '0x')); |
| 24 |
| 25 iframe.onload = () => { |
| 26 const url = iframe.contentWindow.location.href; |
| 27 const result = url.substr(url.indexOf('=') + 1); |
| 28 |
| 29 iframe.remove(); |
| 30 form.remove(); |
| 31 |
| 32 resolve(result); |
| 33 }; |
| 34 form.submit(); |
| 35 }); |
5 } | 36 } |
6 | 37 |
7 function testsDone() | 38 function testEncode(charsetName, unicode, characterSequence) { |
8 { | 39 promise_test(t => { |
9 var form = document.getElementById('form'); | 40 return encodeText(charsetName, unicode).then(result => { |
10 var subframe = document.getElementById('subframe'); | 41 assert_equals(result, characterSequence); |
11 | 42 }); |
12 form.parentNode.removeChild(form); | 43 }, `Encode ${charsetName}: ${unicode} -> ${characterSequence}`); |
13 subframe.parentNode.removeChild(subframe); | |
14 | |
15 description("This tests encoding characters in various character sets."); | |
16 | |
17 for (i = 0; i < charsets.length; ++i) { | |
18 shouldBe("encode('" + charsets[i] + "', '" + unicodes[i] + "')", "'" + e
xpectedResults[i] + "'"); | |
19 } | |
20 | |
21 if (window.testRunner) | |
22 testRunner.notifyDone(); | |
23 } | 44 } |
24 | |
25 function processResult(result) | |
26 { | |
27 var charsetResults = results[charsets[i]]; | |
28 if (!charsetResults) { | |
29 charsetResults = new Object; | |
30 results[charsets[i]] = charsetResults; | |
31 } | |
32 charsetResults[unicodes[i]] = result; | |
33 } | |
34 | |
35 function subframeLoaded() | |
36 { | |
37 var URL = "" + document.getElementById('subframe').contentWindow.location; | |
38 processResult(URL.substr(URL.indexOf('=') + 1)); | |
39 ++i; | |
40 runTest(); | |
41 } | |
42 | |
43 function runTest() | |
44 { | |
45 if (i >= charsets.length) { | |
46 testsDone(); | |
47 return; | |
48 } | |
49 | |
50 var form = document.getElementById('form'); | |
51 var text = document.getElementById('text'); | |
52 var subframe = document.getElementById('subframe'); | |
53 | |
54 form.acceptCharset = charsets[i]; | |
55 form.action = "resources/dummy.html"; | |
56 subframe.onload = subframeLoaded; | |
57 text.value = String.fromCharCode(unicodes[i].replace('U+', '0x')); | |
58 | |
59 form.submit(); | |
60 } | |
61 | |
62 function testEncode(charsetName, unicode, characterSequence) | |
63 { | |
64 charsets.push(charsetName); | |
65 unicodes.push(unicode); | |
66 expectedResults.push(characterSequence); | |
67 } | |
OLD | NEW |