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