Label.cpp 5.8 KB

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