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