From 24c56a413b03002be3677d09b454ea03303345f9 Mon Sep 17 00:00:00 2001 From: nns52k <58716684+nns52k@users.noreply.github.com> Date: Tue, 10 Dec 2019 20:31:38 +0800 Subject: [PATCH 1/4] Fix string escaping in JS code in wxWebView For example, a newline was escaped to be a backslash followed by a newline, but it actually shall be a backslash followed by a character 'n'. It seemed okay with most of codes until in the case that C++ style comments, i.e. single line comments, in the JavaScript codes. If the newline characters are not escaped correctly, the JavaScript interpreter will ignore everything that goes after the single line comments because the interpreter obviously cannot find the newline where it's supposed to stop treating codes as comments. --- include/wx/private/jsscriptwrapper.h | 35 ++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/include/wx/private/jsscriptwrapper.h b/include/wx/private/jsscriptwrapper.h index e60070fc1e..8dcd329ff4 100644 --- a/include/wx/private/jsscriptwrapper.h +++ b/include/wx/private/jsscriptwrapper.h @@ -36,10 +36,37 @@ public: // avoid any possible conflict between different calls. m_outputVarName = wxString::Format("__wxOut%i", (*runScriptCount)++); - // Adds one escape level if there is a single quote, double quotes or - // escape characters - wxRegEx escapeDoubleQuotes("(\\\\*)([\\'\"\n\r\v\t\b\f])"); - escapeDoubleQuotes.Replace(&m_escapedCode,"\\1\\1\\\\\\2"); + // Adds one escape level. + const char *s_szCharsNeededToBeEscaped = "\\\"\n\r\v\t\b\f"; + for ( + size_t pos = m_escapedCode.find_first_of(s_szCharsNeededToBeEscaped, 0); + pos != wxString::npos; + pos = m_escapedCode.find_first_of(s_szCharsNeededToBeEscaped, pos) + ) { + switch (m_escapedCode[pos].GetValue()) + { + case 0x0A: // '\n' + m_escapedCode[pos] = 'n'; + break; + case 0x0D: // '\r' + m_escapedCode[pos] = 'r'; + break; + case 0x0B: // '\v' + m_escapedCode[pos] = 'v'; + break; + case 0x09: // '\t' + m_escapedCode[pos] = 't'; + break; + case 0x08: // '\b' + m_escapedCode[pos] = 'b'; + break; + case 0x0C: // '\f' + m_escapedCode[pos] = 'f'; + break; + } + m_escapedCode.insert(pos, '\\'); + pos += 2; + } } // Get the code to execute, its returned value will be either boolean true, From b7547dbd2730489ccad0d1f6c8df82426ea0708d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 5 Feb 2020 15:37:27 +0100 Subject: [PATCH 2/4] Don't use Hungarian notation for variables Rename a variable to remove the ugly "sz" prefix. Also don't use "s_" prefix for non-static variables. --- include/wx/private/jsscriptwrapper.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/wx/private/jsscriptwrapper.h b/include/wx/private/jsscriptwrapper.h index 8dcd329ff4..e235ea46d3 100644 --- a/include/wx/private/jsscriptwrapper.h +++ b/include/wx/private/jsscriptwrapper.h @@ -37,11 +37,11 @@ public: m_outputVarName = wxString::Format("__wxOut%i", (*runScriptCount)++); // Adds one escape level. - const char *s_szCharsNeededToBeEscaped = "\\\"\n\r\v\t\b\f"; + const char *charsNeededToBeEscaped = "\\\"\n\r\v\t\b\f"; for ( - size_t pos = m_escapedCode.find_first_of(s_szCharsNeededToBeEscaped, 0); + size_t pos = m_escapedCode.find_first_of(charsNeededToBeEscaped, 0); pos != wxString::npos; - pos = m_escapedCode.find_first_of(s_szCharsNeededToBeEscaped, pos) + pos = m_escapedCode.find_first_of(charsNeededToBeEscaped, pos) ) { switch (m_escapedCode[pos].GetValue()) { From a26e81ccb1c773fb692ee94b7c8aa8b28bd1cf85 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 5 Feb 2020 15:40:27 +0100 Subject: [PATCH 3/4] Remove test for \v escaping in wxWebView::RunScript() This escape character doesn't seem to be handled by IE in the default emulation mode and it's not worth complicating the test code just to test for it, so simply remove it from the test. --- tests/controls/webtest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/controls/webtest.cpp b/tests/controls/webtest.cpp index 19a5bc6349..4bcb3ada68 100644 --- a/tests/controls/webtest.cpp +++ b/tests/controls/webtest.cpp @@ -291,8 +291,8 @@ TEST_CASE_METHOD(WebViewTestCase, "WebView", "[wxWebView]") CHECK(m_browser->RunScript("function f(a){return a;}f('Hello World!');", &result)); CHECK(result == _("Hello World!")); - CHECK(m_browser->RunScript("function f(a){return a;}f('a\\\'aa\\n\\rb\vb\\tb\\\\ccc\\\"ddd\\b\\fx');", &result)); - CHECK(result == _("a\'aa\n\rb\vb\tb\\ccc\"ddd\b\fx")); + CHECK(m_browser->RunScript("function f(a){return a;}f('a\\\'aa\\n\\rb\\tb\\\\ccc\\\"ddd\\b\\fx');", &result)); + CHECK(result == _("a\'aa\n\rb\tb\\ccc\"ddd\b\fx")); CHECK(m_browser->RunScript("function f(a){return a;}f(123);", &result)); CHECK(wxAtoi(result) == 123); From f21c7f2d492e909609be9e0f7ba708dc8769a574 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 5 Feb 2020 15:45:26 +0100 Subject: [PATCH 4/4] Add a test for C++ comments in code passed to RunScript() This used to be broken, check that it does work now. --- tests/controls/webtest.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/controls/webtest.cpp b/tests/controls/webtest.cpp index 4bcb3ada68..82b5859786 100644 --- a/tests/controls/webtest.cpp +++ b/tests/controls/webtest.cpp @@ -338,6 +338,13 @@ TEST_CASE_METHOD(WebViewTestCase, "WebView", "[wxWebView]") &result)); CHECK(result == "\"2016-10-08T21:30:40.000Z\""); + // Check for C++-style comments which used to be broken. + CHECK(m_browser->RunScript("function f() {\n" + " // A C++ style comment\n" + " return 17;\n" + "}f();", &result)); + CHECK(result == "17"); + // Check for errors too. CHECK(!m_browser->RunScript("syntax(error")); CHECK(!m_browser->RunScript("syntax(error", &result));