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.regexp;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link HtmlUnitRegExpProxy}.
25   * Test the various properties.
26   *
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class HtmlUnitRegExpProxyInstancePropertiesTest extends WebDriverTestCase {
31  
32      private void testProperties(final String string, final String regexp) throws Exception {
33          final String html = "<html><head>\n"
34              + "<script>\n"
35              + LOG_TITLE_FUNCTION
36              + "  function test() {\n"
37              + "    var str = '" + string + "';\n"
38              + "    var myRegExp = " + regexp + ";\n"
39              + "    log(myRegExp.exec(str));\n"
40              + "    log(myRegExp.source);\n"
41              + "    log(myRegExp.ignoreCase);\n"
42              + "    log(myRegExp.global);\n"
43              + "    log(myRegExp.multiline);\n"
44              + "    log(myRegExp.lastIndex);\n"
45              + "  }\n"
46              + "</script></head><body onload='test()'>\n"
47              + "</body></html>";
48  
49          loadPageVerifyTitle2(html);
50      }
51  
52      /**
53       * @throws Exception if the test fails
54       */
55      @Test
56      @Alerts({"HtmlUnit", "HtmlUnit", "false", "false", "false", "0"})
57      public void regExpPropertyNone() throws Exception {
58          testProperties("HtmlUnit", "new RegExp('HtmlUnit')");
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts({"HtmlUnit", "HtmlUnit", "true", "false", "false", "0"})
66      public void regExpPropertyIgnoreCase() throws Exception {
67          testProperties("HtmlUnit", "new RegExp('HtmlUnit', 'i')");
68      }
69  
70      /**
71       * @throws Exception if the test fails
72       */
73      @Test
74      @Alerts({"HtmlUnit", "HtmlUnit", "false", "true", "false", "8"})
75      public void regExpPropertyGlobal() throws Exception {
76          testProperties("HtmlUnit", "new RegExp('HtmlUnit', 'g')");
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts({"HtmlUnit", "HtmlUnit", "false", "false", "true", "0"})
84      public void regExpPropertyMultiline() throws Exception {
85          testProperties("HtmlUnit", "new RegExp('HtmlUnit', 'm')");
86      }
87  
88      /**
89       * @throws Exception if the test fails
90       */
91      @Test
92      @Alerts({"0", "true", "8", "true", "27", "true", "8", "true", "27", "false", "0"})
93      public void regExpPropertyLastIndexGlobalSetTest() throws Exception {
94          final String html = "<html><head>\n"
95              + "<script>\n"
96              + LOG_TITLE_FUNCTION
97              + "  function test() {\n"
98              + "    var str = 'HtmlUnit is great (HtmlUnit)';\n"
99              + "    var myRegExp = new RegExp('HtmlUnit', 'g');\n"
100             + "    log(myRegExp.lastIndex);\n"
101             + "    log(myRegExp.test(str));\n"
102             + "    log(myRegExp.lastIndex);\n"
103             + "    log(myRegExp.test(str));\n"
104             + "    log(myRegExp.lastIndex);\n"
105             // search again
106             + "    myRegExp.lastIndex = 0;\n"
107             + "    log(myRegExp.test(str));\n"
108             + "    log(myRegExp.lastIndex);\n"
109             // start later
110             + "    myRegExp.lastIndex = 1;\n"
111             + "    log(myRegExp.test(str));\n"
112             + "    log(myRegExp.lastIndex);\n"
113             + "    myRegExp.lastIndex = 50;\n"
114             + "    log(myRegExp.test(str));\n"
115             + "    log(myRegExp.lastIndex);\n"
116             + "  }\n"
117             + "</script></head><body onload='test()'>\n"
118             + "</body></html>";
119 
120         loadPageVerifyTitle2(html);
121     }
122 
123     /**
124      * @throws Exception if the test fails
125      */
126     @Test
127     @Alerts({"0", "HtmlUnit", "8"})
128     public void regExpPropertyLastIndexGlobalExec() throws Exception {
129         final String html = "<html><head>\n"
130             + "<script>\n"
131             + LOG_TITLE_FUNCTION
132             + "  function test() {\n"
133             + "    var str = 'HtmlUnit is great';\n"
134             + "    var myRegExp = new RegExp('HtmlUnit', 'g');\n"
135             + "    log(myRegExp.lastIndex);\n"
136             + "    log(myRegExp.exec(str));\n"
137             + "    log(myRegExp.lastIndex);\n"
138             + "  }\n"
139             + "</script></head><body onload='test()'>\n"
140             + "</body></html>";
141 
142         loadPageVerifyTitle2(html);
143     }
144 
145     /**
146      * Test for bug 1455.
147      * @throws Exception if an error occurs
148      */
149     @Test
150     @Alerts({"html,body,div,div,div", "undefined", "undefined", "undefined",
151                 "html", "1", "undefined", "/html/body/div[5]/div[1]/div[1]"})
152     public void regExResultProperties() throws Exception {
153         final String html = "<html><head>\n"
154             + "<script>\n"
155             + LOG_TITLE_FUNCTION
156             + "  function test() {\n"
157             + "    var source = '/html/body/div[5]/div[1]/div[1]';\n"
158             + "    var myRegexp = new RegExp(\n"
159             + "          '\\\\$?(?:(?![0-9-])[\\\\w-]+:)?(?![0-9-])[\\\\w-]+' ,'g');\n"
160 
161             + "     var result = source.match(myRegexp);\n"
162             + "     log(result);\n"
163             + "     if (result) {\n"
164             + "       log(result.index);\n"
165             + "       log(result.lastIndex);\n"
166             + "       log(result.input);\n"
167             + "     }\n"
168 
169             + "     result = myRegexp.exec(source);\n"
170             + "     log(result);\n"
171             + "     if (result) {\n"
172             + "       log(result.index);\n"
173             + "       log(result.lastIndex);\n"
174             + "       log(result.input);\n"
175             + "     }\n"
176             + "  }\n"
177             + "</script></head>\n"
178             + "<body onload='test()'>\n"
179             + "</body></html>";
180 
181         loadPageVerifyTitle2(html);
182     }
183 }