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

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10124 relevant lines covered (81.44%)

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
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.httpunit.dom;
9

10
import com.meterware.httpunit.protocol.URLEncodedString;
11
import com.meterware.httpunit.scripting.FormScriptable;
12

13
import java.io.IOException;
14
import java.net.URL;
15
import java.util.ArrayList;
16
import java.util.Iterator;
17

18
import org.mozilla.javascript.Scriptable;
19
import org.mozilla.javascript.ScriptableObject;
20
import org.w3c.dom.Element;
21
import org.w3c.dom.NamedNodeMap;
22
import org.w3c.dom.Node;
23
import org.w3c.dom.html.HTMLCollection;
24
import org.w3c.dom.html.HTMLFormElement;
25

26
/**
27
 * The Class HTMLFormElementImpl.
28
 */
29
public class HTMLFormElementImpl extends HTMLElementImpl implements HTMLFormElement, FormScriptable {
1✔
30

31
    /** The Constant serialVersionUID. */
32
    private static final long serialVersionUID = 1L;
33

34
    @Override
35
    ElementImpl create() {
36
        return new HTMLFormElementImpl();
1✔
37
    }
38

39
    // ------------------------------- ScriptableObject methods
40
    // ----------------------------------------------------------
41

42
    @Override
43
    public Object get(String propertyName, Scriptable scriptable) {
44
        HTMLCollection elements = getElements();
1✔
45
        for (int i = 0; i < elements.getLength(); i++) {
1!
46
            Node node = elements.item(i);
1✔
47
            NamedNodeMap attributes = node.getAttributes();
1✔
48
            AttrImpl nameAttribute = (AttrImpl) attributes.getNamedItem("name");
1✔
49
            if (nameAttribute != null && propertyName.equals(nameAttribute.getValue())) {
1✔
50
                return node;
1✔
51
            }
52
            AttrImpl idAttribute = (AttrImpl) attributes.getNamedItem("id");
1✔
53
            if (idAttribute != null && propertyName.equals(idAttribute.getValue())) {
1✔
54
                return node;
1✔
55
            }
56
        }
57
        return super.get(propertyName, scriptable);
×
58
    }
59

60
    // ------------------------------- HTMLFormElement methods
61
    // ----------------------------------------------------------
62

63
    /**
64
     * Gets the accept charset.
65
     *
66
     * @return the accept charset
67
     */
68
    @Override
69
    public String getAcceptCharset() {
70
        return getAttributeWithDefault("accept-charset", "UNKNOWN");
1✔
71
    }
72

73
    /**
74
     * Sets the accept charset.
75
     *
76
     * @param acceptCharset
77
     *            the new accept charset
78
     */
79
    @Override
80
    public void setAcceptCharset(String acceptCharset) {
81
        setAttribute("accept-charset", acceptCharset);
1✔
82
    }
1✔
83

84
    /**
85
     * Gets the action.
86
     *
87
     * @return the action
88
     */
89
    @Override
90
    public String getAction() {
91
        return getAttribute("action");
1✔
92
    }
93

94
    @Override
95
    public void setAction(String action) {
96
        setAttribute("action", action);
1✔
97
    }
1✔
98

99
    @Override
100
    public void setParameterValue(String name, String value) {
101
        Object control = get(name, null);
×
102
        if (control instanceof ScriptableObject) {
×
103
            ((ScriptableObject) control).put("value", this, value);
×
104
        }
105
    }
×
106

107
    /**
108
     * Gets the enctype.
109
     *
110
     * @return the enctype
111
     */
112
    @Override
113
    public String getEnctype() {
114
        return getAttributeWithDefault("enctype", "application/x-www-form-urlencoded");
1✔
115
    }
116

117
    /**
118
     * Sets the enctype.
119
     *
120
     * @param enctype
121
     *            the new enctype
122
     */
123
    @Override
124
    public void setEnctype(String enctype) {
125
        setAttribute("enctype", enctype);
1✔
126
    }
1✔
127

128
    /**
129
     * Gets the method.
130
     *
131
     * @return the method
132
     */
133
    @Override
134
    public String getMethod() {
135
        return getAttributeWithDefault("method", "get");
1✔
136
    }
137

138
    /**
139
     * Sets the method.
140
     *
141
     * @param method
142
     *            the new method
143
     */
144
    @Override
145
    public void setMethod(String method) {
146
        setAttribute("method", method);
1✔
147
    }
1✔
148

149
    /**
150
     * getter for the name.
151
     *
152
     * @return the name
153
     *
154
     * @see org.w3c.dom.html.HTMLFormElement#getName()
155
     */
156
    @Override
157
    public String getName() {
158
        String result = getAttributeWithNoDefault("name");
1✔
159
        if (result == null) {
1✔
160
            result = this.getId();
1✔
161
        }
162
        return result;
1✔
163
    }
164

165
    /**
166
     * Sets the name.
167
     *
168
     * @param name
169
     *            the new name
170
     */
171
    @Override
172
    public void setName(String name) {
173
        setAttribute("name", name);
1✔
174
    }
1✔
175

176
    /**
177
     * Gets the target.
178
     *
179
     * @return the target
180
     */
181
    @Override
182
    public String getTarget() {
183
        return getAttributeWithNoDefault("target");
1✔
184
    }
185

186
    /**
187
     * Sets the target.
188
     *
189
     * @param target
190
     *            the new target
191
     */
192
    @Override
193
    public void setTarget(String target) {
194
        setAttribute("target", target);
1✔
195
    }
1✔
196

197
    /**
198
     * Gets the elements.
199
     *
200
     * @return the elements
201
     */
202
    @Override
203
    public HTMLCollection getElements() {
204
        ArrayList elements = new ArrayList<>();
1✔
205
        String[] names = { "INPUT", "TEXTAREA", "BUTTON", "SELECT" };
1✔
206
        for (Iterator each = preOrderIteratorAfterNode(); each.hasNext();) {
1✔
207
            Node node = (Node) each.next();
1✔
208
            if (node instanceof HTMLFormElement) {
1✔
209
                break;
1✔
210
            }
211

212
            if (node.getNodeType() != ELEMENT_NODE) {
1✔
213
                continue;
1✔
214
            }
215
            String tagName = ((Element) node).getTagName();
1✔
216
            for (String name : names) {
1✔
217
                if (tagName.equalsIgnoreCase(name)) {
1✔
218
                    elements.add(node);
1✔
219
                }
220
            }
221
        }
1✔
222
        return HTMLCollectionImpl.createHTMLCollectionImpl(new NodeListImpl(elements));
1✔
223
    }
224

225
    /**
226
     * Gets the length.
227
     *
228
     * @return the length
229
     */
230
    @Override
231
    public int getLength() {
232
        return 0;
1✔
233
    }
234

235
    /**
236
     * Reset.
237
     */
238
    @Override
239
    public void reset() {
240
        HTMLCollection elements = getElements();
1✔
241
        for (int i = 0; i < elements.getLength(); i++) {
1✔
242
            Node node = elements.item(i);
1✔
243
            if (node instanceof HTMLControl) {
1!
244
                ((HTMLControl) node).reset();
1✔
245
            }
246
        }
247
    }
1✔
248

249
    /**
250
     * Submit.
251
     */
252
    @Override
253
    public void submit() {
254
        doSubmitAction();
1✔
255
    }
1✔
256

257
    /**
258
     * Handles the actual form submission - does not handle the "submit" event.
259
     */
260
    void doSubmitAction() {
261
        try {
262
            if ("get".equalsIgnoreCase(getMethod())) {
1!
263
                getDomWindow().submitRequest(this, getMethod(), getEffectiveUrl(), getTarget(), new byte[0]);
1✔
264
            } else if ("post".equalsIgnoreCase(getMethod())) {
×
265
                getDomWindow().submitRequest(this, getMethod(), getAction(), getTarget(), new byte[0]);
×
266
            }
267
        } catch (Exception e) {
×
268
            throw new RuntimeException("Error submitting form: " + e);
×
269
        } finally {
270
            silenceSubmitButtons();
1✔
271
        }
272
    }
1✔
273

274
    /**
275
     * Silence submit buttons.
276
     */
277
    private void silenceSubmitButtons() {
278
        HTMLCollection controls = getElements();
1✔
279
        for (int i = 0; i < controls.getLength(); i++) {
1✔
280
            ((HTMLControl) controls.item(i)).silenceSubmitButton();
1✔
281
        }
282
    }
1✔
283

284
    /**
285
     * Gets the effective url.
286
     *
287
     * @return the effective url
288
     *
289
     * @throws IOException
290
     *             Signals that an I/O exception has occurred.
291
     */
292
    private String getEffectiveUrl() throws IOException {
293
        StringBuilder spec = new StringBuilder(getAction());
1✔
294
        if ("get".equalsIgnoreCase(getMethod())) {
1!
295
            URLEncodedString parameters = new URLEncodedString();
1✔
296
            HTMLCollection controls = getElements();
1✔
297
            for (int i = 0; i < controls.getLength(); i++) {
1✔
298
                ((HTMLControl) controls.item(i)).addValues(parameters, "us-ascii");
1✔
299
            }
300
            if (spec.indexOf("?") >= 0 && !spec.toString().endsWith("?")) {
1!
301
                spec.append('&');
×
302
            } else {
303
                spec.append('?');
1✔
304
            }
305
            spec.append(parameters.getString());
1✔
306
        }
307
        return new URL(getDomWindow().getUrl(), spec.toString()).toExternalForm();
1✔
308
    }
309

310
    /**
311
     * Gets the dom window.
312
     *
313
     * @return the dom window
314
     */
315
    private DomWindow getDomWindow() {
316
        return ((HTMLDocumentImpl) getOwnerDocument()).getWindow();
1✔
317
    }
318

319
}
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