• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
5  * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
6  *           (C) 2006 Alexey Proskuryakov (ap@nypop.com)
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  */
24 
25 #include "config.h"
26 #include "HTMLFormControlElement.h"
27 
28 #include "Document.h"
29 #include "EventHandler.h"
30 #include "EventNames.h"
31 #include "Frame.h"
32 #include "HTMLFormElement.h"
33 #include "HTMLInputElement.h"
34 #include "HTMLNames.h"
35 #include "HTMLParser.h"
36 #include "HTMLTokenizer.h"
37 #include "RenderTheme.h"
38 
39 namespace WebCore {
40 
41 using namespace HTMLNames;
42 
HTMLFormControlElement(const QualifiedName & tagName,Document * doc,HTMLFormElement * f)43 HTMLFormControlElement::HTMLFormControlElement(const QualifiedName& tagName, Document* doc, HTMLFormElement* f)
44     : HTMLElement(tagName, doc)
45     , m_form(f)
46     , m_disabled(false)
47     , m_readOnly(false)
48     , m_valueMatchesRenderer(false)
49 {
50     if (!m_form)
51         m_form = findFormAncestor();
52     if (m_form)
53         m_form->registerFormElement(this);
54 }
55 
~HTMLFormControlElement()56 HTMLFormControlElement::~HTMLFormControlElement()
57 {
58     if (m_form)
59         m_form->removeFormElement(this);
60 }
61 
parseMappedAttribute(MappedAttribute * attr)62 void HTMLFormControlElement::parseMappedAttribute(MappedAttribute *attr)
63 {
64     if (attr->name() == nameAttr) {
65         // Do nothing.
66     } else if (attr->name() == disabledAttr) {
67         bool oldDisabled = m_disabled;
68         m_disabled = !attr->isNull();
69         if (oldDisabled != m_disabled) {
70             setChanged();
71             if (renderer() && renderer()->style()->hasAppearance())
72                 theme()->stateChanged(renderer(), EnabledState);
73         }
74     } else if (attr->name() == readonlyAttr) {
75         bool oldReadOnly = m_readOnly;
76         m_readOnly = !attr->isNull();
77         if (oldReadOnly != m_readOnly) {
78             setChanged();
79             if (renderer() && renderer()->style()->hasAppearance())
80                 theme()->stateChanged(renderer(), ReadOnlyState);
81         }
82     } else
83         HTMLElement::parseMappedAttribute(attr);
84 }
85 
attach()86 void HTMLFormControlElement::attach()
87 {
88     ASSERT(!attached());
89 
90     HTMLElement::attach();
91 
92     // The call to updateFromElement() needs to go after the call through
93     // to the base class's attach() because that can sometimes do a close
94     // on the renderer.
95     if (renderer())
96         renderer()->updateFromElement();
97 
98     // Focus the element if it should honour its autofocus attribute.
99     // We have to determine if the element is a TextArea/Input/Button/Select,
100     // if input type hidden ignore autofocus. So if disabled or readonly.
101     if (autofocus() && renderer() && !document()->ignoreAutofocus() && !isReadOnlyControl() &&
102             ((hasTagName(inputTag) && !isInputTypeHidden()) || hasTagName(selectTag) ||
103               hasTagName(buttonTag) || hasTagName(textareaTag)))
104          focus();
105 }
106 
insertedIntoTree(bool deep)107 void HTMLFormControlElement::insertedIntoTree(bool deep)
108 {
109     if (!m_form) {
110         // This handles the case of a new form element being created by
111         // JavaScript and inserted inside a form.  In the case of the parser
112         // setting a form, we will already have a non-null value for m_form,
113         // and so we don't need to do anything.
114         m_form = findFormAncestor();
115         if (m_form)
116             m_form->registerFormElement(this);
117         else
118             document()->checkedRadioButtons().addButton(this);
119     }
120 
121     HTMLElement::insertedIntoTree(deep);
122 }
123 
findRoot(Node * n)124 static inline Node* findRoot(Node* n)
125 {
126     Node* root = n;
127     for (; n; n = n->parentNode())
128         root = n;
129     return root;
130 }
131 
removedFromTree(bool deep)132 void HTMLFormControlElement::removedFromTree(bool deep)
133 {
134     // If the form and element are both in the same tree, preserve the connection to the form.
135     // Otherwise, null out our form and remove ourselves from the form's list of elements.
136     HTMLParser* parser = 0;
137     if (Tokenizer* tokenizer = document()->tokenizer())
138         if (tokenizer->isHTMLTokenizer())
139             parser = static_cast<HTMLTokenizer*>(tokenizer)->htmlParser();
140 
141     if (m_form && !(parser && parser->isHandlingResidualStyleAcrossBlocks()) && findRoot(this) != findRoot(m_form)) {
142         m_form->removeFormElement(this);
143         m_form = 0;
144     }
145 
146     HTMLElement::removedFromTree(deep);
147 }
148 
name() const149 const AtomicString& HTMLFormControlElement::name() const
150 {
151     const AtomicString& n = getAttribute(nameAttr);
152     return n.isNull() ? emptyAtom : n;
153 }
154 
setName(const AtomicString & value)155 void HTMLFormControlElement::setName(const AtomicString &value)
156 {
157     setAttribute(nameAttr, value);
158 }
159 
onChange()160 void HTMLFormControlElement::onChange()
161 {
162     dispatchEventForType(eventNames().changeEvent, true, false);
163 }
164 
disabled() const165 bool HTMLFormControlElement::disabled() const
166 {
167     return m_disabled;
168 }
169 
setDisabled(bool b)170 void HTMLFormControlElement::setDisabled(bool b)
171 {
172     setAttribute(disabledAttr, b ? "" : 0);
173 }
174 
setReadOnly(bool b)175 void HTMLFormControlElement::setReadOnly(bool b)
176 {
177     setAttribute(readonlyAttr, b ? "" : 0);
178 }
179 
autofocus() const180 bool HTMLFormControlElement::autofocus() const
181 {
182     return hasAttribute(autofocusAttr);
183 }
184 
setAutofocus(bool b)185 void HTMLFormControlElement::setAutofocus(bool b)
186 {
187     setAttribute(autofocusAttr, b ? "autofocus" : 0);
188 }
189 
recalcStyle(StyleChange change)190 void HTMLFormControlElement::recalcStyle(StyleChange change)
191 {
192     HTMLElement::recalcStyle(change);
193 
194     if (renderer())
195         renderer()->updateFromElement();
196 }
197 
isFocusable() const198 bool HTMLFormControlElement::isFocusable() const
199 {
200     if (disabled() || !renderer() ||
201         (renderer()->style() && renderer()->style()->visibility() != VISIBLE) ||
202         !renderer()->isBox() || toRenderBox(renderer())->size().isEmpty())
203         return false;
204     return true;
205 }
206 
isKeyboardFocusable(KeyboardEvent * event) const207 bool HTMLFormControlElement::isKeyboardFocusable(KeyboardEvent* event) const
208 {
209     if (isFocusable())
210         if (document()->frame())
211             return document()->frame()->eventHandler()->tabsToAllControls(event);
212     return false;
213 }
214 
isMouseFocusable() const215 bool HTMLFormControlElement::isMouseFocusable() const
216 {
217 #if PLATFORM(GTK)
218     return HTMLElement::isMouseFocusable();
219 #else
220     return false;
221 #endif
222 }
223 
tabIndex() const224 short HTMLFormControlElement::tabIndex() const
225 {
226     // Skip the supportsFocus check in HTMLElement.
227     return Element::tabIndex();
228 }
229 
willValidate() const230 bool HTMLFormControlElement::willValidate() const
231 {
232     // FIXME: Implementation shall be completed with these checks:
233     //      The control does not have a repetition template as an ancestor.
234     //      The control does not have a datalist element as an ancestor.
235     //      The control is not an output element.
236     return form() && name().length() && !disabled() && !isReadOnlyControl();
237 }
238 
supportsFocus() const239 bool HTMLFormControlElement::supportsFocus() const
240 {
241     return isFocusable() || (!disabled() && !document()->haveStylesheetsLoaded());
242 }
243 
virtualForm() const244 HTMLFormElement* HTMLFormControlElement::virtualForm() const
245 {
246     return m_form;
247 }
248 
removeFromForm()249 void HTMLFormControlElement::removeFromForm()
250 {
251     if (!m_form)
252         return;
253     m_form->removeFormElement(this);
254     m_form = 0;
255 }
256 
HTMLFormControlElementWithState(const QualifiedName & tagName,Document * doc,HTMLFormElement * f)257 HTMLFormControlElementWithState::HTMLFormControlElementWithState(const QualifiedName& tagName, Document* doc, HTMLFormElement* f)
258     : HTMLFormControlElement(tagName, doc, f)
259 {
260     FormControlElementWithState::registerFormControlElementWithState(this, document());
261 }
262 
~HTMLFormControlElementWithState()263 HTMLFormControlElementWithState::~HTMLFormControlElementWithState()
264 {
265     FormControlElementWithState::unregisterFormControlElementWithState(this, document());
266 }
267 
willMoveToNewOwnerDocument()268 void HTMLFormControlElementWithState::willMoveToNewOwnerDocument()
269 {
270     FormControlElementWithState::unregisterFormControlElementWithState(this, document());
271     HTMLFormControlElement::willMoveToNewOwnerDocument();
272 }
273 
didMoveToNewOwnerDocument()274 void HTMLFormControlElementWithState::didMoveToNewOwnerDocument()
275 {
276     FormControlElementWithState::registerFormControlElementWithState(this, document());
277     HTMLFormControlElement::didMoveToNewOwnerDocument();
278 }
279 
finishParsingChildren()280 void HTMLFormControlElementWithState::finishParsingChildren()
281 {
282     HTMLFormControlElement::finishParsingChildren();
283     Document* doc = document();
284     if (doc->hasStateForNewFormElements()) {
285         String state;
286         if (doc->takeStateForFormElement(name().impl(), type().impl(), state))
287             restoreState(state);
288     }
289 }
290 
291 } // namespace Webcore
292