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".
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 final SelectableTextSelectionDelegate selectionDelegate_ = new SelectableTextSelectionDelegate(this);
39  
40      private final 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      HtmlPasswordInput(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 boolean isSubmittableByEnter() {
59          return true;
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public void select() {
67          selectionDelegate_.select();
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public String getSelectedText() {
75          return selectionDelegate_.getSelectedText();
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public String getText() {
83          return getValueAttribute();
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public void setText(final String text) {
91          setValueAttribute(text);
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public int getSelectionStart() {
99          return selectionDelegate_.getSelectionStart();
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public void setSelectionStart(final int selectionStart) {
107         selectionDelegate_.setSelectionStart(selectionStart);
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public int getSelectionEnd() {
115         return selectionDelegate_.getSelectionEnd();
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public void setSelectionEnd(final int selectionEnd) {
123         selectionDelegate_.setSelectionEnd(selectionEnd);
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     protected void doType(final char c, final boolean startAtEnd, final boolean lastType) {
131         if (startAtEnd) {
132             selectionDelegate_.setSelectionStart(getValueAttribute().length());
133         }
134         doTypeProcessor_.doType(getValueAttribute(), selectionDelegate_, c, this, lastType);
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     protected void doType(final int keyCode, final boolean startAtEnd, final boolean lastType) {
142         if (startAtEnd) {
143             selectionDelegate_.setSelectionStart(getValueAttribute().length());
144         }
145         doTypeProcessor_.doType(getValueAttribute(), selectionDelegate_, keyCode, this, lastType);
146     }
147 
148     /**
149      * {@inheritDoc}
150      */
151     @Override
152     protected void typeDone(final String newValue, final boolean notifyAttributeChangeListeners) {
153         if (newValue.length() <= getMaxLength()) {
154             setAttributeNS(null, "value", newValue, notifyAttributeChangeListeners, false);
155         }
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     protected Object clone() throws CloneNotSupportedException {
163         return new HtmlPasswordInput(getQualifiedName(), getPage(), getAttributesMap());
164     }
165 
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
171             final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
172         super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
173                 notifyMutationObservers);
174         if ("value".equals(qualifiedName)) {
175             final SgmlPage page = getPage();
176             if (page != null && page.isHtmlPage()) {
177                 int pos = 0;
178                 if (!hasFeature(JS_INPUT_SET_VALUE_MOVE_SELECTION_TO_START)) {
179                     pos = attributeValue.length();
180                 }
181                 setSelectionStart(pos);
182                 setSelectionEnd(pos);
183             }
184         }
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 }