HTMLOutputElement.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/HTML/HTMLOutputElement.h>
  8. namespace Web::HTML {
  9. JS_DEFINE_ALLOCATOR(HTMLOutputElement);
  10. HTMLOutputElement::HTMLOutputElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  11. : HTMLElement(document, move(qualified_name))
  12. {
  13. }
  14. HTMLOutputElement::~HTMLOutputElement() = default;
  15. void HTMLOutputElement::initialize(JS::Realm& realm)
  16. {
  17. Base::initialize(realm);
  18. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLOutputElement);
  19. }
  20. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-defaultvalue
  21. String HTMLOutputElement::default_value() const
  22. {
  23. // 1. If this element's default value override is non-null, then return it.
  24. if (m_default_value_override.has_value())
  25. return *m_default_value_override;
  26. // 2. Return this element's descendant text content.
  27. return descendant_text_content();
  28. }
  29. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-defaultvalue
  30. void HTMLOutputElement::set_default_value(String const& default_value)
  31. {
  32. // 1. If this's default value override is null, then string replace all with the given value within this and return.
  33. if (!m_default_value_override.has_value()) {
  34. string_replace_all(default_value);
  35. return;
  36. }
  37. // 2. Set this's default value override to the given value.
  38. m_default_value_override = default_value;
  39. }
  40. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value
  41. String HTMLOutputElement::value() const
  42. {
  43. // The value getter steps are to return this's descendant text content.
  44. return descendant_text_content();
  45. }
  46. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value
  47. void HTMLOutputElement::set_value(String const& value)
  48. {
  49. // 1. Set this's default value override to its default value.
  50. m_default_value_override = default_value();
  51. // 2. String replace all with the given value within this.
  52. string_replace_all(value);
  53. }
  54. // https://html.spec.whatwg.org/multipage/form-elements.html#the-output-element:concept-form-reset-control
  55. void HTMLOutputElement::reset_algorithm()
  56. {
  57. // 1. String replace all with this element's default value within this element.
  58. string_replace_all(default_value());
  59. // 2. Set this element's default value override to null.
  60. m_default_value_override = {};
  61. }
  62. }