• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / httpunit / 636

05 Dec 2025 03:27AM UTC coverage: 80.509%. Remained the same
636

push

github

hazendaz
Cleanup more old since tags

you guessed it, at this point going to jautodoc the rest so the warnings on builds go away ;)

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8249 of 10132 relevant lines covered (81.42%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.46
/src/main/java/com/meterware/httpunit/dom/HTMLFormElementImpl.java
1
/*
2
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.httpunit.dom;
21

22
import com.meterware.httpunit.protocol.URLEncodedString;
23
import com.meterware.httpunit.scripting.FormScriptable;
24

25
import java.io.IOException;
26
import java.net.URL;
27
import java.util.ArrayList;
28
import java.util.Iterator;
29

30
import org.mozilla.javascript.Scriptable;
31
import org.mozilla.javascript.ScriptableObject;
32
import org.w3c.dom.Element;
33
import org.w3c.dom.NamedNodeMap;
34
import org.w3c.dom.Node;
35
import org.w3c.dom.html.HTMLCollection;
36
import org.w3c.dom.html.HTMLFormElement;
37

38
public class HTMLFormElementImpl extends HTMLElementImpl implements HTMLFormElement, FormScriptable {
1✔
39

40
    private static final long serialVersionUID = 1L;
41

42
    @Override
43
    ElementImpl create() {
44
        return new HTMLFormElementImpl();
1✔
45
    }
46

47
    // ------------------------------- ScriptableObject methods
48
    // ----------------------------------------------------------
49

50
    @Override
51
    public Object get(String propertyName, Scriptable scriptable) {
52
        HTMLCollection elements = getElements();
1✔
53
        for (int i = 0; i < elements.getLength(); i++) {
1!
54
            Node node = elements.item(i);
1✔
55
            NamedNodeMap attributes = node.getAttributes();
1✔
56
            AttrImpl nameAttribute = (AttrImpl) attributes.getNamedItem("name");
1✔
57
            if (nameAttribute != null && propertyName.equals(nameAttribute.getValue())) {
1✔
58
                return node;
1✔
59
            }
60
            AttrImpl idAttribute = (AttrImpl) attributes.getNamedItem("id");
1✔
61
            if (idAttribute != null && propertyName.equals(idAttribute.getValue())) {
1✔
62
                return node;
1✔
63
            }
64
        }
65
        return super.get(propertyName, scriptable);
×
66
    }
67

68
    // ------------------------------- HTMLFormElement methods
69
    // ----------------------------------------------------------
70

71
    @Override
72
    public String getAcceptCharset() {
73
        return getAttributeWithDefault("accept-charset", "UNKNOWN");
1✔
74
    }
75

76
    @Override
77
    public void setAcceptCharset(String acceptCharset) {
78
        setAttribute("accept-charset", acceptCharset);
1✔
79
    }
1✔
80

81
    @Override
82
    public String getAction() {
83
        return getAttribute("action");
1✔
84
    }
85

86
    @Override
87
    public void setAction(String action) {
88
        setAttribute("action", action);
1✔
89
    }
1✔
90

91
    @Override
92
    public void setParameterValue(String name, String value) {
93
        Object control = get(name, null);
×
94
        if (control instanceof ScriptableObject) {
×
95
            ((ScriptableObject) control).put("value", this, value);
×
96
        }
97
    }
×
98

99
    @Override
100
    public String getEnctype() {
101
        return getAttributeWithDefault("enctype", "application/x-www-form-urlencoded");
1✔
102
    }
103

104
    @Override
105
    public void setEnctype(String enctype) {
106
        setAttribute("enctype", enctype);
1✔
107
    }
1✔
108

109
    @Override
110
    public String getMethod() {
111
        return getAttributeWithDefault("method", "get");
1✔
112
    }
113

114
    @Override
115
    public void setMethod(String method) {
116
        setAttribute("method", method);
1✔
117
    }
1✔
118

119
    /**
120
     * getter for the name
121
     *
122
     * @see org.w3c.dom.html.HTMLFormElement#getName()
123
     */
124
    @Override
125
    public String getName() {
126
        String result = getAttributeWithNoDefault("name");
1✔
127
        if (result == null) {
1✔
128
            result = this.getId();
1✔
129
        }
130
        return result;
1✔
131
    }
132

133
    @Override
134
    public void setName(String name) {
135
        setAttribute("name", name);
1✔
136
    }
1✔
137

138
    @Override
139
    public String getTarget() {
140
        return getAttributeWithNoDefault("target");
1✔
141
    }
142

143
    @Override
144
    public void setTarget(String target) {
145
        setAttribute("target", target);
1✔
146
    }
1✔
147

148
    @Override
149
    public HTMLCollection getElements() {
150
        ArrayList elements = new ArrayList<>();
1✔
151
        String[] names = { "INPUT", "TEXTAREA", "BUTTON", "SELECT" };
1✔
152
        for (Iterator each = preOrderIteratorAfterNode(); each.hasNext();) {
1✔
153
            Node node = (Node) each.next();
1✔
154
            if (node instanceof HTMLFormElement) {
1✔
155
                break;
1✔
156
            }
157

158
            if (node.getNodeType() != ELEMENT_NODE) {
1✔
159
                continue;
1✔
160
            }
161
            String tagName = ((Element) node).getTagName();
1✔
162
            for (String name : names) {
1✔
163
                if (tagName.equalsIgnoreCase(name)) {
1✔
164
                    elements.add(node);
1✔
165
                }
166
            }
167
        }
1✔
168
        return HTMLCollectionImpl.createHTMLCollectionImpl(new NodeListImpl(elements));
1✔
169
    }
170

171
    @Override
172
    public int getLength() {
173
        return 0;
1✔
174
    }
175

176
    @Override
177
    public void reset() {
178
        HTMLCollection elements = getElements();
1✔
179
        for (int i = 0; i < elements.getLength(); i++) {
1✔
180
            Node node = elements.item(i);
1✔
181
            if (node instanceof HTMLControl) {
1!
182
                ((HTMLControl) node).reset();
1✔
183
            }
184
        }
185
    }
1✔
186

187
    @Override
188
    public void submit() {
189
        doSubmitAction();
1✔
190
    }
1✔
191

192
    /**
193
     * Handles the actual form submission - does not handle the "submit" event.
194
     */
195
    void doSubmitAction() {
196
        try {
197
            if ("get".equalsIgnoreCase(getMethod())) {
1!
198
                getDomWindow().submitRequest(this, getMethod(), getEffectiveUrl(), getTarget(), new byte[0]);
1✔
199
            } else if ("post".equalsIgnoreCase(getMethod())) {
×
200
                getDomWindow().submitRequest(this, getMethod(), getAction(), getTarget(), new byte[0]);
×
201
            }
202
        } catch (Exception e) {
×
203
            throw new RuntimeException("Error submitting form: " + e);
×
204
        } finally {
205
            silenceSubmitButtons();
1✔
206
        }
207
    }
1✔
208

209
    private void silenceSubmitButtons() {
210
        HTMLCollection controls = getElements();
1✔
211
        for (int i = 0; i < controls.getLength(); i++) {
1✔
212
            ((HTMLControl) controls.item(i)).silenceSubmitButton();
1✔
213
        }
214
    }
1✔
215

216
    private String getEffectiveUrl() throws IOException {
217
        StringBuilder spec = new StringBuilder(getAction());
1✔
218
        if ("get".equalsIgnoreCase(getMethod())) {
1!
219
            URLEncodedString parameters = new URLEncodedString();
1✔
220
            HTMLCollection controls = getElements();
1✔
221
            for (int i = 0; i < controls.getLength(); i++) {
1✔
222
                ((HTMLControl) controls.item(i)).addValues(parameters, "us-ascii");
1✔
223
            }
224
            if (spec.indexOf("?") >= 0 && !spec.toString().endsWith("?")) {
1!
225
                spec.append('&');
×
226
            } else {
227
                spec.append('?');
1✔
228
            }
229
            spec.append(parameters.getString());
1✔
230
        }
231
        return new URL(getDomWindow().getUrl(), spec.toString()).toExternalForm();
1✔
232
    }
233

234
    private DomWindow getDomWindow() {
235
        return ((HTMLDocumentImpl) getOwnerDocument()).getWindow();
1✔
236
    }
237

238
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc