1/*
2 * Copyright (c) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "HTMLOutputElement.h"
33
34#include "HTMLFormElement.h"
35#include "HTMLNames.h"
36#include <wtf/IsoMallocInlines.h>
37#include <wtf/NeverDestroyed.h>
38
39namespace WebCore {
40
41WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLOutputElement);
42
43using namespace HTMLNames;
44
45inline HTMLOutputElement::HTMLOutputElement(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
46 : HTMLFormControlElement(tagName, document, form)
47 , m_isDefaultValueMode(true)
48 , m_isSetTextContentInProgress(false)
49 , m_defaultValue(emptyString())
50{
51}
52
53Ref<HTMLOutputElement> HTMLOutputElement::create(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
54{
55 return adoptRef(*new HTMLOutputElement(tagName, document, form));
56}
57
58const AtomicString& HTMLOutputElement::formControlType() const
59{
60 static NeverDestroyed<const AtomicString> output("output", AtomicString::ConstructFromLiteral);
61 return output;
62}
63
64bool HTMLOutputElement::supportsFocus() const
65{
66 return HTMLElement::supportsFocus();
67}
68
69void HTMLOutputElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
70{
71 if (name == forAttr) {
72 if (m_tokens)
73 m_tokens->associatedAttributeValueChanged(value);
74 } else
75 HTMLFormControlElement::parseAttribute(name, value);
76}
77
78void HTMLOutputElement::childrenChanged(const ChildChange& change)
79{
80 HTMLFormControlElement::childrenChanged(change);
81
82 if (change.source == ChildChangeSource::Parser || m_isSetTextContentInProgress) {
83 m_isSetTextContentInProgress = false;
84 return;
85 }
86
87 if (m_isDefaultValueMode)
88 m_defaultValue = textContent();
89}
90
91void HTMLOutputElement::reset()
92{
93 // The reset algorithm for output elements is to set the element's
94 // value mode flag to "default" and then to set the element's textContent
95 // attribute to the default value.
96 m_isDefaultValueMode = true;
97 if (m_defaultValue == value())
98 return;
99 setTextContentInternal(m_defaultValue);
100}
101
102String HTMLOutputElement::value() const
103{
104 return textContent();
105}
106
107void HTMLOutputElement::setValue(const String& value)
108{
109 // The value mode flag set to "value" when the value attribute is set.
110 m_isDefaultValueMode = false;
111 if (value == this->value())
112 return;
113 setTextContentInternal(value);
114}
115
116String HTMLOutputElement::defaultValue() const
117{
118 return m_defaultValue;
119}
120
121void HTMLOutputElement::setDefaultValue(const String& value)
122{
123 if (m_defaultValue == value)
124 return;
125 m_defaultValue = value;
126 // The spec requires the value attribute set to the default value
127 // when the element's value mode flag to "default".
128 if (m_isDefaultValueMode)
129 setTextContentInternal(value);
130}
131
132DOMTokenList& HTMLOutputElement::htmlFor()
133{
134 if (!m_tokens)
135 m_tokens = std::make_unique<DOMTokenList>(*this, forAttr);
136 return *m_tokens;
137}
138
139void HTMLOutputElement::setTextContentInternal(const String& value)
140{
141 ASSERT(!m_isSetTextContentInProgress);
142 m_isSetTextContentInProgress = true;
143 setTextContent(value);
144}
145
146} // namespace
147