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.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link HtmlUnitRegExpProxy}.
23   * Test the various properties.
24   *
25   * @author Ronald Brill
26   * @author Frank Danek
27   */
28  public class HtmlUnitRegExpProxyGlobalPropertiesStringFunctionsTest extends WebDriverTestCase {
29  
30      private void testMatch(final String string, final String regexp) throws Exception {
31          final String html = "<html><head><script>\n"
32              + LOG_TITLE_FUNCTION
33              + "  function test() {\n"
34              + "    var str = '" + string + "';\n"
35              + "    var myRegExp = " + regexp + ";\n"
36              + "    log(str.match(myRegExp));\n"
37              + "    log('$n');\n"
38              + "    log(RegExp.$1);\n"
39              + "    log(RegExp.$2);\n"
40              + "    log(RegExp.$3);\n"
41              + "    log(RegExp.$4);\n"
42              + "    log(RegExp.$5);\n"
43              + "    log(RegExp.$6);\n"
44              + "    log(RegExp.$7);\n"
45              + "    log(RegExp.$8);\n"
46              + "    log(RegExp.$9);\n"
47              + "    log('-');\n"
48              + "    log(RegExp.lastMatch);\n"
49              + "    log(RegExp.lastParen);\n"
50              + "    log(RegExp.leftContext);\n"
51              + "    log(RegExp.rightContext);\n"
52              + "  }\n"
53              + "</script></head><body onload='test()'>\n"
54              + "</body></html>";
55  
56          loadPageVerifyTitle2(html);
57      }
58  
59      /**
60       * @throws Exception if the test fails
61       */
62      @Test
63      @Alerts({"HtmlUnit", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
64      public void regExpMatchNoGroups() throws Exception {
65          testMatch("1234HtmlUnitxyz", "new RegExp('HtmlUnit')");
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      @Alerts({"HtmlUnit,Html", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
73      public void regExpMatchOneGroup() throws Exception {
74          testMatch("1234HtmlUnitxyz", "new RegExp('(Html)Unit')");
75      }
76  
77      /**
78       * @throws Exception if the test fails
79       */
80      @Test
81      @Alerts({"HtmlUnit,Ht,lU", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
82      public void regExpMatchManyGroups() throws Exception {
83          testMatch("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit')");
84      }
85  
86      /**
87       * @throws Exception if the test fails
88       */
89      @Test
90      @Alerts({"HtmlUnitxy,H,t,m,l,U,n,i,t,x,y", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-",
91               "HtmlUnitxy", "y", "1234", "z"})
92      public void regExpMatchTooManyGroups() throws Exception {
93          testMatch("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)')");
94      }
95  
96      /**
97       * @throws Exception if the test fails
98       */
99      @Test
100     @Alerts({"HtmlUnit", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
101     public void regExpMatchNoGroupsIgnoreCase() throws Exception {
102         testMatch("1234HtmlUnitxyz", "new RegExp('HtmlUnit', 'i')");
103     }
104 
105     /**
106      * @throws Exception if the test fails
107      */
108     @Test
109     @Alerts({"HtmlUnit,Html", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
110     public void regExpMatchOneGroupIgnoreCase() throws Exception {
111         testMatch("1234HtmlUnitxyz", "new RegExp('(Html)Unit', 'i')");
112     }
113 
114     /**
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts({"HtmlUnit,Ht,lU", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
119     public void regExpMatchManyGroupsIgnoreCase() throws Exception {
120         testMatch("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit', 'i')");
121     }
122 
123     /**
124      * @throws Exception if the test fails
125      */
126     @Test
127     @Alerts({"HtmlUnitxy,H,t,m,l,U,n,i,t,x,y", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-",
128              "HtmlUnitxy", "y", "1234", "z"})
129     public void regExpMatchTooManyGroupsIgnoreCase() throws Exception {
130         testMatch("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)', 'i')");
131     }
132 
133     /**
134      * @throws Exception if the test fails
135      */
136     @Test
137     @Alerts({"HtmlUnit", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
138     public void regExpMatchNoGroupsGlobal() throws Exception {
139         testMatch("1234HtmlUnitxyz", "new RegExp('HtmlUnit', 'g')");
140     }
141 
142     /**
143      * @throws Exception if the test fails
144      */
145     @Test
146     @Alerts({"HtmlUnit", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
147     public void regExpMatchOneGroupGlobal() throws Exception {
148         testMatch("1234HtmlUnitxyz", "new RegExp('(Html)Unit', 'g')");
149     }
150 
151     /**
152      * @throws Exception if the test fails
153      */
154     @Test
155     @Alerts({"Html,Htnl,Htol", "$n", "Htol", "", "", "", "", "", "", "", "", "-", "Htol", "Htol",
156              "1234HtmlUnit for Htnl; ", "xyz"})
157     public void regExpMatchOneGroupGlobalManyMatches() throws Exception {
158         testMatch("1234HtmlUnit for Htnl; Htolxyz", "new RegExp('(Ht.l)', 'g')");
159     }
160 
161     /**
162      * @throws Exception if the test fails
163      */
164     @Test
165     @Alerts({"HtmlUnit", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
166     public void regExpMatchManyGroupsGlobal() throws Exception {
167         testMatch("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit', 'g')");
168     }
169 
170     /**
171      * @throws Exception if the test fails
172      */
173     @Test
174     @Alerts({"HtmlUnitxy", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-", "HtmlUnitxy", "y", "1234", "z"})
175     public void regExpMatchTooManyGroupsGlobal() throws Exception {
176         testMatch("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)', 'g')");
177     }
178 
179     private void testSearch(final String string, final String regexp) throws Exception {
180         final String html = "<html><head><script>\n"
181             + LOG_TITLE_FUNCTION
182             + "  function test() {\n"
183             + "    var str = '" + string + "';\n"
184             + "    var myRegExp = " + regexp + ";\n"
185             + "    log(str.search(myRegExp));\n"
186             + "    log('$n');\n"
187             + "    log(RegExp.$1);\n"
188             + "    log(RegExp.$2);\n"
189             + "    log(RegExp.$3);\n"
190             + "    log(RegExp.$4);\n"
191             + "    log(RegExp.$5);\n"
192             + "    log(RegExp.$6);\n"
193             + "    log(RegExp.$7);\n"
194             + "    log(RegExp.$8);\n"
195             + "    log(RegExp.$9);\n"
196             + "    log('-');\n"
197             + "    log(RegExp.lastMatch);\n"
198             + "    log(RegExp.lastParen);\n"
199             + "    log(RegExp.leftContext);\n"
200             + "    log(RegExp.rightContext);\n"
201             + "  }\n"
202             + "</script></head><body onload='test()'>\n"
203             + "</body></html>";
204 
205         loadPageVerifyTitle2(html);
206     }
207 
208     /**
209      * @throws Exception if the test fails
210      */
211     @Test
212     @Alerts({"4", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
213     public void regExpSearchNoGroups() throws Exception {
214         testSearch("1234HtmlUnitxyz", "new RegExp('HtmlUnit')");
215     }
216 
217     /**
218      * @throws Exception if the test fails
219      */
220     @Test
221     @Alerts({"4", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
222     public void regExpSearchOneGroup() throws Exception {
223         testSearch("1234HtmlUnitxyz", "new RegExp('(Html)Unit')");
224     }
225 
226     /**
227      * @throws Exception if the test fails
228      */
229     @Test
230     @Alerts({"4", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
231     public void regExpSearchManyGroups() throws Exception {
232         testSearch("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit')");
233     }
234 
235     /**
236      * @throws Exception if the test fails
237      */
238     @Test
239     @Alerts({"4", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-", "HtmlUnitxy", "y", "1234", "z"})
240     public void regExpSearchTooManyGroups() throws Exception {
241         testSearch("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)')");
242     }
243 
244     /**
245      * @throws Exception if the test fails
246      */
247     @Test
248     @Alerts({"4", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
249     public void regExpSearchNoGroupsIgnoreCase() throws Exception {
250         testSearch("1234HtmlUnitxyz", "new RegExp('HtmlUnit', 'i')");
251     }
252 
253     /**
254      * @throws Exception if the test fails
255      */
256     @Test
257     @Alerts({"4", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
258     public void regExpSearchOneGroupIgnoreCase() throws Exception {
259         testSearch("1234HtmlUnitxyz", "new RegExp('(Html)Unit', 'i')");
260     }
261 
262     /**
263      * @throws Exception if the test fails
264      */
265     @Test
266     @Alerts({"4", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
267     public void regExpSearchManyGroupsIgnoreCase() throws Exception {
268         testSearch("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit', 'i')");
269     }
270 
271     /**
272      * @throws Exception if the test fails
273      */
274     @Test
275     @Alerts({"4", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-", "HtmlUnitxy", "y", "1234", "z"})
276     public void regExpSearchTooManyGroupsIgnoreCase() throws Exception {
277         testSearch("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)', 'i')");
278     }
279 
280     /**
281      * @throws Exception if the test fails
282      */
283     @Test
284     @Alerts({"4", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
285     public void regExpSearchNoGroupsGlobal() throws Exception {
286         testSearch("1234HtmlUnitxyz", "new RegExp('HtmlUnit', 'g')");
287     }
288 
289     /**
290      * @throws Exception if the test fails
291      */
292     @Test
293     @Alerts({"4", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
294     public void regExpSearchOneGroupGlobal() throws Exception {
295         testSearch("1234HtmlUnitxyz", "new RegExp('(Html)Unit', 'g')");
296     }
297 
298     /**
299      * @throws Exception if the test fails
300      */
301     @Test
302     @Alerts({"4", "$n", "Html", "", "", "", "", "", "", "", "", "-", "Html", "Html", "1234", "Unit for Html; Htmlxyz"})
303     public void regExpSearchOneGroupGlobalManyMatches() throws Exception {
304         testSearch("1234HtmlUnit for Html; Htmlxyz", "new RegExp('(Html)', 'g')");
305     }
306 
307     /**
308      * @throws Exception if the test fails
309      */
310     @Test
311     @Alerts({"4", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
312     public void regExpSearchManyGroupsGlobal() throws Exception {
313         testSearch("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit', 'g')");
314     }
315 
316     /**
317      * @throws Exception if the test fails
318      */
319     @Test
320     @Alerts({"4", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-", "HtmlUnitxy", "y", "1234", "z"})
321     public void regExpSearchTooManyGroupsGlobal() throws Exception {
322         testSearch("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)', 'g')");
323     }
324 
325     private void testReplace(final String string, final String regexp) throws Exception {
326         final String html = "<html><head><script>\n"
327             + LOG_TITLE_FUNCTION
328             + "  function test() {\n"
329             + "    var str = '" + string + "';\n"
330             + "    var myRegExp = " + regexp + ";\n"
331             + "    log(str.replace(myRegExp, 'RegularExpressions'));\n"
332             + "    log('$n');\n"
333             + "    log(RegExp.$1);\n"
334             + "    log(RegExp.$2);\n"
335             + "    log(RegExp.$3);\n"
336             + "    log(RegExp.$4);\n"
337             + "    log(RegExp.$5);\n"
338             + "    log(RegExp.$6);\n"
339             + "    log(RegExp.$7);\n"
340             + "    log(RegExp.$8);\n"
341             + "    log(RegExp.$9);\n"
342             + "    log('-');\n"
343             + "    log(RegExp.lastMatch);\n"
344             + "    log(RegExp.lastParen);\n"
345             + "    log(RegExp.leftContext);\n"
346             + "    log(RegExp.rightContext);\n"
347             + "  }\n"
348             + "</script></head><body onload='test()'>\n"
349             + "</body></html>";
350 
351         loadPageVerifyTitle2(html);
352     }
353 
354     /**
355      * @throws Exception if the test fails
356      */
357     @Test
358     @Alerts({"1234RegularExpressionsxyz", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
359     public void regExpReplaceNoGroups() throws Exception {
360         testReplace("1234HtmlUnitxyz", "new RegExp('HtmlUnit')");
361     }
362 
363     /**
364      * @throws Exception if the test fails
365      */
366     @Test
367     @Alerts({"1234RegularExpressionsxyz", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234",
368              "xyz"})
369     public void regExpReplaceOneGroup() throws Exception {
370         testReplace("1234HtmlUnitxyz", "new RegExp('(Html)Unit')");
371     }
372 
373     /**
374      * @throws Exception if the test fails
375      */
376     @Test
377     @Alerts({"1234RegularExpressionsxyz", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234",
378              "xyz"})
379     public void regExpReplaceManyGroups() throws Exception {
380         testReplace("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit')");
381     }
382 
383     /**
384      * @throws Exception if the test fails
385      */
386     @Test
387     @Alerts({"1234RegularExpressionsz", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-", "HtmlUnitxy",
388              "y", "1234", "z"})
389     public void regExpReplaceTooManyGroups() throws Exception {
390         testReplace("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)')");
391     }
392 
393     /**
394      * @throws Exception if the test fails
395      */
396     @Test
397     @Alerts({"1234RegularExpressionsxyz", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
398     public void regExpReplaceNoGroupsIgnoreCase() throws Exception {
399         testReplace("1234HtmlUnitxyz", "new RegExp('HtmlUnit', 'i')");
400     }
401 
402     /**
403      * @throws Exception if the test fails
404      */
405     @Test
406     @Alerts({"1234RegularExpressionsxyz", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234",
407              "xyz"})
408     public void regExpReplaceOneGroupIgnoreCase() throws Exception {
409         testReplace("1234HtmlUnitxyz", "new RegExp('(Html)Unit', 'i')");
410     }
411 
412     /**
413      * @throws Exception if the test fails
414      */
415     @Test
416     @Alerts({"1234RegularExpressionsxyz", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234",
417              "xyz"})
418     public void regExpReplaceManyGroupsIgnoreCase() throws Exception {
419         testReplace("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit', 'i')");
420     }
421 
422     /**
423      * @throws Exception if the test fails
424      */
425     @Test
426     @Alerts({"1234RegularExpressionsz", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-", "HtmlUnitxy",
427              "y", "1234", "z"})
428     public void regExpReplaceTooManyGroupsIgnoreCase() throws Exception {
429         testReplace("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)', 'i')");
430     }
431 
432     /**
433      * @throws Exception if the test fails
434      */
435     @Test
436     @Alerts({"1234RegularExpressionsxyz", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
437     public void regExpReplaceNoGroupsGlobal() throws Exception {
438         testReplace("1234HtmlUnitxyz", "new RegExp('HtmlUnit', 'g')");
439     }
440 
441     /**
442      * @throws Exception if the test fails
443      */
444     @Test
445     @Alerts({"1234RegularExpressionsxyz", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234",
446              "xyz"})
447     public void regExpReplaceOneGroupGlobal() throws Exception {
448         testReplace("1234HtmlUnitxyz", "new RegExp('(Html)Unit', 'g')");
449     }
450 
451     /**
452      * @throws Exception if the test fails
453      */
454     @Test
455     @Alerts({"1234RegularExpressionsUnit for RegularExpressions; RegularExpressionsxyz", "$n", "Html", "", "", "", "",
456              "", "", "", "", "-", "Html", "Html", "1234HtmlUnit for Html; ", "xyz"})
457     public void regExpReplaceOneGroupGlobalManyMatches() throws Exception {
458         testReplace("1234HtmlUnit for Html; Htmlxyz", "new RegExp('(Html)', 'g')");
459     }
460 
461     /**
462      * @throws Exception if the test fails
463      */
464     @Test
465     @Alerts({"1234RegularExpressionsxyz", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234",
466              "xyz"})
467     public void regExpReplaceManyGroupsGlobal() throws Exception {
468         testReplace("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit', 'g')");
469     }
470 
471     /**
472      * @throws Exception if the test fails
473      */
474     @Test
475     @Alerts({"1234RegularExpressionsz", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-", "HtmlUnitxy",
476              "y", "1234", "z"})
477     public void regExpReplaceTooManyGroupsGlobal() throws Exception {
478         testReplace("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)', 'g')");
479     }
480 }