📄 html_formimpl.cpp
字号:
}HTMLInputElementImpl::~HTMLInputElementImpl(){ if (getDocument()) getDocument()->deregisterMaintainsState(this);}NodeImpl::Id HTMLInputElementImpl::id() const{ return ID_INPUT;}// Called from JS. Can't merge with parseType since we// also need to actually set ATTR_TYPE, which can't be done there.void HTMLInputElementImpl::setType(const DOMString& t){ setAttribute(ATTR_TYPE, t);}void HTMLInputElementImpl::parseType(const DOMString& t){ typeEnum newType; if ( strcasecmp( t, "password" ) == 0 ) newType = PASSWORD; else if ( strcasecmp( t, "checkbox" ) == 0 ) newType = CHECKBOX; else if ( strcasecmp( t, "radio" ) == 0 ) newType = RADIO; else if ( strcasecmp( t, "submit" ) == 0 ) newType = SUBMIT; else if ( strcasecmp( t, "reset" ) == 0 ) newType = RESET; else if ( strcasecmp( t, "file" ) == 0 ) newType = FILE; else if ( strcasecmp( t, "hidden" ) == 0 ) newType = HIDDEN; else if ( strcasecmp( t, "image" ) == 0 ) newType = IMAGE; else if ( strcasecmp( t, "button" ) == 0 ) newType = BUTTON; else if ( strcasecmp( t, "khtml_isindex" ) == 0 ) newType = ISINDEX; else newType = TEXT; // ### IMPORTANT: Don't allow the type to be changed to FILE after the first // type change, otherwise a JavaScript programmer would be able to set a text // field's value to something like /etc/passwd and then change it to a file field. if (m_type != newType) { if (newType == FILE && m_haveType) { // Set the attribute back to the old value. // Note that this calls parseAttribute again. setAttribute(ATTR_TYPE, type()); } else { m_type = newType; } } m_haveType = true;}DOMString HTMLInputElementImpl::type() const{ // needs to be lowercase according to DOM spec switch (m_type) { case TEXT: return "text"; case PASSWORD: return "password"; case CHECKBOX: return "checkbox"; case RADIO: return "radio"; case SUBMIT: return "submit"; case RESET: return "reset"; case FILE: return "file"; case HIDDEN: return "hidden"; case IMAGE: return "image"; case BUTTON: return "button"; default: return ""; }}QString HTMLInputElementImpl::state( ){ switch (m_type) { case PASSWORD: return QString::fromLatin1("."); // empty string, avoid restoring case CHECKBOX: case RADIO: return QString::fromLatin1(m_checked ? "on" : "off"); case TEXT: if (autoComplete() && value() != getAttribute(ATTR_VALUE) && getDocument()->view()) getDocument()->view()->addFormCompletionItem(name().string(), value().string()); /* nobreak */ default: return value().string() + (m_unsubmittedFormChange ? 'M' : '.'); }}void HTMLInputElementImpl::restoreState(const QString &state){ switch (m_type) { case CHECKBOX: case RADIO: setChecked((state == QString::fromLatin1("on"))); break; case FILE: m_value = DOMString(state.left(state.length()-1)); setChanged(); break; default: setValue(DOMString(state.left(state.length()-1))); m_unsubmittedFormChange = state.endsWith("M"); break; }}void HTMLInputElementImpl::select( ){ if(!m_render) return; if (m_type == TEXT || m_type == PASSWORD) static_cast<RenderLineEdit*>(m_render)->select(); else if (m_type == FILE) static_cast<RenderFileButton*>(m_render)->select();}void HTMLInputElementImpl::click(){ QMouseEvent me(QEvent::MouseButtonRelease, QPoint(0,0),Qt::LeftButton, 0); dispatchMouseEvent(&me,0, 1); dispatchMouseEvent(&me,EventImpl::CLICK_EVENT, 1);}void HTMLInputElementImpl::parseAttribute(AttributeImpl *attr){ switch(attr->id()) { case ATTR_AUTOCOMPLETE: m_autocomplete = strcasecmp( attr->value(), "off" ); break; case ATTR_TYPE: parseType(attr->value()); break; case ATTR_VALUE: if (m_value.isNull()) // We only need to setChanged if the form is looking setChanged(); // at the default value right now. break; case ATTR_CHECKED: // WebCore has m_defaultChecked and m_useDefaultChecked code here.... break; case ATTR_MAXLENGTH: { m_maxLen = -1; if (!attr->val()) break; bool ok; const int ml = attr->val()->toInt(&ok); if (ml > 0 && ml < 1024) m_maxLen = ml; else if (ok && ml <= 0) m_maxLen = 0; setChanged(); } break; case ATTR_SIZE: m_size = attr->val() ? attr->val()->toInt() : 20; break; case ATTR_ALT: case ATTR_SRC: if (m_type == IMAGE) setChanged(); break; case ATTR_USEMAP: // ### ignore for the moment break; case ATTR_ALIGN: if ( m_inited && m_type == IMAGE ) addHTMLAlignment( attr->value() ); break; case ATTR_ACCESSKEY: break; case ATTR_WIDTH: if ( m_type == IMAGE ) addCSSLength(CSS_PROP_WIDTH, attr->value() ); break; case ATTR_HEIGHT: if ( m_type == IMAGE ) addCSSLength(CSS_PROP_HEIGHT, attr->value() ); break; case ATTR_ONSELECT: setHTMLEventListener(EventImpl::SELECT_EVENT, getDocument()->createHTMLEventListener(attr->value().string(), "onselect", this)); break; case ATTR_ONCHANGE: setHTMLEventListener(EventImpl::CHANGE_EVENT, getDocument()->createHTMLEventListener(attr->value().string(), "onchange", this)); break; default: HTMLGenericFormElementImpl::parseAttribute(attr); }}void HTMLInputElementImpl::attach(){ assert(!attached()); assert(!m_render); assert(parentNode()); if (!m_inited) { // FIXME: This needs to be dynamic, doesn't it, since someone could set this // after attachment? if ((uint) m_type <= ISINDEX && !m_value.isEmpty()) { const QString value = m_value.string(); // remove newline stuff.. QString nvalue; unsigned int valueLength = value.length(); for (unsigned int i = 0; i < valueLength; ++i) if (value[i] >= ' ') nvalue += value[i]; m_value = nvalue; } m_checked = (getAttribute(ATTR_CHECKED) != 0); if ( m_type == IMAGE ) addHTMLAlignment( getAttribute( ATTR_ALIGN ) ); m_inited = true; } switch( m_type ) { case PASSWORD: if (getDocument()->isHTMLDocument()) static_cast<HTMLDocumentImpl*>(getDocument())->setAutoFill(); break; case HIDDEN: case IMAGE: if (!getAttribute(ATTR_WIDTH).isNull()) addCSSLength(CSS_PROP_WIDTH, getAttribute(ATTR_WIDTH)); if (!getAttribute(ATTR_HEIGHT).isNull()) addCSSLength(CSS_PROP_HEIGHT, getAttribute(ATTR_HEIGHT)); default: break; }; RenderStyle* const _style = getDocument()->styleSelector()->styleForElement(this); _style->ref(); if (parentNode()->renderer() && _style->display() != NONE) { switch(m_type) { case TEXT: case PASSWORD: case ISINDEX: m_render = new (getDocument()->renderArena()) RenderLineEdit(this); break; case CHECKBOX: m_render = new (getDocument()->renderArena()) RenderCheckBox(this); break; case RADIO: m_render = new (getDocument()->renderArena()) RenderRadioButton(this); break; case SUBMIT: m_render = new (getDocument()->renderArena()) RenderSubmitButton(this); break; case IMAGE: m_render = new (getDocument()->renderArena()) RenderImageButton(this); break; case RESET: m_render = new (getDocument()->renderArena()) RenderResetButton(this); break; case FILE: m_render = new (getDocument()->renderArena()) RenderFileButton(this); break; case BUTTON: m_render = new (getDocument()->renderArena()) RenderPushButton(this); case HIDDEN: break; } } if (m_render) m_render->setStyle(_style); HTMLGenericFormElementImpl::attach(); _style->deref();}DOMString HTMLInputElementImpl::altText() const{ // http://www.w3.org/TR/1998/REC-html40-19980424/appendix/notes.html#altgen // also heavily discussed by Hixie on bugzilla // note this is intentionally different to HTMLImageElementImpl::altText() DOMString alt = getAttribute( ATTR_ALT ); // fall back to title attribute if ( alt.isNull() ) alt = getAttribute( ATTR_TITLE ); if ( alt.isNull() ) alt = getAttribute( ATTR_VALUE ); if ( alt.isEmpty() ) alt = i18n( "Submit" ); return alt;}bool HTMLInputElementImpl::encoding(const QTextCodec* codec, khtml::encodingList& encoding, bool multipart){ const QString nme = name().string(); // image generates its own name's if (nme.isEmpty() && m_type != IMAGE) return false; // IMAGE needs special handling later if(m_type != IMAGE) encoding += fixUpfromUnicode(codec, nme); switch (m_type) { case CHECKBOX: if( checked() ) { encoding += fixUpfromUnicode(codec, value().string()); return true; } break; case RADIO: if( checked() ) { encoding += fixUpfromUnicode(codec, value().string()); return true; } break; case BUTTON: case RESET: // those buttons are never successful return false; case IMAGE: if(m_clicked) { m_clicked = false; QString astr(nme.isEmpty() ? QString::fromLatin1("x") : nme + ".x"); encoding += fixUpfromUnicode(codec, astr); astr.setNum(KMAX( clickX(), 0 )); encoding += fixUpfromUnicode(codec, astr); astr = nme.isEmpty() ? QString::fromLatin1("y") : nme + ".y"; encoding += fixUpfromUnicode(codec, astr); astr.setNum(KMAX( clickY(), 0 ) ); encoding += fixUpfromUnicode(codec, astr); astr = value().string(); if(astr.length() > 0) { encoding += fixUpfromUnicode(codec, nme); encoding += fixUpfromUnicode(codec, astr); } return true; } break; case SUBMIT: if (m_activeSubmit) { QString enc_str = valueWithDefault().string(); if(!enc_str.isEmpty()) { encoding += fixUpfromUnicode(codec, enc_str); return true; } } break; case FILE: // hmm, we have the type FILE also. bad choice here... { // don't submit if display: none or display: hidden if(!renderer() || renderer()->style()->visibility() != khtml::VISIBLE) return false; QString local; KURL fileurl; QString val = value().string(); if (!val.isEmpty() && QDir::isRelativePath(val) && QFile::exists(KGlobalSettings::documentPath() + val)) { fileurl.setPath(KGlobalSettings::documentPath() + val); } else { fileurl = KURL::fromPathOrURL(val); } KIO::UDSEntry filestat; // can't submit file in www-url-form encoded QWidget* const toplevel = static_cast<RenderSubmitButton*>(m_render)->widget()->topLevelWidget(); if (multipart) { QCString filearray( "" ); if ( KIO::NetAccess::stat(fileurl, filestat, toplevel)) { const KFileItem fileitem(filestat, fileurl, true, false); if ( fileitem.isFile() && KIO::NetAccess::download(fileurl, local, toplevel) ) { QFile file(local); filearray.resize(file.size()+1); if ( file.open( IO_ReadOnly ) ) { const int readbytes = file.readBlock( filearray.data(), file.size()); if ( readbytes >= 0 ) filearray[readbytes] = '\0'; file.close(); } KIO::NetAccess::removeTempFile( local ); } } encoding += filearray; return true; } // else fall through } case HIDDEN: case TEXT: case PASSWORD: // always successful encoding += fixUpfromUnicode(codec, value().string()); return true; case ISINDEX: encoding += fixUpfromUnicode(codec, value().string()); return true; } return false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -