ElementForm.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../../Include/RmlUi/Core/Elements/ElementForm.h"
  29. #include "../../../Include/RmlUi/Core/Dictionary.h"
  30. #include "../../../Include/RmlUi/Core/ElementUtilities.h"
  31. #include "../../../Include/RmlUi/Core/Elements/ElementFormControl.h"
  32. namespace Rml {
  33. ElementForm::ElementForm(const String& tag) : Element(tag) {}
  34. ElementForm::~ElementForm() {}
  35. void ElementForm::Submit(const String& name, const String& submit_value)
  36. {
  37. Dictionary values;
  38. if (name.empty())
  39. values["submit"] = submit_value;
  40. else
  41. values[name] = submit_value;
  42. ElementList form_controls;
  43. ElementUtilities::GetElementsByTagName(form_controls, this, "input");
  44. ElementUtilities::GetElementsByTagName(form_controls, this, "textarea");
  45. ElementUtilities::GetElementsByTagName(form_controls, this, "select");
  46. for (size_t i = 0; i < form_controls.size(); i++)
  47. {
  48. ElementFormControl* control = rmlui_dynamic_cast<ElementFormControl*>(form_controls[i]);
  49. if (!control)
  50. continue;
  51. // Skip disabled controls.
  52. if (control->IsDisabled())
  53. continue;
  54. // Only process controls that should be submitted.
  55. if (!control->IsSubmitted())
  56. continue;
  57. String control_name = control->GetName();
  58. String control_value = control->GetValue();
  59. // Skip over unnamed form controls.
  60. if (control_name.empty())
  61. continue;
  62. // If the item already exists, append to it.
  63. Variant* value = GetIf(values, control_name);
  64. if (value != nullptr)
  65. *value = value->Get<String>() + ", " + control_value;
  66. else
  67. values[control_name] = control_value;
  68. }
  69. DispatchEvent(EventId::Submit, values);
  70. }
  71. } // namespace Rml