View Javadoc
1   /*
2    * Copyright (c) 2002-2017 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package com.gargoylesoftware.htmlunit.html;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_INPUT_SET_VALUE_MOVE_SELECTION_TO_START;
18  
19  import java.util.Map;
20  
21  import com.gargoylesoftware.htmlunit.SgmlPage;
22  import com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput;
23  import com.gargoylesoftware.htmlunit.html.impl.SelectableTextSelectionDelegate;
24  
25  /**
26   * Wrapper for the HTML element "input" with type="text".
27   *
28   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
29   * @author David K. Taylor
30   * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
31   * @author Daniel Gredler
32   * @author Ahmed Ashour
33   * @author Marc Guillemot
34   * @author Ronald Brill
35   * @author Frank Danek
36   */
37  public class HtmlTextInput extends HtmlInput implements SelectableTextInput {
38  
39      private final SelectableTextSelectionDelegate selectionDelegate_ = new SelectableTextSelectionDelegate(this);
40  
41      private final DoTypeProcessor doTypeProcessor_ = new DoTypeProcessor(this);
42  
43      /**
44       * Creates an instance.
45       *
46       * @param qualifiedName the qualified name of the element type to instantiate
47       * @param page the page that contains this element
48       * @param attributes the initial attributes
49       */
50      HtmlTextInput(final String qualifiedName, final SgmlPage page,
51              final Map<String, DomAttr> attributes) {
52          super(qualifiedName, page, attributes);
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      protected void doType(final char c, final boolean startAtEnd, final boolean lastType) {
60          if (startAtEnd) {
61              selectionDelegate_.setSelectionStart(getValueAttribute().length());
62          }
63          doTypeProcessor_.doType(getValueAttribute(), selectionDelegate_, c, this, lastType);
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      protected void doType(final int keyCode, final boolean startAtEnd, final boolean lastType) {
71          if (startAtEnd) {
72              selectionDelegate_.setSelectionStart(getValueAttribute().length());
73          }
74          doTypeProcessor_.doType(getValueAttribute(), selectionDelegate_, keyCode, this, lastType);
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      protected void typeDone(final String newValue, final boolean notifyAttributeChangeListeners) {
82          if (newValue.length() <= getMaxLength()) {
83              setAttributeNS(null, "value", newValue, notifyAttributeChangeListeners, false);
84          }
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      protected boolean isSubmittableByEnter() {
92          return true;
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public void select() {
100         selectionDelegate_.select();
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public String getSelectedText() {
108         return selectionDelegate_.getSelectedText();
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public String getText() {
116         return getValueAttribute();
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public void setText(final String text) {
124         setValueAttribute(text);
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public int getSelectionStart() {
132         return selectionDelegate_.getSelectionStart();
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public void setSelectionStart(final int selectionStart) {
140         selectionDelegate_.setSelectionStart(selectionStart);
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public int getSelectionEnd() {
148         return selectionDelegate_.getSelectionEnd();
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public void setSelectionEnd(final int selectionEnd) {
156         selectionDelegate_.setSelectionEnd(selectionEnd);
157     }
158 
159     /**
160      * {@inheritDoc}
161      */
162     @Override
163     protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
164             final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
165         super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
166                 notifyMutationObservers);
167         if ("value".equals(qualifiedName)) {
168             final SgmlPage page = getPage();
169             if (page != null && page.isHtmlPage()) {
170                 int pos = 0;
171                 if (!hasFeature(JS_INPUT_SET_VALUE_MOVE_SELECTION_TO_START)) {
172                     pos = attributeValue.length();
173                 }
174                 setSelectionStart(pos);
175                 setSelectionEnd(pos);
176             }
177         }
178     }
179 
180     /**
181      * {@inheritDoc}
182      */
183     @Override
184     protected Object clone() throws CloneNotSupportedException {
185         return new HtmlTextInput(getQualifiedName(), getPage(), getAttributesMap());
186     }
187 
188     /**
189      * {@inheritDoc}
190      */
191     @Override
192     public void setDefaultValue(final String defaultValue) {
193         final boolean modifyValue = getValueAttribute().equals(getDefaultValue());
194         setDefaultValue(defaultValue, modifyValue);
195     }
196 }