summaryrefslogtreecommitdiffstats
path: root/docshell/test/browser/browser_targetTopLevelLinkClicksToBlank.js
blob: 3bd8815e8139a9db546b204f43d0e8b3ebf3113f (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * This test exercises the behaviour where user-initiated link clicks on
 * the top-level document result in pageloads in a _blank target in a new
 * browser window.
 */

const TEST_PAGE = "https://example.com/browser/";
const TEST_PAGE_2 = "https://example.com/browser/components/";
const TEST_IFRAME_PAGE =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  ) + "dummy_iframe_page.html";

// There is an <a> element with this href=".." in the TEST_PAGE
// that we will click, which should take us up a level.
const LINK_URL = "https://example.com/";

/**
 * Test that a user-initiated link click results in targeting to a new
 * <browser> element, and that this properly sets the referrer on the newly
 * loaded document.
 */
add_task(async function target_to_new_blank_browser() {
  let win = await BrowserTestUtils.openNewBrowserWindow();
  let originalTab = win.gBrowser.selectedTab;
  let originalBrowser = originalTab.linkedBrowser;
  BrowserTestUtils.startLoadingURIString(originalBrowser, TEST_PAGE);
  await BrowserTestUtils.browserLoaded(originalBrowser, false, TEST_PAGE);

  // Now set the targetTopLevelLinkClicksToBlank property to true, since it
  // defaults to false.
  originalBrowser.browsingContext.targetTopLevelLinkClicksToBlank = true;

  let newTabPromise = BrowserTestUtils.waitForNewTab(win.gBrowser, LINK_URL);
  await SpecialPowers.spawn(originalBrowser, [], async () => {
    let anchor = content.document.querySelector(`a[href=".."]`);
    let userInput = content.windowUtils.setHandlingUserInput(true);
    try {
      anchor.click();
    } finally {
      userInput.destruct();
    }
  });
  let newTab = await newTabPromise;
  let newBrowser = newTab.linkedBrowser;

  Assert.ok(
    originalBrowser !== newBrowser,
    "A new browser should have been created."
  );
  await SpecialPowers.spawn(newBrowser, [TEST_PAGE], async referrer => {
    Assert.equal(
      content.document.referrer,
      referrer,
      "Should have gotten the right referrer set"
    );
  });
  await BrowserTestUtils.switchTab(win.gBrowser, originalTab);
  BrowserTestUtils.removeTab(newTab);

  // Now do the same thing with a subframe targeting "_top". This should also
  // get redirected to "_blank".
  BrowserTestUtils.startLoadingURIString(originalBrowser, TEST_IFRAME_PAGE);
  await BrowserTestUtils.browserLoaded(
    originalBrowser,
    false,
    TEST_IFRAME_PAGE
  );

  newTabPromise = BrowserTestUtils.waitForNewTab(win.gBrowser, LINK_URL);
  let frameBC1 = originalBrowser.browsingContext.children[0];
  Assert.ok(frameBC1, "Should have found a subframe BrowsingContext");

  await SpecialPowers.spawn(frameBC1, [LINK_URL], async linkUrl => {
    let anchor = content.document.createElement("a");
    anchor.setAttribute("href", linkUrl);
    anchor.setAttribute("target", "_top");
    content.document.body.appendChild(anchor);
    let userInput = content.windowUtils.setHandlingUserInput(true);
    try {
      anchor.click();
    } finally {
      userInput.destruct();
    }
  });
  newTab = await newTabPromise;
  newBrowser = newTab.linkedBrowser;

  Assert.ok(
    originalBrowser !== newBrowser,
    "A new browser should have been created."
  );
  await SpecialPowers.spawn(
    newBrowser,
    [frameBC1.currentURI.spec],
    async referrer => {
      Assert.equal(
        content.document.referrer,
        referrer,
        "Should have gotten the right referrer set"
      );
    }
  );
  await BrowserTestUtils.switchTab(win.gBrowser, originalTab);
  BrowserTestUtils.removeTab(newTab);

  await BrowserTestUtils.closeWindow(win);
});

/**
 * Test that we don't target to _blank loads caused by:
 * 1. POST requests
 * 2. Any load that isn't "normal" (in the nsIDocShell.LOAD_CMD_NORMAL sense)
 * 3. Any loads that are caused by location.replace
 * 4. Any loads that were caused by setting location.href
 * 5. Link clicks fired without user interaction.
 */
