Label.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  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/Layout/InitialContainingBlock.h>
  12. #include <LibWeb/Layout/Label.h>
  13. #include <LibWeb/Layout/LabelableNode.h>
  14. #include <LibWeb/Layout/TextNode.h>
  15. #include <LibWeb/Page/BrowsingContext.h>
  16. namespace Web::Layout {
  17. Label::Label(DOM::Document& document, HTML::HTMLLabelElement* element, NonnullRefPtr<CSS::StyleProperties> style)
  18. : BlockContainer(document, element, move(style))
  19. {
  20. }
  21. Label::~Label()
  22. {
  23. }
  24. void Label::handle_mousedown_on_label(Badge<TextNode>, const Gfx::IntPoint&, unsigned button)
  25. {
  26. if (button != GUI::MouseButton::Left)
  27. return;
  28. if (auto* control = control_node(); control)
  29. control->handle_associated_label_mousedown({});
  30. m_tracking_mouse = true;
  31. }
  32. void Label::handle_mouseup_on_label(Badge<TextNode>, const Gfx::IntPoint& position, unsigned button)
  33. {
  34. if (!m_tracking_mouse || button != GUI::MouseButton::Left)
  35. return;
  36. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  37. NonnullRefPtr protect = *this;
  38. if (auto* control = control_node(); control) {
  39. bool is_inside_control = enclosing_int_rect(control->absolute_rect()).contains(position);
  40. bool is_inside_label = enclosing_int_rect(absolute_rect()).contains(position);
  41. if (is_inside_control || is_inside_label)
  42. control->handle_associated_label_mouseup({});
  43. }
  44. m_tracking_mouse = false;
  45. }
  46. void Label::handle_mousemove_on_label(Badge<TextNode>, const Gfx::IntPoint& position, unsigned)
  47. {
  48. if (!m_tracking_mouse)
  49. return;
  50. if (auto* control = control_node(); control) {
  51. bool is_inside_control = enclosing_int_rect(control->absolute_rect()).contains(position);
  52. bool is_inside_label = enclosing_int_rect(absolute_rect()).contains(position);
  53. control->handle_associated_label_mousemove({}, is_inside_control || is_inside_label);
  54. }
  55. }
  56. bool Label::is_inside_associated_label(LabelableNode& control, const Gfx::IntPoint& position)
  57. {
  58. if (auto* label = label_for_control_node(control); label)
  59. return enclosing_int_rect(label->absolute_rect()).contains(position);
  60. return false;
  61. }
  62. bool Label::is_associated_label_hovered(LabelableNode& 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. Label* Label::label_for_control_node(LabelableNode& control)
  73. {
  74. Label* label = nullptr;
  75. if (!control.document().layout_node())
  76. return label;
  77. String id = control.dom_node().attribute(HTML::AttributeNames::id);
  78. if (id.is_empty())
  79. return label;
  80. control.document().layout_node()->for_each_in_inclusive_subtree_of_type<Label>([&](auto& node) {
  81. if (node.dom_node().for_() == id) {
  82. label = &node;
  83. return IterationDecision::Break;
  84. }
  85. return IterationDecision::Continue;
  86. });
  87. // FIXME: The spec also allows for associating a label with a labelable node by putting the
  88. // labelable node inside the label.
  89. return label;
  90. }
  91. LabelableNode* Label::control_node()
  92. {
  93. LabelableNode* control = nullptr;
  94. if (!document().layout_node())
  95. return control;
  96. String for_ = dom_node().for_();
  97. if (for_.is_empty())
  98. return control;
  99. document().layout_node()->for_each_in_inclusive_subtree_of_type<LabelableNode>([&](auto& node) {
  100. if (node.dom_node().attribute(HTML::AttributeNames::id) == for_) {
  101. control = &node;
  102. return IterationDecision::Break;
  103. }
  104. return IterationDecision::Continue;
  105. });
  106. // FIXME: The spec also allows for associating a label with a labelable node by putting the
  107. // labelable node inside the label.
  108. return control;
  109. }
  110. }