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_EMAIL_TRIMMED;
18  
19  import java.util.Map;
20  
21  import org.apache.commons.lang3.StringUtils;
22  
23  import com.gargoylesoftware.htmlunit.SgmlPage;
24  import com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput;
25  import com.gargoylesoftware.htmlunit.html.impl.SelectableTextSelectionDelegate;
26  
27  /**
28   * Wrapper for the HTML element "input" where type is "email".
29   *
30   * @author Ahmed Ashour
31   */
32  public class HtmlEmailInput extends HtmlInput implements SelectableTextInput {
33  
34      private final SelectableTextSelectionDelegate selectionDelegate_ = new SelectableTextSelectionDelegate(this);
35  
36      private final DoTypeProcessor doTypeProcessor_ = new DoTypeProcessor(this);
37  
38      /**
39       * Creates an instance.
40       *
41       * @param qualifiedName the qualified name of the element type to instantiate
42       * @param page the page that contains this element
43       * @param attributes the initial attributes
44       */
45      HtmlEmailInput(final String qualifiedName, final SgmlPage page,
46              final Map<String, DomAttr> attributes) {
47          super(qualifiedName, page, attributes);
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public void setValueAttribute(String newValue) {
55          if (StringUtils.isBlank(newValue) && hasFeature(JS_INPUT_SET_VALUE_EMAIL_TRIMMED)) {
56              newValue = "";
57          }
58          super.setValueAttribute(newValue);
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public int getSelectionStart() {
66          return selectionDelegate_.getSelectionStart();
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public void setSelectionStart(final int selectionStart) {
74          selectionDelegate_.setSelectionStart(selectionStart);
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public int getSelectionEnd() {
82          return selectionDelegate_.getSelectionEnd();
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public void setSelectionEnd(final int selectionEnd) {
90          selectionDelegate_.setSelectionEnd(selectionEnd);
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public String getSelectedText() {
98          return selectionDelegate_.getSelectedText();
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public void select() {
106         selectionDelegate_.select();
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public void setText(final String text) {
114         setValueAttribute(text);
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public String getText() {
122         return getValueAttribute();
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 }