• 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

88.72
/src/main/java/com/meterware/httpunit/dom/HTMLDocumentImpl.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 java.net.MalformedURLException;
11
import java.net.URL;
12
import java.util.ArrayList;
13
import java.util.Hashtable;
14
import java.util.Iterator;
15
import java.util.Locale;
16

17
import org.mozilla.javascript.Scriptable;
18
import org.w3c.dom.DOMException;
19
import org.w3c.dom.Element;
20
import org.w3c.dom.Node;
21
import org.w3c.dom.NodeList;
22
import org.w3c.dom.html.HTMLBaseElement;
23
import org.w3c.dom.html.HTMLBodyElement;
24
import org.w3c.dom.html.HTMLCollection;
25
import org.w3c.dom.html.HTMLDocument;
26
import org.w3c.dom.html.HTMLElement;
27
import org.w3c.dom.html.HTMLHeadElement;
28
import org.w3c.dom.html.HTMLHtmlElement;
29
import org.w3c.dom.html.HTMLTitleElement;
30

31
/**
32
 * The Class HTMLDocumentImpl.
33
 */
34
public class HTMLDocumentImpl extends DocumentImpl implements HTMLDocument, HTMLContainerElement {
1✔
35

36
    /** The Constant serialVersionUID. */
37
    private static final long serialVersionUID = 1L;
38

39
    /** The exemplars. */
40
    private static Hashtable<String, HTMLElementImpl> _exemplars = new Hashtable<>();
1✔
41

42
    /** The window. */
43
    private DomWindow _window;
44

45
    /** The write buffer. */
46
    private StringBuilder _writeBuffer;
47

48
    /** The container delegate. */
49
    private HTMLContainerDelegate _containerDelegate = new HTMLContainerDelegate(SKIP_IFRAMES);
1✔
50

51
    /**
52
     * Sets the i frames enabled.
53
     *
54
     * @param enabled
55
     *            the new i frames enabled
56
     */
57
    public void setIFramesEnabled(boolean enabled) {
58
        _containerDelegate = new HTMLContainerDelegate(enabled ? SKIP_IFRAMES : null);
1✔
59
    }
1✔
60

61
    @Override
62
    public Object get(String propertyName, Scriptable scriptable) {
63
        if (propertyName.equals("document")) {
1!
64
            return this;
×
65
        }
66

67
        Object result = super.get(propertyName, scriptable);
1✔
68
        if (result != NOT_FOUND) {
1✔
69
            return result;
1✔
70
        }
71

72
        Element element = getElementById(propertyName);
1✔
73
        if (element != null) {
1!
74
            return element;
×
75
        }
76

77
        NodeList elements = getElementsByName(propertyName);
1✔
78
        if (elements.getLength() >= 1) {
1!
79
            return elements.item(0);
×
80
        }
81

82
        return ScriptingSupport.getNamedProperty(this, getJavaPropertyName(propertyName), scriptable);
1✔
83
    }
84

85
    @Override
86
    public void put(String propertyName, Scriptable initialObject, Object value) {
87
        ScriptingSupport.setNamedProperty(this, getJavaPropertyName(propertyName), value);
1✔
88
    }
1✔
89

90
    // ------------------------------------------ HTMLContainerElement methods
91
    // ----------------------------------------------
92

93
    @Override
94
    public HTMLCollection getLinks() {
95
        return _containerDelegate.getLinks(this);
1✔
96
    }
97

98
    @Override
99
    public HTMLCollection getImages() {
100
        return _containerDelegate.getImages(this);
1✔
101
    }
102

103
    @Override
104
    public HTMLCollection getApplets() {
105
        return _containerDelegate.getApplets(this);
1✔
106
    }
107

108
    @Override
109
    public HTMLCollection getForms() {
110
        return _containerDelegate.getForms(this);
1✔
111
    }
112

113
    @Override
114
    public HTMLCollection getAnchors() {
115
        return _containerDelegate.getAnchors(this);
1✔
116
    }
117

118
    // -------------------------------------------- HTMLDocument methods
119
    // ----------------------------------------------------
120

121
    /**
122
     * Gets the title.
123
     *
124
     * @return the title
125
     */
126
    @Override
127
    public String getTitle() {
128
        HTMLTitleElement result = getTitleElement();
1✔
129
        return result == null ? "" : result.getText();
1✔
130
    }
131

132
    /**
133
     * Gets the title element.
134
     *
135
     * @return the title element
136
     */
137
    private HTMLTitleElement getTitleElement() {
138
        HTMLTitleElement result = null;
1✔
139
        NodeList titleNodes = getElementsByTagName("title");
1✔
140
        for (int i = 0; i < titleNodes.getLength(); i++) {
1✔
141
            Node node = titleNodes.item(i);
1✔
142
            if (node instanceof HTMLTitleElement) {
1!
143
                result = (HTMLTitleElement) node;
1✔
144
            }
145
        }
146
        return result;
1✔
147
    }
148

149
    /**
150
     * Gets the head element.
151
     *
152
     * @return the head element
153
     */
154
    private HTMLHeadElement getHeadElement() {
155
        NodeList headNodes = getElementsByTagName("head");
1✔
156
        for (int i = 0; i < headNodes.getLength(); i++) {
1✔
157
            Node node = headNodes.item(i);
1✔
158
            if (node instanceof HTMLHeadElement) {
1!
159
                return (HTMLHeadElement) node;
1✔
160
            }
161
        }
162

163
        HTMLHeadElement head = (HTMLHeadElement) createElement("head");
1✔
164
        getHtmlElement().appendChild(head);
1✔
165
        return head;
1✔
166
    }
167

168
    /**
169
     * Gets the html element.
170
     *
171
     * @return the html element
172
     */
173
    private HTMLHtmlElement getHtmlElement() {
174
        NodeList htmlNodes = getElementsByTagName("html");
1✔
175
        for (int i = 0; i < htmlNodes.getLength(); i++) {
1✔
176
            Node node = htmlNodes.item(i);
1✔
177
            if (node instanceof HTMLHtmlElement) {
1!
178
                return (HTMLHtmlElement) node;
1✔
179
            }
180
        }
181

182
        HTMLHtmlElement html = (HTMLHtmlElement) createElement("html");
1✔
183
        appendChild(html);
1✔
184
        return html;
1✔
185
    }
186

187
    /**
188
     * Sets the title.
189
     *
190
     * @param title
191
     *            the new title
192
     */
193
    @Override
194
    public void setTitle(String title) {
195
        HTMLTitleElement titleElement = getTitleElement();
1✔
196
        if (titleElement != null) {
1✔
197
            titleElement.setText(title);
1✔
198
        } else {
199
            titleElement = (HTMLTitleElement) createElement("title");
1✔
200
            titleElement.setText(title);
1✔
201
            getHeadElement().appendChild(titleElement);
1✔
202
        }
203
    }
1✔
204

205
    /**
206
     * Gets the referrer.
207
     *
208
     * @return the referrer
209
     */
210
    @Override
211
    public String getReferrer() {
212
        return null;
×
213
    }
214

215
    /**
216
     * Gets the domain.
217
     *
218
     * @return the domain
219
     */
220
    @Override
221
    public String getDomain() {
222
        return null;
×
223
    }
224

225
    /**
226
     * Gets the url.
227
     *
228
     * @return the url
229
     */
230
    @Override
231
    public String getURL() {
232
        return null;
×
233
    }
234

235
    /**
236
     * Gets the body.
237
     *
238
     * @return the body
239
     */
240
    @Override
241
    public HTMLElement getBody() {
242
        NodeList bodyNodes = getElementsByTagName("body");
1✔
243
        for (int i = 0; i < bodyNodes.getLength(); i++) {
1!
244
            Node node = bodyNodes.item(i);
1✔
245
            if (node instanceof HTMLBodyElement) {
1!
246
                return (HTMLBodyElement) node;
1✔
247
            }
248
        }
249
        return null;
×
250
    }
251

252
    /**
253
     * Sets the body.
254
     *
255
     * @param body
256
     *            the new body
257
     */
258
    @Override
259
    public void setBody(HTMLElement body) {
260
        getHtmlElement().appendChild(body);
1✔
261
    }
1✔
262

263
    /**
264
     * Gets the cookie.
265
     *
266
     * @return the cookie
267
     */
268
    @Override
269
    public String getCookie() {
270
        return null;
×
271
    }
272

273
    /**
274
     * Sets the cookie.
275
     *
276
     * @param cookie
277
     *            the new cookie
278
     */
279
    @Override
280
    public void setCookie(String cookie) {
281
    }
×
282

283
    /**
284
     * Open.
285
     */
286
    @Override
287
    public void open() {
288
    }
×
289

290
    /**
291
     * Close.
292
     */
293
    @Override
294
    public void close() {
295
        if (getWindow().replaceText(getWriteBuffer().toString(), getMimeType())) {
1!
296
            clearWriteBuffer();
1✔
297
        }
298
    }
1✔
299

300
    /**
301
     * Gets the mime type.
302
     *
303
     * @return the mime type
304
     */
305
    private String getMimeType() {
306
        return "text/html";
1✔
307
    }
308

309
    /**
310
     * Write.
311
     *
312
     * @param text
313
     *            the text
314
     */
315
    @Override
316
    public void write(String text) {
317
        getWriteBuffer().append(text);
1✔
318
    }
1✔
319

320
    /**
321
     * Writeln.
322
     *
323
     * @param text
324
     *            the text
325
     */
326
    @Override
327
    public void writeln(String text) {
328
        getWriteBuffer().append(text).append((char) 0x0d).append((char) 0x0a);
1✔
329
    }
1✔
330

331
    /**
332
     * Gets the elements by name.
333
     *
334
     * @param elementName
335
     *            the element name
336
     *
337
     * @return the elements by name
338
     */
339
    @Override
340
    public NodeList getElementsByName(String elementName) {
341
        ArrayList<HTMLElementImpl> elements = new ArrayList<HTMLElementImpl>();
1✔
342
        for (Iterator<?> each = preOrderIterator(); each.hasNext();) {
1✔
343
            Node node = (Node) each.next();
1✔
344
            if (!(node instanceof HTMLElementImpl)) {
1✔
345
                continue;
1✔
346
            }
347
            HTMLElementImpl element = (HTMLElementImpl) node;
1✔
348
            if (elementName.equals(element.getAttributeWithNoDefault("name"))) {
1✔
349
                elements.add(element);
1✔
350
            }
351
        }
1✔
352
        return new NodeListImpl(elements);
1✔
353
    }
354

355
    @Override
356
    public Element createElement(String tagName) throws DOMException {
357
        ElementImpl element = getExemplar(tagName).create();
1✔
358
        element.initialize(this, toNodeCase(tagName));
1✔
359
        return element;
1✔
360
    }
361

362
    @Override
363
    public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException {
364
        ElementImpl element = getExemplar(qualifiedName).create();
1✔
365
        element.initialize(this, namespaceURI, toNodeCase(qualifiedName));
1✔
366
        return element;
1✔
367
    }
368

369
    @Override
370
    public NodeList getElementsByTagName(String name) {
371
        return super.getElementsByTagName(toNodeCase(name));
1✔
372
    }
373

374
    @Override
375
    public Node cloneNode(boolean deep) {
376
        HTMLDocumentImpl copy = new HTMLDocumentImpl();
1✔
377

378
        if (deep) {
1!
379
            copy.importChildren(this, copy);
1✔
380
            copy._documentElement = copy.getHtmlElement();
1✔
381
        }
382
        return copy;
1✔
383
    }
384

385
    /**
386
     * Gets the exemplar.
387
     *
388
     * @param tagName
389
     *            the tag name
390
     *
391
     * @return the exemplar
392
     */
393
    private static HTMLElementImpl getExemplar(String tagName) {
394
        HTMLElementImpl impl = (HTMLElementImpl) _exemplars.get(tagName.toLowerCase(Locale.ENGLISH));
1✔
395
        if (impl == null) {
1✔
396
            impl = new HTMLElementImpl();
1✔
397
        }
398
        return impl;
1✔
399
    }
400

401
    /**
402
     * To node case.
403
     *
404
     * @param nodeName
405
     *            the node name
406
     *
407
     * @return the string
408
     */
409
    String toNodeCase(String nodeName) {
410
        return nodeName.toUpperCase();
1✔
411
    }
412

413
    /**
414
     * Gets the container delegate.
415
     *
416
     * @return the container delegate
417
     */
418
    HTMLContainerDelegate getContainerDelegate() {
419
        return _containerDelegate;
1✔
420
    }
421

422
    static {
423
        _exemplars.put("html", new HTMLHtmlElementImpl());
1✔
424
        _exemplars.put("head", new HTMLHeadElementImpl());
1✔
425
        _exemplars.put("link", new HTMLLinkElementImpl());
1✔
426
        _exemplars.put("title", new HTMLTitleElementImpl());
1✔
427
        _exemplars.put("meta", new HTMLMetaElementImpl());
1✔
428
        _exemplars.put("base", new HTMLBaseElementImpl());
1✔
429
        _exemplars.put("style", new HTMLStyleElementImpl());
1✔
430
        _exemplars.put("body", new HTMLBodyElementImpl());
1✔
431
        _exemplars.put("form", new HTMLFormElementImpl());
1✔
432
        _exemplars.put("select", new HTMLSelectElementImpl());
1✔
433
        _exemplars.put("option", new HTMLOptionElementImpl());
1✔
434
        _exemplars.put("input", new HTMLInputElementImpl());
1✔
435
        _exemplars.put("button", new HTMLButtonElementImpl());
1✔
436
        _exemplars.put("textarea", new HTMLTextAreaElementImpl());
1✔
437
        _exemplars.put("a", new HTMLAnchorElementImpl());
1✔
438
        _exemplars.put("area", new HTMLAreaElementImpl());
1✔
439
        _exemplars.put("img", new HTMLImageElementImpl());
1✔
440
        _exemplars.put("td", new HTMLTableCellElementImpl());
1✔
441
        _exemplars.put("th", new HTMLTableCellElementImpl());
1✔
442
        _exemplars.put("tr", new HTMLTableRowElementImpl());
1✔
443
        _exemplars.put("table", new HTMLTableElementImpl());
1✔
444
        _exemplars.put("p", new HTMLParagraphElementImpl());
1✔
445
        _exemplars.put("iframe", new HTMLIFrameElementImpl());
1✔
446
        _exemplars.put("applet", new HTMLAppletElementImpl());
1✔
447
    }
1✔
448

449
    /**
450
     * get the Window.
451
     *
452
     * @return the window
453
     */
454
    public DomWindow getWindow() {
455
        // if there is now window yet
456
        if (_window == null) {
1✔
457
            // create a window for this document
458
            _window = new DomWindow(this);
1✔
459
            setParentScope(_window);
1✔
460
        }
461
        return _window;
1✔
462
    }
463

464
    /**
465
     * Gets the write buffer.
466
     *
467
     * @return the write buffer
468
     */
469
    StringBuilder getWriteBuffer() {
470
        if (_writeBuffer == null) {
1✔
471
            _writeBuffer = new StringBuilder();
1✔
472
        }
473
        return _writeBuffer;
1✔
474
    }
475

476
    /**
477
     * Clear write buffer.
478
     */
479
    public void clearWriteBuffer() {
480
        _writeBuffer = null;
1✔
481
    }
1✔
482

483
    /**
484
     * Gets the base url.
485
     *
486
     * @return the base url
487
     */
488
    URL getBaseUrl() {
489
        NodeList list = getElementsByTagName("base");
1✔
490
        if (list.getLength() == 0) {
1✔
491
            return getWindow().getUrl();
1✔
492
        }
493

494
        HTMLBaseElement base = (HTMLBaseElement) list.item(0);
1✔
495
        try {
496
            return new URL(getWindow().getUrl(), base.getHref());
1✔
497
        } catch (MalformedURLException e) {
×
498
            return getWindow().getUrl();
×
499
        }
500
    }
501
}
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