HTMLInputElement.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/Page/Frame.h>
  39. namespace Web::HTML {
  40. HTMLInputElement::HTMLInputElement(DOM::Document& document, QualifiedName qualified_name)
  41. : HTMLElement(document, move(qualified_name))
  42. {
  43. }
  44. HTMLInputElement::~HTMLInputElement()
  45. {
  46. }
  47. void HTMLInputElement::did_click_button(Badge<Layout::ButtonBox>)
  48. {
  49. // FIXME: This should be a PointerEvent.
  50. dispatch_event(DOM::Event::create(EventNames::click));
  51. if (type().equals_ignoring_case("submit")) {
  52. if (auto* form = first_ancestor_of_type<HTMLFormElement>()) {
  53. form->submit_form(this);
  54. }
  55. return;
  56. }
  57. }
  58. RefPtr<Layout::Node> HTMLInputElement::create_layout_node()
  59. {
  60. if (type() == "hidden")
  61. return nullptr;
  62. auto style = document().style_resolver().resolve_style(*this);
  63. if (style->display() == CSS::Display::None)
  64. return nullptr;
  65. if (type().equals_ignoring_case("submit") || type().equals_ignoring_case("button"))
  66. return adopt(*new Layout::ButtonBox(document(), *this, move(style)));
  67. if (type() == "checkbox")
  68. return adopt(*new Layout::CheckBox(document(), *this, move(style)));
  69. create_shadow_tree_if_needed();
  70. auto layout_node = adopt(*new Layout::BlockBox(document(), this, move(style)));
  71. layout_node->set_inline(true);
  72. return layout_node;
  73. }
  74. void HTMLInputElement::set_checked(bool checked)
  75. {
  76. if (m_checked == checked)
  77. return;
  78. m_checked = checked;
  79. if (layout_node())
  80. layout_node()->set_needs_display();
  81. dispatch_event(DOM::Event::create(EventNames::change));
  82. }
  83. bool HTMLInputElement::enabled() const
  84. {
  85. return !has_attribute(HTML::AttributeNames::disabled);
  86. }
  87. String HTMLInputElement::value() const
  88. {
  89. if (m_text_node)
  90. return m_text_node->data();
  91. return default_value();
  92. }
  93. void HTMLInputElement::set_value(String value)
  94. {
  95. if (m_text_node) {
  96. m_text_node->set_data(move(value));
  97. return;
  98. }
  99. set_attribute(HTML::AttributeNames::value, move(value));
  100. }
  101. void HTMLInputElement::create_shadow_tree_if_needed()
  102. {
  103. if (shadow_root())
  104. return;
  105. // FIXME: This assumes that we want a text box. Is that always true?
  106. auto shadow_root = adopt(*new DOM::ShadowRoot(document(), *this));
  107. auto initial_value = attribute(HTML::AttributeNames::value);
  108. if (initial_value.is_null())
  109. initial_value = String::empty();
  110. auto element = document().create_element(HTML::TagNames::div);
  111. element->set_attribute(HTML::AttributeNames::style, "white-space: pre");
  112. m_text_node = adopt(*new DOM::Text(document(), initial_value));
  113. m_text_node->set_always_editable(true);
  114. element->append_child(*m_text_node);
  115. shadow_root->append_child(move(element));
  116. set_shadow_root(move(shadow_root));
  117. }
  118. }