HTMLInputElement.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGfx/FontDatabase.h>
  27. #include <LibWeb/DOM/Document.h>
  28. #include <LibWeb/DOM/Event.h>
  29. #include <LibWeb/DOM/ShadowRoot.h>
  30. #include <LibWeb/DOM/Text.h>
  31. #include <LibWeb/HTML/EventNames.h>
  32. #include <LibWeb/HTML/HTMLFormElement.h>
  33. #include <LibWeb/HTML/HTMLInputElement.h>
  34. #include <LibWeb/InProcessWebView.h>
  35. #include <LibWeb/Layout/BlockBox.h>
  36. #include <LibWeb/Layout/ButtonBox.h>
  37. #include <LibWeb/Layout/CheckBox.h>
  38. #include <LibWeb/Layout/RadioButton.h>
  39. #include <LibWeb/Page/Frame.h>
  40. namespace Web::HTML {
  41. HTMLInputElement::HTMLInputElement(DOM::Document& document, QualifiedName qualified_name)
  42. : HTMLElement(document, move(qualified_name))
  43. {
  44. }
  45. HTMLInputElement::~HTMLInputElement()
  46. {
  47. }
  48. void HTMLInputElement::did_click_button(Badge<Layout::ButtonBox>)
  49. {
  50. // FIXME: This should be a PointerEvent.
  51. dispatch_event(DOM::Event::create(EventNames::click));
  52. if (type().equals_ignoring_case("submit")) {
  53. if (auto* form = first_ancestor_of_type<HTMLFormElement>()) {
  54. form->submit_form(this);
  55. }
  56. return;
  57. }
  58. }
  59. RefPtr<Layout::Node> HTMLInputElement::create_layout_node()
  60. {
  61. if (type() == "hidden")
  62. return nullptr;
  63. auto style = document().style_resolver().resolve_style(*this);
  64. if (style->display() == CSS::Display::None)
  65. return nullptr;
  66. if (type().equals_ignoring_case("submit") || type().equals_ignoring_case("button"))
  67. return adopt(*new Layout::ButtonBox(document(), *this, move(style)));
  68. if (type() == "checkbox")
  69. return adopt(*new Layout::CheckBox(document(), *this, move(style)));
  70. if (type() == "radio")
  71. return adopt(*new Layout::RadioButton(document(), *this, move(style)));
  72. create_shadow_tree_if_needed();
  73. auto layout_node = adopt(*new Layout::BlockBox(document(), this, move(style)));
  74. layout_node->set_inline(true);
  75. return layout_node;
  76. }
  77. void HTMLInputElement::set_checked(bool checked)
  78. {
  79. if (m_checked == checked)
  80. return;
  81. m_checked = checked;
  82. if (layout_node())
  83. layout_node()->set_needs_display();
  84. dispatch_event(DOM::Event::create(EventNames::change));
  85. }
  86. bool HTMLInputElement::enabled() const
  87. {
  88. return !has_attribute(HTML::AttributeNames::disabled);
  89. }
  90. String HTMLInputElement::value() const
  91. {
  92. if (m_text_node)
  93. return m_text_node->data();
  94. return default_value();
  95. }
  96. void HTMLInputElement::set_value(String value)
  97. {
  98. if (m_text_node) {
  99. m_text_node->set_data(move(value));
  100. return;
  101. }
  102. set_attribute(HTML::AttributeNames::value, move(value));
  103. }
  104. void HTMLInputElement::create_shadow_tree_if_needed()
  105. {
  106. if (shadow_root())
  107. return;
  108. // FIXME: This assumes that we want a text box. Is that always true?
  109. auto shadow_root = adopt(*new DOM::ShadowRoot(document(), *this));
  110. auto initial_value = attribute(HTML::AttributeNames::value);
  111. if (initial_value.is_null())
  112. initial_value = String::empty();
  113. auto element = document().create_element(HTML::TagNames::div);
  114. element->set_attribute(HTML::AttributeNames::style, "white-space: pre");
  115. m_text_node = adopt(*new DOM::Text(document(), initial_value));
  116. m_text_node->set_always_editable(true);
  117. element->append_child(*m_text_node);
  118. shadow_root->append_child(move(element));
  119. set_shadow_root(move(shadow_root));
  120. }
  121. }