HTMLLabelElement.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HTMLLabelElementPrototype.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/FormAssociatedElement.h>
  9. #include <LibWeb/HTML/HTMLLabelElement.h>
  10. #include <LibWeb/Layout/Label.h>
  11. namespace Web::HTML {
  12. GC_DEFINE_ALLOCATOR(HTMLLabelElement);
  13. HTMLLabelElement::HTMLLabelElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  14. : HTMLElement(document, move(qualified_name))
  15. {
  16. }
  17. HTMLLabelElement::~HTMLLabelElement() = default;
  18. void HTMLLabelElement::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLLabelElement);
  22. }
  23. GC::Ptr<Layout::Node> HTMLLabelElement::create_layout_node(GC::Ref<CSS::ComputedProperties> style)
  24. {
  25. bool const has_control = (control() != nullptr);
  26. bool const is_child_text_empty = this->child_text_content().is_empty();
  27. if (!has_control && is_child_text_empty) // Skip layout for an empty label that controls nothing.
  28. return nullptr;
  29. return heap().allocate<Layout::Label>(document(), this, move(style));
  30. }
  31. // https://html.spec.whatwg.org/multipage/forms.html#labeled-control
  32. GC::Ptr<HTMLElement> HTMLLabelElement::control() const
  33. {
  34. GC::Ptr<HTMLElement> control;
  35. // The for attribute may be specified to indicate a form control with which the caption is
  36. // to be associated. If the attribute is specified, the attribute's value must be the ID of
  37. // a labelable element in the same tree as the label element. If the attribute is specified
  38. // and there is an element in the tree whose ID is equal to the value of the for attribute,
  39. // and the first such element in tree order is a labelable element, then that element is the
  40. // label element's labeled control.
  41. if (for_().has_value()) {
  42. root().for_each_in_inclusive_subtree_of_type<HTMLElement>([&](auto& element) {
  43. if (element.id() == *for_() && element.is_labelable()) {
  44. control = &const_cast<HTMLElement&>(element);
  45. return TraversalDecision::Break;
  46. }
  47. return TraversalDecision::Continue;
  48. });
  49. return control;
  50. }
  51. // If the for attribute is not specified, but the label element has a labelable element descendant,
  52. // then the first such descendant in tree order is the label element's labeled control.
  53. for_each_in_subtree_of_type<HTMLElement>([&](auto& element) {
  54. if (element.is_labelable()) {
  55. control = &const_cast<HTMLElement&>(element);
  56. return TraversalDecision::Break;
  57. }
  58. return TraversalDecision::Continue;
  59. });
  60. return control;
  61. }
  62. // https://html.spec.whatwg.org/multipage/forms.html#dom-label-form
  63. GC::Ptr<HTMLFormElement> HTMLLabelElement::form() const
  64. {
  65. auto labeled_control = control();
  66. // 1. If the label element has no labeled control, then return null.
  67. if (!labeled_control)
  68. return {};
  69. // 2. If the label element's labeled control is not a form-associated element, then return null.
  70. if (!is<FormAssociatedElement>(*labeled_control))
  71. return {};
  72. // 3. Return the label element's labeled control's form owner (which can still be null).
  73. return dynamic_cast<FormAssociatedElement*>(labeled_control.ptr())->form();
  74. }
  75. }