File: window-open-noopener.html

package info (click to toggle)
firefox-esr 68.10.0esr-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,143,932 kB
  • sloc: cpp: 5,227,879; javascript: 4,315,531; ansic: 2,467,042; python: 794,975; java: 349,993; asm: 232,034; xml: 228,320; sh: 82,008; lisp: 41,202; makefile: 22,347; perl: 15,555; objc: 5,277; cs: 4,725; yacc: 1,778; ada: 1,681; pascal: 1,673; lex: 1,417; exp: 527; php: 436; ruby: 225; awk: 162; sed: 53; csh: 44
file content (131 lines) | stat: -rw-r--r-- 4,691 bytes parent folder | download | duplicates (2)
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
<!doctype html>
<meta charset=utf-8>
<title>window.open() with "noopener" tests</title>

<meta name="variant" content="?indexed">
<meta name="variant" content="?_self">
<meta name="variant" content="?_parent">
<meta name="variant" content="?_top">
<meta name=timeout content=long>

<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
var testData = [
  { testDescription: "window.open() with 'noopener' should not reuse existing target",
    secondWindowFeatureString: "noopener",
    shouldReuse: false },
  { testDescription: "noopener=1 means the same as noopener",
    secondWindowFeatureString: "noopener=1",
    shouldReuse: false },
  { testDescription: "noopener=0 means lack of noopener",
    secondWindowFeatureString: "noopener=0",
    shouldReuse: true },
  { testDescription: "noopener needs to be present as a token on its own",
    secondWindowFeatureString: "make me noopener",
    shouldReuse: true },
  { testDescription: "Trailing noopener should work",
    secondWindowFeatureString: "abc def,  \n\r noopener",
    shouldReuse: false },
  { testDescription: "Leading noopener should work",
    secondWindowFeatureString: "noopener \f\t , hey, there",
    shouldReuse: false },
  { testDescription: "Interior noopener should work",
    secondWindowFeatureString: "and now, noopener   , hey, there",
    shouldReuse: false },
];

/**
 * Loop over our testData array and kick off an async test for each entry.  Each
 * async test opens a window using window.open() with some per-test unique name,
 * then tries to do a second window.open() call with the same name and the
 * test-specific feature string.  It then checks whether that second
 * window.open() call reuses the existing window, whether the return value of
 * the second window.open() call is correct (it should be null in the noopener
 * cases and non-null in the cases when the existing window gets reused) and so
 * forth.
 */
function indexedTests() {
  var tests = [];
  for(var i = 0; i < testData.length; ++i) {
    var test = testData[i];
    var t = async_test(test.testDescription);
    tests.push(t);
    t.secondWindowFeatureString = test.secondWindowFeatureString;
    t.windowName = "someuniquename" + i;

    if (test.shouldReuse) {
      t.step(function() {
        var windowName = this.windowName;

        var w1 = window.open("", windowName);
        this.add_cleanup(function() { w1.close(); });

        assert_equals(w1.opener, window);

        var w2 = window.open("", windowName, this.secondWindowFeatureString);
        assert_equals(w2, w1);
        assert_equals(w2.opener, w1.opener);
        assert_equals(w2.opener, window);
        this.done();
      });
    } else {
      t.step(function() {
        var w1;
        this.add_cleanup(function() {
          w1.close();
          channel.postMessage(null);
        });

        var windowName = this.windowName;
        var channel = new BroadcastChannel(windowName);

        channel.onmessage = this.step_func_done(function(e) {
          var data = e.data;
          assert_equals(data.name, windowName, "Should have the right name");
          assert_equals(data.haveOpener, false, "Should not have opener");
          assert_equals(w1.opener, window);
          assert_equals(w1.location.href, "about:blank");
        });

        w1 = window.open("", windowName);
        assert_equals(w1.opener, window);

        var w2 = window.open("support/noopener-target.html?" + windowName,
                            windowName, this.secondWindowFeatureString);
        assert_equals(w2, null);

        assert_equals(w1.opener, window);
      });
    }
  }
}

/**
 * Loop over the special targets that ignore noopener and check that doing a
 * window.open() with those targets correctly reuses the existing window.
 */
function specialTargetTest(target) {
  if (["_self", "_parent", "_top"].includes(target)) {
    var t = async_test("noopener window.open targeting " + target);
    t.openedWindow = window.open(`javascript:var w2 = window.open("", "${target}", "noopener"); this.checkValues(w2); this.close(); void(0);`);
    assert_equals(t.openedWindow.opener, window);
    t.openedWindow.checkValues = t.step_func_done(function(win) {
      assert_equals(win, this.openedWindow);
    });
  } else {
    throw 'testError: special target must be one of: _self, _parent, _top'
  }
}

/**
 * Parse the Query string, check if it matches keyword 'indexed' to run the indexed tests,
 * otherwise test it as a special target
 */
var variant = window.location.href.split("?")[1]
if(variant == "indexed") {
  indexedTests();
} else {
  specialTargetTest(variant);
}
</script>