From: <rb...@us...> - 2018-02-09 18:22:03
|
Revision: 15128 http://sourceforge.net/p/htmlunit/code/15128 Author: rbri Date: 2018-02-09 18:22:00 +0000 (Fri, 09 Feb 2018) Log Message: ----------- RegExp: fix processing of /[]/ and /[^]/ Issue 1949 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/regexp/RegExpJsToJavaConverter.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/regexp/HtmlUnitRegExpProxyTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/regexp/RegExpJsToJavaConverterTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2018-02-07 20:38:23 UTC (rev 15127) +++ trunk/htmlunit/src/changes/changes.xml 2018-02-09 18:22:00 UTC (rev 15128) @@ -8,6 +8,9 @@ <body> <release version="2.30" date="xx, 2018" description="Bugfixes, URLSearchParams implemented, start adding support of user defined iterators, CHROME 64"> + <action type="fix" dev="rbri" issue="1949" due-to="Atsushi Nakagawa"> + RegExp: fix processing of /[]/ and /[^]/. + </action> <action type="1682" dev="rbri"> Handling of invalid processing instructions fixed in neko. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/regexp/RegExpJsToJavaConverter.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/regexp/RegExpJsToJavaConverter.java 2018-02-07 20:38:23 UTC (rev 15127) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/regexp/RegExpJsToJavaConverter.java 2018-02-09 18:22:00 UTC (rev 15128) @@ -30,6 +30,7 @@ * * @author Ronald Brill * @author Leszek Hoppe + * @author Atsushi Nakagawa */ public class RegExpJsToJavaConverter { @@ -106,6 +107,14 @@ } /** + * Removes number of chars from the given string. + * @param count the number of chars to remove + */ + public void remove(final int count) { + tape_.delete(currentPos_, currentPos_ + count); + } + + /** * Read the whole tape content. * * @return tape content @@ -221,17 +230,28 @@ next = tape_.read(); if (']' == next) { tape_.move(-3); - tape_.replace(""); - tape_.replace(""); + tape_.remove(2); tape_.replace("."); insideCharClass_ = false; } } } + else if (']' == next) { + // [^] + tape_.move(-3); + tape_.remove(2); + tape_.replace("."); + } else { tape_.move(-1); } } + else if (']' == next) { + // [] + tape_.move(-2); + tape_.remove(1); + tape_.replace("(?!)"); + } else { tape_.move(-1); } @@ -338,7 +358,7 @@ if ("ACEFGHIJKLMNOPQRTUVXYZaeghijklmpqyz".indexOf(escapeSequence) > -1) { // no need to escape this chars tape_.move(-2); - tape_.replace(""); + tape_.remove(1); tape_.move(1); return; } @@ -414,7 +434,7 @@ // drop back reference for (int i = tmpInsertPos; i <= 0; i++) { tape_.move(-1); - tape_.replace(""); + tape_.remove(1); } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/regexp/HtmlUnitRegExpProxyTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/regexp/HtmlUnitRegExpProxyTest.java 2018-02-07 20:38:23 UTC (rev 15127) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/regexp/HtmlUnitRegExpProxyTest.java 2018-02-09 18:22:00 UTC (rev 15128) @@ -33,6 +33,7 @@ * @author Ronald Brill * @author Carsten Steurl * @author Leszek Hoppe + * @author Atsushi Nakagawa */ @RunWith(BrowserRunner.class) public class HtmlUnitRegExpProxyTest extends WebDriverTestCase { @@ -1049,4 +1050,74 @@ loadPageWithAlerts2(html); } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("abc") + public void specialBrackets1() throws Exception { + // [] matches no character in JS + testEvaluate("'abc'.replace(/[]/, 'x')"); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("") + public void specialBrackets2() throws Exception { + // [] matches no character in JS + testEvaluate("'abc'.match(/[]*/)[0]"); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("xaxbxcx") + public void specialBrackets3() throws Exception { + // [] matches no character in JS + testEvaluate("'abc'.replace(/[]*/g, 'x')"); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("xaxbxxcx*xdx") + public void specialBrackets4() throws Exception { + // [] matches no character in JS + testEvaluate("'ab]c*d'.replace(/[]*]*/g, 'x')"); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("xxx") + public void specialBrackets5() throws Exception { + // [^] matches any character in JS + testEvaluate("'abc'.replace(/[^]/g, 'x')"); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("abc") + public void specialBrackets6() throws Exception { + // [^] matches any character in JS + testEvaluate("'abc'.match(/[^]*/)[0]"); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("axcd") + public void specialBrackets7() throws Exception { + // [^] matches any character in JS + testEvaluate("'ab]cd'.replace(/[^]]/g, 'x')"); + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/regexp/RegExpJsToJavaConverterTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/regexp/RegExpJsToJavaConverterTest.java 2018-02-07 20:38:23 UTC (rev 15127) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/regexp/RegExpJsToJavaConverterTest.java 2018-02-09 18:22:00 UTC (rev 15128) @@ -462,4 +462,17 @@ assertEquals("[!\\^()\\[\\]]", regExpJsToJavaConverter.convert("[!\\^()\\[\\]]")); } + /** + * Verifies that square braces can be used non escaped in JS regexp. + */ + @Test + public void squareBracket() { + final RegExpJsToJavaConverter regExpJsToJavaConverter = new RegExpJsToJavaConverter(); + + assertEquals("(?!)", regExpJsToJavaConverter.convert("[]")); + assertEquals("x(?!)y", regExpJsToJavaConverter.convert("x[]y")); + + assertEquals(".", regExpJsToJavaConverter.convert("[^]")); + assertEquals("x.y", regExpJsToJavaConverter.convert("x[^]y")); + } } |