summaryrefslogtreecommitdiffstats
path: root/dom/html/test/test_hash_encoded.html
blob: f814c8ae913a71957f336aaaca310e7b9844335a (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
<!doctype html>
<html>
<head>
<title>Test link.hash attribute</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<pre id="test">

<script>

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [['dom.url.encode_decode_hash', false]]}, runTest);

function runTest() {
  setupTest();
  doTestEncoded();
  SimpleTest.finish();
}

function setupTest() {
  var target1 = document.createElement("a");
  target1.id =  "target1";
  target1.href = "http://www.example.com/#q=♥â¥#hello";
  document.body.appendChild(target1);

  var target2 = document.createElement("a");
  target2.id =  "target2";
  target2.href = "http://www.example.com/#q=%E2%99%A5%C3%A2%C2%A5";
  document.body.appendChild(target2);

  var target3 = document.createElement("a");
  target3.id =  "target3";
  target3.href = "http://www.example.com/#/search/%23important";
  document.body.appendChild(target3);

  var target4 = document.createElement("a");
  target4.id =  "target4";
  target4.href = 'http://www.example.com/#{"a":[13, 42], "b":{"key":"value"}}';
  document.body.appendChild(target4);
}

function doTestEncoded() {
  // Tests Link::GetHash

  // Check that characters aren't being encoded
  var target = document.getElementById("target1");
  is(target.hash, '#q=♥â¥#hello', 'Unexpected link hash');

  // Check that encoded characters aren't being decoded
  target = document.getElementById("target2");
  is(target.hash, '#q=%E2%99%A5%C3%A2%C2%A5', 'Unexpected link hash');

  // A more regular use case
  target = document.getElementById("target3");
  is(target.hash, '#/search/%23important', 'Unexpected link hash');

  // Some JSON
  target = document.getElementById("target4");
  is(target.hash, '#{"a":[13, 42], "b":{"key":"value"}}', 'Unexpected link hash');

  // Tests URL::GetHash

  var url = new URL("http://www.example.com/#q=♥â¥#hello")
  is(url.hash, '#q=♥â¥#hello', 'Unexpected url hash');

  url = new URL("http://www.example.com/#q=%E2%99%A5%C3%A2%C2%A5")
  is(url.hash, '#q=%E2%99%A5%C3%A2%C2%A5', 'Unexpected url hash');

  url = new URL("http://www.example.com/#/search/%23important")
  is(url.hash, '#/search/%23important', 'Unexpected url hash');

  // Test getters and setters

  url = new URL("http://www.example.com/");
  url.hash = "#q=♥â¥#hello%E2%99%A5%C3%A2%C2%A5#/search/%23important"
  is(url.hash, '#q=♥â¥#hello%E2%99%A5%C3%A2%C2%A5#/search/%23important', 'Unexpected url hash');

  // codepath in nsStandardUrl::SetRef is different if the path is non-empty
  url = new URL("http://www.example.com/test/");
  url.hash = "#q=♥â¥#hello%E2%99%A5%C3%A2%C2%A5#/search/%23important"
  is(url.hash, '#q=♥â¥#hello%E2%99%A5%C3%A2%C2%A5#/search/%23important', 'Unexpected url hash');

  url = new URL("http://www.example.com/");
  url.hash = '#{"a":[13, 42], "b":{"key":"value"}}';
  is(target.hash, '#{"a":[13, 42], "b":{"key":"value"}}', 'Unexpected url hash');
  var parsed = JSON.parse(target.hash.substring(1));
  is(parsed.b.key, 'value', 'JSON not parsed correctly');

  url = new URL("http://www.example.com/test/");
  url.hash = '#{"a":[13, 42], "b":{"key":"value"}}';
  is(target.hash, '#{"a":[13, 42], "b":{"key":"value"}}', 'Unexpected url hash');
  parsed = JSON.parse(target.hash.substring(1));
  is(parsed.b.key, 'value', 'JSON not parsed correctly');

  // Tests Location::GetHash

  window.history.pushState(1, document.title, '#q=♥â¥#hello');
  is(location.hash,'#q=♥â¥#hello', 'Unexpected location hash');

  window.history.pushState(1, document.title, '#q=%E2%99%A5%C3%A2%C2%A5');
  is(location.hash,'#q=%E2%99%A5%C3%A2%C2%A5', 'Unexpected location hash');

  window.history.pushState(1, document.title, '#/search/%23important');
  is(location.hash,'#/search/%23important', 'Unexpected location hash');

  window.history.pushState(1, document.title, '#{"a":[13, 42], "b":{"key":"value"}}');
  is(location.hash,'#{"a":[13, 42], "b":{"key":"value"}}', 'Unexpected location hash');

}

</script>

</pre>
</body>
</html>