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