View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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    * https://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 org.htmlunit.html;
16  
17  import java.io.IOException;
18  import java.io.PrintWriter;
19  import java.util.Map;
20  
21  import org.htmlunit.SgmlPage;
22  import org.htmlunit.javascript.host.event.KeyboardEvent;
23  import org.htmlunit.util.NameValuePair;
24  import org.htmlunit.util.StringUtils;
25  
26  /**
27   * Wrapper for the HTML element "input".
28   *
29   * @author Mike Bowler
30   * @author David K. Taylor
31   * @author Christian Sell
32   * @author Daniel Gredler
33   * @author Ahmed Ashour
34   * @author Marc Guillemot
35   * @author Ronald Brill
36   * @author Frank Danek
37   */
38  public class HtmlSubmitInput extends HtmlInput implements LabelableElement {
39  
40      /**
41       * Value to use if no specified <code>value</code> attribute.
42       */
43      public static final String DEFAULT_VALUE = "Submit Query";
44  
45      /**
46       * Creates an instance.
47       *
48       * @param qualifiedName the qualified name of the element type to instantiate
49       * @param page the page that contains this element
50       * @param attributes the initial attributes
51       */
52      HtmlSubmitInput(final String qualifiedName, final SgmlPage page,
53              final Map<String, DomAttr> attributes) {
54          super(qualifiedName, page, attributes);
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      protected void doType(final int keyCode, final boolean lastType) throws IOException {
62          if (keyCode == KeyboardEvent.DOM_VK_SPACE) {
63              click();
64              return;
65          }
66  
67          super.doType(keyCode, lastType);
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException {
75          if (!isDisabled()) {
76              final HtmlForm form = getEnclosingForm();
77              if (form != null) {
78                  form.submit(this);
79                  return false;
80              }
81          }
82          super.doClickStateUpdate(shiftKey, ctrlKey);
83          return false;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public void setValue(final String newValue) {
91          unmarkValueDirty();
92          setDefaultValue(newValue);
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public void setDefaultChecked(final boolean defaultChecked) {
100         // Empty.
101     }
102 
103     /**
104      * {@inheritDoc} This method <b>does nothing</b> for submit input elements.
105      * @see SubmittableElement#reset()
106      */
107     @Override
108     public void reset() {
109         // Empty.
110     }
111 
112     /**
113      * {@inheritDoc} Doesn't print the attribute if it is <code>value="Submit Query"</code>.
114      */
115     @Override
116     protected void printOpeningTagContentAsXml(final PrintWriter printWriter) {
117         printWriter.print(getTagName());
118 
119         for (final DomAttr attribute : getAttributesMap().values()) {
120             final String name = attribute.getNodeName();
121             final String value = attribute.getValue();
122             if (!VALUE_ATTRIBUTE.equals(name) || !DEFAULT_VALUE.equals(value)) {
123                 printWriter.print(" ");
124                 printWriter.print(name);
125                 printWriter.print("=\"");
126                 printWriter.print(StringUtils.escapeXmlAttributeValue(value));
127                 printWriter.print("\"");
128             }
129         }
130     }
131 
132     /**
133      * {@inheritDoc}
134      *
135      * Returns "Submit Query" if <code>name</code> attribute is defined and <code>value</code> attribute is not defined.
136      */
137     @Override
138     public NameValuePair[] getSubmitNameValuePairs() {
139         if (!getNameAttribute().isEmpty() && !hasAttribute(VALUE_ATTRIBUTE)) {
140             return new NameValuePair[]{new NameValuePair(getNameAttribute(), DEFAULT_VALUE)};
141         }
142         return super.getSubmitNameValuePairs();
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     protected boolean isRequiredSupported() {
150         return false;
151     }
152 }