From fef757193127ae206af6de381e81084ae3b5e508 Mon Sep 17 00:00:00 2001 From: Bastiaan van der Plaat Date: Sat, 9 Dec 2023 09:08:01 +0100 Subject: [PATCH] LibWeb: Add output element value --- Tests/LibWeb/Text/expected/output-value.txt | 2 + Tests/LibWeb/Text/input/output-value.html | 27 +++++++++++ .../LibWeb/HTML/HTMLOutputElement.cpp | 47 ++++++++++++++++++- .../Libraries/LibWeb/HTML/HTMLOutputElement.h | 8 ++++ .../LibWeb/HTML/HTMLOutputElement.idl | 4 +- 5 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/output-value.txt create mode 100644 Tests/LibWeb/Text/input/output-value.html diff --git a/Tests/LibWeb/Text/expected/output-value.txt b/Tests/LibWeb/Text/expected/output-value.txt new file mode 100644 index 0000000000..b782f874c9 --- /dev/null +++ b/Tests/LibWeb/Text/expected/output-value.txt @@ -0,0 +1,2 @@ +1. "PASS" +2. "PASS" diff --git a/Tests/LibWeb/Text/input/output-value.html b/Tests/LibWeb/Text/input/output-value.html new file mode 100644 index 0000000000..775149a022 --- /dev/null +++ b/Tests/LibWeb/Text/input/output-value.html @@ -0,0 +1,27 @@ + + diff --git a/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.cpp index 44998f057a..b12bcf0ab8 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.cpp @@ -24,12 +24,55 @@ void HTMLOutputElement::initialize(JS::Realm& realm) set_prototype(&Bindings::ensure_web_prototype(realm, "HTMLOutputElement"_fly_string)); } +// https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-defaultvalue +String HTMLOutputElement::default_value() const +{ + // 1. If this element's default value override is non-null, then return it. + if (m_default_value_override.has_value()) + return *m_default_value_override; + + // 2. Return this element's descendant text content. + return descendant_text_content(); +} + +// https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-defaultvalue +void HTMLOutputElement::set_default_value(String const& default_value) +{ + // 1. If this's default value override is null, then string replace all with the given value within this and return. + if (!m_default_value_override.has_value()) { + string_replace_all(default_value); + return; + } + + // 2. Set this's default value override to the given value. + m_default_value_override = default_value; +} + +// https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value +String HTMLOutputElement::value() const +{ + // The value getter steps are to return this's descendant text content. + return descendant_text_content(); +} + +// https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value +void HTMLOutputElement::set_value(String const& value) +{ + // 1. Set this's default value override to its default value. + m_default_value_override = default_value(); + + // 2. String replace all with the given value within this. + string_replace_all(value); +} + // https://html.spec.whatwg.org/multipage/form-elements.html#the-output-element:concept-form-reset-control void HTMLOutputElement::reset_algorithm() { - // 1. FIXME: String replace all with this element's default value within this element. + // 1. String replace all with this element's default value within this element. + string_replace_all(default_value()); - // 2. FIXME: Set this element's default value override to null. + // 2. Set this element's default value override to null. + m_default_value_override = {}; } } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.h b/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.h index ba0bbefa70..8d046329f7 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.h @@ -29,6 +29,12 @@ public: return output; } + String default_value() const; + void set_default_value(String const&); + + String value() const override; + void set_value(String const&); + // ^FormAssociatedElement // https://html.spec.whatwg.org/multipage/forms.html#category-listed virtual bool is_listed() const override { return true; } @@ -52,6 +58,8 @@ private: HTMLOutputElement(DOM::Document&, DOM::QualifiedName); virtual void initialize(JS::Realm&) override; + + Optional m_default_value_override {}; }; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.idl index 35145e17c6..c01264c2d7 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.idl @@ -11,8 +11,8 @@ interface HTMLOutputElement : HTMLElement { [CEReactions, Reflect] attribute DOMString name; readonly attribute DOMString type; - // FIXME: [CEReactions] attribute DOMString defaultValue; - // FIXME: [CEReactions] attribute DOMString value; + [CEReactions] attribute DOMString defaultValue; + [CEReactions] attribute DOMString value; // FIXME: readonly attribute boolean willValidate; // FIXME: readonly attribute ValidityState validity;