2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 1999 Antti Koivisto (koivisto@kde.org)
6 * (C) 2000 Dirk Mueller (mueller@kde.org)
7 * Copyright (C) 2004 Apple Computer, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
25 #ifndef HTML_FORMIMPL_H
26 #define HTML_FORMIMPL_H
28 #include "html/html_elementimpl.h"
29 #include "dom/html_element.h"
30 #include "html/html_miscimpl.h"
32 #include <qptrvector.h>
33 #include <qmemarray.h>
41 class RenderFormElement;
45 class RenderRadioButton;
46 class RenderFileButton;
56 class HTMLFormElement;
57 class HTMLGenericFormElementImpl;
58 class HTMLImageElementImpl;
59 class HTMLImageLoader;
60 class HTMLOptionElementImpl;
61 class HTMLOptionsCollectionImpl;
63 // -------------------------------------------------------------------------
65 class HTMLFormElementImpl : public HTMLElementImpl
68 HTMLFormElementImpl(DocumentPtr *doc);
69 virtual ~HTMLFormElementImpl();
71 virtual Id id() const;
73 virtual void attach();
74 virtual void detach();
78 DOMString enctype() const { return m_enctype; }
79 void setEnctype( const DOMString & );
81 DOMString boundary() const { return m_boundary; }
82 void setBoundary( const DOMString & );
84 bool autoComplete() const { return m_autocomplete; }
86 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
88 void radioClicked( HTMLGenericFormElementImpl *caller );
90 void registerFormElement(HTMLGenericFormElementImpl *);
91 void removeFormElement(HTMLGenericFormElementImpl *);
92 void makeFormElementDormant(HTMLGenericFormElementImpl *);
93 void registerImgElement(HTMLImageElementImpl *);
94 void removeImgElement(HTMLImageElementImpl *);
97 void submit(bool activateSubmitButton);
100 void setMalformed(bool malformed) { m_malformed = malformed; }
101 virtual bool isMalformed() { return m_malformed; }
103 virtual bool isURLAttribute(AttributeImpl *attr) const;
107 bool formWouldHaveSecureSubmission(const DOMString &url);
110 static void i18nData();
112 friend class HTMLFormElement;
113 friend class HTMLFormCollectionImpl;
115 HTMLCollectionImpl::CollectionInfo *collectionInfo;
117 QPtrVector<HTMLGenericFormElementImpl> formElements;
118 QPtrVector<HTMLGenericFormElementImpl> dormantFormElements;
119 QPtrVector<HTMLImageElementImpl> imgElements;
123 DOMString m_boundary;
124 DOMString m_acceptcharset;
126 bool m_multipart : 1;
127 bool m_autocomplete : 1;
129 bool m_doingsubmit : 1;
131 bool m_malformed : 1;
134 bool formData(khtml::FormData &) const;
140 // -------------------------------------------------------------------------
142 class HTMLGenericFormElementImpl : public HTMLElementImpl
144 friend class HTMLFormElementImpl;
145 friend class khtml::RenderFormElement;
148 HTMLGenericFormElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
149 virtual ~HTMLGenericFormElementImpl();
151 HTMLFormElementImpl *form() { return m_form; }
153 virtual DOMString type() const = 0;
155 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
156 virtual void attach();
157 virtual void insertedIntoDocument();
158 virtual void removedFromDocument();
160 virtual void reset() {}
165 virtual bool disabled() const;
166 void setDisabled(bool _disabled);
168 virtual bool isFocusable() const;
169 virtual bool isKeyboardFocusable() const;
170 virtual bool isMouseFocusable() const;
171 virtual bool isEnumeratable() const { return false; }
173 bool readOnly() const { return m_readOnly; }
174 void setReadOnly(bool _readOnly) { m_readOnly = _readOnly; }
176 virtual void recalcStyle( StyleChange );
178 DOMString name() const;
179 void setName(const DOMString& name);
181 virtual bool isGenericFormElement() const { return true; }
184 * override in derived classes to get the encoded name=value pair
186 * return true for a successful control (see HTML4-17.13.2)
188 virtual bool appendFormData(FormDataList&, bool) { return false; }
190 virtual void defaultEventHandler(EventImpl *evt);
191 virtual bool isEditable();
193 virtual QString state();
194 QString findMatchingState(QStringList &states);
196 virtual bool isSuccessfulSubmitButton() const { return false; }
197 virtual bool isActivatedSubmit() const { return false; }
198 virtual void setActivatedSubmit(bool flag) { }
201 HTMLFormElementImpl *getForm() const;
203 DOMStringImpl* m_name;
204 HTMLFormElementImpl *m_form;
205 bool m_disabled, m_readOnly;
211 // -------------------------------------------------------------------------
213 class HTMLButtonElementImpl : public HTMLGenericFormElementImpl
216 HTMLButtonElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
218 virtual ~HTMLButtonElementImpl();
226 virtual Id id() const;
227 DOMString type() const;
229 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
230 virtual void defaultEventHandler(EventImpl *evt);
231 virtual bool appendFormData(FormDataList&, bool);
233 virtual bool isSuccessfulSubmitButton() const;
234 virtual bool isActivatedSubmit() const;
235 virtual void setActivatedSubmit(bool flag);
237 virtual void click();
238 virtual void accessKeyAction();
242 DOMString m_currValue;
245 bool m_activeSubmit : 1;
248 // -------------------------------------------------------------------------
250 class HTMLFieldSetElementImpl : public HTMLGenericFormElementImpl
253 HTMLFieldSetElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
255 virtual ~HTMLFieldSetElementImpl();
257 virtual Id id() const;
259 virtual bool isFocusable() const;
261 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
263 virtual DOMString type() const;
266 // -------------------------------------------------------------------------
268 class HTMLInputElementImpl : public HTMLGenericFormElementImpl
270 friend class khtml::RenderLineEdit;
271 friend class khtml::RenderRadioButton;
272 friend class khtml::RenderFileButton;
275 friend class HTMLSelectElementImpl;
276 friend class khtml::RenderSlider;
280 // do not change the order!
299 HTMLInputElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
300 virtual ~HTMLInputElementImpl();
302 virtual Id id() const;
304 virtual bool isEnumeratable() const { return inputType() != IMAGE; }
306 bool autoComplete() const { return m_autocomplete; }
308 bool checked() const { return m_checked; }
309 void setChecked(bool);
310 long maxLength() const { return m_maxLen; }
311 int size() const { return m_size; }
312 DOMString type() const;
313 void setType(const DOMString& t);
315 DOMString value() const;
316 void setValue(const DOMString &);
321 virtual bool maintainsState() { return m_type != PASSWORD; }
322 virtual QString state();
323 virtual void restoreState(QStringList &);
327 virtual void click();
328 virtual void accessKeyAction();
330 virtual bool mapToEntry(NodeImpl::Id attr, MappedAttributeEntry& result) const;
331 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
333 virtual void attach();
334 virtual bool rendererIsNeeded(khtml::RenderStyle *);
335 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
336 virtual bool appendFormData(FormDataList&, bool);
338 virtual bool isSuccessfulSubmitButton() const;
339 virtual bool isActivatedSubmit() const;
340 virtual void setActivatedSubmit(bool flag);
342 typeEnum inputType() const { return m_type; }
343 virtual void reset();
345 // used in case input type=image was clicked.
346 int clickX() const { return xPos; }
347 int clickY() const { return yPos; }
349 virtual void defaultEventHandler(EventImpl *evt);
350 virtual bool isEditable();
352 DOMString altText() const;
354 virtual bool isURLAttribute(AttributeImpl *attr) const;
357 long maxResults() const { return m_maxResults; }
361 bool storesValueSeparateFromAttribute() const;
373 HTMLImageLoader* m_imageLoader;
377 bool m_defaultChecked : 1;
378 bool m_useDefaultChecked : 1;
380 bool m_activeSubmit : 1;
381 bool m_autocomplete : 1;
384 // -------------------------------------------------------------------------
386 class HTMLLabelElementImpl : public HTMLElementImpl
389 HTMLLabelElementImpl(DocumentPtr *doc);
390 virtual ~HTMLLabelElementImpl();
392 virtual bool isFocusable() const;
394 virtual Id id() const;
396 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
398 virtual void accessKeyAction();
401 * the form element this label is associated to.
403 ElementImpl *formElement();
406 DOMString m_formElementID;
409 // -------------------------------------------------------------------------
411 class HTMLLegendElementImpl : public HTMLGenericFormElementImpl
414 HTMLLegendElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
415 virtual ~HTMLLegendElementImpl();
417 virtual bool isFocusable() const;
419 virtual Id id() const;
420 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
422 virtual DOMString type() const;
425 // -------------------------------------------------------------------------
427 class HTMLSelectElementImpl : public HTMLGenericFormElementImpl
429 friend class khtml::RenderSelect;
432 HTMLSelectElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
433 ~HTMLSelectElementImpl();
435 virtual Id id() const;
436 DOMString type() const;
438 virtual void recalcStyle( StyleChange );
440 long selectedIndex() const;
441 void setSelectedIndex( long index );
443 virtual bool isEnumeratable() const { return true; }
447 long minWidth() const { return m_minwidth; }
449 long size() const { return m_size; }
451 bool multiple() const { return m_multiple; }
453 void add ( HTMLElementImpl *element, HTMLElementImpl *before );
454 void remove ( long index );
459 void setValue(DOMStringImpl* value);
461 HTMLOptionsCollectionImpl *options();
463 virtual bool maintainsState() { return true; }
464 virtual QString state();
465 virtual void restoreState(QStringList &);
467 virtual NodeImpl *insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode );
468 virtual NodeImpl *replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode );
469 virtual NodeImpl *removeChild ( NodeImpl *oldChild, int &exceptioncode );
470 virtual NodeImpl *appendChild ( NodeImpl *newChild, int &exceptioncode );
471 virtual NodeImpl *addChild( NodeImpl* newChild );
473 virtual void childrenChanged();
475 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
477 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
478 virtual bool appendFormData(FormDataList&, bool);
480 // get the actual listbox index of the optionIndexth option
481 int optionToListIndex(int optionIndex) const;
482 // reverse of optionToListIndex - get optionIndex from listboxIndex
483 int listToOptionIndex(int listIndex) const;
485 void setRecalcListItems();
487 QMemArray<HTMLGenericFormElementImpl*> listItems() const
489 if (m_recalcListItems) const_cast<HTMLSelectElementImpl*>(this)->recalcListItems();
492 virtual void reset();
493 void notifyOptionSelected(HTMLOptionElementImpl *selectedOption, bool selected);
496 virtual void defaultEventHandler(EventImpl *evt);
499 virtual void accessKeyAction();
502 void recalcListItems();
505 mutable QMemArray<HTMLGenericFormElementImpl*> m_listItems;
506 HTMLOptionsCollectionImpl *m_options;
510 bool m_recalcListItems;
513 // -------------------------------------------------------------------------
515 class HTMLKeygenElementImpl : public HTMLSelectElementImpl
518 HTMLKeygenElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
520 virtual Id id() const;
522 DOMString type() const;
524 // ### this is just a rough guess
525 virtual bool isEnumeratable() const { return false; }
527 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
528 virtual bool appendFormData(FormDataList&, bool);
530 AtomicString m_challenge;
531 AtomicString m_keyType;
534 // -------------------------------------------------------------------------
536 class HTMLOptGroupElementImpl : public HTMLGenericFormElementImpl
539 HTMLOptGroupElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
540 virtual ~HTMLOptGroupElementImpl();
542 virtual Id id() const;
543 DOMString type() const;
545 virtual bool isFocusable() const;
547 virtual NodeImpl *insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode );
548 virtual NodeImpl *replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode );
549 virtual NodeImpl *removeChild ( NodeImpl *oldChild, int &exceptioncode );
550 virtual NodeImpl *appendChild ( NodeImpl *newChild, int &exceptioncode );
551 virtual NodeImpl *addChild( NodeImpl* newChild );
552 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
553 void recalcSelectOptions();
558 // ---------------------------------------------------------------------------
560 class HTMLOptionElementImpl : public HTMLGenericFormElementImpl
562 friend class khtml::RenderSelect;
563 friend class DOM::HTMLSelectElementImpl;
566 HTMLOptionElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
568 virtual bool isFocusable() const;
570 virtual Id id() const;
571 DOMString type() const;
573 DOMString text() const;
576 void setIndex( long );
577 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
578 DOMString value() const;
579 void setValue(DOMStringImpl* value);
581 bool selected() const { return m_selected; }
582 void setSelected(bool _selected);
584 HTMLSelectElementImpl *getSelect() const;
586 virtual void childrenChanged();
594 // -------------------------------------------------------------------------
596 class HTMLTextAreaElementImpl : public HTMLGenericFormElementImpl
598 friend class khtml::RenderTextArea;
607 HTMLTextAreaElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
608 ~HTMLTextAreaElementImpl();
610 virtual Id id() const;
612 long cols() const { return m_cols; }
614 long rows() const { return m_rows; }
616 WrapMethod wrap() const { return m_wrap; }
618 virtual bool isEnumeratable() const { return true; }
620 DOMString type() const;
622 virtual bool maintainsState() { return true; }
623 virtual QString state();
624 virtual void restoreState(QStringList &);
628 virtual void childrenChanged();
629 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
630 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
631 virtual bool appendFormData(FormDataList&, bool);
632 virtual void reset();
634 void setValue(DOMString _value);
635 DOMString defaultValue();
636 void setDefaultValue(DOMString _defaultValue);
640 virtual bool isEditable();
642 virtual void accessKeyAction();
652 // -------------------------------------------------------------------------
654 class HTMLIsIndexElementImpl : public HTMLInputElementImpl
657 HTMLIsIndexElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
659 virtual Id id() const;
660 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
666 // -------------------------------------------------------------------------
668 class HTMLOptionsCollectionImpl : public khtml::Shared<HTMLOptionsCollectionImpl>
671 HTMLOptionsCollectionImpl(HTMLSelectElementImpl *impl) : m_select(impl) { }
673 unsigned long length() const;
674 void setLength(unsigned long);
675 NodeImpl *item(unsigned long index) const;
676 NodeImpl *namedItem(const DOMString &name) const;
678 void detach() { m_select = 0; }
681 HTMLSelectElementImpl *m_select;