Decode strings returned by ExecuteScript()

ExecuteScript returns strings as fully
quoted JSON strings so they have to be decoded
to a binary string.
This commit is contained in:
Tobias Taschner
2020-01-17 21:11:26 +01:00
parent fb0e82e9d1
commit 26c82d43d1
2 changed files with 91 additions and 1 deletions

89
include/wx/private/json.h Normal file
View File

@@ -0,0 +1,89 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/private/json.h
// Purpose: Helper functions to handle JSON data
// Author: Tobias Taschner
// Created: 2020-01-17
// Copyright: (c) 2020 wxWidgets development team
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_PRIVATE_JSON_H_
#define _WX_PRIVATE_JSON_H_
namespace wxJSON
{
// Decode a string literal including escape sequences
// If the input is not quoted string it will be returned as the input
wxString DecodeString(const wxString& str)
{
if (!str.starts_with('"') || !str.ends_with('"'))
return str;
wxString result;
const wxWCharBuffer buf = str.wc_str();
const wchar_t* end = buf.data() + buf.length() - 1;
for (const wchar_t* ch = buf.data() + 1; ch < end; ++ch)
{
if (*ch == '\\')
{
switch (*(++ch))
{
case 'b':
result.append('\b');
break;
case 'n':
result.append('\n');
break;
case 'r':
result.append('\r');
break;
case 't':
result.append('\t');
break;
case 'v':
result.append('\v');
break;
case '\'':
result.append('\'');
break;
case '"':
result.append('"');
break;
case '\\':
result.append('\\');
break;
case 'u':
if (wxIsxdigit(ch[1]) && wxIsxdigit(ch[2]) &&
wxIsxdigit(ch[3]) && wxIsxdigit(ch[4]))
{
wxUChar uchar = wxHexToDec(wxString(&ch[3], 2)) |
wxHexToDec(wxString(&ch[1], 2)) >> 8;
result.append(uchar);
ch += 4;
}
break;
case 'x':
if (wxIsxdigit(ch[1]) && wxIsxdigit(ch[2]))
{
wxChar hchar = wxHexToDec(wxString(&ch[1], 2));
result.append(hchar);
ch += 2;
}
break;
default:
result.append(*ch);
break;
}
}
else
result.append(*ch);
}
return result;
}
} // namespace JSON
#endif // _WX_PRIVATE_JSON_H_

View File

@@ -24,6 +24,7 @@
#include "wx/stdpaths.h"
#include "wx/thread.h"
#include "wx/private/jsscriptwrapper.h"
#include "wx/private/json.h"
#include "wx/msw/private.h"
#include "wx/msw/private/webview_edge.h"
@@ -810,7 +811,7 @@ bool wxWebViewEdge::RunScript(const wxString& javascript, wxString* output)
if (RunScriptSync(wrapJS.GetUnwrappedOutputCode() + ";", &result))
{
if (output)
*output = result;
*output = wxJSON::DecodeString(result);
result.clear();
}