Label.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  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 <LibGUI/Event.h>
  27. #include <LibGfx/Painter.h>
  28. #include <LibGfx/StylePainter.h>
  29. #include <LibWeb/DOM/Document.h>
  30. #include <LibWeb/DOM/Element.h>
  31. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  32. #include <LibWeb/Layout/Label.h>
  33. #include <LibWeb/Layout/LabelableNode.h>
  34. #include <LibWeb/Layout/TextNode.h>
  35. #include <LibWeb/Page/Frame.h>
  36. namespace Web::Layout {
  37. Label::Label(DOM::Document& document, HTML::HTMLLabelElement* element, NonnullRefPtr<CSS::StyleProperties> style)
  38. : BlockBox(document, element, move(style))
  39. {
  40. }
  41. Label::~Label()
  42. {
  43. }
  44. void Label::handle_mousedown_on_label(Badge<TextNode>, const Gfx::IntPoint&, unsigned button)
  45. {
  46. if (button != GUI::MouseButton::Left)
  47. return;
  48. if (auto* control = control_node(); control)
  49. control->handle_associated_label_mousedown({});
  50. m_tracking_mouse = true;
  51. }
  52. void Label::handle_mouseup_on_label(Badge<TextNode>, const Gfx::IntPoint& position, unsigned button)
  53. {
  54. if (!m_tracking_mouse || button != GUI::MouseButton::Left)
  55. return;
  56. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  57. NonnullRefPtr protect = *this;
  58. if (auto* control = control_node(); control) {
  59. bool is_inside_control = enclosing_int_rect(control->absolute_rect()).contains(position);
  60. bool is_inside_label = enclosing_int_rect(absolute_rect()).contains(position);
  61. if (is_inside_control || is_inside_label)
  62. control->handle_associated_label_mouseup({});
  63. }
  64. m_tracking_mouse = false;
  65. }
  66. void Label::handle_mousemove_on_label(Badge<TextNode>, const Gfx::IntPoint& position, unsigned)
  67. {
  68. if (!m_tracking_mouse)
  69. return;
  70. if (auto* control = control_node(); control) {
  71. bool is_inside_control = enclosing_int_rect(control->absolute_rect()).contains(position);
  72. bool is_inside_label = enclosing_int_rect(absolute_rect()).contains(position);
  73. control->handle_associated_label_mousemove({}, is_inside_control || is_inside_label);
  74. }
  75. }
  76. bool Label::is_inside_associated_label(LabelableNode& control, const Gfx::IntPoint& position)
  77. {
  78. if (auto* label = label_for_control_node(control); label)
  79. return enclosing_int_rect(label->absolute_rect()).contains(position);
  80. return false;
  81. }
  82. bool Label::is_associated_label_hovered(LabelableNode& control)
  83. {
  84. if (auto* label = label_for_control_node(control); label) {
  85. if (label->document().hovered_node() == &label->dom_node())
  86. return true;
  87. if (auto* child = label->first_child_of_type<TextNode>(); child)
  88. return label->document().hovered_node() == &child->dom_node();
  89. }
  90. return false;
  91. }
  92. Label* Label::label_for_control_node(LabelableNode& control)
  93. {
  94. Label* label = nullptr;
  95. if (!control.document().layout_node())
  96. return label;
  97. String id = control.dom_node().attribute(HTML::AttributeNames::id);
  98. if (id.is_empty())
  99. return label;
  100. control.document().layout_node()->for_each_in_inclusive_subtree_of_type<Label>([&](auto& node) {
  101. if (node.dom_node().for_() == id) {
  102. label = &node;
  103. return IterationDecision::Break;
  104. }
  105. return IterationDecision::Continue;
  106. });
  107. // FIXME: The spec also allows for associating a label with a labelable node by putting the
  108. // labelable node inside the label.
  109. return label;
  110. }
  111. LabelableNode* Label::control_node()
  112. {
  113. LabelableNode* control = nullptr;
  114. if (!document().layout_node())
  115. return control;
  116. String for_ = dom_node().for_();
  117. if (for_.is_empty())
  118. return control;
  119. document().layout_node()->for_each_in_inclusive_subtree_of_type<LabelableNode>([&](auto& node) {
  120. if (node.dom_node().attribute(HTML::AttributeNames::id) == for_) {
  121. control = &node;
  122. return IterationDecision::Break;
  123. }
  124. return IterationDecision::Continue;
  125. });
  126. // FIXME: The spec also allows for associating a label with a labelable node by putting the
  127. // labelable node inside the label.
  128. return control;
  129. }
  130. }