Browse Source

Input element: Fix old properties and event listeners affecting the new type when changing types

Michael Ragazzon 2 years ago
parent
commit
ce4d519890

+ 4 - 0
Source/Core/Elements/ElementFormControlInput.cpp

@@ -127,6 +127,10 @@ void ElementFormControlInput::OnAttributeChange(const ElementAttributes& changed
 
 
 	if (!type || (!new_type_name.empty() && new_type_name != type_name))
 	if (!type || (!new_type_name.empty() && new_type_name != type_name))
 	{
 	{
+		// Reset the existing type before constructing a new one. This ensures the old type removes properties and event
+		// listeners attached to this element, so it does not interfere with new ones being attached by the new type.
+		type.reset();
+
 		if (new_type_name == "password")
 		if (new_type_name == "password")
 			type = MakeUnique<InputTypeText>(this, InputTypeText::OBSCURED);
 			type = MakeUnique<InputTypeText>(this, InputTypeText::OBSCURED);
 		else if (new_type_name == "radio")
 		else if (new_type_name == "radio")

+ 9 - 0
Source/Core/Elements/WidgetTextInput.cpp

@@ -174,6 +174,15 @@ WidgetTextInput::~WidgetTextInput()
 	parent->RemoveEventListener(EventId::Dblclick, this, true);
 	parent->RemoveEventListener(EventId::Dblclick, this, true);
 	parent->RemoveEventListener(EventId::Drag, this, true);
 	parent->RemoveEventListener(EventId::Drag, this, true);
 
 
+	// This widget might be parented by an input element, which may now be constructing a completely different type.
+	// Thus, remove all properties set by this widget so they don't affect the new type.
+	parent->RemoveProperty(PropertyId::WhiteSpace);
+	parent->RemoveProperty(PropertyId::OverflowX);
+	parent->RemoveProperty(PropertyId::OverflowY);
+	parent->RemoveProperty(PropertyId::Drag);
+	parent->RemoveProperty(PropertyId::WordBreak);
+	parent->RemoveProperty(PropertyId::TextTransform);
+
 	// Remove all the children added by the text widget.
 	// Remove all the children added by the text widget.
 	parent->RemoveChild(text_element);
 	parent->RemoveChild(text_element);
 	parent->RemoveChild(selected_text_element);
 	parent->RemoveChild(selected_text_element);