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();
715 for (int i = pos; i < count - 1; i++) {
716 vec.insert(i, vec[i+1]);
718 vec.remove(count - 1);
721 void HTMLFormElementImpl::registerFormElement(HTMLGenericFormElementImpl *e)
723 appendToVector(formElements, e);
724 removeFromVector(dormantFormElements, e);
727 void HTMLFormElementImpl::removeFormElement(HTMLGenericFormElementImpl *e)
729 removeFromVector(formElements, e);
730 removeFromVector(dormantFormElements, e);
733 void HTMLFormElementImpl::makeFormElementDormant(HTMLGenericFormElementImpl *e)
735 appendToVector(dormantFormElements, e);
736 removeFromVector(formElements, e);
739 bool HTMLFormElementImpl::isURLAttribute(AttributeImpl *attr) const
741 return attr->id() == ATTR_ACTION;
744 void HTMLFormElementImpl::registerImgElement(HTMLImageElementImpl *e)
746 appendToVector(imgElements, e);
749 void HTMLFormElementImpl::removeImgElement(HTMLImageElementImpl *e)
751 removeFromVector(imgElements, e);
754 // -------------------------------------------------------------------------
756 HTMLGenericFormElementImpl::HTMLGenericFormElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
757 : HTMLElementImpl(doc)
759 m_disabled = m_readOnly = false;
768 m_form->registerFormElement(this);
771 HTMLGenericFormElementImpl::~HTMLGenericFormElementImpl()
774 m_form->removeFormElement(this);
775 if (m_name) m_name->deref();
778 void HTMLGenericFormElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
785 setDisabled( !attr->isNull() );
789 bool m_oldreadOnly = m_readOnly;
790 m_readOnly = !attr->isNull();
791 if (m_oldreadOnly != m_readOnly) setChanged();
795 HTMLElementImpl::parseHTMLAttribute(attr);
799 void HTMLGenericFormElementImpl::attach()
803 // FIXME: This handles the case of a new form element being created by
804 // JavaScript and inserted inside a form. What it does not handle is
805 // a form element being moved from inside a form to outside, or from one
806 // inside one form to another. The reason this other case is hard to fix
807 // is that during parsing, we may have been passed a form that we are not
808 // inside, DOM-tree-wise. If so, it's hard for us to know when we should
809 // be removed from that form's element list.
813 m_form->registerFormElement(this);
816 HTMLElementImpl::attach();
818 // The call to updateFromElement() needs to go after the call through
819 // to the base class's attach() because that can sometimes do a close
822 m_render->updateFromElement();
824 // Delayed attachment in order to prevent FOUC can result in an object being
825 // programmatically focused before it has a render object. If we have been focused
826 // (i.e., if we are the focusNode) then go ahead and focus our corresponding native widget.
827 // (Attach/detach can also happen as a result of display type changes, e.g., making a widget
828 // block instead of inline, and focus should be restored in that case as well.)
829 if (getDocument()->focusNode() == this && m_render->isWidget() &&
830 static_cast<RenderWidget*>(renderer())->widget())
831 static_cast<RenderWidget*>(renderer())->widget()->setFocus();
835 void HTMLGenericFormElementImpl::insertedIntoDocument()
837 if (m_form && m_dormant)
838 m_form->registerFormElement(this);
842 HTMLElementImpl::insertedIntoDocument();
845 void HTMLGenericFormElementImpl::removedFromDocument()
848 m_form->makeFormElementDormant(this);
852 HTMLElementImpl::removedFromDocument();
855 HTMLFormElementImpl *HTMLGenericFormElementImpl::getForm() const
857 NodeImpl *p = parentNode();
860 if( p->id() == ID_FORM )
861 return static_cast<HTMLFormElementImpl *>(p);
865 kdDebug( 6030 ) << "couldn't find form!" << endl;
870 DOMString HTMLGenericFormElementImpl::name() const
872 if (m_name) return m_name;
875 // DOMString n = getDocument()->htmlMode() != DocumentImpl::XHtml ?
876 // getAttribute(ATTR_NAME) : getAttribute(ATTR_ID);
877 DOMString n = getAttribute(ATTR_NAME);
879 return new DOMStringImpl("");
884 void HTMLGenericFormElementImpl::setName(const DOMString& name)
886 if (m_name) m_name->deref();
887 m_name = name.implementation();
888 if (m_name) m_name->ref();
891 void HTMLGenericFormElementImpl::onSelect()
893 // ### make this work with new form events architecture
894 dispatchHTMLEvent(EventImpl::SELECT_EVENT,true,false);
897 void HTMLGenericFormElementImpl::onChange()
899 // ### make this work with new form events architecture
900 dispatchHTMLEvent(EventImpl::CHANGE_EVENT,true,false);
903 bool HTMLGenericFormElementImpl::disabled() const
908 void HTMLGenericFormElementImpl::setDisabled( bool _disabled )
910 if ( m_disabled != _disabled ) {
911 m_disabled = _disabled;
916 void HTMLGenericFormElementImpl::recalcStyle( StyleChange ch )
918 //bool changed = changed();
919 HTMLElementImpl::recalcStyle( ch );
921 if (m_render /*&& changed*/)
922 m_render->updateFromElement();
925 bool HTMLGenericFormElementImpl::isFocusable() const
927 if (!m_render || (m_render->style() && m_render->style()->visibility() != VISIBLE) || m_render->width() == 0 || m_render->height() == 0)
932 bool HTMLGenericFormElementImpl::isKeyboardFocusable() const
935 if (m_render->isWidget()) {
936 return static_cast<RenderWidget*>(m_render)->widget() &&
937 (static_cast<RenderWidget*>(m_render)->widget()->focusPolicy() & QWidget::TabFocus);
939 if (getDocument()->part())
940 return getDocument()->part()->tabsToAllControls();
945 bool HTMLGenericFormElementImpl::isMouseFocusable() const
948 if (m_render->isWidget()) {
949 return static_cast<RenderWidget*>(m_render)->widget() &&
950 (static_cast<RenderWidget*>(m_render)->widget()->focusPolicy() & QWidget::ClickFocus);
953 // For <input type=image> and <button>, we will assume no mouse focusability. This is
954 // consistent with OS X behavior for buttons.
963 void HTMLGenericFormElementImpl::defaultEventHandler(EventImpl *evt)
965 if (evt->target()==this)
967 // Report focus in/out changes to the browser extension (editable widgets only)
968 KHTMLPart *part = getDocument()->part();
969 if (evt->id()==EventImpl::DOMFOCUSIN_EVENT && isEditable() && part && m_render && m_render->isWidget()) {
970 KHTMLPartBrowserExtension *ext = static_cast<KHTMLPartBrowserExtension *>(part->browserExtension());
971 QWidget *widget = static_cast<RenderWidget*>(m_render)->widget();
973 ext->editableWidgetFocused(widget);
977 // We don't want this default key event handling, we'll count on
978 // Cocoa event dispatch if the event doesn't get blocked.
980 if (evt->id()==EventImpl::KEYDOWN_EVENT ||
981 evt->id()==EventImpl::KEYUP_EVENT)
983 KeyboardEventImpl * k = static_cast<KeyboardEventImpl *>(evt);
984 if (k->keyVal() == QChar('\n').unicode() && m_render && m_render->isWidget() && k->qKeyEvent)
985 QApplication::sendEvent(static_cast<RenderWidget *>(m_render)->widget(), k->qKeyEvent);
989 if (evt->id()==EventImpl::DOMFOCUSOUT_EVENT && isEditable() && part && m_render && m_render->isWidget()) {
990 KHTMLPartBrowserExtension *ext = static_cast<KHTMLPartBrowserExtension *>(part->browserExtension());
991 QWidget *widget = static_cast<RenderWidget*>(m_render)->widget();
993 ext->editableWidgetBlurred(widget);
995 // ### Don't count popup as a valid reason for losing the focus (example: opening the options of a select
996 // combobox shouldn't emit onblur)
999 HTMLElementImpl::defaultEventHandler(evt);
1002 bool HTMLGenericFormElementImpl::isEditable()
1007 // Special chars used to encode form state strings.
1008 // We pick chars that are unlikely to be used in an HTML attr, so we rarely have to really encode.
1009 const char stateSeparator = '&';
1010 const char stateEscape = '<';
1011 static const char stateSeparatorMarker[] = "<A";
1012 static const char stateEscapeMarker[] = "<<";
1014 // Encode an element name so we can put it in a state string without colliding
1015 // with our separator char.
1016 static QString encodedElementName(QString str)
1018 int sepLoc = str.find(stateSeparator);
1019 int escLoc = str.find(stateSeparator);
1020 if (sepLoc >= 0 || escLoc >= 0) {
1021 QString newStr = str;
1022 // replace "<" with "<<"
1023 while (escLoc >= 0) {
1024 newStr.replace(escLoc, 1, stateEscapeMarker);
1025 escLoc = str.find(stateSeparator, escLoc+1);
1027 // replace "&" with "<A"
1028 while (sepLoc >= 0) {
1029 newStr.replace(sepLoc, 1, stateSeparatorMarker);
1030 sepLoc = str.find(stateSeparator, sepLoc+1);
1038 QString HTMLGenericFormElementImpl::state( )
1040 // Build a string that contains ElementName&ElementType&
1041 return encodedElementName(name().string()) + stateSeparator + type().string() + stateSeparator;
1044 QString HTMLGenericFormElementImpl::findMatchingState(QStringList &states)
1046 QString encName = encodedElementName(name().string());
1047 QString typeStr = type().string();
1048 for (QStringList::Iterator it = states.begin(); it != states.end(); ++it) {
1049 QString state = *it;
1050 int sep1 = state.find(stateSeparator);
1051 int sep2 = state.find(stateSeparator, sep1+1);
1055 QString nameAndType = state.left(sep2);
1056 if (encName.length() + typeStr.length() + 1 == (uint)sep2
1057 && nameAndType.startsWith(encName)
1058 && nameAndType.endsWith(typeStr))
1061 return state.mid(sep2+1);
1064 return QString::null;
1067 // -------------------------------------------------------------------------
1069 HTMLButtonElementImpl::HTMLButtonElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
1070 : HTMLGenericFormElementImpl(doc, f)
1074 m_activeSubmit = false;
1077 HTMLButtonElementImpl::~HTMLButtonElementImpl()
1081 NodeImpl::Id HTMLButtonElementImpl::id() const
1086 DOMString HTMLButtonElementImpl::type() const
1088 return getAttribute(ATTR_TYPE);
1091 void HTMLButtonElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
1096 if ( strcasecmp( attr->value(), "submit" ) == 0 )
1098 else if ( strcasecmp( attr->value(), "reset" ) == 0 )
1100 else if ( strcasecmp( attr->value(), "button" ) == 0 )
1104 m_value = attr->value();
1105 m_currValue = m_value;
1107 case ATTR_ACCESSKEY:
1110 setHTMLEventListener(EventImpl::FOCUS_EVENT,
1111 getDocument()->createHTMLEventListener(attr->value().string()));
1114 setHTMLEventListener(EventImpl::BLUR_EVENT,
1115 getDocument()->createHTMLEventListener(attr->value().string()));
1118 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
1122 void HTMLButtonElementImpl::defaultEventHandler(EventImpl *evt)
1124 if (m_type != BUTTON && (evt->id() == EventImpl::DOMACTIVATE_EVENT)) {
1126 if(m_form && m_type == SUBMIT) {
1127 m_activeSubmit = true;
1128 m_form->prepareSubmit();
1129 m_activeSubmit = false; // in case we were canceled
1131 if(m_form && m_type == RESET) m_form->reset();
1133 HTMLGenericFormElementImpl::defaultEventHandler(evt);
1136 bool HTMLButtonElementImpl::isSuccessfulSubmitButton() const
1138 // HTML spec says that buttons must have names
1139 // to be considered successful. However, other browsers
1140 // do not impose this constraint. Therefore, we behave
1141 // differently and can use different buttons than the
1143 // Remove the name constraint for now.
1144 // Was: m_type == SUBMIT && !m_disabled && !name().isEmpty()
1145 return m_type == SUBMIT && !m_disabled;
1148 bool HTMLButtonElementImpl::isActivatedSubmit() const
1150 return m_activeSubmit;
1153 void HTMLButtonElementImpl::setActivatedSubmit(bool flag)
1155 m_activeSubmit = flag;
1158 bool HTMLButtonElementImpl::appendFormData(FormDataList& encoding, bool /*multipart*/)
1160 if (m_type != SUBMIT || name().isEmpty() || !m_activeSubmit)
1162 encoding.appendData(name(), m_currValue);
1166 void HTMLButtonElementImpl::click()
1170 if (renderer() && (widget = static_cast<RenderWidget *>(renderer())->widget())) {
1171 // using this method gives us nice Cocoa user interface feedback
1172 static_cast<QButton *>(widget)->click();
1176 HTMLGenericFormElementImpl::click();
1179 void HTMLButtonElementImpl::accessKeyAction()
1184 // -------------------------------------------------------------------------
1186 HTMLFieldSetElementImpl::HTMLFieldSetElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
1187 : HTMLGenericFormElementImpl(doc, f)
1191 HTMLFieldSetElementImpl::~HTMLFieldSetElementImpl()
1195 bool HTMLFieldSetElementImpl::isFocusable() const
1200 NodeImpl::Id HTMLFieldSetElementImpl::id() const
1205 DOMString HTMLFieldSetElementImpl::type() const
1210 RenderObject* HTMLFieldSetElementImpl::createRenderer(RenderArena* arena, RenderStyle* style)
1212 return new (arena) RenderFieldset(this);
1215 // -------------------------------------------------------------------------
1217 HTMLInputElementImpl::HTMLInputElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
1218 : HTMLGenericFormElementImpl(doc, f), m_imageLoader(0)
1224 m_defaultChecked = false;
1225 m_useDefaultChecked = true;
1228 m_activeSubmit = false;
1229 m_autocomplete = true;
1240 m_autocomplete = f->autoComplete();
1243 HTMLInputElementImpl::~HTMLInputElementImpl()
1245 if (getDocument()) getDocument()->deregisterMaintainsState(this);
1246 delete m_imageLoader;
1249 NodeImpl::Id HTMLInputElementImpl::id() const
1254 void HTMLInputElementImpl::setType(const DOMString& t)
1258 if ( strcasecmp( t, "password" ) == 0 )
1260 else if ( strcasecmp( t, "checkbox" ) == 0 )
1262 else if ( strcasecmp( t, "radio" ) == 0 )
1264 else if ( strcasecmp( t, "submit" ) == 0 )
1266 else if ( strcasecmp( t, "reset" ) == 0 )
1268 else if ( strcasecmp( t, "file" ) == 0 )
1270 else if ( strcasecmp( t, "hidden" ) == 0 )
1272 else if ( strcasecmp( t, "image" ) == 0 )
1274 else if ( strcasecmp( t, "button" ) == 0 )
1276 else if ( strcasecmp( t, "khtml_isindex" ) == 0 )
1279 else if ( strcasecmp( t, "search" ) == 0 )
1281 else if ( strcasecmp( t, "range" ) == 0 )
1287 // ### IMPORTANT: Don't allow the type to be changed to FILE after the first
1288 // type change, otherwise a JavaScript programmer would be able to set a text
1289 // field's value to something like /etc/passwd and then change it to a file field.
1290 if (m_type != newType) {
1291 if (newType == FILE && m_haveType) {
1292 // Set the attribute back to the old value.
1293 // Useful in case we were called from inside parseHTMLAttribute.
1294 setAttribute(ATTR_TYPE, type());
1296 bool wasAttached = m_attached;
1299 bool didStoreValue = storesValueSeparateFromAttribute();
1301 bool willStoreValue = storesValueSeparateFromAttribute();
1302 if (didStoreValue && !willStoreValue && !m_value.isNull()) {
1303 setAttribute(ATTR_VALUE, m_value);
1304 m_value = DOMString();
1306 if (!didStoreValue && willStoreValue) {
1307 m_value = getAttribute(ATTR_VALUE);
1316 DOMString HTMLInputElementImpl::type() const
1318 // needs to be lowercase according to DOM spec
1320 case TEXT: return "text";
1321 case PASSWORD: return "password";
1322 case CHECKBOX: return "checkbox";
1323 case RADIO: return "radio";
1324 case SUBMIT: return "submit";
1325 case RESET: return "reset";
1326 case FILE: return "file";
1327 case HIDDEN: return "hidden";
1328 case IMAGE: return "image";
1329 case BUTTON: return "button";
1331 case SEARCH: return "search";
1332 case RANGE: return "range";
1334 case ISINDEX: return "";
1339 QString HTMLInputElementImpl::state( )
1341 assert(m_type != PASSWORD); // should never save/restore password fields
1343 QString state = HTMLGenericFormElementImpl::state();
1347 return state + (checked() ? "on" : "off");
1349 return state + value().string()+'.'; // Make sure the string is not empty!
1353 void HTMLInputElementImpl::restoreState(QStringList &states)
1355 assert(m_type != PASSWORD); // should never save/restore password fields
1357 QString state = HTMLGenericFormElementImpl::findMatchingState(states);
1358 if (state.isNull()) return;
1363 setChecked((state == "on"));
1366 setValue(DOMString(state.left(state.length()-1)));
1371 void HTMLInputElementImpl::select( )
1373 if(!m_render) return;
1375 if (m_type == TEXT || m_type == PASSWORD)
1376 static_cast<RenderLineEdit*>(m_render)->select();
1377 else if (m_type == FILE)
1378 static_cast<RenderFileButton*>(m_render)->select();
1381 void HTMLInputElementImpl::click()
1383 switch (inputType()) {
1385 // a no-op for this type
1395 if (renderer() && (widget = static_cast<RenderWidget *>(renderer())->widget())) {
1396 // using this method gives us nice Cocoa user interface feedback
1397 static_cast<QButton *>(widget)->click();
1402 HTMLGenericFormElementImpl::click();
1407 static_cast<RenderFileButton *>(renderer())->click();
1411 HTMLGenericFormElementImpl::click();
1421 HTMLGenericFormElementImpl::click();
1426 void HTMLInputElementImpl::accessKeyAction()
1428 switch (inputType()) {
1430 // a no-op for this type
1457 bool HTMLInputElementImpl::mapToEntry(NodeImpl::Id attr, MappedAttributeEntry& result) const
1464 result = eUniversal;
1467 result = eReplaced; // Share with <img> since the alignment behavior is the same.
1473 return HTMLElementImpl::mapToEntry(attr, result);
1476 void HTMLInputElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
1480 case ATTR_AUTOCOMPLETE:
1481 m_autocomplete = strcasecmp( attr->value(), "off" );
1484 setType(attr->value());
1485 if (m_type != IMAGE && m_imageLoader) {
1486 delete m_imageLoader;
1491 // We only need to setChanged if the form is looking at the default value right now.
1492 if (m_value.isNull())
1496 m_defaultChecked = !attr->isNull();
1497 if (m_useDefaultChecked) {
1498 setChecked(m_defaultChecked);
1499 m_useDefaultChecked = true;
1502 case ATTR_MAXLENGTH:
1503 m_maxLen = !attr->isNull() ? attr->value().toInt() : -1;
1507 m_size = !attr->isNull() ? attr->value().toInt() : 20;
1510 if (m_render && m_type == IMAGE)
1511 static_cast<RenderImage*>(m_render)->updateAltText();
1514 if (m_render && m_type == IMAGE) {
1516 m_imageLoader = new HTMLImageLoader(this);
1517 m_imageLoader->updateFromElement();
1521 case ATTR_ACCESSKEY:
1522 // ### ignore for the moment
1525 addCSSLength(attr, CSS_PROP_MARGIN_TOP, attr->value());
1526 addCSSLength(attr, CSS_PROP_MARGIN_BOTTOM, attr->value());
1529 addCSSLength(attr, CSS_PROP_MARGIN_LEFT, attr->value());
1530 addCSSLength(attr, CSS_PROP_MARGIN_RIGHT, attr->value());
1533 addHTMLAlignment(attr);
1536 addCSSLength(attr, CSS_PROP_WIDTH, attr->value() );
1539 addCSSLength(attr, CSS_PROP_HEIGHT, attr->value() );
1542 setHTMLEventListener(EventImpl::FOCUS_EVENT,
1543 getDocument()->createHTMLEventListener(attr->value().string()));
1546 setHTMLEventListener(EventImpl::BLUR_EVENT,
1547 getDocument()->createHTMLEventListener(attr->value().string()));
1550 setHTMLEventListener(EventImpl::SELECT_EVENT,
1551 getDocument()->createHTMLEventListener(attr->value().string()));
1554 setHTMLEventListener(EventImpl::CHANGE_EVENT,
1555 getDocument()->createHTMLEventListener(attr->value().string()));
1558 setHTMLEventListener(EventImpl::INPUT_EVENT,
1559 getDocument()->createHTMLEventListener(attr->value().string()));
1562 // Search field and slider attributes all just cause updateFromElement to be called through style
1565 setHTMLEventListener(EventImpl::SEARCH_EVENT,
1566 getDocument()->createHTMLEventListener(attr->value().string()));
1569 m_maxResults = !attr->isNull() ? attr->value().toInt() : 0;
1572 case ATTR_INCREMENTAL:
1573 case ATTR_PLACEHOLDER:
1576 case ATTR_PRECISION:
1581 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
1585 bool HTMLInputElementImpl::rendererIsNeeded(RenderStyle *style)
1602 case BUTTON: return HTMLGenericFormElementImpl::rendererIsNeeded(style);
1603 case HIDDEN: return false;
1609 RenderObject *HTMLInputElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
1618 case ISINDEX: return new (arena) RenderLineEdit(this);
1619 case CHECKBOX: return new (arena) RenderCheckBox(this);
1620 case RADIO: return new (arena) RenderRadioButton(this);
1621 case SUBMIT: return new (arena) RenderSubmitButton(this);
1622 case IMAGE: return new (arena) RenderImageButton(this);
1623 case RESET: return new (arena) RenderResetButton(this);
1624 case FILE: return new (arena) RenderFileButton(this);
1625 case BUTTON: return new (arena) RenderPushButton(this);
1627 case RANGE: return new (arena) RenderSlider(this);
1635 void HTMLInputElementImpl::attach()
1639 setType(getAttribute(ATTR_TYPE));
1641 // FIXME: This needs to be dynamic, doesn't it, since someone could set this
1642 // after attachment?
1643 DOMString val = getAttribute(ATTR_VALUE);
1644 if ((uint) m_type <= ISINDEX && !val.isEmpty()) {
1645 // remove newline stuff..
1647 for (unsigned int i = 0; i < val.length(); ++i)
1651 if (val.length() != nvalue.length())
1652 setAttribute(ATTR_VALUE, nvalue);
1655 m_defaultChecked = (!getAttribute(ATTR_CHECKED).isNull());
1660 // Disallow the width attribute on inputs other than HIDDEN and IMAGE.
1661 // Dumb Web sites will try to set the width as an attribute on form controls that aren't
1662 // images or hidden.
1663 if (hasMappedAttributes() && m_type != HIDDEN && m_type != IMAGE && !getAttribute(ATTR_WIDTH).isEmpty()) {
1665 removeAttribute(ATTR_WIDTH, excCode);
1668 HTMLGenericFormElementImpl::attach();
1670 if (m_type == IMAGE) {
1672 m_imageLoader = new HTMLImageLoader(this);
1673 m_imageLoader->updateFromElement();
1675 RenderImage* imageObj = static_cast<RenderImage*>(renderer());
1676 imageObj->setImage(m_imageLoader->image());
1681 // note we don't deal with calling passwordFieldRemoved() on detach, because the timing
1682 // was such that it cleared our state too early
1683 if (m_type == PASSWORD)
1684 getDocument()->passwordFieldAdded();
1688 DOMString HTMLInputElementImpl::altText() const
1690 // http://www.w3.org/TR/1998/REC-html40-19980424/appendix/notes.html#altgen
1691 // also heavily discussed by Hixie on bugzilla
1692 // note this is intentionally different to HTMLImageElementImpl::altText()
1693 DOMString alt = getAttribute( ATTR_ALT );
1694 // fall back to title attribute
1696 alt = getAttribute( ATTR_TITLE );
1698 alt = getAttribute( ATTR_VALUE );
1699 if ( alt.isEmpty() )
1701 alt = inputElementAltText();
1703 alt = i18n( "Submit" );
1709 bool HTMLInputElementImpl::isSuccessfulSubmitButton() const
1711 // HTML spec says that buttons must have names
1712 // to be considered successful. However, other browsers
1713 // do not impose this constraint. Therefore, we behave
1714 // differently and can use different buttons than the
1716 // Was: (m_type == SUBMIT && !name().isEmpty())
1717 return !m_disabled && (m_type == IMAGE || m_type == SUBMIT);
1720 bool HTMLInputElementImpl::isActivatedSubmit() const
1722 return m_activeSubmit;
1725 void HTMLInputElementImpl::setActivatedSubmit(bool flag)
1727 m_activeSubmit = flag;
1730 bool HTMLInputElementImpl::appendFormData(FormDataList &encoding, bool multipart)
1732 // image generates its own names
1733 if (name().isEmpty() && m_type != IMAGE) return false;
1743 // always successful
1744 encoding.appendData(name(), value());
1750 encoding.appendData(name(), value());
1757 // those buttons are never successful
1763 encoding.appendData(name().isEmpty() ? QString::fromLatin1("x") : (name().string() + ".x"), clickX());
1764 encoding.appendData(name().isEmpty() ? QString::fromLatin1("y") : (name().string() + ".y"), clickY());
1772 QString enc_str = value().string();
1773 if (enc_str.isEmpty())
1774 enc_str = static_cast<RenderSubmitButton*>(m_render)->defaultLabel();
1775 if (!enc_str.isEmpty()) {
1776 encoding.appendData(name(), enc_str);
1784 // can't submit file on GET
1785 // don't submit if display: none or display: hidden
1786 if(!multipart || !renderer() || renderer()->style()->visibility() != khtml::VISIBLE)
1789 // if no filename at all is entered, return successful, however empty
1790 // null would be more logical but netscape posts an empty file. argh.
1791 if (value().isEmpty()) {
1792 encoding.appendData(name(), QString(""));
1796 KURL fileurl("file:///");
1797 fileurl.setPath(value().string());
1798 KIO::UDSEntry filestat;
1800 if (!KIO::NetAccess::stat(fileurl, filestat)) {
1802 // FIXME: Figure out how to report this error.
1804 KMessageBox::sorry(0L, i18n("Error fetching file for submission:\n%1").arg(KIO::NetAccess::lastErrorString()));
1809 KFileItem fileitem(filestat, fileurl, true, false);
1810 if (fileitem.isDir()) {
1815 if ( KIO::NetAccess::download(fileurl, local) )
1818 if (file.open(IO_ReadOnly))
1820 QCString filearray(file.size()+1);
1821 int readbytes = file.readBlock( filearray.data(), file.size());
1822 if ( readbytes >= 0 )
1823 filearray[readbytes] = '\0';
1826 encoding.appendData(name(), filearray);
1827 KIO::NetAccess::removeTempFile( local );
1835 // FIXME: Figure out how to report this error.
1837 KMessageBox::sorry(0L, i18n("Error fetching file for submission:\n%1").arg(KIO::NetAccess::lastErrorString()));
1844 encoding.appendData(name(), value());
1850 void HTMLInputElementImpl::reset()
1852 if (storesValueSeparateFromAttribute())
1853 setValue(DOMString());
1854 setChecked(m_defaultChecked);
1855 m_useDefaultChecked = true;
1858 void HTMLInputElementImpl::setChecked(bool _checked)
1860 if (checked() == _checked) return;
1862 if (m_form && m_type == RADIO && _checked && !name().isEmpty())
1863 m_form->radioClicked(this);
1865 m_useDefaultChecked = false;
1866 m_checked = _checked;
1871 DOMString HTMLInputElementImpl::value() const
1873 DOMString value = m_value;
1875 // It's important *not* to fall back to the value attribute for file inputs,
1876 // because that would allow a malicious web page to upload files by setting the
1877 // value attribute in markup.
1878 if (value.isNull() && m_type != FILE)
1879 value = getAttribute(ATTR_VALUE);
1881 // If no attribute exists, then just use "on" or "" based off the checked() state of the control.
1882 if (value.isNull() && (m_type == CHECKBOX || m_type == RADIO))
1883 return DOMString(checked() ? "on" : "");
1889 void HTMLInputElementImpl::setValue(const DOMString &value)
1891 if (m_type == FILE) return;
1893 if (storesValueSeparateFromAttribute()) {
1897 setAttribute(ATTR_VALUE, value);
1901 bool HTMLInputElementImpl::storesValueSeparateFromAttribute() const
1927 void HTMLInputElementImpl::blur()
1929 if(getDocument()->focusNode() == this)
1930 getDocument()->setFocusNode(0);
1933 void HTMLInputElementImpl::focus()
1935 getDocument()->setFocusNode(this);
1938 void HTMLInputElementImpl::defaultEventHandler(EventImpl *evt)
1940 if (evt->isMouseEvent() &&
1941 ( evt->id() == EventImpl::KHTML_CLICK_EVENT || evt->id() == EventImpl::KHTML_DBLCLICK_EVENT ) &&
1944 // record the mouse position for when we get the DOMActivate event
1945 MouseEventImpl *me = static_cast<MouseEventImpl*>(evt);
1946 int offsetX, offsetY;
1947 m_render->absolutePosition(offsetX,offsetY);
1948 xPos = me->clientX()-offsetX;
1949 yPos = me->clientY()-offsetY;
1951 me->setDefaultHandled();
1954 // DOMActivate events cause the input to be "activated" - in the case of image and submit inputs, this means
1955 // actually submitting the form. For reset inputs, the form is reset. These events are sent when the user clicks
1956 // on the element, or presses enter while it is the active element. Javacsript code wishing to activate the element
1957 // must dispatch a DOMActivate event - a click event will not do the job.
1958 if ((evt->id() == EventImpl::DOMACTIVATE_EVENT) &&
1959 (m_type == IMAGE || m_type == SUBMIT || m_type == RESET)){
1961 if (!m_form || !m_render)
1964 if (m_type == RESET) {
1968 m_activeSubmit = true;
1969 if (!m_form->prepareSubmit()) {
1973 m_activeSubmit = false;
1978 // Use key press event here since sending simulated mouse events
1979 // on key down blocks the proper sending of the key press event.
1980 if (evt->id() == EventImpl::KEYPRESS_EVENT && evt->isKeyboardEvent()) {
1981 DOMString key = static_cast<KeyboardEventImpl *>(evt)->keyIdentifier();
1990 // Simulate mouse click for enter or spacebar for these types of elements.
1991 // The AppKit already does this for spacebar for some, but not all, of them.
1992 if (key == "U+000020" || key == "Enter") {
1994 evt->setDefaultHandled();
2003 // Simulate mouse click on the default form button for enter for these types of elements.
2004 if (key == "Enter" && m_form) {
2005 m_form->submitClick();
2006 evt->setDefaultHandled();
2013 HTMLGenericFormElementImpl::defaultEventHandler(evt);
2016 bool HTMLInputElementImpl::isEditable()
2018 return ((m_type == TEXT) || (m_type == PASSWORD) ||
2019 (m_type == SEARCH) || (m_type == ISINDEX) || (m_type == FILE));
2022 bool HTMLInputElementImpl::isURLAttribute(AttributeImpl *attr) const
2024 return (attr->id() == ATTR_SRC);
2027 // -------------------------------------------------------------------------
2029 HTMLLabelElementImpl::HTMLLabelElementImpl(DocumentPtr *doc)
2030 : HTMLElementImpl(doc)
2034 HTMLLabelElementImpl::~HTMLLabelElementImpl()
2038 bool HTMLLabelElementImpl::isFocusable() const
2043 NodeImpl::Id HTMLLabelElementImpl::id() const
2048 void HTMLLabelElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
2053 setHTMLEventListener(EventImpl::FOCUS_EVENT,
2054 getDocument()->createHTMLEventListener(attr->value().string()));
2057 setHTMLEventListener(EventImpl::BLUR_EVENT,
2058 getDocument()->createHTMLEventListener(attr->value().string()));
2061 HTMLElementImpl::parseHTMLAttribute(attr);
2065 ElementImpl *HTMLLabelElementImpl::formElement()
2067 DOMString formElementId = getAttribute(ATTR_FOR);
2068 if (formElementId.isNull()) {
2069 // Search children of the label element for a form element.
2070 NodeImpl *node = this;
2071 while ((node = node->traverseNextNode(this))) {
2072 if (node->isHTMLElement()) {
2073 HTMLElementImpl *element = static_cast<HTMLElementImpl *>(node);
2074 if (element->isGenericFormElement()) {
2081 if (formElementId.isEmpty())
2083 return getDocument()->getElementById(formElementId);
2086 void HTMLLabelElementImpl::accessKeyAction()
2088 ElementImpl *element = formElement();
2090 element->accessKeyAction();
2093 // -------------------------------------------------------------------------
2095 HTMLLegendElementImpl::HTMLLegendElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
2096 : HTMLGenericFormElementImpl(doc, f)
2100 HTMLLegendElementImpl::~HTMLLegendElementImpl()
2104 bool HTMLLegendElementImpl::isFocusable() const
2109 NodeImpl::Id HTMLLegendElementImpl::id() const
2114 RenderObject* HTMLLegendElementImpl::createRenderer(RenderArena* arena, RenderStyle* style)
2116 return new (arena) RenderLegend(this);
2119 DOMString HTMLLegendElementImpl::type() const
2124 // -------------------------------------------------------------------------
2126 HTMLSelectElementImpl::HTMLSelectElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
2127 : HTMLGenericFormElementImpl(doc, f), m_options(0)
2130 m_recalcListItems = false;
2131 // 0 means invalid (i.e. not set)
2136 HTMLSelectElementImpl::~HTMLSelectElementImpl()
2138 if (getDocument()) getDocument()->deregisterMaintainsState(this);
2140 m_options->detach();
2145 NodeImpl::Id HTMLSelectElementImpl::id() const
2150 void HTMLSelectElementImpl::recalcStyle( StyleChange ch )
2152 if (hasChangedChild() && m_render) {
2153 static_cast<khtml::RenderSelect*>(m_render)->setOptionsChanged(true);
2156 HTMLGenericFormElementImpl::recalcStyle( ch );
2160 DOMString HTMLSelectElementImpl::type() const
2162 return (m_multiple ? "select-multiple" : "select-one");
2165 long HTMLSelectElementImpl::selectedIndex() const
2167 // return the number of the first option selected
2169 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2170 for (unsigned int i = 0; i < items.size(); i++) {
2171 if (items[i]->id() == ID_OPTION) {
2172 if (static_cast<HTMLOptionElementImpl*>(items[i])->selected())
2177 Q_ASSERT(m_multiple);
2181 void HTMLSelectElementImpl::setSelectedIndex( long index )
2183 // deselect all other options and select only the new one
2184 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2186 for (listIndex = 0; listIndex < int(items.size()); listIndex++) {
2187 if (items[listIndex]->id() == ID_OPTION)
2188 static_cast<HTMLOptionElementImpl*>(items[listIndex])->setSelected(false);
2190 listIndex = optionToListIndex(index);
2192 static_cast<HTMLOptionElementImpl*>(items[listIndex])->setSelected(true);
2197 long HTMLSelectElementImpl::length() const
2201 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2202 for (i = 0; i < items.size(); i++) {
2203 if (items[i]->id() == ID_OPTION)
2209 void HTMLSelectElementImpl::add( HTMLElementImpl *element, HTMLElementImpl *before )
2211 if (!element || element->id() != ID_OPTION)
2214 int exceptioncode = 0;
2215 insertBefore(element, before, exceptioncode);
2217 setRecalcListItems();
2220 void HTMLSelectElementImpl::remove( long index )
2222 int exceptioncode = 0;
2223 int listIndex = optionToListIndex(index);
2225 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2226 if(listIndex < 0 || index >= int(items.size()))
2227 return; // ### what should we do ? remove the last item?
2229 removeChild(items[listIndex], exceptioncode);
2230 if( !exceptioncode )
2231 setRecalcListItems();
2234 void HTMLSelectElementImpl::blur()
2236 if(getDocument()->focusNode() == this)
2237 getDocument()->setFocusNode(0);
2240 void HTMLSelectElementImpl::focus()
2242 getDocument()->setFocusNode(this);
2245 DOMString HTMLSelectElementImpl::value( )
2248 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2249 for (i = 0; i < items.size(); i++) {
2250 if ( items[i]->id() == ID_OPTION
2251 && static_cast<HTMLOptionElementImpl*>(items[i])->selected())
2252 return static_cast<HTMLOptionElementImpl*>(items[i])->value();
2254 return DOMString("");
2257 void HTMLSelectElementImpl::setValue(DOMStringImpl* value)
2259 // find the option with value() matching the given parameter
2260 // and make it the current selection.
2261 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2262 for (unsigned i = 0; i < items.size(); i++)
2263 if (items[i]->id() == ID_OPTION && static_cast<HTMLOptionElementImpl*>(items[i])->value() == value) {
2264 static_cast<HTMLOptionElementImpl*>(items[i])->setSelected(true);
2269 QString HTMLSelectElementImpl::state( )
2274 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2276 int l = items.count();
2279 QChar stateChars[l];
2281 for(int i = 0; i < l; i++)
2282 if(items[i]->id() == ID_OPTION && static_cast<HTMLOptionElementImpl*>(items[i])->selected())
2283 stateChars[i] = 'X';
2285 stateChars[i] = '.';
2286 QString state(stateChars, l);
2287 #else /* APPLE_CHANGES not defined */
2289 for(int i = 0; i < l; i++)
2290 if(items[i]->id() == ID_OPTION && static_cast<HTMLOptionElementImpl*>(items[i])->selected())
2292 #endif /* APPLE_CHANGES not defined */
2294 return HTMLGenericFormElementImpl::state() + state;
2297 void HTMLSelectElementImpl::restoreState(QStringList &_states)
2299 QString _state = HTMLGenericFormElementImpl::findMatchingState(_states);
2300 if (_state.isNull()) return;
2304 QString state = _state;
2305 if(!state.isEmpty() && !state.contains('X') && !m_multiple) {
2306 qWarning("should not happen in restoreState!");
2308 // KWQString doesn't support this operation. Should never get here anyway.
2315 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2317 int l = items.count();
2318 for(int i = 0; i < l; i++) {
2319 if(items[i]->id() == ID_OPTION) {
2320 HTMLOptionElementImpl* oe = static_cast<HTMLOptionElementImpl*>(items[i]);
2321 oe->setSelected(state[i] == 'X');
2327 NodeImpl *HTMLSelectElementImpl::insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode )
2329 NodeImpl *result = HTMLGenericFormElementImpl::insertBefore(newChild,refChild, exceptioncode );
2331 setRecalcListItems();
2335 NodeImpl *HTMLSelectElementImpl::replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode )
2337 NodeImpl *result = HTMLGenericFormElementImpl::replaceChild(newChild,oldChild, exceptioncode);
2338 if( !exceptioncode )
2339 setRecalcListItems();
2343 NodeImpl *HTMLSelectElementImpl::removeChild ( NodeImpl *oldChild, int &exceptioncode )
2345 NodeImpl *result = HTMLGenericFormElementImpl::removeChild(oldChild, exceptioncode);
2346 if( !exceptioncode )
2347 setRecalcListItems();
2351 NodeImpl *HTMLSelectElementImpl::appendChild ( NodeImpl *newChild, int &exceptioncode )
2353 NodeImpl *result = HTMLGenericFormElementImpl::appendChild(newChild, exceptioncode);
2354 if( !exceptioncode )
2355 setRecalcListItems();
2360 NodeImpl* HTMLSelectElementImpl::addChild(NodeImpl* newChild)
2362 setRecalcListItems();
2363 return HTMLGenericFormElementImpl::addChild(newChild);
2366 void HTMLSelectElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
2371 m_size = QMAX( attr->value().toInt(), 1 );
2374 m_minwidth = QMAX( attr->value().toInt(), 0 );
2377 m_multiple = (!attr->isNull());
2379 case ATTR_ACCESSKEY:
2380 // ### ignore for the moment
2383 setHTMLEventListener(EventImpl::FOCUS_EVENT,
2384 getDocument()->createHTMLEventListener(attr->value().string()));
2387 setHTMLEventListener(EventImpl::BLUR_EVENT,
2388 getDocument()->createHTMLEventListener(attr->value().string()));
2391 setHTMLEventListener(EventImpl::CHANGE_EVENT,
2392 getDocument()->createHTMLEventListener(attr->value().string()));
2395 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
2399 RenderObject *HTMLSelectElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
2401 return new (arena) RenderSelect(this);
2404 bool HTMLSelectElementImpl::appendFormData(FormDataList& encoded_values, bool)
2406 bool successful = false;
2407 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2410 for (i = 0; i < items.size(); i++) {
2411 if (items[i]->id() == ID_OPTION) {
2412 HTMLOptionElementImpl *option = static_cast<HTMLOptionElementImpl*>(items[i]);
2413 if (option->selected()) {
2414 encoded_values.appendData(name(), option->value());
2420 // ### this case should not happen. make sure that we select the first option
2421 // in any case. otherwise we have no consistency with the DOM interface. FIXME!
2422 // we return the first one if it was a combobox select
2423 if (!successful && !m_multiple && m_size <= 1 && items.size() &&
2424 (items[0]->id() == ID_OPTION) ) {
2425 HTMLOptionElementImpl *option = static_cast<HTMLOptionElementImpl*>(items[0]);
2426 if (option->value().isNull())
2427 encoded_values.appendData(name(), option->text().string().stripWhiteSpace());
2429 encoded_values.appendData(name(), option->value());
2436 int HTMLSelectElementImpl::optionToListIndex(int optionIndex) const
2438 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2439 if (optionIndex < 0 || optionIndex >= int(items.size()))
2443 int optionIndex2 = 0;
2445 optionIndex2 < int(items.size()) && optionIndex2 <= optionIndex;
2446 listIndex++) { // not a typo!
2447 if (items[listIndex]->id() == ID_OPTION)
2454 int HTMLSelectElementImpl::listToOptionIndex(int listIndex) const
2456 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2457 if (listIndex < 0 || listIndex >= int(items.size()) ||
2458 items[listIndex]->id() != ID_OPTION)
2461 int optionIndex = 0; // actual index of option not counting OPTGROUP entries that may be in list
2463 for (i = 0; i < listIndex; i++)
2464 if (items[i]->id() == ID_OPTION)
2469 HTMLOptionsCollectionImpl *HTMLSelectElementImpl::options()
2472 m_options = new HTMLOptionsCollectionImpl(this);
2478 void HTMLSelectElementImpl::recalcListItems()
2480 NodeImpl* current = firstChild();
2481 m_listItems.resize(0);
2482 HTMLOptionElementImpl* foundSelected = 0;
2484 if (current->id() == ID_OPTGROUP && current->firstChild()) {
2485 // ### what if optgroup contains just comments? don't want one of no options in it...
2486 m_listItems.resize(m_listItems.size()+1);
2487 m_listItems[m_listItems.size()-1] = static_cast<HTMLGenericFormElementImpl*>(current);
2488 current = current->firstChild();
2490 if (current->id() == ID_OPTION) {
2491 m_listItems.resize(m_listItems.size()+1);
2492 m_listItems[m_listItems.size()-1] = static_cast<HTMLGenericFormElementImpl*>(current);
2493 if (!foundSelected && !m_multiple && m_size <= 1) {
2494 foundSelected = static_cast<HTMLOptionElementImpl*>(current);
2495 foundSelected->m_selected = true;
2497 else if (foundSelected && !m_multiple && static_cast<HTMLOptionElementImpl*>(current)->selected()) {
2498 foundSelected->m_selected = false;
2499 foundSelected = static_cast<HTMLOptionElementImpl*>(current);
2502 NodeImpl *parent = current->parentNode();
2503 current = current->nextSibling();
2506 current = parent->nextSibling();
2509 m_recalcListItems = false;
2512 void HTMLSelectElementImpl::childrenChanged()
2514 setRecalcListItems();
2516 HTMLGenericFormElementImpl::childrenChanged();
2519 void HTMLSelectElementImpl::setRecalcListItems()
2521 m_recalcListItems = true;
2523 static_cast<khtml::RenderSelect*>(m_render)->setOptionsChanged(true);
2527 void HTMLSelectElementImpl::reset()
2529 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2531 for (i = 0; i < items.size(); i++) {
2532 if (items[i]->id() == ID_OPTION) {
2533 HTMLOptionElementImpl *option = static_cast<HTMLOptionElementImpl*>(items[i]);
2534 bool selected = (!option->getAttribute(ATTR_SELECTED).isNull());
2535 option->setSelected(selected);
2539 static_cast<RenderSelect*>(m_render)->setSelectionChanged(true);
2543 void HTMLSelectElementImpl::notifyOptionSelected(HTMLOptionElementImpl *selectedOption, bool selected)
2545 if (selected && !m_multiple) {
2546 // deselect all other options
2547 QMemArray<HTMLGenericFormElementImpl*> items = listItems();
2549 for (i = 0; i < items.size(); i++) {
2550 if (items[i]->id() == ID_OPTION)
2551 static_cast<HTMLOptionElementImpl*>(items[i])->m_selected = (items[i] == selectedOption);
2555 static_cast<RenderSelect*>(m_render)->setSelectionChanged(true);
2562 void HTMLSelectElementImpl::defaultEventHandler(EventImpl *evt)
2564 // Use key press event here since sending simulated mouse events
2565 // on key down blocks the proper sending of the key press event.
2566 if (evt->id() == EventImpl::KEYPRESS_EVENT) {
2568 if (!m_form || !m_render || !evt->isKeyboardEvent())
2571 DOMString key = static_cast<KeyboardEventImpl *>(evt)->keyIdentifier();
2573 if (key == "Enter") {
2574 m_form->submitClick();
2575 evt->setDefaultHandled();
2578 HTMLGenericFormElementImpl::defaultEventHandler(evt);
2581 #endif // APPLE_CHANGES
2583 void HTMLSelectElementImpl::accessKeyAction()
2588 // -------------------------------------------------------------------------
2590 HTMLKeygenElementImpl::HTMLKeygenElementImpl(DocumentPtr* doc, HTMLFormElementImpl* f)
2591 : HTMLSelectElementImpl(doc, f)
2593 QStringList keys = KSSLKeyGen::supportedKeySizes();
2594 for (QStringList::Iterator i = keys.begin(); i != keys.end(); ++i) {
2595 HTMLOptionElementImpl* o = new HTMLOptionElementImpl(doc, form());
2597 o->addChild(new TextImpl(doc, DOMString(*i)));
2601 NodeImpl::Id HTMLKeygenElementImpl::id() const
2606 DOMString HTMLKeygenElementImpl::type() const
2611 void HTMLKeygenElementImpl::parseHTMLAttribute(HTMLAttributeImpl* attr)
2615 case ATTR_CHALLENGE:
2616 m_challenge = attr->value();
2619 m_keyType = attr->value();
2622 // skip HTMLSelectElementImpl parsing!
2623 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
2627 bool HTMLKeygenElementImpl::appendFormData(FormDataList& encoded_values, bool)
2630 // Only RSA is supported at this time.
2631 if (!m_keyType.isNull() && strcasecmp(m_keyType, "rsa")) {
2634 QString value = KSSLKeyGen::signedPublicKeyAndChallengeString(selectedIndex(), m_challenge.string(), getDocument()->part()->baseURL());
2635 if (value.isNull()) {
2638 encoded_values.appendData(name(), value.utf8());
2641 bool successful = false;
2643 // pop up the fancy certificate creation dialog here
2644 KSSLKeyGen *kg = new KSSLKeyGen(static_cast<RenderWidget *>(m_render)->widget(), "Key Generator", true);
2647 successful = (QDialog::Accepted == kg->exec());
2651 encoded_values.appendData(name(), "deadbeef");
2657 // -------------------------------------------------------------------------
2659 HTMLOptGroupElementImpl::HTMLOptGroupElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
2660 : HTMLGenericFormElementImpl(doc, f)
2664 HTMLOptGroupElementImpl::~HTMLOptGroupElementImpl()
2668 bool HTMLOptGroupElementImpl::isFocusable() const
2673 NodeImpl::Id HTMLOptGroupElementImpl::id() const
2678 DOMString HTMLOptGroupElementImpl::type() const
2683 NodeImpl *HTMLOptGroupElementImpl::insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode )
2685 NodeImpl *result = HTMLGenericFormElementImpl::insertBefore(newChild,refChild, exceptioncode);
2686 if ( !exceptioncode )
2687 recalcSelectOptions();
2691 NodeImpl *HTMLOptGroupElementImpl::replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode )
2693 NodeImpl *result = HTMLGenericFormElementImpl::replaceChild(newChild,oldChild, exceptioncode);
2695 recalcSelectOptions();
2699 NodeImpl *HTMLOptGroupElementImpl::removeChild ( NodeImpl *oldChild, int &exceptioncode )
2701 NodeImpl *result = HTMLGenericFormElementImpl::removeChild(oldChild, exceptioncode);
2702 if( !exceptioncode )
2703 recalcSelectOptions();
2707 NodeImpl *HTMLOptGroupElementImpl::appendChild ( NodeImpl *newChild, int &exceptioncode )
2709 NodeImpl *result = HTMLGenericFormElementImpl::appendChild(newChild, exceptioncode);
2710 if( !exceptioncode )
2711 recalcSelectOptions();
2715 NodeImpl* HTMLOptGroupElementImpl::addChild(NodeImpl* newChild)
2717 recalcSelectOptions();
2719 return HTMLGenericFormElementImpl::addChild(newChild);
2722 void HTMLOptGroupElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
2724 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
2725 recalcSelectOptions();
2728 void HTMLOptGroupElementImpl::recalcSelectOptions()
2730 NodeImpl *select = parentNode();
2731 while (select && select->id() != ID_SELECT)
2732 select = select->parentNode();
2734 static_cast<HTMLSelectElementImpl*>(select)->setRecalcListItems();
2737 // -------------------------------------------------------------------------
2739 HTMLOptionElementImpl::HTMLOptionElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
2740 : HTMLGenericFormElementImpl(doc, f)
2745 bool HTMLOptionElementImpl::isFocusable() const
2750 NodeImpl::Id HTMLOptionElementImpl::id() const
2755 DOMString HTMLOptionElementImpl::type() const
2760 DOMString HTMLOptionElementImpl::text() const
2763 // WinIE does not use the label attribute, so as a quirk, we ignore it.
2764 if (getDocument() && !getDocument()->inCompatMode())
2765 label = getAttribute(ATTR_LABEL);
2766 if (label.isEmpty() && firstChild() && firstChild()->nodeType() == Node::TEXT_NODE) {
2767 if (firstChild()->nextSibling()) {
2769 NodeImpl *n = firstChild();
2770 for (; n; n = n->nextSibling()) {
2771 if (n->nodeType() == Node::TEXT_NODE ||
2772 n->nodeType() == Node::CDATA_SECTION_NODE)
2773 ret += n->nodeValue();
2778 return firstChild()->nodeValue();
2784 long HTMLOptionElementImpl::index() const
2786 // Let's do this dynamically. Might be a bit slow, but we're sure
2787 // we won't forget to update a member variable in some cases...
2788 QMemArray<HTMLGenericFormElementImpl*> items = getSelect()->listItems();
2789 int l = items.count();
2790 int optionIndex = 0;
2791 for(int i = 0; i < l; i++) {
2792 if(items[i]->id() == ID_OPTION)
2794 if (static_cast<HTMLOptionElementImpl*>(items[i]) == this)
2799 kdWarning() << "HTMLOptionElementImpl::index(): option not found!" << endl;
2803 void HTMLOptionElementImpl::setIndex( long )
2805 kdWarning() << "Unimplemented HTMLOptionElementImpl::setIndex(long) called" << endl;
2809 void HTMLOptionElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
2814 m_selected = (!attr->isNull());
2817 m_value = attr->value();
2820 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
2824 DOMString HTMLOptionElementImpl::value() const
2826 if ( !m_value.isNull() )
2828 // Use the text if the value wasn't set.
2829 return text().string().stripWhiteSpace();
2832 void HTMLOptionElementImpl::setValue(DOMStringImpl* value)
2834 setAttribute(ATTR_VALUE, value);
2837 void HTMLOptionElementImpl::setSelected(bool _selected)
2839 if(m_selected == _selected)
2841 m_selected = _selected;
2842 HTMLSelectElementImpl *select = getSelect();
2844 select->notifyOptionSelected(this,_selected);
2847 void HTMLOptionElementImpl::childrenChanged()
2849 HTMLSelectElementImpl *select = getSelect();
2851 select->childrenChanged();
2854 HTMLSelectElementImpl *HTMLOptionElementImpl::getSelect() const
2856 NodeImpl *select = parentNode();
2857 while (select && select->id() != ID_SELECT)
2858 select = select->parentNode();
2859 return static_cast<HTMLSelectElementImpl*>(select);
2862 // -------------------------------------------------------------------------
2864 HTMLTextAreaElementImpl::HTMLTextAreaElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
2865 : HTMLGenericFormElementImpl(doc, f)
2867 // DTD requires rows & cols be specified, but we will provide reasonable defaults
2870 m_wrap = ta_Virtual;
2871 m_dirtyvalue = true;
2874 HTMLTextAreaElementImpl::~HTMLTextAreaElementImpl()
2876 if (getDocument()) getDocument()->deregisterMaintainsState(this);
2879 NodeImpl::Id HTMLTextAreaElementImpl::id() const
2884 DOMString HTMLTextAreaElementImpl::type() const
2889 QString HTMLTextAreaElementImpl::state( )
2891 // Make sure the string is not empty!
2892 return HTMLGenericFormElementImpl::state() + value().string()+'.';
2895 void HTMLTextAreaElementImpl::restoreState(QStringList &states)
2897 QString state = HTMLGenericFormElementImpl::findMatchingState(states);
2898 if (state.isNull()) return;
2899 setDefaultValue(state.left(state.length()-1));
2900 // the close() in the rendertree will take care of transferring defaultvalue to 'value'
2903 void HTMLTextAreaElementImpl::select( )
2906 static_cast<RenderTextArea*>(m_render)->select();
2910 void HTMLTextAreaElementImpl::childrenChanged()
2912 setValue(defaultValue());
2915 void HTMLTextAreaElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
2920 m_rows = !attr->isNull() ? attr->value().toInt() : 3;
2922 renderer()->setNeedsLayoutAndMinMaxRecalc();
2925 m_cols = !attr->isNull() ? attr->value().toInt() : 60;
2927 renderer()->setNeedsLayoutAndMinMaxRecalc();
2930 // virtual / physical is Netscape extension of HTML 3.0, now deprecated
2931 // soft/ hard / off is recommendation for HTML 4 extension by IE and NS 4
2932 if ( strcasecmp( attr->value(), "virtual" ) == 0 || strcasecmp( attr->value(), "soft") == 0)
2933 m_wrap = ta_Virtual;
2934 else if ( strcasecmp ( attr->value(), "physical" ) == 0 || strcasecmp( attr->value(), "hard") == 0)
2935 m_wrap = ta_Physical;
2936 else if(strcasecmp( attr->value(), "on" ) == 0)
2937 m_wrap = ta_Physical;
2938 else if(strcasecmp( attr->value(), "off") == 0)
2941 renderer()->setNeedsLayoutAndMinMaxRecalc();
2943 case ATTR_ACCESSKEY:
2944 // ignore for the moment
2947 setHTMLEventListener(EventImpl::FOCUS_EVENT,
2948 getDocument()->createHTMLEventListener(attr->value().string()));
2951 setHTMLEventListener(EventImpl::BLUR_EVENT,
2952 getDocument()->createHTMLEventListener(attr->value().string()));
2955 setHTMLEventListener(EventImpl::SELECT_EVENT,
2956 getDocument()->createHTMLEventListener(attr->value().string()));
2959 setHTMLEventListener(EventImpl::CHANGE_EVENT,
2960 getDocument()->createHTMLEventListener(attr->value().string()));
2963 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
2967 RenderObject *HTMLTextAreaElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
2969 return new (arena) RenderTextArea(this);
2972 bool HTMLTextAreaElementImpl::appendFormData(FormDataList& encoding, bool)
2974 if (name().isEmpty()) return false;
2975 encoding.appendData(name(), value());
2979 void HTMLTextAreaElementImpl::reset()
2981 setValue(defaultValue());
2984 DOMString HTMLTextAreaElementImpl::value()
2986 if ( m_dirtyvalue) {
2988 m_value = static_cast<RenderTextArea*>( m_render )->text();
2990 m_value = defaultValue().string();
2991 m_dirtyvalue = false;
2994 if ( m_value.isNull() ) return "";
2999 void HTMLTextAreaElementImpl::setValue(DOMString _value)
3001 m_value = _value.string();
3002 m_dirtyvalue = false;
3007 DOMString HTMLTextAreaElementImpl::defaultValue()
3010 // there may be comments - just grab the text nodes
3012 for (n = firstChild(); n; n = n->nextSibling())
3013 if (n->isTextNode())
3014 val += static_cast<TextImpl*>(n)->data();
3015 if (val[0] == '\r' && val[1] == '\n') {
3019 else if (val[0] == '\r' || val[0] == '\n') {
3027 void HTMLTextAreaElementImpl::setDefaultValue(DOMString _defaultValue)
3029 // there may be comments - remove all the text nodes and replace them with one
3030 QPtrList<NodeImpl> toRemove;
3032 for (n = firstChild(); n; n = n->nextSibling())
3033 if (n->isTextNode())
3035 QPtrListIterator<NodeImpl> it(toRemove);
3036 int exceptioncode = 0;
3037 for (; it.current(); ++it) {
3038 removeChild(it.current(), exceptioncode);
3040 insertBefore(getDocument()->createTextNode(_defaultValue),firstChild(), exceptioncode);
3041 setValue(_defaultValue);
3044 void HTMLTextAreaElementImpl::blur()
3046 if(getDocument()->focusNode() == this)
3047 getDocument()->setFocusNode(0);
3050 void HTMLTextAreaElementImpl::focus()
3052 getDocument()->setFocusNode(this);
3055 bool HTMLTextAreaElementImpl::isEditable()
3060 void HTMLTextAreaElementImpl::accessKeyAction()
3065 // -------------------------------------------------------------------------
3067 HTMLIsIndexElementImpl::HTMLIsIndexElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f)
3068 : HTMLInputElementImpl(doc, f)
3074 NodeImpl::Id HTMLIsIndexElementImpl::id() const
3079 void HTMLIsIndexElementImpl::parseHTMLAttribute(HTMLAttributeImpl* attr)
3084 setValue(attr->value());
3086 // don't call HTMLInputElement::parseHTMLAttribute here, as it would
3087 // accept attributes this element does not support
3088 HTMLGenericFormElementImpl::parseHTMLAttribute(attr);
3092 // -------------------------------------------------------------------------
3094 unsigned long HTMLOptionsCollectionImpl::length() const
3096 // Not yet implemented.
3100 void HTMLOptionsCollectionImpl::setLength(unsigned long length)
3102 // Not yet implemented.
3105 NodeImpl *HTMLOptionsCollectionImpl::item(unsigned long index) const
3107 // Not yet implemented.
3111 NodeImpl *HTMLOptionsCollectionImpl::namedItem(const DOMString &name) const
3113 // Not yet implemented.
3117 // -------------------------------------------------------------------------
3119 FormDataList::FormDataList(QTextCodec *c)
3124 void FormDataList::appendString(const QCString &s)
3126 m_strings.append(s);
3129 void FormDataList::appendString(const QString &s)
3131 QCString cstr = fixLineBreaks(m_codec->fromUnicode(s));
3132 cstr.truncate(cstr.length());
3133 m_strings.append(cstr);
3136 QValueListConstIterator<QCString> FormDataList::begin() const
3138 return m_strings.begin();
3141 QValueListConstIterator<QCString> FormDataList::end() const
3143 return m_strings.end();