Skip to content

Commit f810e65

Browse files
committed
implement RegExpEngineAccess
1 parent 80bded4 commit f810e65

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.htmlunit.corejs.javascript.regexp;
2+
3+
import org.htmlunit.corejs.javascript.Context;
4+
5+
/**
6+
* Simple wrapper to to use the RegExp engine without js.
7+
* This is required for implementing input validation.
8+
*
9+
* @author Ronald Brill
10+
*/
11+
public class RegExpEngineAccess {
12+
13+
public static final class CompiledRegExp {
14+
private final RECompiled reCompiled_;
15+
16+
public CompiledRegExp(final RECompiled reCompiled) {
17+
reCompiled_ = reCompiled;
18+
}
19+
}
20+
21+
public static CompiledRegExp compile(final Context cx, final String regExp, final String regExpArgs) {
22+
final RECompiled reCompiled = NativeRegExp.compileRE(cx, regExp, regExpArgs, false);
23+
return new CompiledRegExp(reCompiled);
24+
}
25+
26+
public static boolean matches(final Context cx, final String input, final CompiledRegExp compiledRegExp) {
27+
boolean matches = NativeRegExp.matchRegExp(cx,
28+
new REGlobalData(),
29+
compiledRegExp.reCompiled_,
30+
input, 0, input.length(),
31+
false);
32+
return matches;
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.htmlunit.corejs.javascript.regexp;
2+
3+
import org.htmlunit.corejs.javascript.Context;
4+
import org.htmlunit.corejs.javascript.regexp.NativeRegExp;
5+
import org.htmlunit.corejs.javascript.regexp.RECompiled;
6+
import org.htmlunit.corejs.javascript.regexp.REGlobalData;
7+
import org.junit.Assert;
8+
import org.junit.jupiter.api.Test;
9+
10+
/**
11+
* Tests to use the RegExp engine without js. This is required
12+
* for implementing input validation.
13+
*
14+
* @author Ronald Brill
15+
*/
16+
public class RegExpImplStandaloneTest {
17+
18+
/**
19+
* @throws Exception if the test fails
20+
*/
21+
@Test
22+
public void testChangeAttributesFromSubclass() throws Exception {
23+
Context cx = new Context() {};
24+
25+
String regExp = "^(?:HtmlU[i-t]*)$";
26+
String regExpArgs = ""; // v
27+
28+
29+
RegExpEngineAccess.CompiledRegExp compiled = RegExpEngineAccess.compile(cx, regExp, regExpArgs);
30+
31+
String experiment = "HtmlUniiiiiot";
32+
final boolean matches = RegExpEngineAccess.matches(cx, experiment, compiled);
33+
Assert.assertTrue(matches);
34+
}
35+
}

0 commit comments

Comments
 (0)