Browse Source

Support value attribute on button and submit input elements for HTML conformance (#798)

mcukstorm 4 months ago
parent
commit
8f11e0ff7d

+ 18 - 0
Source/Core/Elements/InputTypeButton.cpp

@@ -29,6 +29,7 @@
 #include "InputTypeButton.h"
 #include "../../../Include/RmlUi/Core/Elements/ElementForm.h"
 #include "../../../Include/RmlUi/Core/Elements/ElementFormControlInput.h"
+#include "../../../Include/RmlUi/Core/Factory.h"
 
 namespace Rml {
 
@@ -42,6 +43,23 @@ bool InputTypeButton::IsSubmitted()
 	return false;
 }
 
+bool InputTypeButton::OnAttributeChange(const ElementAttributes& changed_attributes)
+{
+	if (changed_attributes.find("value") != changed_attributes.end())
+	{
+		auto value = element->GetAttribute<String>("value", "");
+		if (!value.empty() && !value_element)
+			value_element =
+				rmlui_static_cast<ElementText*>(element->AppendChild(Factory::InstanceElement(element, "#text", "", XMLAttributes()), true));
+
+		if (value_element)
+			value_element->SetText(value);
+
+		return false;
+	}
+	return true;
+}
+
 void InputTypeButton::ProcessDefaultAction(Event& /*event*/) {}
 
 bool InputTypeButton::GetIntrinsicDimensions(Vector2f& /*dimensions*/, float& /*ratio*/)

+ 9 - 0
Source/Core/Elements/InputTypeButton.h

@@ -30,6 +30,7 @@
 #define RMLUI_CORE_ELEMENTS_INPUTTYPEBUTTON_H
 
 #include "../../../Include/RmlUi/Core/ElementDocument.h"
+#include "../../../Include/RmlUi/Core/ElementText.h"
 #include "../../../Include/RmlUi/Core/EventListener.h"
 #include "InputType.h"
 
@@ -51,6 +52,11 @@ public:
 	/// @return True if the form control is to be submitted, false otherwise.
 	bool IsSubmitted() override;
 
+	/// Called when an attribute of the element has changed.
+	/// @param[in] changed_attributes The attributes that have changed.
+	/// @return True if no layout is required, false if the layout needs to be dirtied.
+	bool OnAttributeChange(const ElementAttributes& changed_attributes) override;
+
 	/// Checks for necessary functional changes in the control as a result of the event.
 	/// @param[in] event The event to process.
 	void ProcessDefaultAction(Event& event) override;
@@ -58,6 +64,9 @@ public:
 	/// Sizes the dimensions to the element's inherent size.
 	/// @return False.
 	bool GetIntrinsicDimensions(Vector2f& dimensions, float& ratio) override;
+
+private:
+	ElementText* value_element = nullptr;
 };
 
 } // namespace Rml

+ 18 - 1
Source/Core/Elements/InputTypeSubmit.cpp

@@ -29,19 +29,36 @@
 #include "InputTypeSubmit.h"
 #include "../../../Include/RmlUi/Core/Elements/ElementForm.h"
 #include "../../../Include/RmlUi/Core/Elements/ElementFormControlInput.h"
+#include "../../../Include/RmlUi/Core/Factory.h"
 
 namespace Rml {
 
 InputTypeSubmit::InputTypeSubmit(ElementFormControlInput* element) : InputType(element) {}
 
 InputTypeSubmit::~InputTypeSubmit() {}
-
 bool InputTypeSubmit::IsSubmitted()
 {
 	// Submit buttons are never submitted; they submit themselves if appropriate.
 	return false;
 }
 
+bool InputTypeSubmit::OnAttributeChange(const ElementAttributes& changed_attributes)
+{
+	if (changed_attributes.find("value") != changed_attributes.end())
+	{
+		auto value = element->GetAttribute<String>("value", "");
+		if (!value.empty() && !value_element)
+			value_element =
+				rmlui_static_cast<ElementText*>(element->AppendChild(Factory::InstanceElement(element, "#text", "", XMLAttributes()), true));
+
+		if (value_element)
+			value_element->SetText(value);
+
+		return false;
+	}
+	return true;
+}
+
 void InputTypeSubmit::ProcessDefaultAction(Event& event)
 {
 	if (event == EventId::Click && !element->IsDisabled())

+ 9 - 0
Source/Core/Elements/InputTypeSubmit.h

@@ -29,6 +29,7 @@
 #ifndef RMLUI_CORE_ELEMENTS_INPUTTYPESUBMIT_H
 #define RMLUI_CORE_ELEMENTS_INPUTTYPESUBMIT_H
 
+#include "../../../Include/RmlUi/Core/ElementText.h"
 #include "InputType.h"
 
 namespace Rml {
@@ -48,6 +49,11 @@ public:
 	/// @return True if the form control is to be submitted, false otherwise.
 	bool IsSubmitted() override;
 
+	/// Called when an attribute of the element has changed.
+	/// @param[in] changed_attributes The attributes that have changed.
+	/// @return True if no layout is required, false if the layout needs to be dirtied.
+	bool OnAttributeChange(const ElementAttributes& changed_attributes) override;
+
 	/// Checks for necessary functional changes in the control as a result of the event.
 	/// @param[in] event The event to process.
 	void ProcessDefaultAction(Event& event) override;
@@ -55,6 +61,9 @@ public:
 	/// Sizes the dimensions to the element's inherent size.
 	/// @return False.
 	bool GetIntrinsicDimensions(Vector2f& dimensions, float& ratio) override;
+
+private:
+	ElementText* value_element = nullptr;
 };
 
 } // namespace Rml