summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/mochitest/test_private_field_dom.html
blob: 4a50c7ca9539a035bd488737f3f986345863b8c6 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=????
-->

<head>
  <meta charset="utf-8">
  <title>Test for Bug ????</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <iframe id="ifr"></iframe>
</head>

<body>
  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1094930">Mozilla Bug 1094930</a>
  <p id="display"></p>
  <div id="test_contents">
    <!-- Extracted from nsHTMLTagList.h -->
    <applet></applet>
    <area></area>
    <audio></audio>
    <base>
    </base>
    <bgsound></bgsound>
    <blockquote></blockquote>

    <body></body>
    <br></br>
    <button></button>
    <canvas></canvas>
    <caption></caption>
    <col>
    </col>
    <colgroup></colgroup>
    <data></data>
    <datalist></datalist>
    <del></del>
    <details></details>
    <dialog></dialog>
    <dir></dir>
    <div></div>
    <dl></dl>
    <embed></embed>
    <fieldset></fieldset>
    <font></font>
    <form></form>
    <frame></frame>
    <frameset></frameset>
    <h1></h1>
    <h2></h2>
    <h3></h3>
    <h4></h4>
    <h5></h5>
    <h6></h6>

    <head></head>
    <hr>
    </hr>
    <html>

    </html>
    <iframe></iframe>
    <img></img>
    <input></input>
    <ins></ins>
    <keygen></keygen>
    <label></label>
    <legend></legend>
    <li></li>
    <link>
    </link>
    <listing></listing>
    <map></map>
    <marquee></marquee>
    <menu></menu>
    <menuitem>
    </menuitem>
    <meta>
    </meta>
    <meter></meter>
    <multicol></multicol>
    <object></object>
    <ol></ol>
    <optgroup></optgroup>
    <option></option>
    <output></output>
    <p></p>
    <param>
    </param>
    <picture></picture>
    <pre></pre>
    <progress></progress>
    <q></q>
    <script></script>
    <select></select>
    <slot></slot>
    <source>
    </source>
    <span></span>
    <style></style>
    <summary></summary>
    <table></table>
    <tbody></tbody>
    <td></td>
    <textarea></textarea>
    <tfoot></tfoot>
    <th></th>
    <thead></thead>
    <template></template>
    <time></time>
    <title></title>
    <tr></tr>
    <track>
    </track>
    <ul></ul>
    <video></video>
    <xmp></xmp>
  </div>
  <script type="application/javascript">
    SimpleTest.waitForExplicitFinish();
    info("running")


    // Because private fields may not be enabled, we construct A via the below eval of an IFFE,
    // and return early if it syntax errors.
    var A = undefined;
    try {
       A = eval(`(function(){
                      class Base {
                          constructor(o) {
                              return o;
                          }
                      }

                      class A extends Base {
                        #x = 1;
                        static g(o) {
                          return o.#x;
                        }
                        static s(o, v) {
                          o.#x = v;
                        }
                      }
                      
                      return A;
                  })();`);
    } catch (e) {
      is(e instanceof SyntaxError, true, "Threw Syntax Error, Private Fields Not Enabled");
      is(/private fields are not currently supported/.test(e.message), true, "correct message");
    }

    if (A instanceof Function) {
      function assertThrewInstance(f, error) {
        var threw = true;
        try {
          f();
          threw = false;
        } catch (e) {
          // info("Caught " + e.name);
          is(e instanceof error, true, "Correct Error thrown");
        }
        is(threw, true, "Error was thrown");
      }

      function testNode(node) {
        info("Testing node " + node.nodeName);

        assertThrewInstance(() => A.g(node), TypeError);
        assertThrewInstance(() => A.s(node, 'node'), TypeError);
        // info("Stamping Node");
        new A(node);
        // info("Asserting read");
        is(A.g(node), 1, "correct value read");
        // info("Setting");
        A.s(node, 'node');
        // info("Verifing setter set the value");
        is(A.g(node), 'node', "updated value read");
        // info("Verifying we cannot double-init");
        assertThrewInstance(() => new A(node), TypeError);
      }

      function testNodeRecursive(node) {
        testNode(node);
        for (c of node.children) {
          testNodeRecursive(c)
        }
      }

      const test_contents = document.getElementById('test_contents');
      testNodeRecursive(test_contents);

      info("Checking Window");
      // Window itself isn't allowed to host private fields, because it's
      // a cross-origin object
      assertThrewInstance(() => A.g(window), TypeError)

      info("Checking Window Prototype Chain")
      // However, it's prototype chain can.
      w = Object.getPrototypeOf(window);
      while (w) {
        testNode(w);
        w = Object.getPrototypeOf(w);
      }

      info("Test Document")
      testNode(document);


      info("Test CSSRuleList")
      testNode(document.styleSheets[0].cssRules)

      info("Test DOMTokenList")
      const div = document.createElement('div');
      testNode(div.classList);
    }
    SimpleTest.finish();
  </script>
</body>

</html>