add_task(async function skip_blank_target_for_some_loads() {
  let win = await BrowserTestUtils.openNewBrowserWindow();
  let currentBrowser = win.gBrowser.selectedBrowser;
  BrowserTestUtils.startLoadingURIString(currentBrowser, TEST_PAGE);
  await BrowserTestUtils.browserLoaded(currentBrowser, false, TEST_PAGE);

  // Now set the targetTopLevelLinkClicksToBlank property to true, since it
  // defaults to false.
  currentBrowser.browsingContext.targetTopLevelLinkClicksToBlank = true;

  let ensureSingleBrowser = () => {
    Assert.equal(
      win.gBrowser.browsers.length,
      1,
      "There should only be 1 browser."
    );

    Assert.ok(
      currentBrowser.browsingContext.targetTopLevelLinkClicksToBlank,
      "Should still be targeting top-level clicks to _blank"
    );
  };

  // First we'll test a POST request
  let sameBrowserLoad = BrowserTestUtils.browserLoaded(
    currentBrowser,
    false,
    TEST_PAGE
  );
  await SpecialPowers.spawn(currentBrowser, [], async () => {
    let doc = content.document;
    let form = doc.createElement("form");
    form.setAttribute("method", "post");
    doc.body.appendChild(form);
    let userInput = content.windowUtils.setHandlingUserInput(true);
    try {
      form.submit();
    } finally {
      userInput.destruct();
    }
  });
  await sameBrowserLoad;
  ensureSingleBrowser();

  // Next, we'll try a non-normal load - specifically, we'll try a reload.
  // Since we've got a page loaded via a POST request, an attempt to reload
  // will cause the "repost" dialog to appear, so we temporarily allow the
  // repost to go through with the always_accept testing pref.
  await SpecialPowers.pushPrefEnv({
    set: [["dom.confirm_repost.testing.always_accept", true]],
  });
  sameBrowserLoad = BrowserTestUtils.browserLoaded(
    currentBrowser,
    false,
    TEST_PAGE
  );
  await SpecialPowers.spawn(currentBrowser, [], async () => {
    let userInput = content.windowUtils.setHandlingUserInput(true);
    try {
      content.location.reload();
    } finally {
      userInput.destruct();
    }
  });
  await sameBrowserLoad;
  ensureSingleBrowser();
  await SpecialPowers.popPrefEnv();

  // Next, we'll try a location.replace
  sameBrowserLoad = BrowserTestUtils.browserLoaded(
    currentBrowser,
    false,
    TEST_PAGE_2
  );
  await SpecialPowers.spawn(currentBrowser, [TEST_PAGE_2], async page2 => {
    let userInput = content.windowUtils.setHandlingUserInput(true);
    try {
      content.location.replace(page2);
    } finally {
      userInput.destruct();
    }
  });
  await sameBrowserLoad;
  ensureSingleBrowser();

  // Finally we'll try setting location.href
  sameBrowserLoad = BrowserTestUtils.browserLoaded(
    currentBrowser,
    false,
    TEST_PAGE
  );
  await SpecialPowers.spawn(currentBrowser, [TEST_PAGE], async page1 => {
    let userInput = content.windowUtils.setHandlingUserInput(true);
    try {
      content.location.href = page1;
    } finally {
      userInput.destruct();
    }
  });
  await sameBrowserLoad;
  ensureSingleBrowser();

  // Now that we're back at TEST_PAGE, let's try a scripted link click. This
  // shouldn't target to _blank.
  sameBrowserLoad = BrowserTestUtils.browserLoaded(
    currentBrowser,
    false,
    LINK_URL
  );
  await SpecialPowers.spawn(currentBrowser, [], async () => {
    let anchor = content.document.querySelector(`a[href=".."]`);
    anchor.click();
  });
  await sameBrowserLoad;
  ensureSingleBrowser();

  // A javascript:void(0); link should also not target to _blank.
  sameBrowserLoad = BrowserTestUtils.browserLoaded(
    currentBrowser,
    false,
    TEST_PAGE
  );
  await SpecialPowers.spawn(currentBrowser, [TEST_PAGE], async newPageURL => {
    let anchor = content.document.querySelector(`a[href=".."]`);
    anchor.href = "javascript:void(0);";
    anchor.addEventListener("click", () => {
      content.location.href = newPageURL;
    });
    let userInput = content.windowUtils.setHandlingUserInput(true);
    try {
      anchor.click();
    } finally {
      userInput.destruct();
    }
  });
  await sameBrowserLoad;
  ensureSingleBrowser();

  // Let's also try a non-void javascript: location.
  sameBrowserLoad = BrowserTestUtils.browserLoaded(
    currentBrowser,
    false,
    TEST_PAGE
  );
  await SpecialPowers.spawn(currentBrowser, [TEST_PAGE], async newPageURL => {
    let anchor = content.document.querySelector(`a[href=".."]`);
    anchor.href = `javascript:"string-to-navigate-to"`;
    anchor.addEventListener("click", () => {
      content.location.href = newPageURL;
    });
    let userInput = content.windowUtils.setHandlingUserInput(true);
    try {
      anchor.click();
    } finally {
      userInput.destruct();
    }
  });
  await sameBrowserLoad;
  ensureSingleBrowser();

  await BrowserTestUtils.closeWindow(win);
});