Skip to content

Commit

Permalink
Make Element::GetRML output the local style of the element
Browse files Browse the repository at this point in the history
  • Loading branch information
mikke89 committed Aug 24, 2024
1 parent 552fa39 commit 193d3e6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Source/Core/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1962,8 +1962,11 @@ void Element::GetRML(String& content)

for (auto& pair : attributes)
{
auto& name = pair.first;
auto& variant = pair.second;
const String& name = pair.first;
if (name == "style")
continue;

const Variant& variant = pair.second;
String value;
if (variant.GetInto(value))
{
Expand All @@ -1975,6 +1978,24 @@ void Element::GetRML(String& content)
}
}

const PropertyMap& local_properties = meta->style.GetLocalStyleProperties();
if (!local_properties.empty())
content += " style=\"";

for (const auto& pair : local_properties)
{
const PropertyId id = pair.first;
const Property& property = pair.second;

content += StyleSheetSpecification::GetPropertyName(id);
content += ": ";
content += StringUtilities::EncodeRml(property.ToString());
content += "; ";
}

if (!local_properties.empty())
content.back() = '\"';

if (HasChildNodes())
{
content += ">";
Expand Down
30 changes: 30 additions & 0 deletions Tests/Source/UnitTests/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,36 @@ TEST_CASE("Element")
CHECK(element_ptr->GetInnerRML() == "text");
}

SUBCASE("GetInnerRML")
{
String inner_rml;
Element* div = document->GetFirstChild();
Element* span = div->GetChild(1);
REQUIRE(div);
REQUIRE(div->GetTagName() == "div");
REQUIRE(span);
REQUIRE(span->GetTagName() == "span");

inner_rml = document->GetInnerRML();
CHECK(inner_rml == R"(<div style="background-color: #ff0000;">This is a <span>sample</span>.</div>)");

div->SetProperty("background-color", "white");
inner_rml = document->GetInnerRML();
CHECK(inner_rml == R"(<div style="background-color: #ffffff;">This is a <span>sample</span>.</div>)");

div->RemoveProperty("background-color");
inner_rml = document->GetInnerRML();
CHECK(inner_rml == R"(<div>This is a <span>sample</span>.</div>)");

div->SetProperty("cursor", "x<y");
inner_rml = document->GetInnerRML();
CHECK(inner_rml == R"(<div style="cursor: x&lt;y;">This is a <span>sample</span>.</div>)");

span->SetProperty("font-weight", "bold");
inner_rml = document->GetInnerRML();
CHECK(inner_rml == R"(<div style="cursor: x&lt;y;">This is a <span style="font-weight: bold;">sample</span>.</div>)");
}

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

0 comments on commit 193d3e6

Please sign in to comment.