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
37namespace WebCore {
38
39inline HTMLOutputElement::HTMLOutputElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
40    : HTMLFormControlElement(tagName, document, form)
41    , m_isDefaultValueMode(true)
42    , m_isSetTextContentInProgress(false)
43    , m_defaultValue("")
44    , m_tokens(DOMSettableTokenList::create())
45{
46}
47
48PassRefPtr<HTMLOutputElement> HTMLOutputElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
49{
50    return adoptRef(new HTMLOutputElement(tagName, document, form));
51}
52
53const AtomicString& HTMLOutputElement::formControlType() const
54{
55    DEFINE_STATIC_LOCAL(const AtomicString, output, ("output"));
56    return output;
57}
58
59void HTMLOutputElement::parseMappedAttribute(Attribute* attr)
60{
61    if (attr->name() == HTMLNames::forAttr)
62        setFor(attr->value());
63    else
64        HTMLFormControlElement::parseMappedAttribute(attr);
65}
66
67DOMSettableTokenList* HTMLOutputElement::htmlFor() const
68{
69    return m_tokens.get();
70}
71
72void HTMLOutputElement::setFor(const String& value)
73{
74    m_tokens->setValue(value);
75}
76
77void HTMLOutputElement::childrenChanged(bool createdByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
78{
79    if (createdByParser || m_isSetTextContentInProgress) {
80        m_isSetTextContentInProgress = false;
81        return;
82    }
83
84    if (m_isDefaultValueMode)
85        m_defaultValue = textContent();
86    HTMLFormControlElement::childrenChanged(createdByParser, beforeChange, afterChange, childCountDelta);
87}
88
89void HTMLOutputElement::reset()
90{
91    // The reset algorithm for output elements is to set the element's
92    // value mode flag to "default" and then to set the element's textContent
93    // attribute to the default value.
94    m_isDefaultValueMode = true;
95    if (m_defaultValue == value())
96        return;
97    setTextContentInternal(m_defaultValue);
98}
99
100String HTMLOutputElement::value() const
101{
102    return textContent();
103}
104
105void HTMLOutputElement::setValue(const String& value)
106{
107    // The value mode flag set to "value" when the value attribute is set.
108    m_isDefaultValueMode = false;
109    if (value == this->value())
110        return;
111    setTextContentInternal(value);
112}
113
114String HTMLOutputElement::defaultValue() const
115{
116    return m_defaultValue;
117}
118
119void HTMLOutputElement::setDefaultValue(const String& value)
120{
121    if (m_defaultValue == value)
122        return;
123    m_defaultValue = value;
124    // The spec requires the value attribute set to the default value
125    // when the element's value mode flag to "default".
126    if (m_isDefaultValueMode)
127        setTextContentInternal(value);
128}
129
130void HTMLOutputElement::setTextContentInternal(const String& value)
131{
132    ASSERT(!m_isSetTextContentInProgress);
133    ExceptionCode ec;
134    m_isSetTextContentInProgress = true;
135    setTextContent(value, ec);
136}
137
138} // namespace
139