View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.htmlunit.util.MimeType;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for general Rhino problems.
25   *
26   * @author Ronald Brill
27   */
28  public class RhinoTest extends WebDriverTestCase {
29  
30      /**
31       * Some samples from
32       * <a href="https://stackoverflow.com/questions/10480108/is-there-any-way-to-check-if-strict-mode-is-enforced">
33       * https://stackoverflow.com/questions/10480108/is-there-any-way-to-check-if-strict-mode-is-enforced</a>.
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts({"true", "true"})
38      @HtmlUnitNYI(CHROME = {"false", "false"},
39              EDGE = {"false", "false"},
40              FF = {"false", "false"},
41              FF_ESR = {"false", "false"})
42      public void isStrict_GlobalThis() throws Exception {
43          final String html = DOCTYPE_HTML
44              + "<html>\n"
45              + "<body>\n"
46              + "<script>\n"
47              + "  'use strict'\n"
48              + LOG_TITLE_FUNCTION
49  
50              + "  var isStrict = (function() { return !this; })();\n"
51              + "  log(isStrict);\n"
52  
53              + "  isStrict = (function() { return !!!this; })();\n"
54              + "  log(isStrict);\n"
55              + "</script>\n"
56              + "</body></html>";
57  
58          loadPageVerifyTitle2(html);
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts("true")
66      @HtmlUnitNYI(CHROME = "false",
67              EDGE = "false",
68              FF = "false",
69              FF_ESR = "false")
70      public void isStrict_evalVar() throws Exception {
71          final String html = DOCTYPE_HTML
72              + "<html>\n"
73              + "<body>\n"
74              + "<script>\n"
75              + "  'use strict'\n"
76              + LOG_TITLE_FUNCTION
77  
78              + "  function isStrict() {\n"
79              + "    var x = true;\n"
80              + "    eval('var x = false');\n"
81              + "    return x;\n"
82              + "  }\n"
83  
84              + "  log(isStrict());\n"
85              + "</script>\n"
86              + "</body></html>";
87  
88          loadPageVerifyTitle2(html);
89      }
90  
91      /**
92       * @throws Exception if the test fails
93       */
94      @Test
95      @Alerts("true")
96      public void isStrict_argumentsCallee() throws Exception {
97          final String html = DOCTYPE_HTML
98              + "<html>\n"
99              + "<body>\n"
100             + "<script>\n"
101             + "  'use strict'\n"
102             + LOG_TITLE_FUNCTION
103 
104             + "  function isStrict() {\n"
105             + "    try { arguments.callee } catch(e) { return true; };"
106             + "    return false;\n"
107             + "  }\n"
108 
109             + "  log(isStrict());\n"
110             + "</script>\n"
111             + "</body></html>";
112 
113         loadPageVerifyTitle2(html);
114     }
115 
116     /**
117      * @throws Exception if the test fails
118      */
119     @Test
120     @Alerts({"true", "true", "true"})
121     public void isStrict_constructor() throws Exception {
122         final String html = DOCTYPE_HTML
123             + "<html>\n"
124             + "<body>\n"
125             + "<script>\n"
126             + "  'use strict'\n"
127             + LOG_TITLE_FUNCTION
128 
129             + "  function Foo () {}\n"
130 
131             + "  try {\n"
132             + "    true.constructor = Foo;\n"
133             + "    log('true.constructor');\n"
134             + "  } catch(e) { log(e instanceof TypeError); }\n"
135 
136             + "  try {\n"
137             + "    var o = 1;\n"
138             + "    o.constructor = Foo;\n"
139             + "    log('1.constructor');\n"
140             + "  } catch(e) { log(e instanceof TypeError); }\n"
141 
142             + "  try {\n"
143             + "    'test'.constructor = Foo;\n"
144             + "    log('test.constructor');\n"
145             + "  } catch(e) { log(e instanceof TypeError); }\n"
146 
147             + "</script>\n"
148             + "</body></html>";
149 
150         loadPageVerifyTitle2(html);
151     }
152 
153     /**
154      * @throws Exception if the test fails
155      */
156     @Test
157     @Alerts("from script")
158     public void consStringAsSetterFunctionParam() throws Exception {
159         final String html = DOCTYPE_HTML
160                 + "<html>\n"
161                 + "  <head>\n"
162                 + "    <script>\n"
163                 + LOG_TITLE_FUNCTION
164                 + "      function test() {\n"
165 
166                 + "        var script = document.createElement('script');\n"
167                 + "        script.id = 'b';\n"
168                 + "        script.type = 'text/javascript';\n"
169                 + "        Object.defineProperty(script, 'source', {\n"
170                 + "          get: function() { return this.src },\n"
171                 + "          set: function(source) {\n"
172                 + "            var srcDesc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(script), 'src');\n"
173                 + "            srcDesc.set.call(this, source);\n"
174                 + "          }\n"
175                 + "        });\n"
176 
177                 + "        var part1 = 'sc';\n"
178                 + "        script.source = part1 + 'r' + 'ipt.js';\n"
179                 + "        document.body.appendChild(script);\n"
180                 + "      }\n"
181                 + "    </script>\n"
182                 + "  </head>\n"
183                 + "  <body onload='test()'>\n"
184                 + "  </body></html>";
185 
186         final String js = "log('from script');";
187         getMockWebConnection().setDefaultResponse(js, MimeType.TEXT_JAVASCRIPT);
188 
189         loadPageVerifyTitle2(html);
190     }
191 }