Label.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/DOM/Element.h>
  8. #include <LibWeb/Layout/Label.h>
  9. #include <LibWeb/Layout/LabelableNode.h>
  10. #include <LibWeb/Layout/TextNode.h>
  11. #include <LibWeb/Layout/Viewport.h>
  12. #include <LibWeb/Painting/LabelablePaintable.h>
  13. #include <LibWeb/UIEvents/MouseButton.h>
  14. namespace Web::Layout {
  15. JS_DEFINE_ALLOCATOR(Label);
  16. Label::Label(DOM::Document& document, HTML::HTMLLabelElement* element, CSS::StyleProperties style)
  17. : BlockContainer(document, element, move(style))
  18. {
  19. }
  20. Label::~Label() = default;
  21. void Label::handle_mousedown_on_label(Badge<Painting::TextPaintable>, CSSPixelPoint, unsigned button)
  22. {
  23. if (button != UIEvents::MouseButton::Primary)
  24. return;
  25. if (auto control = dom_node().control(); control && is<Painting::LabelablePaintable>(control->paintable())) {
  26. auto& labelable_paintable = verify_cast<Painting::LabelablePaintable>(*control->paintable());
  27. labelable_paintable.handle_associated_label_mousedown({});
  28. }
  29. m_tracking_mouse = true;
  30. }
  31. void Label::handle_mouseup_on_label(Badge<Painting::TextPaintable>, CSSPixelPoint position, unsigned button)
  32. {
  33. if (!m_tracking_mouse || button != UIEvents::MouseButton::Primary)
  34. return;
  35. if (auto control = dom_node().control(); control && is<Painting::LabelablePaintable>(control->paintable())) {
  36. bool is_inside_control = control->paintable_box()->absolute_rect().contains(position);
  37. bool is_inside_label = paintable_box()->absolute_rect().contains(position);
  38. if (is_inside_control || is_inside_label) {
  39. auto& labelable_paintable = verify_cast<Painting::LabelablePaintable>(*control->paintable());
  40. labelable_paintable.handle_associated_label_mouseup({});
  41. }
  42. }
  43. m_tracking_mouse = false;
  44. }
  45. void Label::handle_mousemove_on_label(Badge<Painting::TextPaintable>, CSSPixelPoint position, unsigned)
  46. {
  47. if (!m_tracking_mouse)
  48. return;
  49. if (auto control = dom_node().control(); control && is<Painting::LabelablePaintable>(control->paintable())) {
  50. bool is_inside_control = control->paintable_box()->absolute_rect().contains(position);
  51. bool is_inside_label = paintable_box()->absolute_rect().contains(position);
  52. auto& labelable_paintable = verify_cast<Painting::LabelablePaintable>(*control->paintable());
  53. labelable_paintable.handle_associated_label_mousemove({}, is_inside_control || is_inside_label);
  54. }
  55. }
  56. bool Label::is_inside_associated_label(LabelableNode const& control, CSSPixelPoint position)
  57. {
  58. if (auto* label = label_for_control_node(control); label)
  59. return label->paintable_box()->absolute_rect().contains(position);
  60. return false;
  61. }
  62. bool Label::is_associated_label_hovered(LabelableNode const& control)
  63. {
  64. if (auto* label = label_for_control_node(control); label) {
  65. if (label->document().hovered_node() == &label->dom_node())
  66. return true;
  67. if (auto* child = label->first_child_of_type<TextNode>(); child)
  68. return label->document().hovered_node() == &child->dom_node();
  69. }
  70. return false;
  71. }
  72. // https://html.spec.whatwg.org/multipage/forms.html#labeled-control
  73. Label const* Label::label_for_control_node(LabelableNode const& control)
  74. {
  75. if (!control.document().layout_node())
  76. return nullptr;
  77. // The for attribute may be specified to indicate a form control with which the caption is to be associated.
  78. // If the attribute is specified, the attribute's value must be the ID of a labelable element in the
  79. // same tree as the label element. If the attribute is specified and there is an element in the tree
  80. // whose ID is equal to the value of the for attribute, and the first such element in tree order is
  81. // a labelable element, then that element is the label element's labeled control.
  82. if (auto const& id = control.dom_node().id(); id.has_value() && !id->is_empty()) {
  83. Label const* label = nullptr;
  84. control.document().layout_node()->for_each_in_inclusive_subtree_of_type<Label>([&](auto& node) {
  85. if (node.dom_node().for_() == id) {
  86. label = &node;
  87. return TraversalDecision::Break;
  88. }
  89. return TraversalDecision::Continue;
  90. });
  91. if (label)
  92. return label;
  93. }
  94. // If the for attribute is not specified, but the label element has a labelable element descendant,
  95. // then the first such descendant in tree order is the label element's labeled control.
  96. return control.first_ancestor_of_type<Label>();
  97. }
  98. }