Label.cpp 5.5 KB

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