/src/serenity/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, the SerenityOS developers. |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/Bindings/HTMLOutputElementPrototype.h> |
8 | | #include <LibWeb/Bindings/Intrinsics.h> |
9 | | #include <LibWeb/DOM/DOMTokenList.h> |
10 | | #include <LibWeb/HTML/HTMLOutputElement.h> |
11 | | |
12 | | namespace Web::HTML { |
13 | | |
14 | | JS_DEFINE_ALLOCATOR(HTMLOutputElement); |
15 | | |
16 | | HTMLOutputElement::HTMLOutputElement(DOM::Document& document, DOM::QualifiedName qualified_name) |
17 | 0 | : HTMLElement(document, move(qualified_name)) |
18 | 0 | { |
19 | 0 | } |
20 | | |
21 | 0 | HTMLOutputElement::~HTMLOutputElement() = default; |
22 | | |
23 | | void HTMLOutputElement::initialize(JS::Realm& realm) |
24 | 0 | { |
25 | 0 | Base::initialize(realm); |
26 | 0 | WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLOutputElement); |
27 | 0 | } |
28 | | |
29 | | void HTMLOutputElement::visit_edges(Cell::Visitor& visitor) |
30 | 0 | { |
31 | 0 | Base::visit_edges(visitor); |
32 | 0 | visitor.visit(m_html_for); |
33 | 0 | } |
34 | | |
35 | | void HTMLOutputElement::form_associated_element_attribute_changed(FlyString const& name, Optional<String> const& value) |
36 | 0 | { |
37 | 0 | if (name == HTML::AttributeNames::for_) { |
38 | 0 | if (m_html_for) |
39 | 0 | m_html_for->associated_attribute_changed(value.value_or(String {})); |
40 | 0 | } |
41 | 0 | } |
42 | | |
43 | | // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-htmlfor |
44 | | JS::NonnullGCPtr<DOM::DOMTokenList> HTMLOutputElement::html_for() |
45 | 0 | { |
46 | | // The htmlFor IDL attribute must reflect the for content attribute. |
47 | 0 | if (!m_html_for) |
48 | 0 | m_html_for = DOM::DOMTokenList::create(*this, HTML::AttributeNames::for_); |
49 | 0 | return *m_html_for; |
50 | 0 | } |
51 | | |
52 | | // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-defaultvalue |
53 | | String HTMLOutputElement::default_value() const |
54 | 0 | { |
55 | | // 1. If this element's default value override is non-null, then return it. |
56 | 0 | if (m_default_value_override.has_value()) |
57 | 0 | return *m_default_value_override; |
58 | | |
59 | | // 2. Return this element's descendant text content. |
60 | 0 | return descendant_text_content(); |
61 | 0 | } |
62 | | |
63 | | // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-defaultvalue |
64 | | void HTMLOutputElement::set_default_value(String const& default_value) |
65 | 0 | { |
66 | | // 1. If this's default value override is null, then string replace all with the given value within this and return. |
67 | 0 | if (!m_default_value_override.has_value()) { |
68 | 0 | string_replace_all(default_value); |
69 | 0 | return; |
70 | 0 | } |
71 | | |
72 | | // 2. Set this's default value override to the given value. |
73 | 0 | m_default_value_override = default_value; |
74 | 0 | } |
75 | | |
76 | | // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value |
77 | | String HTMLOutputElement::value() const |
78 | 0 | { |
79 | | // The value getter steps are to return this's descendant text content. |
80 | 0 | return descendant_text_content(); |
81 | 0 | } |
82 | | |
83 | | // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value |
84 | | void HTMLOutputElement::set_value(String const& value) |
85 | 0 | { |
86 | | // 1. Set this's default value override to its default value. |
87 | 0 | m_default_value_override = default_value(); |
88 | | |
89 | | // 2. String replace all with the given value within this. |
90 | 0 | string_replace_all(value); |
91 | 0 | } |
92 | | |
93 | | // https://html.spec.whatwg.org/multipage/form-elements.html#the-output-element:concept-form-reset-control |
94 | | void HTMLOutputElement::reset_algorithm() |
95 | 0 | { |
96 | | // 1. String replace all with this element's default value within this element. |
97 | 0 | string_replace_all(default_value()); |
98 | | |
99 | | // 2. Set this element's default value override to null. |
100 | 0 | m_default_value_override = {}; |
101 | 0 | } |
102 | | |
103 | | // https://w3c.github.io/webdriver/#dfn-clear-algorithm |
104 | | void HTMLOutputElement::clear_algorithm() |
105 | 0 | { |
106 | | // The clear algorithm for output elements is set the element's value mode flag to default |
107 | 0 | m_default_value_override = default_value(); |
108 | | |
109 | | // and then to set the element's textContent IDL attribute to an empty string (thus clearing the element's child nodes). |
110 | 0 | string_replace_all({}); |
111 | 0 | } |
112 | | |
113 | | } |