Merge branch 'webview-js-escape'

Fix JS strings escaping in wxWebView.

See https://github.com/wxWidgets/wxWidgets/pull/1727.

Closes #18602.
This commit is contained in:
Vadim Zeitlin
2020-02-11 22:32:51 +01:00
2 changed files with 40 additions and 6 deletions

View File

@@ -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 *charsNeededToBeEscaped = "\\\"\n\r\v\t\b\f";
for (
size_t pos = m_escapedCode.find_first_of(charsNeededToBeEscaped, 0);
pos != wxString::npos;
pos = m_escapedCode.find_first_of(charsNeededToBeEscaped, 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,

View File

@@ -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);
@@ -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));