📄 htmlinputelement.cpp
字号:
/* * Copyright (C) 1999 Lars Knoll (knoll@kde.org) * (C) 1999 Antti Koivisto (koivisto@kde.org) * (C) 2001 Dirk Mueller (mueller@kde.org) * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. * (C) 2006 Alexey Proskuryakov (ap@nypop.com) * Copyright (C) 2007 Samuel Weinig (sam@webkit.org) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */#include "config.h"#include "HTMLInputElement.h"#include "ChromeClient.h"#include "CSSPropertyNames.h"#include "Document.h"#include "Editor.h"#include "Event.h"#include "EventHandler.h"#include "EventNames.h"#include "File.h"#include "FileList.h"#include "FocusController.h"#include "FormDataList.h"#include "Frame.h"#include "HTMLFormElement.h"#include "HTMLImageLoader.h"#include "HTMLNames.h"#include "KeyboardEvent.h"#include "LocalizedStrings.h"#include "MouseEvent.h"#include "Page.h"#include "RenderButton.h"#include "RenderFileUploadControl.h"#include "RenderImage.h"#include "RenderSlider.h"#include "RenderText.h"#include "RenderTextControlSingleLine.h"#include "RenderTheme.h"#include "TextEvent.h"#include <wtf/StdLibExtras.h>using namespace std;namespace WebCore {using namespace HTMLNames;const int maxSavedResults = 256;HTMLInputElement::HTMLInputElement(const QualifiedName& tagName, Document* doc, HTMLFormElement* f) : HTMLFormControlElementWithState(tagName, doc, f) , m_data(this, this) , m_xPos(0) , m_yPos(0) , m_maxResults(-1) , m_type(TEXT) , m_checked(false) , m_defaultChecked(false) , m_useDefaultChecked(true) , m_indeterminate(false) , m_haveType(false) , m_activeSubmit(false) , m_autocomplete(Uninitialized) , m_autofilled(false) , m_inited(false){ ASSERT(hasTagName(inputTag) || hasTagName(isindexTag));}HTMLInputElement::~HTMLInputElement(){ if (needsActivationCallback()) document()->unregisterForDocumentActivationCallbacks(this); document()->checkedRadioButtons().removeButton(this); // Need to remove this from the form while it is still an HTMLInputElement, // so can't wait for the base class's destructor to do it. removeFromForm();}const AtomicString& HTMLInputElement::name() const{ return m_data.name();}bool HTMLInputElement::autoComplete() const{ if (m_autocomplete != Uninitialized) return m_autocomplete == On; // Assuming we're still in a Form, respect the Form's setting if (HTMLFormElement* form = this->form()) return form->autoComplete(); // The default is true return true;}static inline HTMLFormElement::CheckedRadioButtons& checkedRadioButtons(const HTMLInputElement *element){ if (HTMLFormElement* form = element->form()) return form->checkedRadioButtons(); return element->document()->checkedRadioButtons();}bool HTMLInputElement::isKeyboardFocusable(KeyboardEvent* event) const{ // If text fields can be focused, then they should always be keyboard focusable if (isTextField()) return HTMLFormControlElementWithState::isFocusable(); // If the base class says we can't be focused, then we can stop now. if (!HTMLFormControlElementWithState::isKeyboardFocusable(event)) return false; if (inputType() == RADIO) { // Never allow keyboard tabbing to leave you in the same radio group. Always // skip any other elements in the group. Node* currentFocusedNode = document()->focusedNode(); if (currentFocusedNode && currentFocusedNode->hasTagName(inputTag)) { HTMLInputElement* focusedInput = static_cast<HTMLInputElement*>(currentFocusedNode); if (focusedInput->inputType() == RADIO && focusedInput->form() == form() && focusedInput->name() == name()) return false; } // Allow keyboard focus if we're checked or if nothing in the group is checked. return checked() || !checkedRadioButtons(this).checkedButtonForGroup(name()); } return true;}bool HTMLInputElement::isMouseFocusable() const{ if (isTextField()) return HTMLFormControlElementWithState::isFocusable(); return HTMLFormControlElementWithState::isMouseFocusable();}void HTMLInputElement::updateFocusAppearance(bool restorePreviousSelection){ if (isTextField()) InputElement::updateFocusAppearance(m_data, document(), restorePreviousSelection); else HTMLFormControlElementWithState::updateFocusAppearance(restorePreviousSelection);}void HTMLInputElement::aboutToUnload(){ InputElement::aboutToUnload(m_data, document());}bool HTMLInputElement::shouldUseInputMethod() const{ return m_type == TEXT || m_type == SEARCH || m_type == ISINDEX;}void HTMLInputElement::dispatchFocusEvent(){ InputElement::dispatchFocusEvent(m_data, document()); if (isTextField()) m_autofilled = false; HTMLFormControlElementWithState::dispatchFocusEvent();}void HTMLInputElement::dispatchBlurEvent(){ InputElement::dispatchBlurEvent(m_data, document()); HTMLFormControlElementWithState::dispatchBlurEvent();}void HTMLInputElement::setType(const String& t){ if (t.isEmpty()) { int exccode; removeAttribute(typeAttr, exccode); } else setAttribute(typeAttr, t);}void HTMLInputElement::setInputType(const String& t){ InputType newType; if (equalIgnoringCase(t, "password")) newType = PASSWORD; else if (equalIgnoringCase(t, "checkbox")) newType = CHECKBOX; else if (equalIgnoringCase(t, "radio")) newType = RADIO; else if (equalIgnoringCase(t, "submit")) newType = SUBMIT; else if (equalIgnoringCase(t, "reset")) newType = RESET; else if (equalIgnoringCase(t, "file")) newType = FILE; else if (equalIgnoringCase(t, "hidden")) newType = HIDDEN; else if (equalIgnoringCase(t, "image")) newType = IMAGE; else if (equalIgnoringCase(t, "button")) newType = BUTTON; else if (equalIgnoringCase(t, "khtml_isindex")) newType = ISINDEX; else if (equalIgnoringCase(t, "search")) newType = SEARCH; else if (equalIgnoringCase(t, "range")) newType = RANGE; 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 (inputType() != newType) { if (newType == FILE && m_haveType) // Set the attribute back to the old value. // Useful in case we were called from inside parseMappedAttribute. setAttribute(typeAttr, type()); else { checkedRadioButtons(this).removeButton(this); if (newType == FILE && !m_fileList) m_fileList = FileList::create(); bool wasAttached = attached(); if (wasAttached) detach(); bool didStoreValue = storesValueSeparateFromAttribute(); bool wasPasswordField = inputType() == PASSWORD; bool didRespectHeightAndWidth = respectHeightAndWidthAttrs(); m_type = newType; bool willStoreValue = storesValueSeparateFromAttribute(); bool isPasswordField = inputType() == PASSWORD; bool willRespectHeightAndWidth = respectHeightAndWidthAttrs(); if (didStoreValue && !willStoreValue && !m_data.value().isNull()) { setAttribute(valueAttr, m_data.value()); m_data.setValue(String()); } if (!didStoreValue && willStoreValue) m_data.setValue(constrainValue(getAttribute(valueAttr))); else InputElement::updateValueIfNeeded(m_data); if (wasPasswordField && !isPasswordField) unregisterForActivationCallbackIfNeeded(); else if (!wasPasswordField && isPasswordField) registerForActivationCallbackIfNeeded(); if (didRespectHeightAndWidth != willRespectHeightAndWidth) { NamedMappedAttrMap* map = mappedAttributes(); if (Attribute* height = map->getAttributeItem(heightAttr)) attributeChanged(height, false); if (Attribute* width = map->getAttributeItem(widthAttr)) attributeChanged(width, false); if (Attribute* align = map->getAttributeItem(alignAttr)) attributeChanged(align, false); } if (wasAttached) { attach(); if (document()->focusedNode() == this) updateFocusAppearance(true); } checkedRadioButtons(this).addButton(this); } InputElement::notifyFormStateChanged(m_data, document()); } m_haveType = true; if (inputType() != IMAGE && m_imageLoader) m_imageLoader.clear();}const AtomicString& HTMLInputElement::type() const{ // needs to be lowercase according to DOM spec switch (inputType()) { case BUTTON: { DEFINE_STATIC_LOCAL(const AtomicString, button, ("button")); return button; } case CHECKBOX: { DEFINE_STATIC_LOCAL(const AtomicString, checkbox, ("checkbox")); return checkbox; } case FILE: { DEFINE_STATIC_LOCAL(const AtomicString, file, ("file")); return file; } case HIDDEN: { DEFINE_STATIC_LOCAL(const AtomicString, hidden, ("hidden")); return hidden; } case IMAGE: { DEFINE_STATIC_LOCAL(const AtomicString, image, ("image")); return image; } case ISINDEX: return emptyAtom; case PASSWORD: { DEFINE_STATIC_LOCAL(const AtomicString, password, ("password")); return password; } case RADIO: { DEFINE_STATIC_LOCAL(const AtomicString, radio, ("radio")); return radio; } case RANGE: { DEFINE_STATIC_LOCAL(const AtomicString, range, ("range")); return range; } case RESET: { DEFINE_STATIC_LOCAL(const AtomicString, reset, ("reset")); return reset; } case SEARCH: { DEFINE_STATIC_LOCAL(const AtomicString, search, ("search")); return search; } case SUBMIT: { DEFINE_STATIC_LOCAL(const AtomicString, submit, ("submit")); return submit; } case TEXT: { DEFINE_STATIC_LOCAL(const AtomicString, text, ("text")); return text; } } return emptyAtom;}bool HTMLInputElement::saveState(String& result) const{ if (!autoComplete()) return false; switch (inputType()) { case BUTTON: case FILE: case HIDDEN: case IMAGE: case ISINDEX: case RANGE: case RESET: case SEARCH: case SUBMIT: case TEXT: result = value(); return true; case CHECKBOX: case RADIO: result = checked() ? "on" : "off"; return true; case PASSWORD: return false; } ASSERT_NOT_REACHED(); return false;}void HTMLInputElement::restoreState(const String& state){ ASSERT(inputType() != PASSWORD); // should never save/restore password fields switch (inputType()) { case BUTTON:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -