ElementForm.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 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. // Constructs a new ElementForm. This should not be called directly; use the Factory instead.
  34. ElementForm::ElementForm(const String& tag) : Element(tag)
  35. {
  36. }
  37. ElementForm::~ElementForm()
  38. {
  39. }
  40. // Submits the form.
  41. void ElementForm::Submit(const String& name, const String& submit_value)
  42. {
  43. Dictionary values;
  44. if (name.empty())
  45. values["submit"] = submit_value;
  46. else
  47. values[name] = submit_value;
  48. ElementList form_controls;
  49. ElementUtilities::GetElementsByTagName(form_controls, this, "input");
  50. ElementUtilities::GetElementsByTagName(form_controls, this, "textarea");
  51. ElementUtilities::GetElementsByTagName(form_controls, this, "select");
  52. for (size_t i = 0; i < form_controls.size(); i++)
  53. {
  54. ElementFormControl* control = rmlui_dynamic_cast< ElementFormControl* >(form_controls[i]);
  55. if (!control)
  56. continue;
  57. // Skip disabled controls.
  58. if (control->IsDisabled())
  59. continue;
  60. // Only process controls that should be submitted.
  61. if (!control->IsSubmitted())
  62. continue;
  63. String control_name = control->GetName();
  64. String control_value = control->GetValue();
  65. // Skip over unnamed form controls.
  66. if (control_name.empty())
  67. continue;
  68. // If the item already exists, append to it.
  69. Variant* value = GetIf(values, control_name);
  70. if (value != nullptr)
  71. *value = value->Get< String >() + ", " + control_value;
  72. else
  73. values[control_name] = control_value;
  74. }
  75. DispatchEvent(EventId::Submit, values);
  76. }
  77. } // namespace Rml