Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML unicode escaping sequences #401

Merged
merged 10 commits into from
Jan 12, 2023
6 changes: 4 additions & 2 deletions Source/Core/ElementText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ ElementText::~ElementText() {}

void ElementText::SetText(const String& _text)
{
if (text != _text)
auto unescaped_text = StringUtilities::DecodeRml(_text);

if (text != unescaped_text)
{
text = _text;
text = unescaped_text;

if (dirty_layout_on_change)
DirtyLayout();
Expand Down
53 changes: 53 additions & 0 deletions Source/Core/StringUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sstream>

namespace Rml {

Expand Down Expand Up @@ -152,6 +153,58 @@ String StringUtilities::DecodeRml(const String& s)
i += 6;
continue;
}
else if (s[i+1] == '#')
{
size_t start = i + 2;
if (s[i+2] == 'x')
{
start++;
size_t j = 0;
for(; j < 8; j++)
{
auto const& c = s[start + j];
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
break;
}

if (j > 0 && s[start + j] == ';')
{
String tmp = s.substr(start, j);
std::istringstream iss(tmp);
uint32_t code_point;
if (iss >> std::hex >> code_point)
{
result += ToUTF8(static_cast<Character>(code_point));
i = start + j + 1;
continue;
}
}
}
else
{
size_t j = 0;
for(; j < 8; j++)
{
auto const& c = s[start + j];
if (!(c >= '0' && c <= '9'))
break;
}

if (j > 0 && s[start + j] == ';')
{
String tmp = s.substr(start, j);
std::istringstream iss(tmp);
uint32_t code_point;
if (iss >> code_point)
{
result += ToUTF8(static_cast<Character>(code_point));
i = start + j + 1;
continue;
}
}
}

}
}
result += s[i];
i += 1;
Expand Down
35 changes: 35 additions & 0 deletions Tests/Source/UnitTests/XMLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ static const String document_xml_tags_in_css = R"(
</rml>
)";

static const String document_escaping = R"(
<rml>
<head>
<style>
p {
font-family: LatoLatin;
}
</style>
</head>
<body>
<p id="p">&#x20AC;&#8364;</p>
</body>
</rml>
)";

TEST_CASE("XMLParser")
{
Context* context = TestsShell::GetContext();
Expand All @@ -75,3 +90,23 @@ TEST_CASE("XMLParser")
document->Close();
TestsShell::ShutdownShell();
}

TEST_CASE("XMLParser.escaping")
{
Context* context = TestsShell::GetContext();
REQUIRE(context);

ElementDocument* document = context->LoadDocumentFromMemory(document_escaping);
REQUIRE(document);
document->Show();

TestsShell::RenderLoop();

auto element = document->GetElementById("p");
REQUIRE(element);

CHECK(element->GetInnerRML() == "€€");

document->Close();
TestsShell::ShutdownShell();
}