Browse Source

Make `Element::GetRML` output the local style of the element

Michael Ragazzon 1 year ago
parent
commit
193d3e64f7
2 changed files with 53 additions and 2 deletions
  1. 23 2
      Source/Core/Element.cpp
  2. 30 0
      Tests/Source/UnitTests/Element.cpp

+ 23 - 2
Source/Core/Element.cpp

@@ -1962,8 +1962,11 @@ void Element::GetRML(String& content)
 
 
 	for (auto& pair : attributes)
 	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;
 		String value;
 		if (variant.GetInto(value))
 		if (variant.GetInto(value))
 		{
 		{
@@ -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())
 	if (HasChildNodes())
 	{
 	{
 		content += ">";
 		content += ">";

+ 30 - 0
Tests/Source/UnitTests/Element.cpp

@@ -292,6 +292,36 @@ TEST_CASE("Element")
 		CHECK(element_ptr->GetInnerRML() == "text");
 		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();
 	document->Close();
 	TestsShell::ShutdownShell();
 	TestsShell::ShutdownShell();
 }
 }