Label.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 = dom_node().control(); control && control->paintable()) {
  25. auto& labelable_paintable = verify_cast<Painting::LabelablePaintable>(*control->paintable());
  26. labelable_paintable.handle_associated_label_mousedown({});
  27. }
  28. m_tracking_mouse = true;
  29. }
  30. void Label::handle_mouseup_on_label(Badge<Painting::TextPaintable>, CSSPixelPoint position, unsigned button)
  31. {
  32. if (!m_tracking_mouse || button != GUI::MouseButton::Primary)
  33. return;
  34. if (auto control = dom_node().control(); control && control->paintable()) {
  35. bool is_inside_control = control->paintable_box()->absolute_rect().contains(position);
  36. bool is_inside_label = paintable_box()->absolute_rect().contains(position);
  37. if (is_inside_control || is_inside_label) {
  38. auto& labelable_paintable = verify_cast<Painting::LabelablePaintable>(*control->paintable());
  39. labelable_paintable.handle_associated_label_mouseup({});
  40. }
  41. }
  42. m_tracking_mouse = false;
  43. }
  44. void Label::handle_mousemove_on_label(Badge<Painting::TextPaintable>, CSSPixelPoint position, unsigned)
  45. {
  46. if (!m_tracking_mouse)
  47. return;
  48. if (auto control = dom_node().control(); control && control->paintable()) {
  49. bool is_inside_control = control->paintable_box()->absolute_rect().contains(position);
  50. bool is_inside_label = paintable_box()->absolute_rect().contains(position);
  51. auto& labelable_paintable = verify_cast<Painting::LabelablePaintable>(*control->paintable());
  52. labelable_paintable.handle_associated_label_mousemove({}, is_inside_control || is_inside_label);
  53. }
  54. }
  55. bool Label::is_inside_associated_label(LabelableNode const& control, CSSPixelPoint position)
  56. {
  57. if (auto* label = label_for_control_node(control); label)
  58. return label->paintable_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 const& id = control.dom_node().id(); id.has_value() && !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. }