Pārlūkot izejas kodu

Copy over inline style properties manually when cloning an element, see #218

Michael Ragazzon 4 gadi atpakaļ
vecāks
revīzija
36f76c22e8
2 mainītis faili ar 23 papildinājumiem un 6 dzēšanām
  1. 9 3
      Source/Core/Element.cpp
  2. 14 3
      Tests/Source/UnitTests/Element.cpp

+ 9 - 3
Source/Core/Element.cpp

@@ -258,10 +258,16 @@ ElementPtr Element::Clone() const
 	else
 		clone = Factory::InstanceElement(nullptr, GetTagName(), GetTagName(), attributes);
 
-	if (clone != nullptr)
+	if (clone)
 	{
-		// Set the attributes manually in case the instancer does not set them.
-		clone->SetAttributes(attributes);
+		// Copy over the attributes. The 'style' attribute is skipped because inline styles are copied manually below. This is necessary in
+		// case any properties have been set manually, in that case the 'style' attribute is out of sync with the inline properties.
+		ElementAttributes clone_attributes = attributes;
+		clone_attributes.erase("style");
+		clone->SetAttributes(clone_attributes);
+
+		for (auto& id_property : GetStyle()->GetLocalStyleProperties())
+			clone->SetProperty(id_property.first, id_property.second);
 
 		String inner_rml;
 		GetInnerRML(inner_rml);

+ 14 - 3
Tests/Source/UnitTests/Element.cpp

@@ -52,7 +52,7 @@ static const String document_clone_rml = R"(
 		}
 		div {
 			drag: clone;
-			background: #ccc;
+			background-color: #fff;
 			height: 100px;
 		}
 		span {
@@ -62,7 +62,7 @@ static const String document_clone_rml = R"(
 </head>
 
 <body>
-<div>This is a <span>sample</span>.</div>
+<div style="background-color: #f00">This is a <span>sample</span>.</div>
 </body>
 </rml>
 )";
@@ -80,6 +80,7 @@ TEST_CASE("Element")
 	context->Render();
 
 	TestsShell::RenderLoop();
+
 	SUBCASE("Attribute")
 	{
 		auto* button = document->AppendChild(document->CreateElement("button"));
@@ -148,7 +149,7 @@ TEST_CASE("Element")
 		}
 	}
 
-	SUBCASE("Clone")
+	SUBCASE("CloneDrag")
 	{
 		// Simulate input for mouse click and drag
 		context->ProcessMouseMove(10, 10, 0);
@@ -170,6 +171,16 @@ TEST_CASE("Element")
 		context->ProcessMouseButtonUp(0, 0);
 	}
 
+	SUBCASE("CloneManual")
+	{
+		Element* element = document->GetFirstChild();
+		REQUIRE(element->GetProperty<String>("background-color") == "255, 0, 0, 255");
+		CHECK(element->Clone()->GetProperty<String>("background-color") == "255, 0, 0, 255");
+
+		element->SetProperty("background-color", "#0f0");
+		CHECK(element->Clone()->GetProperty<String>("background-color") == "0, 255, 0, 255");
+	}
+
 	document->Close();
 	TestsShell::ShutdownShell();
 }