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) 2001 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.
29 #include "html/html_formimpl.h"
31 #include "khtmlview.h"
32 #include "khtml_part.h"
33 #include "html/html_documentimpl.h"
34 #include "html_imageimpl.h"
35 #include "khtml_settings.h"
36 #include "misc/htmlhashes.h"
37 #include "misc/formdata.h"
39 #include "css/cssstyleselector.h"
40 #include "css/cssproperties.h"
41 #include "css/csshelper.h"
42 #include "xml/dom_textimpl.h"
43 #include "xml/dom2_eventsimpl.h"
44 #include "khtml_ext.h"
46 #include "rendering/render_form.h"
48 #include <kcharsets.h>
51 #include <kmimetype.h>
52 #include <kmessagebox.h>
54 #include <netaccess.h>
55 #include <kfileitem.h>
57 #include <qtextcodec.h>
61 #include <ksslkeygen.h>
65 using namespace khtml;
71 FormDataList(QTextCodec *);
73 void appendData(const DOMString &key, const DOMString &value)
74 { appendString(key.string()); appendString(value.string()); }
75 void appendData(const DOMString &key, const QString &value)
76 { appendString(key.string()); appendString(value); }
77 void appendData(const DOMString &key, const QCString &value)
78 { appendString(key.string()); appendString(value); }
79 void appendData(const DOMString &key, int value)
80 { appendString(key.string()); appendString(QString::number(value)); }
81 void appendFile(const DOMString &key, const DOMString &filename);
84 QValueListConstIterator<QCString> begin() const;
85 QValueListConstIterator<QCString> end() const;
88 void appendString(const QCString &s);
89 void appendString(const QString &s);
92 QValueList<QCString> m_strings;
95 HTMLFormElementImpl::HTMLFormElementImpl(DocumentPtr *doc)
96 : HTMLElementImpl(doc)
100 m_autocomplete = true;
102 m_doingsubmit = false;
104 m_enctype = "application/x-www-form-urlencoded";
105 m_boundary = "----------0xKhTmLbOuNdArY";
106 m_acceptcharset = "UNKNOWN";
110 HTMLFormElementImpl::~HTMLFormElementImpl()
112 for (unsigned i = 0; i < formElements.count(); ++i)
113 formElements[i]->m_form = 0;
114 for (unsigned i = 0; i < dormantFormElements.count(); ++i)
115 dormantFormElements[i]->m_form = 0;
116 for (unsigned i = 0; i < imgElements.count(); ++i)
117 imgElements[i]->m_form = 0;
120 NodeImpl::Id HTMLFormElementImpl::id() const
127 bool HTMLFormElementImpl::formWouldHaveSecureSubmission(const DOMString &url)
132 return getDocument()->completeURL(url.string()).startsWith("https:", false);
137 void HTMLFormElementImpl::attach()
139 HTMLElementImpl::attach();
141 if (getDocument()->isHTMLDocument()) {
142 HTMLDocumentImpl *document = static_cast<HTMLDocumentImpl *>(getDocument());
143 document->addNamedImageOrForm(oldNameAttr);
144 document->addNamedImageOrForm(oldIdAttr);
148 // note we don't deal with calling secureFormRemoved() on detach, because the timing
149 // was such that it cleared our state too early
150 if (formWouldHaveSecureSubmission(m_url))
151 getDocument()->secureFormAdded();
155 void HTMLFormElementImpl::detach()
157 if (getDocument()->isHTMLDocument()) {
158 HTMLDocumentImpl *document = static_cast<HTMLDocumentImpl *>(getDocument());
159 document->removeNamedImageOrForm(oldNameAttr);
160 document->removeNamedImageOrForm(oldIdAttr);
163 HTMLElementImpl::detach();
166 long HTMLFormElementImpl::length() const
169 for (unsigned i = 0; i < formElements.count(); ++i)
170 if (formElements[i]->isEnumeratable())
178 void HTMLFormElementImpl::submitClick()
180 bool submitFound = false;
181 for (unsigned i = 0; i < formElements.count(); ++i) {
182 if (formElements[i]->id() == ID_INPUT) {
183 HTMLInputElementImpl *element = static_cast<HTMLInputElementImpl *>(formElements[i]);
184 if (element->isSuccessfulSubmitButton() && element->renderer()) {
191 if (!submitFound) // submit the form without a submit or image input
195 #endif // APPLE_CHANGES
197 static QCString encodeCString(const QCString& e)
199 // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
200 // safe characters like NS handles them for compatibility
201 static const char *safe = "-._*";
202 int elen = e.length();
203 QCString encoded(( elen+e.contains( '\n' ) )*3+1);
206 //QCString orig(e.data(), e.size());
208 for(int pos = 0; pos < elen; pos++) {
209 unsigned char c = e[pos];
211 if ( (( c >= 'A') && ( c <= 'Z')) ||
212 (( c >= 'a') && ( c <= 'z')) ||
213 (( c >= '0') && ( c <= '9')) ||
216 encoded[enclen++] = c;
218 encoded[enclen++] = '+';
219 else if ( c == '\n' || ( c == '\r' && e[pos+1] != '\n' ) )
221 encoded[enclen++] = '%';
222 encoded[enclen++] = '0';
223 encoded[enclen++] = 'D';
224 encoded[enclen++] = '%';
225 encoded[enclen++] = '0';
226 encoded[enclen++] = 'A';
228 else if ( c != '\r' )
230 encoded[enclen++] = '%';
231 unsigned int h = c / 16;
232 h += (h > 9) ? ('A' - 10) : '0';
233 encoded[enclen++] = h;
235 unsigned int l = c % 16;
236 l += (l > 9) ? ('A' - 10) : '0';
237 encoded[enclen++] = l;
240 encoded[enclen++] = '\0';
241 encoded.truncate(enclen);
246 // Change plain CR and plain LF to CRLF pairs.
247 static QCString fixLineBreaks(const QCString &s)
249 // Compute the length.
251 const char *p = s.data();
252 while (char c = *p++) {
254 // Safe to look ahead because of trailing '\0'.
256 // Turn CR into CRLF.
259 } else if (c == '\n') {
260 // Turn LF into CRLF.
263 // Leave other characters alone.
267 if (newLen == s.length()) {
271 // Make a copy of the string.
273 QCString result(newLen + 1);
274 char *q = result.data();
275 while (char c = *p++) {
277 // Safe to look ahead because of trailing '\0'.
279 // Turn CR into CRLF.
283 } else if (c == '\n') {
284 // Turn LF into CRLF.
288 // Leave other characters alone.
297 void HTMLFormElementImpl::i18nData()
299 QString foo1 = i18n( "You're about to send data to the Internet "
300 "via an unencrypted connection. It might be possible "
301 "for others to see this information.\n"
302 "Do you want to continue?");
303 QString foo2 = i18n("KDE Web browser");
304 QString foo3 = i18n("When you send a password unencrypted to the Internet, "
305 "it might be possible for others to capture it as plain text.\n"
306 "Do you want to continue?");
307 QString foo5 = i18n("Your data submission is redirected to "
308 "an insecure site. The data is sent unencrypted.\n"
309 "Do you want to continue?");
310 QString foo6 = i18n("The page contents expired. You can repost the form"
311 "data by using <a href=\"javascript:go(0);\">Reload</a>");
316 bool HTMLFormElementImpl::formData(FormData &form_data) const
319 kdDebug( 6030 ) << "form: formData()" << endl;
322 QCString enc_string = ""; // used for non-multipart data
324 // find out the QTextcodec to use
325 QString str = m_acceptcharset.string();
326 str.replace(',', ' ');
327 QStringList charsets = QStringList::split(' ', str);
328 QTextCodec* codec = 0;
329 KHTMLPart *part = getDocument()->part();
330 for ( QStringList::Iterator it = charsets.begin(); it != charsets.end(); ++it )
333 if(enc.contains("UNKNOWN"))
335 // use standard document encoding
338 enc = part->encoding();
340 if((codec = KGlobal::charsets()->codecForName(enc.latin1())))
345 codec = QTextCodec::codecForLocale();
348 QStringList fileUploads;
351 for (unsigned i = 0; i < formElements.count(); ++i) {
352 HTMLGenericFormElementImpl* current = formElements[i];
353 FormDataList lst(codec);
355 if (!current->disabled() && current->appendFormData(lst, m_multipart))
357 //kdDebug(6030) << "adding name " << current->name().string() << endl;
358 for(QValueListConstIterator<QCString> it = lst.begin(); it != lst.end(); ++it )
362 // handle ISINDEX / <input name=isindex> special
363 // but only if its the first entry
364 if ( enc_string.isEmpty() && *it == "isindex" ) {
366 enc_string += encodeCString( *it );
369 if(!enc_string.isEmpty())
372 enc_string += encodeCString(*it);
375 enc_string += encodeCString(*it);
381 hstr += m_boundary.string().latin1();
383 hstr += "Content-Disposition: form-data; name=\"";
384 hstr += (*it).data();
387 // if the current type is FILE, then we also need to
388 // include the filename
389 if (current->nodeType() == Node::ELEMENT_NODE && current->id() == ID_INPUT &&
390 static_cast<HTMLInputElementImpl*>(current)->inputType() == HTMLInputElementImpl::FILE)
392 QString path = static_cast<HTMLInputElementImpl*>(current)->value().string();
394 if (path.length()) fileUploads << path;
397 // FIXME: This won't work if the filename includes a " mark,
398 // or control characters like CR or LF. This also does strange
399 // things if the filename includes characters you can't encode
400 // in the website's character set.
401 hstr += "; filename=\"";
402 hstr += codec->fromUnicode(path.mid(path.findRev('/') + 1));
405 if(!static_cast<HTMLInputElementImpl*>(current)->value().isEmpty())
408 QString mimeType = part ? KWQ(part)->mimeTypeForFileName(path) : QString();
410 KMimeType::Ptr ptr = KMimeType::findByURL(KURL(path));
411 QString mimeType = ptr->name();
413 if (!mimeType.isEmpty()) {
414 hstr += "\r\nContent-Type: ";
415 hstr += mimeType.ascii();
424 form_data.appendData(hstr.data(), hstr.length());
425 form_data.appendData(*it, (*it).size() - 1);
426 form_data.appendData("\r\n", 2);
433 if (fileUploads.count()) {
434 int result = KMessageBox::warningContinueCancelList( 0,
435 i18n("You're about to transfer the following files from "
436 "your local computer to the Internet.\n"
437 "Do you really want to continue?"),
441 if (result == KMessageBox::Cancel) {
448 enc_string = ("--" + m_boundary.string() + "--\r\n").ascii();
450 form_data.appendData(enc_string.data(), enc_string.length());
454 void HTMLFormElementImpl::setEnctype( const DOMString& type )
456 if(type.string().find("multipart", 0, false) != -1 || type.string().find("form-data", 0, false) != -1)
458 m_enctype = "multipart/form-data";
461 } else if (type.string().find("text", 0, false) != -1 || type.string().find("plain", 0, false) != -1)
463 m_enctype = "text/plain";
468 m_enctype = "application/x-www-form-urlencoded";
473 void HTMLFormElementImpl::setBoundary( const DOMString& bound )
478 bool HTMLFormElementImpl::prepareSubmit()
480 KHTMLPart *part = getDocument()->part();
481 if(m_insubmit || !part || part->onlyLocalReferences())
485 m_doingsubmit = false;
487 if ( dispatchHTMLEvent(EventImpl::SUBMIT_EVENT,false,true) && !m_doingsubmit )
488 m_doingsubmit = true;
495 return m_doingsubmit;
498 void HTMLFormElementImpl::submit( bool activateSubmitButton )
500 KHTMLView *view = getDocument()->view();
501 KHTMLPart *part = getDocument()->part();
502 if (!view || !part) {
507 m_doingsubmit = true;
514 kdDebug( 6030 ) << "submitting!" << endl;
517 HTMLGenericFormElementImpl* firstSuccessfulSubmitButton = 0;
518 bool needButtonActivation = activateSubmitButton; // do we need to activate a submit button?
521 KWQ(part)->clearRecordedFormValues();
523 for (unsigned i = 0; i < formElements.count(); ++i) {
524 HTMLGenericFormElementImpl* current = formElements[i];
526 // Our app needs to get form values for password fields for doing password autocomplete,
527 // so we are more lenient in pushing values, and let the app decide what to save when.
528 if (current->id() == ID_INPUT) {
529 HTMLInputElementImpl *input = static_cast<HTMLInputElementImpl*>(current);
530 if (input->inputType() == HTMLInputElementImpl::TEXT
531 || input->inputType() == HTMLInputElementImpl::PASSWORD
532 || input->inputType() == HTMLInputElementImpl::SEARCH)
534 KWQ(part)->recordFormValue(input->name().string(), input->value().string(), this);
535 if (input->renderer() && input->inputType() == HTMLInputElementImpl::SEARCH)
536 static_cast<RenderLineEdit*>(input->renderer())->addSearchResult();
540 if (current->id() == ID_INPUT &&
541 static_cast<HTMLInputElementImpl*>(current)->inputType() == HTMLInputElementImpl::TEXT &&
542 static_cast<HTMLInputElementImpl*>(current)->autoComplete() )
544 HTMLInputElementImpl *input = static_cast<HTMLInputElementImpl *>(current);
545 view->addFormCompletionItem(input->name().string(), input->value().string());
549 if (needButtonActivation) {
550 if (current->isActivatedSubmit()) {
551 needButtonActivation = false;
552 } else if (firstSuccessfulSubmitButton == 0 && current->isSuccessfulSubmitButton()) {
553 firstSuccessfulSubmitButton = current;
558 if (needButtonActivation && firstSuccessfulSubmitButton) {
559 firstSuccessfulSubmitButton->setActivatedSubmit(true);
563 if (formData(form_data)) {
565 part->submitForm( "post", m_url.string(), form_data,
568 boundary().string() );
571 part->submitForm( "get", m_url.string(), form_data,
576 if (needButtonActivation && firstSuccessfulSubmitButton) {
577 firstSuccessfulSubmitButton->setActivatedSubmit(false);
580 m_doingsubmit = m_insubmit = false;
583 void HTMLFormElementImpl::reset( )
585 KHTMLPart *part = getDocument()->part();
586 if(m_inreset || !part) return;
591 kdDebug( 6030 ) << "reset pressed!" << endl;
594 // ### DOM2 labels this event as not cancelable, however
595 // common browsers( sick! ) allow it be cancelled.
596 if ( !dispatchHTMLEvent(EventImpl::RESET_EVENT,true, true) ) {
601 for (unsigned i = 0; i < formElements.count(); ++i)
602 formElements[i]->reset();
607 void HTMLFormElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
614 bool oldURLWasSecure = formWouldHaveSecureSubmission(m_url);
616 m_url = khtml::parseURL(attr->value());
618 bool newURLIsSecure = formWouldHaveSecureSubmission(m_url);
620 if (m_attached && (oldURLWasSecure != newURLIsSecure))
622 getDocument()->secureFormAdded();
624 getDocument()->secureFormRemoved();
629 m_target = attr->value();
632 if ( strcasecmp( attr->value(), "post" ) == 0 )
634 else if ( strcasecmp( attr->value(), "get" ) == 0 )
638 setEnctype( attr->value() );
640 case ATTR_ACCEPT_CHARSET:
641 // space separated list of charsets the server
642 // accepts - see rfc2045
643 m_acceptcharset = attr->value();
646 // ignore this one for the moment...
648 case ATTR_AUTOCOMPLETE:
649 m_autocomplete = strcasecmp( attr->value(), "off" );
652 setHTMLEventListener(EventImpl::SUBMIT_EVENT,
653 getDocument()->createHTMLEventListener(attr->value().string()));
656 setHTMLEventListener(EventImpl::RESET_EVENT,
657 getDocument()->createHTMLEventListener(attr->value().string()));
661 QString newNameAttr = attr->value().string();
662 if (attached() && getDocument()->isHTMLDocument()) {
663 HTMLDocumentImpl *document = static_cast<HTMLDocumentImpl *>(getDocument());
664 document->removeNamedImageOrForm(oldNameAttr);
665 document->addNamedImageOrForm(newNameAttr);
667 oldNameAttr = newNameAttr;
672 QString newIdAttr = attr->value().string();
673 if (attached() && getDocument()->isHTMLDocument()) {
674 HTMLDocumentImpl *document = static_cast<HTMLDocumentImpl *>(getDocument());
675 document->removeNamedImageOrForm(oldIdAttr);
676 document->addNamedImageOrForm(newIdAttr);
678 oldIdAttr = newIdAttr;
682 HTMLElementImpl::parseHTMLAttribute(attr);
686 void HTMLFormElementImpl::radioClicked( HTMLGenericFormElementImpl *caller )
688 for (unsigned i = 0; i < formElements.count(); ++i) {
689 HTMLGenericFormElementImpl *current = formElements[i];
690 if (current->id() == ID_INPUT &&
691 static_cast<HTMLInputElementImpl*>(current)->inputType() == HTMLInputElementImpl::RADIO &&
692 current != caller && current->form() == caller->form() && current->name() == caller->name()) {
693 static_cast<HTMLInputElementImpl*>(current)->setChecked(false);
698 template<class T> static void appendToVector(QPtrVector<T> &vec, T *item)
700 unsigned size = vec.size();
701 unsigned count = vec.count();
703 vec.resize(size == 0 ? 8 : (int)(size * 1.5));
704 vec.insert(count, item);
707 template<class T> static void removeFromVector(QPtrVector<T> &vec, T *item)
709 int pos = vec.findRef(item);
710 int count = vec.count();
714 for (int i = pos; i < count - 2; i++) {
715 vec.insert(i, vec[i+1]);
717 vec.remove(count - 1);
720 void HTMLFormElementImpl::registerFormElement(HTMLGenericFormElementImpl *e)
722 appendToVector(formElements, e);
723 removeFromVector(dormantFormElements, e);
726 void HTMLFormElementImpl::removeFormElement(HTMLGenericFormElementImpl *e)
728 removeFromVector(formElements, e);
729 removeFromVector(dormantFormElements, e);
732 void HTMLFormElementImpl::makeFormElementDormant(HTMLGenericFormElementImpl *e)
734 appendToVector(dormantFormElements, e);
735 removeFromVector(formElements, e);
738 bool HTMLFormElementImpl::isURLAttribute(AttributeImpl *attr) const
740 return attr->id() == ATTR_ACTION;
743 void HTMLFormElementImpl::registerImgElement(HTMLImageElementImpl *e)
745 appendToVector(imgElements, e);
748 void HTMLFormElementImpl::removeImgElement(HTMLImageElementImpl *e)
750 removeFromVector(imgElements, e);
753 // -------------------------------------------------------------------------
755 HTMLGenericFormElementImpl::HTMLGenericFormElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
756 : HTMLElementImpl(doc)
758 m_disabled = m_readOnly = false;
767 m_form->registerFormElement(this);
770 HTMLGenericFormElementImpl::~HTMLGenericFormElementImpl()
773 m_form->removeFormElement(this);
774 if (m_name) m_name->deref();
777 void HTMLGenericFormElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
784 setDisabled( !attr->isNull() );
788 bool m_oldreadOnly = m_readOnly;
789 m_readOnly = !attr->isNull();
790 if (m_oldreadOnly != m_readOnly) setChanged();
794 HTMLElementImpl::parseHTMLAttribute(attr);
798 void HTMLGenericFormElementImpl::attach()
802 // FIXME: This handles the case of a new form element being created by
803 // JavaScript and inserted inside a form. What it does not handle is
804 // a form element being moved from inside a form to outside, or from one
805 // inside one form to another. The reason this other case is hard to fix
806 // is that during parsing, we may have been passed a form that we are not
807 // inside, DOM-tree-wise. If so, it's hard for us to know when we should
808 // be removed from that form's element list.
812 m_form->registerFormElement(this);
815 HTMLElementImpl::attach();
817 // The call to updateFromElement() needs to go after the call through
818 // to the base class's attach() because that can sometimes do a close
821 m_render->updateFromElement();
823 // Delayed attachment in order to prevent FOUC can result in an object being
824 // programmatically focused before it has a render object. If we have been focused
825 // (i.e., if we are the focusNode) then go ahead and focus our corresponding native widget.
826 // (Attach/detach can also happen as a result of display type changes, e.g., making a widget
827 // block instead of inline, and focus should be restored in that case as well.)
828 if (getDocument()->focusNode() == this && m_render->isWidget() &&
829 static_cast<RenderWidget*>(renderer())->widget())
830 static_cast<RenderWidget*>(renderer())->widget()->setFocus();
834 void HTMLGenericFormElementImpl::insertedIntoDocument()
836 if (m_form && m_dormant)
837 m_form->registerFormElement(this);
841 HTMLElementImpl::insertedIntoDocument();
844 void HTMLGenericFormElementImpl::removedFromDocument()
847 m_form->makeFormElementDormant(this);
851 HTMLElementImpl::removedFromDocument();
854 HTMLFormElementImpl *HTMLGenericFormElementImpl::getForm() const
856 NodeImpl *p = parentNode();
859 if( p->id() == ID_FORM )
860 return static_cast<HTMLFormElementImpl *>(p);
864 kdDebug( 6030 ) << "couldn't find form!" << endl;
869 DOMString HTMLGenericFormElementImpl::name() const
871 if (m_name) return m_name;
874 // DOMString n = getDocument()->htmlMode() != DocumentImpl::XHtml ?
875 // getAttribute(ATTR_NAME) : getAttribute(ATTR_ID);
876 DOMString n = getAttribute(ATTR_NAME);
878 return new DOMStringImpl("");
883 void HTMLGenericFormElementImpl::setName(const DOMString& name)
885 if (m_name) m_name->deref();
886 m_name = name.implementation();
887 if (m_name) m_name->ref();
890 void HTMLGenericFormElementImpl::onSelect()
892 // ### make this work with new form events architecture
893 dispatchHTMLEvent(EventImpl::SELECT_EVENT,true,false);
896 void HTMLGenericFormElementImpl::onChange()
898 // ### make this work with new form events architecture
899 dispatchHTMLEvent(EventImpl::CHANGE_EVENT,true,false);
902 bool HTMLGenericFormElementImpl::disabled() const
907 void HTMLGenericFormElementImpl::setDisabled( bool _disabled )
909 if ( m_disabled != _disabled ) {
910 m_disabled = _disabled;
915 void HTMLGenericFormElementImpl::recalcStyle( StyleChange ch )
917 //bool changed = changed();
918 HTMLElementImpl::recalcStyle( ch );
920 if (m_render /*&& changed*/)
921 m_render->updateFromElement();
924 bool HTMLGenericFormElementImpl::isFocusable() const
926 if (!m_render || (m_render->style() && m_render->style()->visibility() != VISIBLE) || m_render->width() == 0 || m_render->height() == 0)
931 bool HTMLGenericFormElementImpl::isKeyboardFocusable() const
934 if (m_render->isWidget()) {
935 return static_cast<RenderWidget*>(m_render)->widget() &&
936 (static_cast<RenderWidget*>(m_render)->widget()->focusPolicy() & QWidget::TabFocus);
938 if (getDocument()->part())
939 return getDocument()->part()->tabsToAllControls();
944 bool HTMLGenericFormElementImpl::isMouseFocusable() const
947 if (m_render->isWidget()) {
948 return static_cast<RenderWidget*>(m_render)->widget() &&
949 (static_cast<RenderWidget*>(m_render)->widget()->focusPolicy() & QWidget::ClickFocus);
952 // For <input type=image> and <button>, we will assume no mouse focusability. This is
953 // consistent with OS X behavior for buttons.
962 void HTMLGenericFormElementImpl::defaultEventHandler(EventImpl *evt)
964 if (evt->target()==this)
966 // Report focus in/out changes to the browser extension (editable widgets only)
967 KHTMLPart *part = getDocument()->part();
968 if (evt->id()==EventImpl::DOMFOCUSIN_EVENT && isEditable() && part && m_render && m_render->isWidget()) {
969 KHTMLPartBrowserExtension *ext = static_cast<KHTMLPartBrowserExtension *>(part->browserExtension());
970 QWidget *widget = static_cast<RenderWidget*>(m_render)->widget();
972 ext->editableWidgetFocused(widget);
976 // We don't want this default key event handling, we'll count on
977 // Cocoa event dispatch if the event doesn't get blocked.
979 if (evt->id()==EventImpl::KEYDOWN_EVENT ||
980 evt->id()==EventImpl::KEYUP_EVENT)
982 KeyboardEventImpl * k = static_cast<KeyboardEventImpl *>(evt);
983 if (k->keyVal() == QChar('\n').unicode() && m_render && m_render->isWidget() && k->qKeyEvent)
984 QApplication::sendEvent(static_cast<RenderWidget *>(m_render)->widget(), k->qKeyEvent);
988 if (evt->id()==EventImpl::DOMFOCUSOUT_EVENT && isEditable() && part && m_render && m_render->isWidget()) {
989 KHTMLPartBrowserExtension *ext = static_cast<KHTMLPartBrowserExtension *>(part->browserExtension());
990 QWidget *widget = static_cast<RenderWidget*>(m_render)->widget();
992 ext->editableWidgetBlurred(widget);
994 // ### Don't count popup as a valid reason for losing the focus (example: opening the options of a select
995 // combobox shouldn't emit onblur)
998 HTMLElementImpl::defaultEventHandler(evt);
1001 bool HTMLGenericFormElementImpl::isEditable()
1006 // Special chars used to encode form state strings.
1007 // We pick chars that are unlikely to be used in an HTML attr, so we rarely have to really encode.
1008 const char stateSeparator = '&';
1009 const char stateEscape = '<';
1010 static const char stateSeparatorMarker[] = "<A";
1011 static const char stateEscapeMarker[] = "<<";
1013 // Encode an element name so we can put it in a state string without colliding
1014 // with our separator char.
1015 static QString encodedElementName(QString str)
1017 int sepLoc = str.find(stateSeparator);
1018 int escLoc = str.find(stateSeparator);
1019 if (sepLoc >= 0 || escLoc >= 0) {
1020 QString newStr = str;
1021 // replace "<" with "<<"
1022 while (escLoc >= 0) {
1023 newStr.replace(escLoc, 1, stateEscapeMarker);
1024 escLoc = str.find(stateSeparator, escLoc+1);
1026 // replace "&" with "<A"
1027 while (sepLoc >= 0) {
1028 newStr.replace(sepLoc, 1, stateSeparatorMarker);
1029 sepLoc = str.find(stateSeparator, sepLoc+1);
1037 QString HTMLGenericFormElementImpl::state( )
1039 // Build a string that contains ElementName&ElementType&
1040 return encodedElementName(name().string()) + stateSeparator + type().string() + stateSeparator;
1043 QString HTMLGenericFormElementImpl::findMatchingState(QStringList &states)
1045 QString encName = encodedElementName(name().string());
1046 QString typeStr = type().string();
1047 for (QStringList::Iterator it = states.begin(); it != states.end(); ++it) {
1048 QString state = *it;
1049 int sep1 = state.find(stateSeparator);
1050 int sep2 = state.find(stateSeparator, sep1+1);
1054 QString nameAndType = state.left(sep2);
1055 if (encName.length() + typeStr.length() + 1 == (uint)sep2
1056 && nameAndType.startsWith(encName)
1057 && nameAndType.endsWith(typeStr))
1060 return state.mid(sep2+1);
1063 return QString::null;
1066 // -------------------------------------------------------------------------
1068 HTMLButtonElementImpl::HTMLButtonElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
1069 : HTMLGenericFormElementImpl(doc, f)
1073 m_activeSubmit = false;
1076 HTMLButtonElementImpl::~HTMLButtonElementImpl()
1080 NodeImpl::Id HTMLButtonElementImpl::id() const
1085 DOMString HTMLButtonElementImpl::type() const
1087 return getAttribute(ATTR_TYPE);
1090 void HTMLButtonElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
1095 if ( strcasecmp( attr->value(), "submit" ) == 0 )
1097 else if ( strcasecmp( attr->value(), "reset" ) == 0 )
1099 else if ( strcasecmp( attr->value(), "button" ) == 0 )
1103 m_value = attr->value();
1104 m_currValue = m_value;
1106 case ATTR_ACCESSKEY:
1109 setHTMLEventListener(EventImpl::FOCUS_EVENT,
1110 getDocument()->createHTMLEventListener(attr->value().string()));
1113 setHTMLEventListener(EventImpl::BLUR_EVENT,
1114 getDocument()->createHTMLEventListener(attr->value().string()));
1117 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
1121 void HTMLButtonElementImpl::defaultEventHandler(EventImpl *evt)
1123 if (m_type != BUTTON && (evt->id() == EventImpl::DOMACTIVATE_EVENT)) {
1125 if(m_form && m_type == SUBMIT) {
1126 m_activeSubmit = true;
1127 m_form->prepareSubmit();
1128 m_activeSubmit = false; // in case we were canceled
1130 if(m_form && m_type == RESET) m_form->reset();
1132 HTMLGenericFormElementImpl::defaultEventHandler(evt);
1135 bool HTMLButtonElementImpl::isSuccessfulSubmitButton() const
1137 // HTML spec says that buttons must have names
1138 // to be considered successful. However, other browsers
1139 // do not impose this constraint. Therefore, we behave
1140 // differently and can use different buttons than the
1142 // Remove the name constraint for now.
1143 // Was: m_type == SUBMIT && !m_disabled && !name().isEmpty()
1144 return m_type == SUBMIT && !m_disabled;
1147 bool HTMLButtonElementImpl::isActivatedSubmit() const
1149 return m_activeSubmit;
1152 void HTMLButtonElementImpl::setActivatedSubmit(bool flag)
1154 m_activeSubmit = flag;
1157 bool HTMLButtonElementImpl::appendFormData(FormDataList& encoding, bool /*multipart*/)
1159 if (m_type != SUBMIT || name().isEmpty() || !m_activeSubmit)
1161 encoding.appendData(name(), m_currValue);
1165 void HTMLButtonElementImpl::click()
1169 if (renderer() && (widget = static_cast<RenderWidget *>(renderer())->widget())) {
1170 // using this method gives us nice Cocoa user interface feedback
1171 static_cast<QButton *>(widget)->click();
1175 HTMLGenericFormElementImpl::click();
1178 void HTMLButtonElementImpl::accessKeyAction()
1183 // -------------------------------------------------------------------------
1185 HTMLFieldSetElementImpl::HTMLFieldSetElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
1186 : HTMLGenericFormElementImpl(doc, f)
1190 HTMLFieldSetElementImpl::~HTMLFieldSetElementImpl()
1194 bool HTMLFieldSetElementImpl::isFocusable() const
1199 NodeImpl::Id HTMLFieldSetElementImpl::id() const
1204 DOMString HTMLFieldSetElementImpl::type() const
1209 RenderObject* HTMLFieldSetElementImpl::createRenderer(RenderArena* arena, RenderStyle* style)
1211 return new (arena) RenderFieldset(this);
1214 // -------------------------------------------------------------------------
1216 HTMLInputElementImpl::HTMLInputElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
1217 : HTMLGenericFormElementImpl(doc, f), m_imageLoader(0)
1223 m_defaultChecked = false;
1224 m_useDefaultChecked = true;
1227 m_activeSubmit = false;
1228 m_autocomplete = true;
1239 m_autocomplete = f->autoComplete();
1242 HTMLInputElementImpl::~HTMLInputElementImpl()
1244 if (getDocument()) getDocument()->deregisterMaintainsState(this);
1245 delete m_imageLoader;
1248 NodeImpl::Id HTMLInputElementImpl::id() const
1253 void HTMLInputElementImpl::setType(const DOMString& t)
1257 if ( strcasecmp( t, "password" ) == 0 )
1259 else if ( strcasecmp( t, "checkbox" ) == 0 )
1261 else if ( strcasecmp( t, "radio" ) == 0 )
1263 else if ( strcasecmp( t, "submit" ) == 0 )
1265 else if ( strcasecmp( t, "reset" ) == 0 )
1267 else if ( strcasecmp( t, "file" ) == 0 )
1269 else if ( strcasecmp( t, "hidden" ) == 0 )
1271 else if ( strcasecmp( t, "image" ) == 0 )
1273 else if ( strcasecmp( t, "button" ) == 0 )
1275 else if ( strcasecmp( t, "khtml_isindex" ) == 0 )
1278 else if ( strcasecmp( t, "search" ) == 0 )
1280 else if ( strcasecmp( t, "range" ) == 0 )
1286 // ### IMPORTANT: Don't allow the type to be changed to FILE after the first
1287 // type change, otherwise a JavaScript programmer would be able to set a text
1288 // field's value to something like /etc/passwd and then change it to a file field.
1289 if (m_type != newType) {
1290 if (newType == FILE && m_haveType) {
1291 // Set the attribute back to the old value.
1292 // Useful in case we were called from inside parseHTMLAttribute.
1293 setAttribute(ATTR_TYPE, type());
1295 bool wasAttached = m_attached;
1298 bool didStoreValue = storesValueSeparateFromAttribute();
1300 bool willStoreValue = storesValueSeparateFromAttribute();
1301 if (didStoreValue && !willStoreValue && !m_value.isNull()) {
1302 setAttribute(ATTR_VALUE, m_value);
1303 m_value = DOMString();
1305 if (!didStoreValue && willStoreValue) {
1306 m_value = getAttribute(ATTR_VALUE);
1315 DOMString HTMLInputElementImpl::type() const
1317 // needs to be lowercase according to DOM spec
1319 case TEXT: return "text";
1320 case PASSWORD: return "password";
1321 case CHECKBOX: return "checkbox";
1322 case RADIO: return "radio";
1323 case SUBMIT: return "submit";
1324 case RESET: return "reset";
1325 case FILE: return "file";
1326 case HIDDEN: return "hidden";
1327 case IMAGE: return "image";
1328 case BUTTON: return "button";
1330 case SEARCH: return "search";
1331 case RANGE: return "range";
1333 case ISINDEX: return "";
1338 QString HTMLInputElementImpl::state( )
1340 assert(m_type != PASSWORD); // should never save/restore password fields
1342 QString state = HTMLGenericFormElementImpl::state();
1346 return state + (checked() ? "on" : "off");
1348 return state + value().string()+'.'; // Make sure the string is not empty!
1352 void HTMLInputElementImpl::restoreState(QStringList &states)
1354 assert(m_type != PASSWORD); // should never save/restore password fields
1356 QString state = HTMLGenericFormElementImpl::findMatchingState(states);
1357 if (state.isNull()) return;
1362 setChecked((state == "on"));
1365 setValue(DOMString(state.left(state.length()-1)));
1370 void HTMLInputElementImpl::select( )
1372 if(!m_render) return;
1374 if (m_type == TEXT || m_type == PASSWORD)
1375 static_cast<RenderLineEdit*>(m_render)->select();
1376 else if (m_type == FILE)
1377 static_cast<RenderFileButton*>(m_render)->select();
1380 void HTMLInputElementImpl::click()
1382 switch (inputType()) {
1384 // a no-op for this type
1394 if (renderer() && (widget = static_cast<RenderWidget *>(renderer())->widget())) {
1395 // using this method gives us nice Cocoa user interface feedback
1396 static_cast<QButton *>(widget)->click();
1401 HTMLGenericFormElementImpl::click();
1406 static_cast<RenderFileButton *>(renderer())->click();
1410 HTMLGenericFormElementImpl::click();
1420 HTMLGenericFormElementImpl::click();
1425 void HTMLInputElementImpl::accessKeyAction()
1427 switch (inputType()) {
1429 // a no-op for this type
1456 bool HTMLInputElementImpl::mapToEntry(NodeImpl::Id attr, MappedAttributeEntry& result) const
1463 result = eUniversal;
1466 result = eReplaced; // Share with <img> since the alignment behavior is the same.
1472 return HTMLElementImpl::mapToEntry(attr, result);
1475 void HTMLInputElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
1479 case ATTR_AUTOCOMPLETE:
1480 m_autocomplete = strcasecmp( attr->value(), "off" );
1483 setType(attr->value());
1484 if (m_type != IMAGE && m_imageLoader) {
1485 delete m_imageLoader;
1490 // We only need to setChanged if the form is looking at the default value right now.
1491 if (m_value.isNull())
1495 m_defaultChecked = !attr->isNull();
1496 if (m_useDefaultChecked) {
1497 setChecked(m_defaultChecked);
1498 m_useDefaultChecked = true;
1501 case ATTR_MAXLENGTH:
1502 m_maxLen = !attr->isNull() ? attr->value().toInt() : -1;
1506 m_size = !attr->isNull() ? attr->value().toInt() : 20;
1509 if (m_render && m_type == IMAGE)
1510 static_cast<RenderImage*>(m_render)->updateAltText();
1513 if (m_render && m_type == IMAGE) {
1515 m_imageLoader = new HTMLImageLoader(this);
1516 m_imageLoader->updateFromElement();
1520 case ATTR_ACCESSKEY:
1521 // ### ignore for the moment
1524 addCSSLength(attr, CSS_PROP_MARGIN_TOP, attr->value());
1525 addCSSLength(attr, CSS_PROP_MARGIN_BOTTOM, attr->value());
1528 addCSSLength(attr, CSS_PROP_MARGIN_LEFT, attr->value());
1529 addCSSLength(attr, CSS_PROP_MARGIN_RIGHT, attr->value());
1532 addHTMLAlignment(attr);
1535 addCSSLength(attr, CSS_PROP_WIDTH, attr->value() );
1538 addCSSLength(attr, CSS_PROP_HEIGHT, attr->value() );
1541 setHTMLEventListener(EventImpl::FOCUS_EVENT,
1542 getDocument()->createHTMLEventListener(attr->value().string()));
1545 setHTMLEventListener(EventImpl::BLUR_EVENT,
1546 getDocument()->createHTMLEventListener(attr->value().string()));
1549 setHTMLEventListener(EventImpl::SELECT_EVENT,
1550 getDocument()->createHTMLEventListener(attr->value().string()));
1553 setHTMLEventListener(EventImpl::CHANGE_EVENT,
1554 getDocument()->createHTMLEventListener(attr->value().string()));
1557 setHTMLEventListener(EventImpl::INPUT_EVENT,
1558 getDocument()->createHTMLEventListener(attr->value().string()));
1561 // Search field and slider attributes all just cause updateFromElement to be called through style
1564 setHTMLEventListener(EventImpl::SEARCH_EVENT,
1565 getDocument()->createHTMLEventListener(attr->value().string()));
1568 m_maxResults = !attr->isNull() ? attr->value().toInt() : 0;
1571 case ATTR_INCREMENTAL:
1572 case ATTR_PLACEHOLDER:
1575 case ATTR_PRECISION:
1580 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
1584 bool HTMLInputElementImpl::rendererIsNeeded(RenderStyle *style)
1601 case BUTTON: return HTMLGenericFormElementImpl::rendererIsNeeded(style);
1602 case HIDDEN: return false;
1608 RenderObject *HTMLInputElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
1617 case ISINDEX: return new (arena) RenderLineEdit(this);
1618 case CHECKBOX: return new (arena) RenderCheckBox(this);
1619 case RADIO: return new (arena) RenderRadioButton(this);
1620 case SUBMIT: return new (arena) RenderSubmitButton(this);
1621 case IMAGE: return new (arena) RenderImageButton(this);
1622 case RESET: return new (arena) RenderResetButton(this);
1623 case FILE: return new (arena) RenderFileButton(this);
1624 case BUTTON: return new (arena) RenderPushButton(this);
1626 case RANGE: return new (arena) RenderSlider(this);
1634 void HTMLInputElementImpl::attach()
1638 setType(getAttribute(ATTR_TYPE));
1640 // FIXME: This needs to be dynamic, doesn't it, since someone could set this
1641 // after attachment?
1642 DOMString val = getAttribute(ATTR_VALUE);
1643 if ((uint) m_type <= ISINDEX && !val.isEmpty()) {
1644 // remove newline stuff..
1646 for (unsigned int i = 0; i < val.length(); ++i)
1650 if (val.length() != nvalue.length())
1651 setAttribute(ATTR_VALUE, nvalue);
1654 m_defaultChecked = (!getAttribute(ATTR_CHECKED).isNull());
1659 // Disallow the width attribute on inputs other than HIDDEN and IMAGE.
1660 // Dumb Web sites will try to set the width as an attribute on form controls that aren't
1661 // images or hidden.
1662 if (hasMappedAttributes() && m_type != HIDDEN && m_type != IMAGE && !getAttribute(ATTR_WIDTH).isEmpty()) {
1664 removeAttribute(ATTR_WIDTH, excCode);
1667 HTMLGenericFormElementImpl::attach();
1669 if (m_type == IMAGE) {
1671 m_imageLoader = new HTMLImageLoader(this);
1672 m_imageLoader->updateFromElement();
1674 RenderImage* imageObj = static_cast<RenderImage*>(renderer());
1675 imageObj->setImage(m_imageLoader->image());
1680 // note we don't deal with calling passwordFieldRemoved() on detach, because the timing
1681 // was such that it cleared our state too early
1682 if (m_type == PASSWORD)
1683 getDocument()->passwordFieldAdded();
1687 DOMString HTMLInputElementImpl::altText() const
1689 // http://www.w3.org/TR/1998/REC-html40-19980424/appendix/notes.html#altgen
1690 // also heavily discussed by Hixie on bugzilla
1691 // note this is intentionally different to HTMLImageElementImpl::altText()
1692 DOMString alt = getAttribute( ATTR_ALT );
1693 // fall back to title attribute
1695 alt = getAttribute( ATTR_TITLE );
1697 alt = getAttribute( ATTR_VALUE );
1698 if ( alt.isEmpty() )
1700 alt = inputElementAltText();
1702 alt = i18n( "Submit" );
1708 bool HTMLInputElementImpl::isSuccessfulSubmitButton() const
1710 // HTML spec says that buttons must have names
1711 // to be considered successful. However, other browsers
1712 // do not impose this constraint. Therefore, we behave
1713 // differently and can use different buttons than the
1715 // Was: (m_type == SUBMIT && !name().isEmpty())
1716 return !m_disabled && (m_type == IMAGE || m_type == SUBMIT);
1719 bool HTMLInputElementImpl::isActivatedSubmit() const
1721 return m_activeSubmit;
1724 void HTMLInputElementImpl::setActivatedSubmit(bool flag)
1726 m_activeSubmit = flag;
1729 bool HTMLInputElementImpl::appendFormData(FormDataList &encoding, bool multipart)
1731 // image generates its own names
1732 if (name().isEmpty() && m_type != IMAGE) return false;
1742 // always successful
1743 encoding.appendData(name(), value());
1749 encoding.appendData(name(), value());
1756 // those buttons are never successful
1762 encoding.appendData(name().isEmpty() ? QString::fromLatin1("x") : (name().string() + ".x"), clickX());
1763 encoding.appendData(name().isEmpty() ? QString::fromLatin1("y") : (name().string() + ".y"), clickY());
1771 QString enc_str = value().string();
1772 if (enc_str.isEmpty())
1773 enc_str = static_cast<RenderSubmitButton*>(m_render)->defaultLabel();
1774 if (!enc_str.isEmpty()) {
1775 encoding.appendData(name(), enc_str);
1783 // can't submit file on GET
1784 // don't submit if display: none or display: hidden
1785 if(!multipart || !renderer() || renderer()->style()->visibility() != khtml::VISIBLE)
1788 // if no filename at all is entered, return successful, however empty
1789 // null would be more logical but netscape posts an empty file. argh.
1790 if (value().isEmpty()) {
1791 encoding.appendData(name(), QString(""));
1795 KURL fileurl("file:///");
1796 fileurl.setPath(value().string());
1797 KIO::UDSEntry filestat;
1799 if (!KIO::NetAccess::stat(fileurl, filestat)) {
1801 // FIXME: Figure out how to report this error.
1803 KMessageBox::sorry(0L, i18n("Error fetching file for submission:\n%1").arg(KIO::NetAccess::lastErrorString()));
1808 KFileItem fileitem(filestat, fileurl, true, false);
1809 if (fileitem.isDir()) {
1814 if ( KIO::NetAccess::download(fileurl, local) )
1817 if (file.open(IO_ReadOnly))
1819 QCString filearray(file.size()+1);
1820 int readbytes = file.readBlock( filearray.data(), file.size());
1821 if ( readbytes >= 0 )
1822 filearray[readbytes] = '\0';
1825 encoding.appendData(name(), filearray);
1826 KIO::NetAccess::removeTempFile( local );
1834 // FIXME: Figure out how to report this error.
1836 KMessageBox::sorry(0L, i18n("Error fetching file for submission:\n%1").arg(KIO::NetAccess::lastErrorString()));
1843 encoding.appendData(name(), value());
1849 void HTMLInputElementImpl::reset()
1851 if (storesValueSeparateFromAttribute())
1852 setValue(DOMString());
1853 setChecked(m_defaultChecked);
1854 m_useDefaultChecked = true;
1857 void HTMLInputElementImpl::setChecked(bool _checked)
1859 if (checked() == _checked) return;
1861 if (m_form && m_type == RADIO && _checked && !name().isEmpty())
1862 m_form->radioClicked(this);
1864 m_useDefaultChecked = false;
1865 m_checked = _checked;
1870 DOMString HTMLInputElementImpl::value() const
1872 DOMString value = m_value;
1874 // It's important *not* to fall back to the value attribute for file inputs,
1875 // because that would allow a malicious web page to upload files by setting the
1876 // value attribute in markup.
1877 if (value.isNull() && m_type != FILE)
1878 value = getAttribute(ATTR_VALUE);
1880 // If no attribute exists, then just use "on" or "" based off the checked() state of the control.
1881 if (value.isNull() && (m_type == CHECKBOX || m_type == RADIO))
1882 return DOMString(checked() ? "on" : "");
1888 void HTMLInputElementImpl::setValue(const DOMString &value)
1890 if (m_type == FILE) return;
1892 if (storesValueSeparateFromAttribute()) {
1896 setAttribute(ATTR_VALUE, value);
1900 bool HTMLInputElementImpl::storesValueSeparateFromAttribute() const
1926 void HTMLInputElementImpl::blur()
1928 if(getDocument()->focusNode() == this)
1929 getDocument()->setFocusNode(0);
1932 void HTMLInputElementImpl::focus()
1934 getDocument()->setFocusNode(this);
1937 void HTMLInputElementImpl::defaultEventHandler(EventImpl *evt)
1939 if (evt->isMouseEvent() &&
1940 ( evt->id() == EventImpl::KHTML_CLICK_EVENT || evt->id() == EventImpl::KHTML_DBLCLICK_EVENT ) &&
1943 // record the mouse position for when we get the DOMActivate event
1944 MouseEventImpl *me = static_cast<MouseEventImpl*>(evt);
1945 int offsetX, offsetY;
1946 m_render->absolutePosition(offsetX,offsetY);
1947 xPos = me->clientX()-offsetX;
1948 yPos = me->clientY()-offsetY;
1950 me->setDefaultHandled();
1953 // DOMActivate events cause the input to be "activated" - in the case of image and submit inputs, this means
1954 // actually submitting the form. For reset inputs, the form is reset. These events are sent when the user clicks
1955 // on the element, or presses enter while it is the active element. Javacsript code wishing to activate the element
1956 // must dispatch a DOMActivate event - a click event will not do the job.
1957 if ((evt->id() == EventImpl::DOMACTIVATE_EVENT) &&
1958 (m_type == IMAGE || m_type == SUBMIT || m_type == RESET)){
1960 if (!m_form || !m_render)
1963 if (m_type == RESET) {
1967 m_activeSubmit = true;
1968 if (!m_form->prepareSubmit()) {
1972 m_activeSubmit = false;
1977 // Use key press event here since sending simulated mouse events
1978 // on key down blocks the proper sending of the key press event.
1979 if (evt->id() == EventImpl::KEYPRESS_EVENT && evt->isKeyboardEvent()) {
1980 DOMString key = static_cast<KeyboardEventImpl *>(evt)->keyIdentifier();
1989 // Simulate mouse click for enter or spacebar for these types of elements.
1990 // The AppKit already does this for spacebar for some, but not all, of them.
1991 if (key == "U+000020" || key == "Enter") {
1993 evt->setDefaultHandled();
2002 // Simulate mouse click on the default form button for enter for these types of elements.
2003 if (key == "Enter" && m_form) {
2004 m_form->submitClick();
2005 evt->setDefaultHandled();
2012 HTMLGenericFormElementImpl::defaultEventHandler(evt);
2015 bool HTMLInputElementImpl::isEditable()
2017 return ((m_type == TEXT) || (m_type == PASSWORD) ||
2018 (m_type == SEARCH) || (m_type == ISINDEX) || (m_type == FILE));
2021 bool HTMLInputElementImpl::isURLAttribute(AttributeImpl *attr) const
2023 return (attr->id() == ATTR_SRC);
2026 // -------------------------------------------------------------------------
2028 HTMLLabelElementImpl::HTMLLabelElementImpl(DocumentPtr *doc)
2029 : HTMLElementImpl(doc)
2033 HTMLLabelElementImpl::~HTMLLabelElementImpl()
2037 bool HTMLLabelElementImpl::isFocusable() const
2042 NodeImpl::Id HTMLLabelElementImpl::id() const
2047 void HTMLLabelElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
2052 setHTMLEventListener(EventImpl::FOCUS_EVENT,
2053 getDocument()->createHTMLEventListener(attr->value().string()));
2056 setHTMLEventListener(EventImpl::BLUR_EVENT,
2057 getDocument()->createHTMLEventListener(attr->value().string()));
2060 HTMLElementImpl::parseHTMLAttribute(attr);
2064 ElementImpl *HTMLLabelElementImpl::formElement()
2066 DOMString formElementId = getAttribute(ATTR_FOR);
2067 if (formElementId.isNull()) {
2068 // Search children of the label element for a form element.
2069 NodeImpl *node = this;
2070 while ((node = node->traverseNextNode(this))) {
2071 if (node->isHTMLElement()) {
2072 HTMLElementImpl *element = static_cast<HTMLElementImpl *>(node);
2073 if (element->isGenericFormElement()) {
2080 if (formElementId.isEmpty())
2082 return getDocument()->getElementById(formElementId);
2085 void HTMLLabelElementImpl::accessKeyAction()
2087 ElementImpl *element = formElement();
2089 element->accessKeyAction();
2092 // -------------------------------------------------------------------------
2094 HTMLLegendElementImpl::HTMLLegendElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
2095 : HTMLGenericFormElementImpl(doc, f)
2099 HTMLLegendElementImpl::~HTMLLegendElementImpl()
2103 bool HTMLLegendElementImpl::isFocusable() const
2108 NodeImpl::Id HTMLLegendElementImpl::id() const
2113 RenderObject* HTMLLegendElementImpl::createRenderer(RenderArena* arena, RenderStyle* style)
2115 return new (arena) RenderLegend(this);
2118 DOMString HTMLLegendElementImpl::type() const
2123 // -------------------------------------------------------------------------
2125 HTMLSelectElementImpl::HTMLSelectElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
2126 : HTMLGenericFormElementImpl(doc, f), m_options(0)
2129 m_recalcListItems = false;
2130 // 0 means invalid (i.e. not set)
2135 HTMLSelectElementImpl::~HTMLSelectElementImpl()
2137 if (getDocument()) getDocument()->deregisterMaintainsState(this);
2139 m_options->detach();
2144 NodeImpl::Id HTMLSelectElementImpl::id() const
2149 void HTMLSelectElementImpl::recalcStyle( StyleChange ch )
2151 if (hasChangedChild() && m_render) {
2152 static_cast<khtml::RenderSelect*>(m_render)->setOptionsChanged(true);
2155 HTMLGenericFormElementImpl::recalcStyle( ch );
2159 DOMString HTMLSelectElementImpl::type() const
2161 return (m_multiple ? "select-multiple" : "select-one");
2164 long HTMLSelectElementImpl::selectedIndex() const
2166 // return the number of the first option selected
2168 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2169 for (unsigned int i = 0; i < items.size(); i++) {
2170 if (items[i]->id() == ID_OPTION) {
2171 if (static_cast<HTMLOptionElementImpl*>(items[i])->selected())
2176 Q_ASSERT(m_multiple);
2180 void HTMLSelectElementImpl::setSelectedIndex( long index )
2182 // deselect all other options and select only the new one
2183 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2185 for (listIndex = 0; listIndex < int(items.size()); listIndex++) {
2186 if (items[listIndex]->id() == ID_OPTION)
2187 static_cast<HTMLOptionElementImpl*>(items[listIndex])->setSelected(false);
2189 listIndex = optionToListIndex(index);
2191 static_cast<HTMLOptionElementImpl*>(items[listIndex])->setSelected(true);
2196 long HTMLSelectElementImpl::length() const
2200 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2201 for (i = 0; i < items.size(); i++) {
2202 if (items[i]->id() == ID_OPTION)
2208 void HTMLSelectElementImpl::add( HTMLElementImpl *element, HTMLElementImpl *before )
2210 if (!element || element->id() != ID_OPTION)
2213 int exceptioncode = 0;
2214 insertBefore(element, before, exceptioncode);
2216 setRecalcListItems();
2219 void HTMLSelectElementImpl::remove( long index )
2221 int exceptioncode = 0;
2222 int listIndex = optionToListIndex(index);
2224 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2225 if(listIndex < 0 || index >= int(items.size()))
2226 return; // ### what should we do ? remove the last item?
2228 removeChild(items[listIndex], exceptioncode);
2229 if( !exceptioncode )
2230 setRecalcListItems();
2233 void HTMLSelectElementImpl::blur()
2235 if(getDocument()->focusNode() == this)
2236 getDocument()->setFocusNode(0);
2239 void HTMLSelectElementImpl::focus()
2241 getDocument()->setFocusNode(this);
2244 DOMString HTMLSelectElementImpl::value( )
2247 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2248 for (i = 0; i < items.size(); i++) {
2249 if ( items[i]->id() == ID_OPTION
2250 && static_cast<HTMLOptionElementImpl*>(items[i])->selected())
2251 return static_cast<HTMLOptionElementImpl*>(items[i])->value();
2253 return DOMString("");
2256 void HTMLSelectElementImpl::setValue(DOMStringImpl* value)
2258 // find the option with value() matching the given parameter
2259 // and make it the current selection.
2260 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2261 for (unsigned i = 0; i < items.size(); i++)
2262 if (items[i]->id() == ID_OPTION && static_cast<HTMLOptionElementImpl*>(items[i])->value() == value) {
2263 static_cast<HTMLOptionElementImpl*>(items[i])->setSelected(true);
2268 QString HTMLSelectElementImpl::state( )
2273 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2275 int l = items.count();
2278 QChar stateChars[l];
2280 for(int i = 0; i < l; i++)
2281 if(items[i]->id() == ID_OPTION && static_cast<HTMLOptionElementImpl*>(items[i])->selected())
2282 stateChars[i] = 'X';
2284 stateChars[i] = '.';
2285 QString state(stateChars, l);
2286 #else /* APPLE_CHANGES not defined */
2288 for(int i = 0; i < l; i++)
2289 if(items[i]->id() == ID_OPTION && static_cast<HTMLOptionElementImpl*>(items[i])->selected())
2291 #endif /* APPLE_CHANGES not defined */
2293 return HTMLGenericFormElementImpl::state() + state;
2296 void HTMLSelectElementImpl::restoreState(QStringList &_states)
2298 QString _state = HTMLGenericFormElementImpl::findMatchingState(_states);
2299 if (_state.isNull()) return;
2303 QString state = _state;
2304 if(!state.isEmpty() && !state.contains('X') && !m_multiple) {
2305 qWarning("should not happen in restoreState!");
2307 // KWQString doesn't support this operation. Should never get here anyway.
2314 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2316 int l = items.count();
2317 for(int i = 0; i < l; i++) {
2318 if(items[i]->id() == ID_OPTION) {
2319 HTMLOptionElementImpl* oe = static_cast<HTMLOptionElementImpl*>(items[i]);
2320 oe->setSelected(state[i] == 'X');
2326 NodeImpl *HTMLSelectElementImpl::insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode )
2328 NodeImpl *result = HTMLGenericFormElementImpl::insertBefore(newChild,refChild, exceptioncode );
2330 setRecalcListItems();
2334 NodeImpl *HTMLSelectElementImpl::replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode )
2336 NodeImpl *result = HTMLGenericFormElementImpl::replaceChild(newChild,oldChild, exceptioncode);
2337 if( !exceptioncode )
2338 setRecalcListItems();
2342 NodeImpl *HTMLSelectElementImpl::removeChild ( NodeImpl *oldChild, int &exceptioncode )
2344 NodeImpl *result = HTMLGenericFormElementImpl::removeChild(oldChild, exceptioncode);
2345 if( !exceptioncode )
2346 setRecalcListItems();
2350 NodeImpl *HTMLSelectElementImpl::appendChild ( NodeImpl *newChild, int &exceptioncode )
2352 NodeImpl *result = HTMLGenericFormElementImpl::appendChild(newChild, exceptioncode);
2353 if( !exceptioncode )
2354 setRecalcListItems();
2359 NodeImpl* HTMLSelectElementImpl::addChild(NodeImpl* newChild)
2361 setRecalcListItems();
2362 return HTMLGenericFormElementImpl::addChild(newChild);
2365 void HTMLSelectElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
2370 m_size = QMAX( attr->value().toInt(), 1 );
2373 m_minwidth = QMAX( attr->value().toInt(), 0 );
2376 m_multiple = (!attr->isNull());
2378 case ATTR_ACCESSKEY:
2379 // ### ignore for the moment
2382 setHTMLEventListener(EventImpl::FOCUS_EVENT,
2383 getDocument()->createHTMLEventListener(attr->value().string()));
2386 setHTMLEventListener(EventImpl::BLUR_EVENT,
2387 getDocument()->createHTMLEventListener(attr->value().string()));
2390 setHTMLEventListener(EventImpl::CHANGE_EVENT,
2391 getDocument()->createHTMLEventListener(attr->value().string()));
2394 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
2398 RenderObject *HTMLSelectElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
2400 return new (arena) RenderSelect(this);
2403 bool HTMLSelectElementImpl::appendFormData(FormDataList& encoded_values, bool)
2405 bool successful = false;
2406 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2409 for (i = 0; i < items.size(); i++) {
2410 if (items[i]->id() == ID_OPTION) {
2411 HTMLOptionElementImpl *option = static_cast<HTMLOptionElementImpl*>(items[i]);
2412 if (option->selected()) {
2413 encoded_values.appendData(name(), option->value());
2419 // ### this case should not happen. make sure that we select the first option
2420 // in any case. otherwise we have no consistency with the DOM interface. FIXME!
2421 // we return the first one if it was a combobox select
2422 if (!successful && !m_multiple && m_size <= 1 && items.size() &&
2423 (items[0]->id() == ID_OPTION) ) {
2424 HTMLOptionElementImpl *option = static_cast<HTMLOptionElementImpl*>(items[0]);
2425 if (option->value().isNull())
2426 encoded_values.appendData(name(), option->text().string().stripWhiteSpace());
2428 encoded_values.appendData(name(), option->value());
2435 int HTMLSelectElementImpl::optionToListIndex(int optionIndex) const
2437 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2438 if (optionIndex < 0 || optionIndex >= int(items.size()))
2442 int optionIndex2 = 0;
2444 optionIndex2 < int(items.size()) && optionIndex2 <= optionIndex;
2445 listIndex++) { // not a typo!
2446 if (items[listIndex]->id() == ID_OPTION)
2453 int HTMLSelectElementImpl::listToOptionIndex(int listIndex) const
2455 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2456 if (listIndex < 0 || listIndex >= int(items.size()) ||
2457 items[listIndex]->id() != ID_OPTION)
2460 int optionIndex = 0; // actual index of option not counting OPTGROUP entries that may be in list
2462 for (i = 0; i < listIndex; i++)
2463 if (items[i]->id() == ID_OPTION)
2468 HTMLOptionsCollectionImpl *HTMLSelectElementImpl::options()
2471 m_options = new HTMLOptionsCollectionImpl(this);
2477 void HTMLSelectElementImpl::recalcListItems()
2479 NodeImpl* current = firstChild();
2480 m_listItems.resize(0);
2481 HTMLOptionElementImpl* foundSelected = 0;
2483 if (current->id() == ID_OPTGROUP && current->firstChild()) {
2484 // ### what if optgroup contains just comments? don't want one of no options in it...
2485 m_listItems.resize(m_listItems.size()+1);
2486 m_listItems[m_listItems.size()-1] = static_cast<HTMLGenericFormElementImpl*>(current);
2487 current = current->firstChild();
2489 if (current->id() == ID_OPTION) {
2490 m_listItems.resize(m_listItems.size()+1);
2491 m_listItems[m_listItems.size()-1] = static_cast<HTMLGenericFormElementImpl*>(current);
2492 if (!foundSelected && !m_multiple && m_size <= 1) {
2493 foundSelected = static_cast<HTMLOptionElementImpl*>(current);
2494 foundSelected->m_selected = true;
2496 else if (foundSelected && !m_multiple && static_cast<HTMLOptionElementImpl*>(current)->selected()) {
2497 foundSelected->m_selected = false;
2498 foundSelected = static_cast<HTMLOptionElementImpl*>(current);
2501 NodeImpl *parent = current->parentNode();
2502 current = current->nextSibling();
2505 current = parent->nextSibling();
2508 m_recalcListItems = false;
2511 void HTMLSelectElementImpl::childrenChanged()
2513 setRecalcListItems();
2515 HTMLGenericFormElementImpl::childrenChanged();
2518 void HTMLSelectElementImpl::setRecalcListItems()
2520 m_recalcListItems = true;
2522 static_cast<khtml::RenderSelect*>(m_render)->setOptionsChanged(true);
2526 void HTMLSelectElementImpl::reset()
2528 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2530 for (i = 0; i < items.size(); i++) {
2531 if (items[i]->id() == ID_OPTION) {
2532 HTMLOptionElementImpl *option = static_cast<HTMLOptionElementImpl*>(items[i]);
2533 bool selected = (!option->getAttribute(ATTR_SELECTED).isNull());
2534 option->setSelected(selected);
2538 static_cast<RenderSelect*>(m_render)->setSelectionChanged(true);
2542 void HTMLSelectElementImpl::notifyOptionSelected(HTMLOptionElementImpl *selectedOption, bool selected)
2544 if (selected && !m_multiple) {
2545 // deselect all other options
2546 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2548 for (i = 0; i < items.size(); i++) {
2549 if (items[i]->id() == ID_OPTION)
2550 static_cast<HTMLOptionElementImpl*>(items[i])->m_selected = (items[i] == selectedOption);
2554 static_cast<RenderSelect*>(m_render)->setSelectionChanged(true);
2561 void HTMLSelectElementImpl::defaultEventHandler(EventImpl *evt)
2563 // Use key press event here since sending simulated mouse events
2564 // on key down blocks the proper sending of the key press event.
2565 if (evt->id() == EventImpl::KEYPRESS_EVENT) {
2567 if (!m_form || !m_render || !evt->isKeyboardEvent())
2570 DOMString key = static_cast<KeyboardEventImpl *>(evt)->keyIdentifier();
2572 if (key == "Enter") {
2573 m_form->submitClick();
2574 evt->setDefaultHandled();
2577 HTMLGenericFormElementImpl::defaultEventHandler(evt);
2580 #endif // APPLE_CHANGES
2582 void HTMLSelectElementImpl::accessKeyAction()
2587 // -------------------------------------------------------------------------
2589 HTMLKeygenElementImpl::HTMLKeygenElementImpl(DocumentPtr* doc, HTMLFormElementImpl* f)
2590 : HTMLSelectElementImpl(doc, f)
2592 QStringList keys = KSSLKeyGen::supportedKeySizes();
2593 for (QStringList::Iterator i = keys.begin(); i != keys.end(); ++i) {
2594 HTMLOptionElementImpl* o = new HTMLOptionElementImpl(doc, form());
2596 o->addChild(new TextImpl(doc, DOMString(*i)));
2600 NodeImpl::Id HTMLKeygenElementImpl::id() const
2605 DOMString HTMLKeygenElementImpl::type() const
2610 void HTMLKeygenElementImpl::parseHTMLAttribute(HTMLAttributeImpl* attr)
2614 case ATTR_CHALLENGE:
2615 m_challenge = attr->value();
2618 m_keyType = attr->value();
2621 // skip HTMLSelectElementImpl parsing!
2622 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
2626 bool HTMLKeygenElementImpl::appendFormData(FormDataList& encoded_values, bool)
2629 // Only RSA is supported at this time.
2630 if (!m_keyType.isNull() && strcasecmp(m_keyType, "rsa")) {
2633 QString value = KSSLKeyGen::signedPublicKeyAndChallengeString(selectedIndex(), m_challenge.string(), getDocument()->part()->baseURL());
2634 if (value.isNull()) {
2637 encoded_values.appendData(name(), value.utf8());
2640 bool successful = false;
2642 // pop up the fancy certificate creation dialog here
2643 KSSLKeyGen *kg = new KSSLKeyGen(static_cast<RenderWidget *>(m_render)->widget(), "Key Generator", true);
2646 successful = (QDialog::Accepted == kg->exec());
2650 encoded_values.appendData(name(), "deadbeef");
2656 // -------------------------------------------------------------------------
2658 HTMLOptGroupElementImpl::HTMLOptGroupElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
2659 : HTMLGenericFormElementImpl(doc, f)
2663 HTMLOptGroupElementImpl::~HTMLOptGroupElementImpl()
2667 bool HTMLOptGroupElementImpl::isFocusable() const
2672 NodeImpl::Id HTMLOptGroupElementImpl::id() const
2677 DOMString HTMLOptGroupElementImpl::type() const
2682 NodeImpl *HTMLOptGroupElementImpl::insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode )
2684 NodeImpl *result = HTMLGenericFormElementImpl::insertBefore(newChild,refChild, exceptioncode);
2685 if ( !exceptioncode )
2686 recalcSelectOptions();
2690 NodeImpl *HTMLOptGroupElementImpl::replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode )
2692 NodeImpl *result = HTMLGenericFormElementImpl::replaceChild(newChild,oldChild, exceptioncode);
2694 recalcSelectOptions();
2698 NodeImpl *HTMLOptGroupElementImpl::removeChild ( NodeImpl *oldChild, int &exceptioncode )
2700 NodeImpl *result = HTMLGenericFormElementImpl::removeChild(oldChild, exceptioncode);
2701 if( !exceptioncode )
2702 recalcSelectOptions();
2706 NodeImpl *HTMLOptGroupElementImpl::appendChild ( NodeImpl *newChild, int &exceptioncode )
2708 NodeImpl *result = HTMLGenericFormElementImpl::appendChild(newChild, exceptioncode);
2709 if( !exceptioncode )
2710 recalcSelectOptions();
2714 NodeImpl* HTMLOptGroupElementImpl::addChild(NodeImpl* newChild)
2716 recalcSelectOptions();
2718 return HTMLGenericFormElementImpl::addChild(newChild);
2721 void HTMLOptGroupElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
2723 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
2724 recalcSelectOptions();
2727 void HTMLOptGroupElementImpl::recalcSelectOptions()
2729 NodeImpl *select = parentNode();
2730 while (select && select->id() != ID_SELECT)
2731 select = select->parentNode();
2733 static_cast<HTMLSelectElementImpl*>(select)->setRecalcListItems();
2736 // -------------------------------------------------------------------------
2738 HTMLOptionElementImpl::HTMLOptionElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
2739 : HTMLGenericFormElementImpl(doc, f)
2744 bool HTMLOptionElementImpl::isFocusable() const
2749 NodeImpl::Id HTMLOptionElementImpl::id() const
2754 DOMString HTMLOptionElementImpl::type() const
2759 DOMString HTMLOptionElementImpl::text() const
2762 // WinIE does not use the label attribute, so as a quirk, we ignore it.
2763 if (getDocument() && !getDocument()->inCompatMode())
2764 label = getAttribute(ATTR_LABEL);
2765 if (label.isEmpty() && firstChild() && firstChild()->nodeType() == Node::TEXT_NODE) {
2766 if (firstChild()->nextSibling()) {
2768 NodeImpl *n = firstChild();
2769 for (; n; n = n->nextSibling()) {
2770 if (n->nodeType() == Node::TEXT_NODE ||
2771 n->nodeType() == Node::CDATA_SECTION_NODE)
2772 ret += n->nodeValue();
2777 return firstChild()->nodeValue();
2783 long HTMLOptionElementImpl::index() const
2785 // Let's do this dynamically. Might be a bit slow, but we're sure
2786 // we won't forget to update a member variable in some cases...
2787 QMemArray<HTMLGenericFormElementImpl*> items = getSelect()->listItems();
2788 int l = items.count();
2789 int optionIndex = 0;
2790 for(int i = 0; i < l; i++) {
2791 if(items[i]->id() == ID_OPTION)
2793 if (static_cast<HTMLOptionElementImpl*>(items[i]) == this)
2798 kdWarning() << "HTMLOptionElementImpl::index(): option not found!" << endl;
2802 void HTMLOptionElementImpl::setIndex( long )
2804 kdWarning() << "Unimplemented HTMLOptionElementImpl::setIndex(long) called" << endl;
2808 void HTMLOptionElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
2813 m_selected = (!attr->isNull());
2816 m_value = attr->value();
2819 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
2823 DOMString HTMLOptionElementImpl::value() const
2825 if ( !m_value.isNull() )
2827 // Use the text if the value wasn't set.
2828 return text().string().stripWhiteSpace();
2831 void HTMLOptionElementImpl::setValue(DOMStringImpl* value)
2833 setAttribute(ATTR_VALUE, value);
2836 void HTMLOptionElementImpl::setSelected(bool _selected)
2838 if(m_selected == _selected)
2840 m_selected = _selected;
2841 HTMLSelectElementImpl *select = getSelect();
2843 select->notifyOptionSelected(this,_selected);
2846 void HTMLOptionElementImpl::childrenChanged()
2848 HTMLSelectElementImpl *select = getSelect();
2850 select->childrenChanged();
2853 HTMLSelectElementImpl *HTMLOptionElementImpl::getSelect() const
2855 NodeImpl *select = parentNode();
2856 while (select && select->id() != ID_SELECT)
2857 select = select->parentNode();
2858 return static_cast<HTMLSelectElementImpl*>(select);
2861 // -------------------------------------------------------------------------
2863 HTMLTextAreaElementImpl::HTMLTextAreaElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
2864 : HTMLGenericFormElementImpl(doc, f)
2866 // DTD requires rows & cols be specified, but we will provide reasonable defaults
2869 m_wrap = ta_Virtual;
2870 m_dirtyvalue = true;
2873 HTMLTextAreaElementImpl::~HTMLTextAreaElementImpl()
2875 if (getDocument()) getDocument()->deregisterMaintainsState(this);
2878 NodeImpl::Id HTMLTextAreaElementImpl::id() const
2883 DOMString HTMLTextAreaElementImpl::type() const
2888 QString HTMLTextAreaElementImpl::state( )
2890 // Make sure the string is not empty!
2891 return HTMLGenericFormElementImpl::state() + value().string()+'.';
2894 void HTMLTextAreaElementImpl::restoreState(QStringList &states)
2896 QString state = HTMLGenericFormElementImpl::findMatchingState(states);
2897 if (state.isNull()) return;
2898 setDefaultValue(state.left(state.length()-1));
2899 // the close() in the rendertree will take care of transferring defaultvalue to 'value'
2902 void HTMLTextAreaElementImpl::select( )
2905 static_cast<RenderTextArea*>(m_render)->select();
2909 void HTMLTextAreaElementImpl::childrenChanged()
2911 setValue(defaultValue());
2914 void HTMLTextAreaElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
2919 m_rows = !attr->isNull() ? attr->value().toInt() : 3;
2921 renderer()->setNeedsLayoutAndMinMaxRecalc();
2924 m_cols = !attr->isNull() ? attr->value().toInt() : 60;
2926 renderer()->setNeedsLayoutAndMinMaxRecalc();
2929 // virtual / physical is Netscape extension of HTML 3.0, now deprecated
2930 // soft/ hard / off is recommendation for HTML 4 extension by IE and NS 4
2931 if ( strcasecmp( attr->value(), "virtual" ) == 0 || strcasecmp( attr->value(), "soft") == 0)
2932 m_wrap = ta_Virtual;
2933 else if ( strcasecmp ( attr->value(), "physical" ) == 0 || strcasecmp( attr->value(), "hard") == 0)
2934 m_wrap = ta_Physical;
2935 else if(strcasecmp( attr->value(), "on" ) == 0)
2936 m_wrap = ta_Physical;
2937 else if(strcasecmp( attr->value(), "off") == 0)
2940 renderer()->setNeedsLayoutAndMinMaxRecalc();
2942 case ATTR_ACCESSKEY:
2943 // ignore for the moment
2946 setHTMLEventListener(EventImpl::FOCUS_EVENT,
2947 getDocument()->createHTMLEventListener(attr->value().string()));
2950 setHTMLEventListener(EventImpl::BLUR_EVENT,
2951 getDocument()->createHTMLEventListener(attr->value().string()));
2954 setHTMLEventListener(EventImpl::SELECT_EVENT,
2955 getDocument()->createHTMLEventListener(attr->value().string()));
2958 setHTMLEventListener(EventImpl::CHANGE_EVENT,
2959 getDocument()->createHTMLEventListener(attr->value().string()));
2962 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
2966 RenderObject *HTMLTextAreaElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
2968 return new (arena) RenderTextArea(this);
2971 bool HTMLTextAreaElementImpl::appendFormData(FormDataList& encoding, bool)
2973 if (name().isEmpty()) return false;
2974 encoding.appendData(name(), value());
2978 void HTMLTextAreaElementImpl::reset()
2980 setValue(defaultValue());
2983 DOMString HTMLTextAreaElementImpl::value()
2985 if ( m_dirtyvalue) {
2987 m_value = static_cast<RenderTextArea*>( m_render )->text();
2989 m_value = defaultValue().string();
2990 m_dirtyvalue = false;
2993 if ( m_value.isNull() ) return "";
2998 void HTMLTextAreaElementImpl::setValue(DOMString _value)
3000 m_value = _value.string();
3001 m_dirtyvalue = false;
3006 DOMString HTMLTextAreaElementImpl::defaultValue()
3009 // there may be comments - just grab the text nodes
3011 for (n = firstChild(); n; n = n->nextSibling())
3012 if (n->isTextNode())
3013 val += static_cast<TextImpl*>(n)->data();
3014 if (val[0] == '\r' && val[1] == '\n') {
3018 else if (val[0] == '\r' || val[0] == '\n') {
3026 void HTMLTextAreaElementImpl::setDefaultValue(DOMString _defaultValue)
3028 // there may be comments - remove all the text nodes and replace them with one
3029 QPtrList<NodeImpl> toRemove;
3031 for (n = firstChild(); n; n = n->nextSibling())
3032 if (n->isTextNode())
3034 QPtrListIterator<NodeImpl> it(toRemove);
3035 int exceptioncode = 0;
3036 for (; it.current(); ++it) {
3037 removeChild(it.current(), exceptioncode);
3039 insertBefore(getDocument()->createTextNode(_defaultValue),firstChild(), exceptioncode);
3040 setValue(_defaultValue);
3043 void HTMLTextAreaElementImpl::blur()
3045 if(getDocument()->focusNode() == this)
3046 getDocument()->setFocusNode(0);
3049 void HTMLTextAreaElementImpl::focus()
3051 getDocument()->setFocusNode(this);
3054 bool HTMLTextAreaElementImpl::isEditable()
3059 void HTMLTextAreaElementImpl::accessKeyAction()
3064 // -------------------------------------------------------------------------
3066 HTMLIsIndexElementImpl::HTMLIsIndexElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
3067 : HTMLInputElementImpl(doc, f)
3073 NodeImpl::Id HTMLIsIndexElementImpl::id() const
3078 void HTMLIsIndexElementImpl::parseHTMLAttribute(HTMLAttributeImpl* attr)
3083 setValue(attr->value());
3085 // don't call HTMLInputElement::parseHTMLAttribute here, as it would
3086 // accept attributes this element does not support
3087 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
3091 // -------------------------------------------------------------------------
3093 unsigned long HTMLOptionsCollectionImpl::length() const
3095 // Not yet implemented.
3099 void HTMLOptionsCollectionImpl::setLength(unsigned long length)
3101 // Not yet implemented.
3104 NodeImpl *HTMLOptionsCollectionImpl::item(unsigned long index) const
3106 // Not yet implemented.
3110 NodeImpl *HTMLOptionsCollectionImpl::namedItem(const DOMString &name) const
3112 // Not yet implemented.
3116 // -------------------------------------------------------------------------
3118 FormDataList::FormDataList(QTextCodec *c)
3123 void FormDataList::appendString(const QCString &s)
3125 m_strings.append(s);
3128 void FormDataList::appendString(const QString &s)
3130 QCString cstr = fixLineBreaks(m_codec->fromUnicode(s));
3131 cstr.truncate(cstr.length());
3132 m_strings.append(cstr);
3135 QValueListConstIterator<QCString> FormDataList::begin() const
3137 return m_strings.begin();
3140 QValueListConstIterator<QCString> FormDataList::end() const
3142 return m_strings.end();