• 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

75.89
/src/main/java/com/meterware/httpunit/dom/ElementImpl.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.util.ArrayList;
11
import java.util.Hashtable;
12
import java.util.Iterator;
13

14
import org.w3c.dom.Attr;
15
import org.w3c.dom.DOMException;
16
import org.w3c.dom.Element;
17
import org.w3c.dom.NamedNodeMap;
18
import org.w3c.dom.NodeList;
19
import org.w3c.dom.TypeInfo;
20

21
/**
22
 * The Class ElementImpl.
23
 */
24
public class ElementImpl extends NamespaceAwareNodeImpl implements Element {
1✔
25

26
    /** The Constant serialVersionUID. */
27
    private static final long serialVersionUID = 1L;
28

29
    /** The attributes. */
30
    private Hashtable _attributes = new Hashtable<>();
1✔
31

32
    /** The listeners. */
33
    private ArrayList _listeners = new ArrayList<>();
1✔
34

35
    /**
36
     * Creates the element.
37
     *
38
     * @param owner
39
     *            the owner
40
     * @param tagName
41
     *            the tag name
42
     *
43
     * @return the element impl
44
     */
45
    static ElementImpl createElement(DocumentImpl owner, String tagName) {
46
        ElementImpl element = new ElementImpl();
1✔
47
        element.initialize(owner, tagName);
1✔
48
        return element;
1✔
49
    }
50

51
    /**
52
     * Creates the element.
53
     *
54
     * @param owner
55
     *            the owner
56
     * @param namespaceURI
57
     *            the namespace URI
58
     * @param qualifiedName
59
     *            the qualified name
60
     *
61
     * @return the element
62
     */
63
    public static Element createElement(DocumentImpl owner, String namespaceURI, String qualifiedName) {
64
        ElementImpl element = new ElementImpl();
1✔
65
        element.initialize(owner, namespaceURI, qualifiedName);
1✔
66
        return element;
1✔
67
    }
68

69
    /**
70
     * Adds the dom listener.
71
     *
72
     * @param listener
73
     *            the listener
74
     */
75
    public void addDomListener(DomListener listener) {
76
        synchronized (_listeners) {
1✔
77
            _listeners.add(listener);
1✔
78
        }
1✔
79
    }
1✔
80

81
    /**
82
     * Report property changed.
83
     *
84
     * @param propertyName
85
     *            the property name
86
     */
87
    protected void reportPropertyChanged(String propertyName) {
88
        ArrayList listeners;
89
        synchronized (_listeners) {
1✔
90
            listeners = (ArrayList) _listeners.clone();
1✔
91
        }
1✔
92

93
        for (Iterator each = listeners.iterator(); each.hasNext();) {
1✔
94
            ((DomListener) each.next()).propertyChanged(this, propertyName);
1✔
95
        }
96
    }
1✔
97

98
    // ---------------------------------------- Element methods
99
    // -------------------------------------------------------------
100

101
    @Override
102
    public short getNodeType() {
103
        return ELEMENT_NODE;
1✔
104
    }
105

106
    @Override
107
    public String getNodeValue() throws DOMException {
108
        return null;
1✔
109
    }
110

111
    @Override
112
    public void setNodeValue(String nodeValue) throws DOMException {
113
    }
1✔
114

115
    @Override
116
    public boolean hasAttributes() {
117
        return !_attributes.isEmpty();
1✔
118
    }
119

120
    @Override
121
    public NamedNodeMap getAttributes() {
122
        return new NamedNodeMapImpl(_attributes);
1✔
123
    }
124

125
    /**
126
     * get the attribute with the given name
127
     *
128
     * @param name
129
     *            - the name of the attribute to get
130
     */
131
    @Override
132
    public String getAttribute(String name) {
133
        Attr attr = getAttributeNode(name);
1✔
134
        return attr == null ? "" : attr.getValue();
1✔
135
    }
136

137
    @Override
138
    public void setAttribute(String name, String value) throws DOMException {
139
        if (value.equals(getAttribute(name))) {
1!
140
            return;
×
141
        }
142

143
        Attr attribute = getOwnerDocument().createAttribute(name);
1✔
144
        attribute.setValue(value);
1✔
145
        setAttributeNode(attribute);
1✔
146
        reportPropertyChanged(name);
1✔
147
    }
1✔
148

149
    /**
150
     * get the event Handler script for the event e.g. onchange, onmousedown, onclick, onmouseup execute the script if
151
     * it's assigned by calling doEvent for the script
152
     *
153
     * @param eventName
154
     *            the event name
155
     *
156
     * @return true, if successful
157
     */
158
    @Override
159
    public boolean handleEvent(String eventName) {
160
        // check whether onclick is activated
161
        if (eventName.equalsIgnoreCase("onclick")) {
×
162
            handleEvent("onmousedown");
×
163
        }
164
        String eventScript = getAttribute(eventName);
×
165
        boolean result = doEventScript(eventScript);
×
166
        if (eventName.equalsIgnoreCase("onclick")) {
×
167
            handleEvent("onmouseup");
×
168
        }
169
        return result;
×
170
    }
171

172
    @Override
173
    public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException {
174
        Attr attribute = getOwnerDocument().createAttributeNS(namespaceURI, qualifiedName);
1✔
175
        attribute.setValue(value);
1✔
176
        setAttributeNodeNS(attribute);
1✔
177
    }
1✔
178

179
    @Override
180
    public void removeAttribute(String name) throws DOMException {
181
        _attributes.remove(name);
1✔
182
    }
1✔
183

184
    @Override
185
    public Attr getAttributeNode(String name) {
186
        return (Attr) _attributes.get(name);
1✔
187
    }
188

189
    @Override
190
    public Attr setAttributeNode(Attr newAttr) throws DOMException {
191
        if (newAttr.getOwnerDocument() != getOwnerDocument()) {
1!
192
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
×
193
                    "attribute must be from the same document as the element");
194
        }
195

196
        ((AttrImpl) newAttr).setOwnerElement(this);
1✔
197
        AttrImpl oldAttr = (AttrImpl) _attributes.put(newAttr.getName(), newAttr);
1✔
198
        if (oldAttr != null) {
1✔
199
            oldAttr.setOwnerElement(null);
1✔
200
        }
201
        return oldAttr;
1✔
202
    }
