| 1 | /* |
| 2 | * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 | * Copyright (C) 2018 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #pragma once |
| 33 | |
| 34 | #include "AutoFillButtonElement.h" |
| 35 | #include "DataListButtonElement.h" |
| 36 | #include "DataListSuggestionPicker.h" |
| 37 | #include "DataListSuggestionsClient.h" |
| 38 | #include "InputType.h" |
| 39 | #include "SpinButtonElement.h" |
| 40 | |
| 41 | namespace WebCore { |
| 42 | |
| 43 | class DOMFormData; |
| 44 | class TextControlInnerTextElement; |
| 45 | |
| 46 | // The class represents types of which UI contain text fields. |
| 47 | // It supports not only the types for BaseTextInputType but also type=number. |
| 48 | class TextFieldInputType : public InputType, protected SpinButtonElement::SpinButtonOwner, protected AutoFillButtonElement::AutoFillButtonOwner |
| 49 | #if ENABLE(DATALIST_ELEMENT) |
| 50 | , private DataListSuggestionsClient, protected DataListButtonElement::DataListButtonOwner |
| 51 | #endif |
| 52 | { |
| 53 | protected: |
| 54 | explicit TextFieldInputType(HTMLInputElement&); |
| 55 | virtual ~TextFieldInputType(); |
| 56 | ShouldCallBaseEventHandler handleKeydownEvent(KeyboardEvent&) override; |
| 57 | void handleKeydownEventForSpinButton(KeyboardEvent&); |
| 58 | #if ENABLE(DATALIST_ELEMENT) |
| 59 | void handleClickEvent(MouseEvent&) final; |
| 60 | #endif |
| 61 | |
| 62 | HTMLElement* containerElement() const final; |
| 63 | HTMLElement* innerBlockElement() const final; |
| 64 | RefPtr<TextControlInnerTextElement> innerTextElement() const final; |
| 65 | HTMLElement* innerSpinButtonElement() const final; |
| 66 | HTMLElement* capsLockIndicatorElement() const final; |
| 67 | HTMLElement* autoFillButtonElement() const final; |
| 68 | #if ENABLE(DATALIST_ELEMENT) |
| 69 | HTMLElement* dataListButtonElement() const final; |
| 70 | #endif |
| 71 | |
| 72 | virtual bool needsContainer() const; |
| 73 | void createShadowSubtree() override; |
| 74 | void destroyShadowSubtree() override; |
| 75 | void attributeChanged(const QualifiedName&) override; |
| 76 | void disabledStateChanged() final; |
| 77 | void readOnlyStateChanged() final; |
| 78 | bool supportsReadOnly() const final; |
| 79 | void handleFocusEvent(Node* oldFocusedNode, FocusDirection) final; |
| 80 | void handleBlurEvent() final; |
| 81 | void setValue(const String&, bool valueChanged, TextFieldEventBehavior) override; |
| 82 | void updateInnerTextValue() final; |
| 83 | String sanitizeValue(const String&) const override; |
| 84 | |
| 85 | virtual String convertFromVisibleValue(const String&) const; |
| 86 | virtual void didSetValueByUserEdit(); |
| 87 | |
| 88 | private: |
| 89 | bool isKeyboardFocusable(KeyboardEvent*) const final; |
| 90 | bool isMouseFocusable() const final; |
| 91 | bool isTextField() const final; |
| 92 | bool isEmptyValue() const final; |
| 93 | bool valueMissing(const String&) const final; |
| 94 | void handleBeforeTextInsertedEvent(BeforeTextInsertedEvent&) final; |
| 95 | void forwardEvent(Event&) final; |
| 96 | bool shouldSubmitImplicitly(Event&) final; |
| 97 | RenderPtr<RenderElement> createInputRenderer(RenderStyle&&) override; |
| 98 | bool shouldUseInputMethod() const override; |
| 99 | bool shouldRespectListAttribute() override; |
| 100 | HTMLElement* placeholderElement() const final; |
| 101 | void updatePlaceholderText() final; |
| 102 | bool appendFormData(DOMFormData&, bool multipart) const final; |
| 103 | void subtreeHasChanged() final; |
| 104 | void capsLockStateMayHaveChanged() final; |
| 105 | void updateAutoFillButton() final; |
| 106 | void elementDidBlur() final; |
| 107 | |
| 108 | // SpinButtonElement::SpinButtonOwner functions. |
| 109 | void focusAndSelectSpinButtonOwner() final; |
| 110 | bool shouldSpinButtonRespondToMouseEvents() final; |
| 111 | bool shouldSpinButtonRespondToWheelEvents() final; |
| 112 | void spinButtonStepDown() final; |
| 113 | void spinButtonStepUp() final; |
| 114 | |
| 115 | // AutoFillButtonElement::AutoFillButtonOwner |
| 116 | void autoFillButtonElementWasClicked() final; |
| 117 | |
| 118 | bool shouldHaveSpinButton() const; |
| 119 | bool shouldHaveCapsLockIndicator() const; |
| 120 | bool shouldDrawCapsLockIndicator() const; |
| 121 | bool shouldDrawAutoFillButton() const; |
| 122 | |
| 123 | void createContainer(); |
| 124 | void createAutoFillButton(AutoFillButtonType); |
| 125 | |
| 126 | #if ENABLE(DATALIST_ELEMENT) |
| 127 | void createDataListDropdownIndicator(); |
| 128 | bool isPresentingAttachedView() const final; |
| 129 | void listAttributeTargetChanged() final; |
| 130 | void displaySuggestions(DataListSuggestionActivationType); |
| 131 | void closeSuggestions(); |
| 132 | |
| 133 | // DataListSuggestionsClient |
| 134 | IntRect elementRectInRootViewCoordinates() const final; |
| 135 | Vector<String> suggestions() final; |
| 136 | void didSelectDataListOption(const String&) final; |
| 137 | void didCloseSuggestions() final; |
| 138 | |
| 139 | void dataListButtonElementWasClicked() final; |
| 140 | RefPtr<DataListButtonElement> m_dataListDropdownIndicator; |
| 141 | |
| 142 | std::pair<String, Vector<String>> m_cachedSuggestions; |
| 143 | std::unique_ptr<DataListSuggestionPicker> m_suggestionPicker; |
| 144 | #endif |
| 145 | |
| 146 | RefPtr<HTMLElement> m_container; |
| 147 | RefPtr<HTMLElement> m_innerBlock; |
| 148 | RefPtr<TextControlInnerTextElement> m_innerText; |
| 149 | RefPtr<HTMLElement> m_placeholder; |
| 150 | RefPtr<SpinButtonElement> m_innerSpinButton; |
| 151 | RefPtr<HTMLElement> m_capsLockIndicator; |
| 152 | RefPtr<HTMLElement> m_autoFillButton; |
| 153 | }; |
| 154 | |
| 155 | } // namespace WebCore |
| 156 | |