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

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

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
import java.util.Locale;
28

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

43
/**
44
 * The Class HTMLDocumentImpl.
45
 */
46
public class HTMLDocumentImpl extends DocumentImpl implements HTMLDocument, HTMLContainerElement {
1✔
47

48
    /** The Constant serialVersionUID. */
49
    private static final long serialVersionUID = 1L;
50

51
    /** The exemplars. */
52
    private static Hashtable<String, HTMLElementImpl> _exemplars = new Hashtable<>();
1✔
53

54
    /** The window. */
55
    private DomWindow _window;
56

57
    /** The write buffer. */
58
    private StringBuilder _writeBuffer;
59

60
    /** The container delegate. */
61
    private HTMLContainerDelegate _containerDelegate = new HTMLContainerDelegate(SKIP_IFRAMES);
1✔
62

63
    /**
64
     * Sets the i frames enabled.
65
     *
66
     * @param enabled
67
     *            the new i frames enabled
68
     */
69
    public void setIFramesEnabled(boolean enabled) {
70
        _containerDelegate = new HTMLContainerDelegate(enabled ? SKIP_IFRAMES : null);
1✔
71
    }
1✔
72

73
    @Override
74
    public Object get(String propertyName, Scriptable scriptable) {
75
        if (propertyName.equals("document")) {
1!
76
            return this;
×
77
        }
78

79
        Object result = super.get(propertyName, scriptable);
1✔
80
        if (result != NOT_FOUND) {
1✔
81
            return result;
1✔
82
        }
83

84
        Element element = getElementById(propertyName);
1✔
85
        if (element != null) {
1!
86
            return element;
×
87
        }
88

89
        NodeList elements = getElementsByName(propertyName);
1✔
90
        if (elements.getLength() >= 1) {
1!
91
            return elements.item(0);
×
92
        }
93

94
        return ScriptingSupport.getNamedProperty(this, getJavaPropertyName(propertyName), scriptable);
1✔
95
    }
96

97
    @Override
98
    public void put(String propertyName, Scriptable initialObject, Object value) {
99
        ScriptingSupport.setNamedProperty(this, getJavaPropertyName(propertyName), value);
1✔
100
    }
1✔
101

102
    // ------------------------------------------ HTMLContainerElement methods
103
    // ----------------------------------------------
104

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

110
    @Override
111
    public HTMLCollection getImages() {
112
        return _containerDelegate.getImages(this);
1✔
113
    }
114

115
    @Override
116
    public HTMLCollection getApplets() {
117
        return _containerDelegate.getApplets(this);
1✔
118
    }
119

120
    @Override
121
    public HTMLCollection getForms() {
122
        return _containerDelegate.getForms(this);
1✔
123
    }
124

125
    @Override
126
    public HTMLCollection getAnchors() {
127
        return _containerDelegate.getAnchors(this);
1✔
128
    }
129

130
    // -------------------------------------------- HTMLDocument methods
131
    // ----------------------------------------------------
132

133
    /**
134
     * Gets the title.
135
     *
136
     * @return the title
137
     */
138
    @Override
139
    public String getTitle() {
140
        HTMLTitleElement result = getTitleElement();
1✔
141
        return result == null ? "" : result.getText();
1✔
142
    }
143

144
    /**
145
     * Gets the title element.
146
     *
147
     * @return the title element
148
     */
149
    private HTMLTitleElement getTitleElement() {
150
        HTMLTitleElement result = null;
1✔
151
        NodeList titleNodes = getElementsByTagName("title");
1✔
152
        for (int i = 0; i < titleNodes.getLength(); i++) {
1✔
153
            Node node = titleNodes.item(i);
1✔
154
            if (node instanceof HTMLTitleElement) {
1!
155
                result = (HTMLTitleElement) node;
1✔
156
            }
157
        }
158
        return result;
1✔
159
    }
160

161
    /**
162
     * Gets the head element.
163
     *
164
     * @return the head element
165
     */
166
    private HTMLHeadElement getHeadElement() {
167
        NodeList headNodes = getElementsByTagName("head");
1✔
168
        for (int i = 0; i < headNodes.getLength(); i++) {
1✔
169
            Node node = headNodes.item(i);
1✔
170
            if (node instanceof HTMLHeadElement) {
1!
171
                return (HTMLHeadElement) node;
1✔
172
            }
173
        }
174

175
        HTMLHeadElement head = (HTMLHeadElement) createElement("head");
1✔
176
        getHtmlElement().appendChild(head);
1✔
177
        return head;
1✔
178
    }
179

180
    /**
181
     * Gets the html element.
182
     *
183
     * @return the html element
184
     */
185
    private HTMLHtmlElement getHtmlElement() {
186
        NodeList htmlNodes = getElementsByTagName("html");
1✔
187
        for (int i = 0; i < htmlNodes.getLength(); i++) {
1✔
188
            Node node = htmlNodes.item(i);
1✔
189
            if (node instanceof HTMLHtmlElement) {
1!
190
                return (HTMLHtmlElement) node;
1✔
191
            }
192
        }
193

194
        HTMLHtmlElement html = (HTMLHtmlElement) createElement("html");
1✔
195
        appendChild(html);
1✔
196
        return html;
1✔
197
    }
198

199
    /**
200
     * Sets the title.
201
     *
202
     * @param title
203
     *            the new title
204
     */
205
    @Override
206
    public void setTitle(String title) {
207
        HTMLTitleElement titleElement = getTitleElement();
1✔
208
        if (titleElement != null) {
1✔
209
            titleElement.setText(title);
1✔
210
        } else {
211
            titleElement = (HTMLTitleElement) createElement("title");
1✔
212
            titleElement.setText(title);
1✔
213
            getHeadElement().appendChild(titleElement);
1✔
214
        }
215
    }
1✔
216

217
    /**
218
     * Gets the referrer.
219
     *
220
     * @return the referrer
221
     */
222
    @Override
223
    public String getReferrer() {
224
        return null;
×
225
    }
226

227
    /**
228
     * Gets the domain.
229
     *
230
     * @return the domain
231
     */
232
    @Override
233
    public String getDomain() {
234
        return null;
×
235
    }
236

237
    /**
238
     * Gets the url.
239
     *
240
     * @return the url
241
     */
242
    @Override
243
    public String getURL() {
244
        return null;
×
245
    }
246

247
    /**
248
     * Gets the body.
249
     *
250
     * @return the body
251
     */
252
    @Override
253
    public HTMLElement getBody() {
254
        NodeList bodyNodes = getElementsByTagName("body");
1✔
255
        for (int i = 0; i < bodyNodes.getLength(); i++) {
1!
256
            Node node = bodyNodes.item(i);
1✔
257
            if (node instanceof HTMLBodyElement) {
1!
258
                return (HTMLBodyElement) node;
1✔
259
            }
260
        }
261
        return null;
×
262
    }
263

264
    /**
265
     * Sets the body.
266
     *
267
     * @param body
268
     *            the new body
269
     */
270
    @Override
271
    public void setBody(HTMLElement body) {
272
        getHtmlElement().appendChild(body);
1✔
273
    }
1✔
274

275
    /**
276
     * Gets the cookie.
277
     *
278
     * @return the cookie
279
     */
280
    @Override
281
    public String getCookie() {
282
        return null;
×
283
    }
284

285
    /**
286
     * Sets the cookie.
287
     *
288
     * @param cookie
289
     *            the new cookie
290
     */
291
    @Override
292
    public void setCookie(String cookie) {
293
    }
×
294

295
    /**
296
     * Open.
297
     */
298
    @Override
299
    public void open() {
300
    }
×
301

302
    /**
303
     * Close.
304
     */
305
    @Override
306
    public void close() {
307
        if (getWindow().replaceText(getWriteBuffer().toString(), getMimeType())) {
1!
308
            clearWriteBuffer();
1✔
309
        }
310
    }
1✔
311

312
    /**
313
     * Gets the mime type.
314
     *
315
     * @return the mime type
316
     */
317
    private String getMimeType() {
318
        return "text/html";
1✔
319
    }
320

321
    /**
322
     * Write.
323
     *
324
     * @param text
325
     *            the text
326
     */
327
    @Override
328
    public void write(String text) {
329
        getWriteBuffer().append(text);
1✔
330
    }
1✔
331

332
    /**
333
     * Writeln.
334
     *
335
     * @param text
336
     *            the text
337
     */
338
    @Override
339
    public void writeln(String text) {
340
        getWriteBuffer().append(text).append((char) 0x0d).append((char) 0x0a);
1✔
341
    }
1✔
342

343
    /**
344
     * Gets the elements by name.
345
     *
346
     * @param elementName
347
     *            the element name
348
     *
349
     * @return the elements by name
350
     */
351
    @Override
352
    public NodeList getElementsByName(String elementName) {
353
        ArrayList<HTMLElementImpl> elements = new ArrayList<HTMLElementImpl>();
1✔
354
        for (Iterator<?> each = preOrderIterator(); each.hasNext();) {
1✔
355
            Node node = (Node) each.next();
1✔
356
            if (!(node instanceof HTMLElementImpl)) {
1✔
357
                continue;
1✔
358
            }
359
            HTMLElementImpl element = (HTMLElementImpl) node;
1✔
360
            if (elementName.equals(element.getAttributeWithNoDefault("name"))) {
1✔
361
                elements.add(element);
1✔
362
            }
363
        }
1✔
364
        return new NodeListImpl(elements);
1✔
365
    }
366

367
    @Override
368
    public Element createElement(String tagName) throws DOMException {
369
        ElementImpl element = getExemplar(tagName).create();
1✔
370
        element.initialize(this, toNodeCase(tagName));
1✔
371
        return element;
1✔
372
    }
373

374
    @Override
375
    public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException {
376
        ElementImpl element = getExemplar(qualifiedName).create();
1✔
377
        element.initialize(this, namespaceURI, toNodeCase(qualifiedName));
1✔
378
        return element;
1✔
379
    }
380

381
    @Override
382
    public NodeList getElementsByTagName(String name) {
383
        return super.getElementsByTagName(toNodeCase(name));
1✔
384
    }
385

386
    @Override
387
    public Node cloneNode(boolean deep) {
388
        HTMLDocumentImpl copy = new HTMLDocumentImpl();
1✔
389

390
        if (deep) {
1!
391
            copy.importChildren(this, copy);
1✔
392
            copy._documentElement = copy.getHtmlElement();
1✔
393
        }
394
        return copy;
1✔
395
    }
396

397
    /**
398
     * Gets the exemplar.
399
     *
400
     * @param tagName
401
     *            the tag name
402
     *
403
     * @return the exemplar
404
     */
405
    private static HTMLElementImpl getExemplar(String tagName) {
406
        HTMLElementImpl impl = (HTMLElementImpl) _exemplars.get(tagName.toLowerCase(Locale.ENGLISH));
1✔
407
        if (impl == null) {
1✔
408
            impl = new HTMLElementImpl();
1✔
409
        }
410
        return impl;
1✔
411
    }
412

413
    /**
414
     * To node case.
415
     *
416
     * @param nodeName
417
     *            the node name
418
     *
419
     * @return the string
420
     */
421
    String toNodeCase(String nodeName) {
422
        return nodeName.toUpperCase();
1✔
423
    }
424

425
    /**
426
     * Gets the container delegate.
427
     *
428
     * @return the container delegate
429
     */
430
    HTMLContainerDelegate getContainerDelegate() {
431
        return _containerDelegate;
1✔
432
    }
433

434
    static {
435
        _exemplars.put("html", new HTMLHtmlElementImpl());
1✔
436
        _exemplars.put("head", new HTMLHeadElementImpl());
1✔
437
        _exemplars.put("link", new HTMLLinkElementImpl());
1✔
438
        _exemplars.put("title", new HTMLTitleElementImpl());
1✔
439
        _exemplars.put("meta", new HTMLMetaElementImpl());
1✔
440
        _exemplars.put("base", new HTMLBaseElementImpl());
1✔
441
        _exemplars.put("style", new HTMLStyleElementImpl());
1✔
442
        _exemplars.put("body", new HTMLBodyElementImpl());
1✔
443
        _exemplars.put("form", new HTMLFormElementImpl());
1✔
444
        _exemplars.put("select", new HTMLSelectElementImpl());
1✔
445
        _exemplars.put("option", new HTMLOptionElementImpl());
1✔
446
        _exemplars.put("input", new HTMLInputElementImpl());
1✔
447
        _exemplars.put("button", new HTMLButtonElementImpl());
1✔
448
        _exemplars.put("textarea", new HTMLTextAreaElementImpl());
1✔
449
        _exemplars.put("a", new HTMLAnchorElementImpl());
1✔
450
        _exemplars.put("area", new HTMLAreaElementImpl());
1✔
451
        _exemplars.put("img", new HTMLImageElementImpl());
1✔
452
        _exemplars.put("td", new HTMLTableCellElementImpl());
1✔
453
        _exemplars.put("th", new HTMLTableCellElementImpl());
1✔
454
        _exemplars.put("tr", new HTMLTableRowElementImpl());
1✔
455
        _exemplars.put("table", new HTMLTableElementImpl());
1✔
456
        _exemplars.put("p", new HTMLParagraphElementImpl());
1✔
457
        _exemplars.put("iframe", new HTMLIFrameElementImpl());
1✔
458
        _exemplars.put("applet", new HTMLAppletElementImpl());
1✔
459
    }
1✔
460

461
    /**
462
     * get the Window.
463
     *
464
     * @return the window
465
     */
466
    public DomWindow getWindow() {
467
        // if there is now window yet
468
        if (_window == null) {
1✔
469
            // create a window for this document
470
            _window = new DomWindow(this);
1✔
471
            setParentScope(_window);
1✔
472
        }
473
        return _window;
1✔
474
    }
475

476
    /**
477
     * Gets the write buffer.
478
     *
479
     * @return the write buffer
480
     */
481
    StringBuilder getWriteBuffer() {
482
        if (_writeBuffer == null) {
1✔
483
            _writeBuffer = new StringBuilder();
1✔
484
        }
485
        return _writeBuffer;
1✔
486
    }
487

488
    /**
489
     * Clear write buffer.
490
     */
491
    public void clearWriteBuffer() {
492
        _writeBuffer = null;
1✔
493
    }
1✔
494

495
    /**
496
     * Gets the base url.
497
     *
498
     * @return the base url
499
     */
500
    URL getBaseUrl() {
501
        NodeList list = getElementsByTagName("base");
1✔
502
        if (list.getLength() == 0) {
1✔
503
            return getWindow().getUrl();
1✔
504
        }
505

506
        HTMLBaseElement base = (HTMLBaseElement) list.item(0);
1✔
507
        try {
508
            return new URL(getWindow().getUrl(), base.getHref());
1✔
509
        } catch (MalformedURLException e) {
×
510
            return getWindow().getUrl();
×
511
        }
512
    }
513
}
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