• 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

88.72
/src/main/java/com/meterware/httpunit/dom/HTMLDocumentImpl.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 java.net.MalformedURLException;
23
import java.net.URL;
24
import java.util.ArrayList;
25
import java.util.Hashtable;
26
import java.util.Iterator;
27

28
import org.mozilla.javascript.Scriptable;
29
import org.w3c.dom.DOMException;
30
import org.w3c.dom.Element;
31
import org.w3c.dom.Node;
32
import org.w3c.dom.NodeList;
33
import org.w3c.dom.html.HTMLBaseElement;
34
import org.w3c.dom.html.HTMLBodyElement;
35
import org.w3c.dom.html.HTMLCollection;
36
import org.w3c.dom.html.HTMLDocument;
37
import org.w3c.dom.html.HTMLElement;
38
import org.w3c.dom.html.HTMLHeadElement;
39
import org.w3c.dom.html.HTMLHtmlElement;
40
import org.w3c.dom.html.HTMLTitleElement;
41

42
public class HTMLDocumentImpl extends DocumentImpl implements HTMLDocument, HTMLContainerElement {
1✔
43

44
    private static final long serialVersionUID = 1L;
45
    private static Hashtable<String, HTMLElementImpl> _exemplars = new Hashtable<>();
1✔
46
    private DomWindow _window;
47
    private StringBuilder _writeBuffer;
48
    private HTMLContainerDelegate _containerDelegate = new HTMLContainerDelegate(SKIP_IFRAMES);
1✔
49

50
    public void setIFramesEnabled(boolean enabled) {
51
        _containerDelegate = new HTMLContainerDelegate(enabled ? SKIP_IFRAMES : null);
1✔
52
    }
1✔
53

54
    @Override
55
    public Object get(String propertyName, Scriptable scriptable) {
56
        if (propertyName.equals("document")) {
1!
57
            return this;
×
58
        }
59

60
        Object result = super.get(propertyName, scriptable);
1✔
61
        if (result != NOT_FOUND) {
1✔
62
            return result;
1✔
63
        }
64

65
        Element element = getElementById(propertyName);
1✔
66
        if (element != null) {
1!
67
            return element;
×
68
        }
69

70
        NodeList elements = getElementsByName(propertyName);
1✔
71
        if (elements.getLength() >= 1) {
1!
72
            return elements.item(0);
×
73
        }
74

75
        return ScriptingSupport.getNamedProperty(this, getJavaPropertyName(propertyName), scriptable);
1✔
76
    }
77

78
    @Override
79
    public void put(String propertyName, Scriptable initialObject, Object value) {
80
        ScriptingSupport.setNamedProperty(this, getJavaPropertyName(propertyName), value);
1✔
81
    }
1✔
82

83
    // ------------------------------------------ HTMLContainerElement methods
84
    // ----------------------------------------------
85

86
    @Override
87
    public HTMLCollection getLinks() {
88
        return _containerDelegate.getLinks(this);
1✔
89
    }
90

91
    @Override
92
    public HTMLCollection getImages() {
93
        return _containerDelegate.getImages(this);
1✔
94
    }
95

96
    @Override
97
    public HTMLCollection getApplets() {
98
        return _containerDelegate.getApplets(this);
1✔
99
    }
100

101
    @Override
102
    public HTMLCollection getForms() {
103
        return _containerDelegate.getForms(this);
1✔
104
    }
105

106
    @Override
107
    public HTMLCollection getAnchors() {
108
        return _containerDelegate.getAnchors(this);
1✔
109
    }
110

111
    // -------------------------------------------- HTMLDocument methods
112
    // ----------------------------------------------------
113

114
    @Override
115
    public String getTitle() {
116
        HTMLTitleElement result = getTitleElement();
1✔
117
        return result == null ? "" : result.getText();
1✔
118
    }
119

120
    private HTMLTitleElement getTitleElement() {
121
        HTMLTitleElement result = null;
1✔
122
        NodeList titleNodes = getElementsByTagName("title");
1✔
123
        for (int i = 0; i < titleNodes.getLength(); i++) {
1✔
124
            Node node = titleNodes.item(i);
1✔
125
            if (node instanceof HTMLTitleElement) {
1!
126
                result = (HTMLTitleElement) node;
1✔
127
            }
128
        }
129
        return result;
1✔
130
    }
131

132
    private HTMLHeadElement getHeadElement() {
133
        NodeList headNodes = getElementsByTagName("head");
1✔
134
        for (int i = 0; i < headNodes.getLength(); i++) {
1✔
135
            Node node = headNodes.item(i);
1✔
136
            if (node instanceof HTMLHeadElement) {
1!
137
                return (HTMLHeadElement) node;
1✔
138
            }
139
        }
140

141
        HTMLHeadElement head = (HTMLHeadElement) createElement("head");
1✔
142
        getHtmlElement().appendChild(head);
1✔
143
        return head;
1✔
144
    }
145

146
    private HTMLHtmlElement getHtmlElement() {
147
        NodeList htmlNodes = getElementsByTagName("html");
1✔
148
        for (int i = 0; i < htmlNodes.getLength(); i++) {
1✔
149
            Node node = htmlNodes.item(i);
1✔
150
            if (node instanceof HTMLHtmlElement) {
1!
151
                return (HTMLHtmlElement) node;
1✔
152
            }
153
        }
154

155
        HTMLHtmlElement html = (HTMLHtmlElement) createElement("html");
1✔
156
        appendChild(html);
1✔
157
        return html;
1✔
158
    }
159

160
    @Override
161
    public void setTitle(String title) {
162
        HTMLTitleElement titleElement = getTitleElement();
1✔
163
        if (titleElement != null) {
1✔
164
            titleElement.setText(title);
1✔
165
        } else {
166
            titleElement = (HTMLTitleElement) createElement("title");
1✔
167
            titleElement.setText(title);
1✔
168
            getHeadElement().appendChild(titleElement);
1✔
169
        }
170
    }
1✔
171

172
    @Override
173
    public String getReferrer() {
174
        return null;
×
175
    }
176

177
    @Override
178
    public String getDomain() {
179
        return null;
×
180
    }
181

182
    @Override
183
    public String getURL() {
184
        return null;
×
185
    }
186

187
    @Override
188
    public HTMLElement getBody() {
189
        NodeList bodyNodes = getElementsByTagName("body");
1✔
190
        for (int i = 0; i < bodyNodes.getLength(); i++) {
1!
191
            Node node = bodyNodes.item(i);
1✔
192
            if (node instanceof HTMLBodyElement) {
1!
193
                return (HTMLBodyElement) node;
1✔
194
            }
195
        }
196
        return null;
×
197
    }
198

199
    @Override
200
    public void setBody(HTMLElement body) {
201
        getHtmlElement().appendChild(body);
1✔
202
    }
1✔
203

204
    @Override
205
    public String getCookie() {
206
        return null;
×
207
    }
208

209
    @Override
210
    public void setCookie(String cookie) {
211
    }
×
212

213
    @Override
214
    public void open() {
215
    }
×
216

217
    @Override
218
    public void close() {
219
        if (getWindow().replaceText(getWriteBuffer().toString(), getMimeType())) {
1!
220
            clearWriteBuffer();
1✔
221
        }
222
    }
1✔
223

224
    private String getMimeType() {
225
        return "text/html";
1✔
226
    }
227

228
    @Override
229
    public void write(String text) {
230
        getWriteBuffer().append(text);
1✔
231
    }
1✔
232

233
    @Override
234
    public void writeln(String text) {
235
        getWriteBuffer().append(text).append((char) 0x0d).append((char) 0x0a);
1✔
236
    }
1✔
237

238
    @Override
239
    public NodeList getElementsByName(String elementName) {
240
        ArrayList<HTMLElementImpl> elements = new ArrayList<HTMLElementImpl>();
1✔
241
        for (Iterator<?> each = preOrderIterator(); each.hasNext();) {
1✔
242
            Node node = (Node) each.next();
1✔
243
            if (!(node instanceof HTMLElementImpl)) {
1✔
244
                continue;
1✔
245
            }
246
            HTMLElementImpl element = (HTMLElementImpl) node;
1✔
247
            if (elementName.equals(element.getAttributeWithNoDefault("name"))) {
1✔
248
                elements.add(element);
1✔
249
            }
250
        }
1✔
251
        return new NodeListImpl(elements);
1✔
252
    }
253

254
    @Override
255
    public Element createElement(String tagName) throws DOMException {
256
        ElementImpl element = getExemplar(tagName).create();
1✔
257
        element.initialize(this, toNodeCase(tagName));
1✔
258
        return element;
1✔
259
    }
260

261
    @Override
262
    public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException {
263
        ElementImpl element = getExemplar(qualifiedName).create();
1✔
264
        element.initialize(this, namespaceURI, toNodeCase(qualifiedName));
1✔
265
        return element;
1✔
266
    }
267

268
    @Override
269
    public NodeList getElementsByTagName(String name) {
270
        return super.getElementsByTagName(toNodeCase(name));
1✔
271
    }
272

273
    @Override
274
    public Node cloneNode(boolean deep) {
275
        HTMLDocumentImpl copy = new HTMLDocumentImpl();
1✔
276

277
        if (deep) {
1!
278
            copy.importChildren(this, copy);
1✔
279
            copy._documentElement = copy.getHtmlElement();
1✔
280
        }
281
        return copy;
1✔
282
    }
283

284
    private static HTMLElementImpl getExemplar(String tagName) {
285
        HTMLElementImpl impl = (HTMLElementImpl) _exemplars.get(tagName.toLowerCase());
1✔
286
        if (impl == null) {
1✔
287
            impl = new HTMLElementImpl();
1✔
288
        }
289
        return impl;
1✔
290
    }
291

292
    String toNodeCase(String nodeName) {
293
        return nodeName.toUpperCase();
1✔
294
    }
295

296
    HTMLContainerDelegate getContainerDelegate() {
297
        return _containerDelegate;
1✔
298
    }
299

300
    static {
301
        _exemplars.put("html", new HTMLHtmlElementImpl());
1✔
302
        _exemplars.put("head", new HTMLHeadElementImpl());
1✔
303
        _exemplars.put("link", new HTMLLinkElementImpl());
1✔
304
        _exemplars.put("title", new HTMLTitleElementImpl());
1✔
305
        _exemplars.put("meta", new HTMLMetaElementImpl());
1✔
306
        _exemplars.put("base", new HTMLBaseElementImpl());
1✔
307
        _exemplars.put("style", new HTMLStyleElementImpl());
1✔
308
        _exemplars.put("body", new HTMLBodyElementImpl());
1✔
309
        _exemplars.put("form", new HTMLFormElementImpl());
1✔
310
        _exemplars.put("select", new HTMLSelectElementImpl());
1✔
311
        _exemplars.put("option", new HTMLOptionElementImpl());
1✔
312
        _exemplars.put("input", new HTMLInputElementImpl());
1✔
313
        _exemplars.put("button", new HTMLButtonElementImpl());
1✔
314
        _exemplars.put("textarea", new HTMLTextAreaElementImpl());
1✔
315
        _exemplars.put("a", new HTMLAnchorElementImpl());
1✔
316
        _exemplars.put("area", new HTMLAreaElementImpl());
1✔
317
        _exemplars.put("img", new HTMLImageElementImpl());
1✔
318
        _exemplars.put("td", new HTMLTableCellElementImpl());
1✔
319
        _exemplars.put("th", new HTMLTableCellElementImpl());
1✔
320
        _exemplars.put("tr", new HTMLTableRowElementImpl());
1✔
321
        _exemplars.put("table", new HTMLTableElementImpl());
1✔
322
        _exemplars.put("p", new HTMLParagraphElementImpl());
1✔
323
        _exemplars.put("iframe", new HTMLIFrameElementImpl());
1✔
324
        _exemplars.put("applet", new HTMLAppletElementImpl());
1✔
325
    }
1✔
326

327
    /**
328
     * get the Window
329
     *
330
     * @return the window
331
     */
332
    public DomWindow getWindow() {
333
        // if there is now window yet
334
        if (_window == null) {
1✔
335
            // create a window for this document
336
            _window = new DomWindow(this);
1✔
337
            setParentScope(_window);
1✔
338
        }
339
        return _window;
1✔
340
    }
341

342
    StringBuilder getWriteBuffer() {
343
        if (_writeBuffer == null) {
1✔
344
            _writeBuffer = new StringBuilder();
1✔
345
        }
346
        return _writeBuffer;
1✔
347
    }
348

349
    public void clearWriteBuffer() {
350
        _writeBuffer = null;
1✔
351
    }
1✔
352

353
    URL getBaseUrl() {
354
        NodeList list = getElementsByTagName("base");
1✔
355
        if (list.getLength() == 0) {
1✔
356
            return getWindow().getUrl();
1✔
357
        }
358

359
        HTMLBaseElement base = (HTMLBaseElement) list.item(0);
1✔
360
        try {
361
            return new URL(getWindow().getUrl(), base.getHref());
1✔
362
        } catch (MalformedURLException e) {
×
363
            return getWindow().getUrl();
×
364
        }
365
    }
366
}
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