HTMLOutputElement.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HTMLOutputElementPrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/DOM/DOMTokenList.h>
  9. #include <LibWeb/HTML/HTMLOutputElement.h>
  10. namespace Web::HTML {
  11. JS_DEFINE_ALLOCATOR(HTMLOutputElement);
  12. HTMLOutputElement::HTMLOutputElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : HTMLElement(document, move(qualified_name))
  14. {
  15. }
  16. HTMLOutputElement::~HTMLOutputElement() = default;
  17. void HTMLOutputElement::initialize(JS::Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLOutputElement);
  21. }
  22. void HTMLOutputElement::visit_edges(Cell::Visitor& visitor)
  23. {
  24. Base::visit_edges(visitor);
  25. visitor.visit(m_html_for);
  26. }
  27. void HTMLOutputElement::form_associated_element_attribute_changed(FlyString const& name, Optional<String> const& value)
  28. {
  29. if (name == HTML::AttributeNames::for_) {
  30. if (m_html_for)
  31. m_html_for->associated_attribute_changed(value.value_or(String {}));
  32. }
  33. }
  34. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-htmlfor
  35. JS::NonnullGCPtr<DOM::DOMTokenList> HTMLOutputElement::html_for()
  36. {
  37. // The htmlFor IDL attribute must reflect the for content attribute.
  38. if (!m_html_for)
  39. m_html_for = DOM::DOMTokenList::create(*this, HTML::AttributeNames::for_);
  40. return *m_html_for;
  41. }
  42. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-defaultvalue
  43. String HTMLOutputElement::default_value() const
  44. {
  45. // 1. If this element's default value override is non-null, then return it.
  46. if (m_default_value_override.has_value())
  47. return *m_default_value_override;
  48. // 2. Return this element's descendant text content.
  49. return descendant_text_content();
  50. }
  51. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-defaultvalue
  52. void HTMLOutputElement::set_default_value(String const& default_value)
  53. {
  54. // 1. If this's default value override is null, then string replace all with the given value within this and return.
  55. if (!m_default_value_override.has_value()) {
  56. string_replace_all(default_value);
  57. return;
  58. }
  59. // 2. Set this's default value override to the given value.
  60. m_default_value_override = default_value;
  61. }
  62. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value
  63. String HTMLOutputElement::value() const
  64. {
  65. // The value getter steps are to return this's descendant text content.
  66. return descendant_text_content();
  67. }
  68. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value
  69. void HTMLOutputElement::set_value(String const& value)
  70. {
  71. // 1. Set this's default value override to its default value.
  72. m_default_value_override = default_value();
  73. // 2. String replace all with the given value within this.
  74. string_replace_all(value);
  75. }
  76. // https://html.spec.whatwg.org/multipage/form-elements.html#the-output-element:concept-form-reset-control
  77. void HTMLOutputElement::reset_algorithm()
  78. {
  79. // 1. String replace all with this element's default value within this element.
  80. string_replace_all(default_value());
  81. // 2. Set this element's default value override to null.
  82. m_default_value_override = {};
  83. }
  84. }