Label.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Event.h>
  7. #include <LibGfx/StylePainter.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Element.h>
  10. #include <LibWeb/Layout/InitialContainingBlock.h>
  11. #include <LibWeb/Layout/Label.h>
  12. #include <LibWeb/Layout/LabelableNode.h>
  13. #include <LibWeb/Layout/TextNode.h>
  14. #include <LibWeb/Painting/LabelablePaintable.h>
  15. namespace Web::Layout {
  16. Label::Label(DOM::Document& document, HTML::HTMLLabelElement* element, NonnullRefPtr<CSS::StyleProperties> style)
  17. : BlockContainer(document, element, move(style))
  18. {
  19. }
  20. Label::~Label()
  21. {
  22. }
  23. void Label::handle_mousedown_on_label(Badge<Painting::TextPaintable>, Gfx::IntPoint const&, unsigned button)
  24. {
  25. if (button != GUI::MouseButton::Primary)
  26. return;
  27. if (auto* control = labeled_control(); control)
  28. control->paintable()->handle_associated_label_mousedown({});
  29. m_tracking_mouse = true;
  30. }
  31. void Label::handle_mouseup_on_label(Badge<Painting::TextPaintable>, Gfx::IntPoint const& position, unsigned button)
  32. {
  33. if (!m_tracking_mouse || button != GUI::MouseButton::Primary)
  34. return;
  35. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  36. NonnullRefPtr protect = *this;
  37. if (auto* control = labeled_control(); control) {
  38. bool is_inside_control = enclosing_int_rect(control->paint_box()->absolute_rect()).contains(position);
  39. bool is_inside_label = enclosing_int_rect(paint_box()->absolute_rect()).contains(position);
  40. if (is_inside_control || is_inside_label)
  41. control->paintable()->handle_associated_label_mouseup({});
  42. }
  43. m_tracking_mouse = false;
  44. }
  45. void Label::handle_mousemove_on_label(Badge<Painting::TextPaintable>, Gfx::IntPoint const& position, unsigned)
  46. {
  47. if (!m_tracking_mouse)
  48. return;
  49. if (auto* control = labeled_control(); control) {
  50. bool is_inside_control = enclosing_int_rect(control->paint_box()->absolute_rect()).contains(position);
  51. bool is_inside_label = enclosing_int_rect(paint_box()->absolute_rect()).contains(position);
  52. control->paintable()->handle_associated_label_mousemove({}, is_inside_control || is_inside_label);
  53. }
  54. }
  55. bool Label::is_inside_associated_label(LabelableNode const& control, const Gfx::IntPoint& position)
  56. {
  57. if (auto* label = label_for_control_node(control); label)
  58. return enclosing_int_rect(label->paint_box()->absolute_rect()).contains(position);
  59. return false;
  60. }
  61. bool Label::is_associated_label_hovered(LabelableNode const& control)
  62. {
  63. if (auto* label = label_for_control_node(control); label) {
  64. if (label->document().hovered_node() == &label->dom_node())
  65. return true;
  66. if (auto* child = label->first_child_of_type<TextNode>(); child)
  67. return label->document().hovered_node() == &child->dom_node();
  68. }
  69. return false;
  70. }
  71. // https://html.spec.whatwg.org/multipage/forms.html#labeled-control
  72. Label const* Label::label_for_control_node(LabelableNode const& control)
  73. {
  74. if (!control.document().layout_node())
  75. return nullptr;
  76. // The for attribute may be specified to indicate a form control with which the caption is to be associated.
  77. // If the attribute is specified, the attribute's value must be the ID of a labelable element in the
  78. // same tree as the label element. If the attribute is specified and there is an element in the tree
  79. // whose ID is equal to the value of the for attribute, and the first such element in tree order is
  80. // a labelable element, then that element is the label element's labeled control.
  81. if (auto id = control.dom_node().attribute(HTML::AttributeNames::id); !id.is_empty()) {
  82. Label const* label = nullptr;
  83. control.document().layout_node()->for_each_in_inclusive_subtree_of_type<Label>([&](auto& node) {
  84. if (node.dom_node().for_() == id) {
  85. label = &node;
  86. return IterationDecision::Break;
  87. }
  88. return IterationDecision::Continue;
  89. });
  90. if (label)
  91. return label;
  92. }
  93. // If the for attribute is not specified, but the label element has a labelable element descendant,
  94. // then the first such descendant in tree order is the label element's labeled control.
  95. return control.first_ancestor_of_type<Label>();
  96. }
  97. // https://html.spec.whatwg.org/multipage/forms.html#labeled-control
  98. LabelableNode* Label::labeled_control()
  99. {
  100. if (!document().layout_node())
  101. return nullptr;
  102. LabelableNode* control = nullptr;
  103. // The for attribute may be specified to indicate a form control with which the caption is to be associated.
  104. // If the attribute is specified, the attribute's value must be the ID of a labelable element in the
  105. // same tree as the label element. If the attribute is specified and there is an element in the tree
  106. // whose ID is equal to the value of the for attribute, and the first such element in tree order is
  107. // a labelable element, then that element is the label element's labeled control.
  108. if (auto for_ = dom_node().for_(); !for_.is_null()) {
  109. document().layout_node()->for_each_in_inclusive_subtree_of_type<LabelableNode>([&](auto& node) {
  110. if (node.dom_node().attribute(HTML::AttributeNames::id) == for_) {
  111. control = &node;
  112. return IterationDecision::Break;
  113. }
  114. return IterationDecision::Continue;
  115. });
  116. return control;
  117. }
  118. // If the for attribute is not specified, but the label element has a labelable element descendant,
  119. // then the first such descendant in tree order is the label element's labeled control.
  120. for_each_in_subtree_of_type<LabelableNode>([&](auto& labelable_node) {
  121. control = &labelable_node;
  122. return IterationDecision::Break;
  123. });
  124. return control;
  125. }
  126. }