HTMLLabelElement.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. JS_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. JS::GCPtr<Layout::Node> HTMLLabelElement::create_layout_node(CSS::StyleProperties style)
  24. {
  25. return heap().allocate<Layout::Label>(document(), this, move(style));
  26. }
  27. // https://html.spec.whatwg.org/multipage/forms.html#labeled-control
  28. JS::GCPtr<HTMLElement> HTMLLabelElement::control() const
  29. {
  30. JS::GCPtr<HTMLElement> control;
  31. // The for attribute may be specified to indicate a form control with which the caption is
  32. // to be associated. If the attribute is specified, the attribute's value must be the ID of
  33. // a labelable element in the same tree as the label element. If the attribute is specified
  34. // and there is an element in the tree whose ID is equal to the value of the for attribute,
  35. // and the first such element in tree order is a labelable element, then that element is the
  36. // label element's labeled control.
  37. if (for_().has_value()) {
  38. root().for_each_in_inclusive_subtree_of_type<HTMLElement>([&](auto& element) {
  39. if (element.id() == *for_() && element.is_labelable()) {
  40. control = &const_cast<HTMLElement&>(element);
  41. return TraversalDecision::Break;
  42. }
  43. return TraversalDecision::Continue;
  44. });
  45. return control;
  46. }
  47. // If the for attribute is not specified, but the label element has a labelable element descendant,
  48. // then the first such descendant in tree order is the label element's labeled control.
  49. for_each_in_subtree_of_type<HTMLElement>([&](auto& element) {
  50. if (element.is_labelable()) {
  51. control = &const_cast<HTMLElement&>(element);
  52. return TraversalDecision::Break;
  53. }
  54. return TraversalDecision::Continue;
  55. });
  56. return control;
  57. }
  58. // https://html.spec.whatwg.org/multipage/forms.html#dom-label-form
  59. JS::GCPtr<HTMLFormElement> HTMLLabelElement::form() const
  60. {
  61. auto labeled_control = control();
  62. // 1. If the label element has no labeled control, then return null.
  63. if (!labeled_control)
  64. return {};
  65. // 2. If the label element's labeled control is not a form-associated element, then return null.
  66. if (!is<FormAssociatedElement>(*labeled_control))
  67. return {};
  68. // 3. Return the label element's labeled control's form owner (which can still be null).
  69. return dynamic_cast<FormAssociatedElement*>(labeled_control.ptr())->form();
  70. }
  71. }