View Javadoc
1   /*
2    * Copyright (c) 2002-2018 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 SelectableTextSelectionDelegate selectionDelegate_ = new SelectableTextSelectionDelegate(this);
40      private DoTypeProcessor doTypeProcessor_ = new DoTypeProcessor(this);
41  
42      /**
43       * Creates an instance.
44       *
45       * @param qualifiedName the qualified name of the element type to instantiate
46       * @param page the page that contains this element
47       * @param attributes the initial attributes
48       */
49      HtmlTextInput(final String qualifiedName, final SgmlPage page,
50              final Map<String, DomAttr> attributes) {
51          super(qualifiedName, page, attributes);
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      protected void doType(final char c, final boolean startAtEnd, final boolean lastType) {
59          if (startAtEnd) {
60              selectionDelegate_.setSelectionStart(getValueAttribute().length());
61          }
62          doTypeProcessor_.doType(getValueAttribute(), selectionDelegate_, c, this, lastType);
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      protected void doType(final int keyCode, final boolean startAtEnd, final boolean lastType) {
70          if (startAtEnd) {
71              selectionDelegate_.setSelectionStart(getValueAttribute().length());
72          }
73          doTypeProcessor_.doType(getValueAttribute(), selectionDelegate_, keyCode, this, lastType);
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      protected void typeDone(final String newValue, final boolean notifyAttributeChangeListeners) {
81          if (newValue.length() <= getMaxLength()) {
82              setAttributeNS(null, "value", newValue, notifyAttributeChangeListeners, false);
83          }
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      protected boolean isSubmittableByEnter() {
91          return true;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public void select() {
99          selectionDelegate_.select();
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public String getSelectedText() {
107         return selectionDelegate_.getSelectedText();
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public String getText() {
115         return getValueAttribute();
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public void setText(final String text) {
123         setValueAttribute(text);
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public int getSelectionStart() {
131         return selectionDelegate_.getSelectionStart();
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public void setSelectionStart(final int selectionStart) {
139         selectionDelegate_.setSelectionStart(selectionStart);
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     @Override
146     public int getSelectionEnd() {
147         return selectionDelegate_.getSelectionEnd();
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public void setSelectionEnd(final int selectionEnd) {
155         selectionDelegate_.setSelectionEnd(selectionEnd);
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
163             final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
164         super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
165                 notifyMutationObservers);
166         if ("value".equals(qualifiedName)) {
167             final SgmlPage page = getPage();
168             if (page != null && page.isHtmlPage()) {
169                 int pos = 0;
170                 if (!hasFeature(JS_INPUT_SET_VALUE_MOVE_SELECTION_TO_START)) {
171                     pos = attributeValue.length();
172                 }
173                 setSelectionStart(pos);
174                 setSelectionEnd(pos);
175             }
176         }
177     }
178 
179     /**
180      * {@inheritDoc}
181      */
182     @Override
183     protected Object clone() throws CloneNotSupportedException {
184         return new HtmlTextInput(getQualifiedName(), getPage(), getAttributesMap());
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     public void setDefaultValue(final String defaultValue) {
192         final boolean modifyValue = getValueAttribute().equals(getDefaultValue());
193         setDefaultValue(defaultValue, modifyValue);
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public DomNode cloneNode(final boolean deep) {
201         final HtmlTextInput newnode = (HtmlTextInput) super.cloneNode(deep);
202         newnode.selectionDelegate_ = new SelectableTextSelectionDelegate(newnode);
203         newnode.doTypeProcessor_ = new DoTypeProcessor(newnode);
204 
205         return newnode;
206     }
207 }