203

204
    @Override
205
    public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
206
        if (newAttr.getOwnerDocument() != getOwnerDocument()) {
1!
207
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
×
208
                    "attribute must be from the same document as the element");
209
        }
210

211
        ((AttrImpl) newAttr).setOwnerElement(this);
1✔
212
        AttrImpl oldAttr = (AttrImpl) _attributes.put(newAttr.getName(), newAttr);
1✔
213
        if (oldAttr != null) {
1✔
214
            oldAttr.setOwnerElement(null);
1✔
215
        }
216
        return oldAttr;
1✔
217
    }
218

219
    @Override
220
    public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
221
        if (!_attributes.containsValue(oldAttr)) {
1✔
222
            throw new DOMException(DOMException.NOT_FOUND_ERR, "Specified attribute is not defined for this element");
1✔
223
        }
224

225
        AttrImpl removedAttr = (AttrImpl) _attributes.remove(oldAttr.getName());
1✔
226
        if (removedAttr != null) {
1!
227
            removedAttr.setOwnerElement(null);
1✔
228
        }
229
        return removedAttr;
1✔
230
    }
231

232
    @Override
233
    public boolean hasAttribute(String name) {
234
        return _attributes.containsKey(name);
1✔
235
    }
236

237
    // ----------------------- namespaces are not supported at present --------------------------------
238

239
    @Override
240
    public String getAttributeNS(String namespaceURI, String localName) {
241
        return null;
×
242
    }
243

244
    @Override
245
    public void removeAttributeNS(String namespaceURI, String localName) throws DOMException {
246
    }
×
247

248
    @Override
249
    public Attr getAttributeNodeNS(String namespaceURI, String localName) {
250
        return null;
×
251
    }
252

253
    @Override
254
    public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
255
        return null;
×
256
    }
257

258
    @Override
259
    public boolean hasAttributeNS(String namespaceURI, String localName) {
260
        return false;
×
261
    }
262

263
    /**
264
     * Import node.
265
     *
266
     * @param document
267
     *            the document
268
     * @param original
269
     *            the original
270
     * @param deep
271
     *            the deep
272
     *
273
     * @return the element
274
     */
275
    public static Element importNode(DocumentImpl document, Element original, boolean deep) {
276
        Element copy = document.createElementNS(original.getNamespaceURI(), original.getTagName());
1✔
277
        NamedNodeMap attributes = original.getAttributes();
1✔
278
        for (int i = 0; i < attributes.getLength(); i++) {
1✔
279
            copy.setAttributeNode((Attr) document.importNode(attributes.item(i), false));
1✔
280
        }
281
        if (deep) {
1✔
282
            document.importChildren(original, copy);
1✔
283
        }
284
        return copy;
1✔
285
    }
286

287
    // ------------------------------------- DOM level 3 methods
288
    // ------------------------------------------------------------
289

290
    @Override
291
    public TypeInfo getSchemaTypeInfo() {
292
        return null; // To change body of implemented methods use File | Settings | File Templates.
×
293
    }
294

295
    @Override
296
    public void setIdAttribute(String name, boolean isId) throws DOMException {
297
        // To change body of implemented methods use File | Settings | File Templates.
298
    }
×
299

300
    @Override
301
    public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException {
302
        // To change body of implemented methods use File | Settings | File Templates.
303
    }
×
304

305
    @Override
306
    public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
307
        // To change body of implemented methods use File | Settings | File Templates.
308
    }
×
309
}
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