1 /*
2 * Copyright (c) 2002-2026 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.html;
16
17 import static org.htmlunit.BrowserVersionFeatures.EVENT_CONTEXT_MENU_HAS_DETAIL_1;
18 import static org.htmlunit.BrowserVersionFeatures.JS_AREA_WITHOUT_HREF_FOCUSABLE;
19
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.io.Serializable;
23 import java.io.StringWriter;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Comparator;
27 import java.util.Iterator;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.Map;
32 import java.util.NoSuchElementException;
33 import java.util.Set;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.htmlunit.BrowserVersion;
38 import org.htmlunit.Page;
39 import org.htmlunit.ScriptResult;
40 import org.htmlunit.SgmlPage;
41 import org.htmlunit.WebClient;
42 import org.htmlunit.css.ComputedCssStyleDeclaration;
43 import org.htmlunit.css.CssStyleSheet;
44 import org.htmlunit.css.StyleElement;
45 import org.htmlunit.cssparser.dom.CSSStyleDeclarationImpl;
46 import org.htmlunit.cssparser.dom.Property;
47 import org.htmlunit.cssparser.parser.CSSException;
48 import org.htmlunit.cssparser.parser.selector.Selector;
49 import org.htmlunit.cssparser.parser.selector.SelectorList;
50 import org.htmlunit.cssparser.parser.selector.SelectorSpecificity;
51 import org.htmlunit.cyberneko.util.FastHashMap;
52 import org.htmlunit.html.DefaultElementFactory.OrderedFastHashMapWithLowercaseKeys;
53 import org.htmlunit.html.csp.ContentSecurityPolicy;
54 import org.htmlunit.javascript.AbstractJavaScriptEngine;
55 import org.htmlunit.javascript.JavaScriptEngine;
56 import org.htmlunit.javascript.host.event.Event;
57 import org.htmlunit.javascript.host.event.EventTarget;
58 import org.htmlunit.javascript.host.event.MouseEvent;
59 import org.htmlunit.javascript.host.event.PointerEvent;
60 import org.htmlunit.util.OrderedFastHashMap;
61 import org.htmlunit.util.StringUtils;
62 import org.w3c.dom.Attr;
63 import org.w3c.dom.DOMException;
64 import org.w3c.dom.Element;
65 import org.w3c.dom.NamedNodeMap;
66 import org.w3c.dom.Node;
67 import org.w3c.dom.TypeInfo;
68 import org.xml.sax.SAXException;
69
70 /**
71 * A DOM element in an HTML or XML document.
72 *
73 * @author Ahmed Ashour
74 * @author Marc Guillemot
75 * @author Tom Anderson
76 * @author Ronald Brill
77 * @author Frank Danek
78 * @author Sven Strickroth
79 * @author Ronny Shapiro
80 */
81 public class DomElement extends DomNamespaceNode implements Element {
82
83 private static final Log LOG = LogFactory.getLog(DomElement.class);
84
85 /** id. */
86 public static final String ID_ATTRIBUTE = "id";
87
88 /** name. */
89 public static final String NAME_ATTRIBUTE = "name";
90
91 /** src. */
92 public static final String SRC_ATTRIBUTE = "src";
93
94 /** The "nonce" attribute. */
95 public static final String NONCE_ATTRIBUTE = "nonce";
96
97 /** value. */
98 public static final String VALUE_ATTRIBUTE = "value";
99
100 /** type. */
101 public static final String TYPE_ATTRIBUTE = "type";
102
103 /** Constant meaning that the specified attribute was not defined. */
104 public static final String ATTRIBUTE_NOT_DEFINED = new String("");
105
106 /**
107 * Constant meaning that the specified attribute was found but its value was empty.
108 *
109 * @deprecated as of version 5.8.0; an empty attribute value is an empty string that is not
110 * {@link #ATTRIBUTE_NOT_DEFINED}, check this with {@code value.isEmpty()} after
111 * the check for {@link #ATTRIBUTE_NOT_DEFINED}
112 */
113 @Deprecated
114 public static final String ATTRIBUTE_VALUE_EMPTY = new String();
115
116 /** The map holding the attributes, keyed by name. */
117 private NamedAttrNodeMapImpl attributes_;
118
119 /** The map holding the namespaces, keyed by URI. */
120 private FastHashMap<String, String> namespaces_;
121
122 /** Cache for the styles. */
123 private String styleString_;
124 // the value of the style attribute blocked by the Content-Security-Policy (style-src-attr)
125 private String blockedStyleAttribute_;
126 // true while the CSSOM writes the style attribute (not checked by the Content-Security-Policy)
127 private boolean writingCssomStyle_;
128 private LinkedHashMap<String, StyleElement> styleMap_;
129
130 private static final Comparator<StyleElement> STYLE_ELEMENT_COMPARATOR =
131 (first, second) -> StyleElement.compareToByImportanceAndSpecificity(first, second);
132
133 /**
134 * Whether the Mouse is currently over this element or not.
135 */
136 private boolean mouseOver_;
137
138 /** The internal nonce slot; null if the slot follows the nonce attribute. */
139 private String nonce_;
140
141 /**
142 * Creates an instance of a DOM element that can have a namespace.
143 *
144 * @param namespaceURI the URI that identifies an XML namespace
145 * @param qualifiedName the qualified name of the element type to instantiate
146 * @param page the page that contains this element
147 * @param attributes a map ready initialized with the attributes for this element, or
148 * {@code null}. The map will be stored as is, not copied.
149 */
150 public DomElement(final String namespaceURI, final String qualifiedName, final SgmlPage page,
151 final Map<String, DomAttr> attributes) {
152 super(namespaceURI, qualifiedName, page);
153
154 if (attributes == null) {
155 attributes_ = new NamedAttrNodeMapImpl(this, isAttributeCaseSensitive());
156 }
157 else {
158 attributes_ = new NamedAttrNodeMapImpl(this, isAttributeCaseSensitive(), attributes);
159
160 for (final DomAttr entry : attributes.values()) {
161 entry.setParentNode(this);
162 final String attrNamespaceURI = entry.getNamespaceURI();
163 final String prefix = entry.getPrefix();
164
165 if (attrNamespaceURI != null && prefix != null) {
166 if (namespaces_ == null) {
167 namespaces_ = new FastHashMap<>(1, 0.5f);
168 }
169 namespaces_.put(attrNamespaceURI, prefix);
170 }
171 }
172
173 final DomAttr styleAttr = attributes.get("style");
174 if (styleAttr != null) {
175 checkStyleAttribute(styleAttr.getValue());
176 }
177 }
178 }
179
180 /**
181 * Checks the value of the style attribute against the Content-Security-Policy (style-src-attr);
182 * a blocked value is not applied.
183 *
184 * @param value the value of the style attribute
185 */
186 private void checkStyleAttribute(final String value) {
187 blockedStyleAttribute_ = null;
188 if (writingCssomStyle_ || value == null || value.isEmpty()
189 || !(getPage() instanceof HtmlPage htmlPage)) {
190 return;
191 }
192 if (!ContentSecurityPolicy.allowsStyleAttribute(htmlPage, this, value)) {
193 blockedStyleAttribute_ = value;
194 }
195 }
196
197 /**
198 * {@inheritDoc}
199 */
200 @Override
201 public String getNodeName() {
202 return getQualifiedName();
203 }
204
205 /**
206 * {@inheritDoc}
207 */
208 @Override
209 public final short getNodeType() {
210 return ELEMENT_NODE;
211 }
212
213 /**
214 * Returns the tag name of this element.
215 * @return the tag name of this element
216 */
217 @Override
218 public final String getTagName() {
219 return getNodeName();
220 }
221
222 /**
223 * {@inheritDoc}
224 */
225 @Override
226 public final boolean hasAttributes() {
227 return !attributes_.isEmpty();
228 }
229
230 /**
231 * Returns whether the attribute specified by name has a value.
232 *
233 * @param attributeName the name of the attribute
234 * @return true if an attribute with the given name is specified on this element or has a
235 * default value, false otherwise.
236 */
237 @Override
238 public boolean hasAttribute(final String attributeName) {
239 return attributes_.containsKey(attributeName);
240 }
241
242 /**
243 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
244 *
245 * Replaces the value of the named style attribute. If there is no style attribute with the
246 * specified name, a new one is added. If the specified value is an empty (or all whitespace)
247 * string, this method actually removes the named style attribute.
248 * @param name the attribute name (delimiter-separated, not camel-cased)
249 * @param value the attribute value
250 * @param priority the new priority of the property; <code>"important"</code>or the empty string if none.
251 */
252 public void replaceStyleAttribute(final String name, final String value, final String priority) {
253 if (StringUtils.isBlank(value)) {
254 removeStyleAttribute(name);
255 return;
256 }
257
258 final Map<String, StyleElement> styleMap = getStyleMap();
259 final StyleElement old = styleMap.get(name);
260 final StyleElement element;
261 if (old == null) {
262 element = new StyleElement(name, value, priority, SelectorSpecificity.FROM_STYLE_ATTRIBUTE);
263 }
264 else {
265 element = new StyleElement(name, value, priority,
266 SelectorSpecificity.FROM_STYLE_ATTRIBUTE, old.getIndex());
267 }
268 styleMap.put(name, element);
269 writeStyleToElement(styleMap);
270 }
271
272 /**
273 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
274 *
275 * Removes the specified style attribute, returning the value of the removed attribute.
276 * @param name the attribute name (delimiter-separated, not camel-cased)
277 * @return the removed value
278 */
279 public String removeStyleAttribute(final String name) {
280 final Map<String, StyleElement> styleMap = getStyleMap();
281 final StyleElement value = styleMap.get(name);
282 if (value == null) {
283 return "";
284 }
285 styleMap.remove(name);
286 writeStyleToElement(styleMap);
287 return value.getValue();
288 }
289
290 /**
291 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
292 *
293 * Determines the StyleElement for the given name.
294 *
295 * @param name the name of the requested StyleElement
296 * @return the StyleElement or null if not found
297 */
298 public StyleElement getStyleElement(final String name) {
299 final Map<String, StyleElement> map = getStyleMap();
300 if (map != null) {
301 return map.get(name);
302 }
303 return null;
304 }
305
306 /**
307 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
308 *
309 * Determines the StyleElement for the given name.
310 * This ignores the case of the name.
311 *
312 * @param name the name of the requested StyleElement
313 * @return the StyleElement or null if not found
314 */
315 public StyleElement getStyleElementCaseInSensitive(final String name) {
316 final Map<String, StyleElement> map = getStyleMap();
317 if (map != null) {
318 for (final Map.Entry<String, StyleElement> entry : map.entrySet()) {
319 if (entry.getKey().equalsIgnoreCase(name)) {
320 return entry.getValue();
321 }
322 }
323 }
324 return null;
325 }
326
327 /**
328 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
329 *
330 * Returns a sorted map containing style elements, keyed on style element name. We use a
331 * {@link LinkedHashMap} map so that results are deterministic and are thus testable.
332 *
333 * @return a sorted map containing style elements, keyed on style element name
334 */
335 public LinkedHashMap<String, StyleElement> getStyleMap() {
336 final String styleAttribute = getAttributeDirect("style");
337 if (styleString_ == styleAttribute) {
338 return styleMap_;
339 }
340
341 final LinkedHashMap<String, StyleElement> styleMap = new LinkedHashMap<>();
342 if (ATTRIBUTE_NOT_DEFINED == styleAttribute || styleAttribute.isEmpty()
343 || styleAttribute.equals(blockedStyleAttribute_)) {
344 styleMap_ = styleMap;
345 styleString_ = styleAttribute;
346 return styleMap_;
347 }
348
349 final CSSStyleDeclarationImpl cssStyle = new CSSStyleDeclarationImpl(null);
350 try {
351 // use the configured cssErrorHandler here to do the same error handling during
352 // parsing of inline styles like for external css
353 cssStyle.setCssText(styleAttribute, getPage().getWebClient().getCssErrorHandler());
354 }
355 catch (final Exception e) {
356 if (LOG.isErrorEnabled()) {
357 LOG.error("Error while parsing style value '" + styleAttribute + "'", e);
358 }
359 }
360
361 for (final Property prop : cssStyle.getProperties()) {
362 final String key = prop.getName().toLowerCase(Locale.ROOT);
363 final StyleElement element = new StyleElement(key,
364 prop.getValue().getCssText(),
365 prop.isImportant() ? StyleElement.PRIORITY_IMPORTANT : "",
366 SelectorSpecificity.FROM_STYLE_ATTRIBUTE);
367 styleMap.put(key, element);
368 }
369
370 styleMap_ = styleMap;
371 styleString_ = styleAttribute;
372 // styleString_ = cssStyle.getCssText();
373 return styleMap_;
374 }
375
376 /**
377 * Prints the content between "<" and ">" (or "/>") in the output of the tag name
378 * and its attributes in XML format.
379 * @param printWriter the writer to print in
380 */
381 protected void printOpeningTagContentAsXml(final PrintWriter printWriter) {
382 printWriter.print(getTagName());
383 for (final Map.Entry<String, DomAttr> entry : attributes_.entrySet()) {
384 printWriter.print(" ");
385 printWriter.print(entry.getKey());
386 printWriter.print("=\"");
387 printWriter.print(StringUtils.escapeXmlAttributeValue(entry.getValue().getNodeValue()));
388 printWriter.print("\"");
389 }
390 }
391
392 /**
393 * {@inheritDoc}
394 */
395 @Override
396 protected boolean printXml(final String indent, final boolean indentBefore, final PrintWriter printWriter) {
397 final boolean hasChildren = getFirstChild() != null;
398
399 if (indentBefore) {
400 printWriter.print("\r\n");
401 printWriter.print(indent);
402 }
403
404 printWriter.print('<');
405 printOpeningTagContentAsXml(printWriter);
406
407 if (hasChildren) {
408 printWriter.print(">");
409 final boolean indBefore = printChildrenAsXml(indent, false, printWriter);
410 if (indBefore) {
411 printWriter.print("\r\n");
412 printWriter.print(indent);
413 }
414 printWriter.print("</");
415 printWriter.print(getTagName());
416 printWriter.print(">");
417 }
418 else if (isEmptyXmlTagExpanded()) {
419 printWriter.print("></");
420 printWriter.print(getTagName());
421 printWriter.print(">");
422 }
423 else {
424 printWriter.print("/>");
425 }
426
427 return false;
428 }
429
430 /**
431 * Indicates if a node without children should be written in expanded form as XML
432 * (i.e. with closing tag rather than with "/>")
433 * @return {@code false} by default
434 */
435 protected boolean isEmptyXmlTagExpanded() {
436 return false;
437 }
438
439 /**
440 * Returns the qualified name (prefix:local) for the specified namespace and local name,
441 * or {@code null} if the specified namespace URI does not exist.
442 *
443 * @param namespaceURI the URI that identifies an XML namespace
444 * @param localName the name within the namespace
445 * @return the qualified name for the specified namespace and local name
446 */
447 String getQualifiedName(final String namespaceURI, final String localName) {
448 final String qualifiedName;
449 if (namespaceURI == null) {
450 qualifiedName = localName;
451 }
452 else {
453 final String prefix = namespaces_ == null ? null : namespaces_.get(namespaceURI);
454 if (prefix == null) {
455 qualifiedName = null;
456 }
457 else {
458 qualifiedName = prefix + ':' + localName;
459 }
460 }
461 return qualifiedName;
462 }
463
464 /**
465 * Returns the value of the attribute specified by name or an empty string. If the
466 * attribute wasn't specified, the result is {@link #ATTRIBUTE_NOT_DEFINED} (check with {@code ==});
467 * any other empty string means the attribute was specified, but it was empty.
468 *
469 * @param attributeName the name of the attribute
470 * @return the value of the attribute or {@link #ATTRIBUTE_NOT_DEFINED}
471 */
472 @Override
473 public String getAttribute(final String attributeName) {
474 final DomAttr attr = attributes_.get(attributeName);
475 if (attr != null) {
476 return attr.getNodeValue();
477 }
478 return ATTRIBUTE_NOT_DEFINED;
479 }
480
481 /**
482 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
483 *
484 * @param attributeName the name of the attribute
485 * @return the value of the attribute or {@link #ATTRIBUTE_NOT_DEFINED}
486 */
487 public String getAttributeDirect(final String attributeName) {
488 final DomAttr attr = attributes_.getDirect(attributeName);
489 if (attr != null) {
490 return attr.getNodeValue();
491 }
492 return ATTRIBUTE_NOT_DEFINED;
493 }
494
495 /**
496 * Removes an attribute specified by name from this element.
497 * @param attributeName the attribute attributeName
498 */
499 @Override
500 public void removeAttribute(final String attributeName) {
501 attributes_.remove(attributeName);
502 if (NONCE_ATTRIBUTE.equals(attributeName)) {
503 nonce_ = null;
504 }
505 }
506
507 /**
508 * Removes an attribute specified by namespace and local name from this element.
509 * @param namespaceURI the URI that identifies an XML namespace
510 * @param localName the name within the namespace
511 */
512 @Override
513 public final void removeAttributeNS(final String namespaceURI, final String localName) {
514 final String qualifiedName = getQualifiedName(namespaceURI, localName);
515 if (qualifiedName != null) {
516 removeAttribute(qualifiedName);
517 }
518 }
519
520 /**
521 * {@inheritDoc}
522 * Not yet implemented.
523 */
524 @Override
525 public final Attr removeAttributeNode(final Attr attribute) {
526 throw new UnsupportedOperationException("DomElement.removeAttributeNode is not yet implemented.");
527 }
528
529 /**
530 * Returns whether the attribute specified by namespace and local name has a value.
531 *
532 * @param namespaceURI the URI that identifies an XML namespace
533 * @param localName the name within the namespace
534 * @return true if an attribute with the given name is specified on this element or has a
535 * default value, false otherwise.
536 */
537 @Override
538 public final boolean hasAttributeNS(final String namespaceURI, final String localName) {
539 final String qualifiedName = getQualifiedName(namespaceURI, localName);
540 if (qualifiedName != null) {
541 return attributes_.get(qualifiedName) != null;
542 }
543 return false;
544 }
545
546 /**
547 * Returns the map holding the attributes, keyed by name.
548 * @return the attributes map
549 */
550 public final Map<String, DomAttr> getAttributesMap() {
551 return attributes_;
552 }
553
554 /**
555 * {@inheritDoc}
556 */
557 @Override
558 public NamedNodeMap getAttributes() {
559 return attributes_;
560 }
561
562 /**
563 * Sets the value of the attribute specified by name.
564 *
565 * @param attributeName the name of the attribute
566 * @param attributeValue the value of the attribute
567 */
568 @Override
569 public void setAttribute(final String attributeName, final String attributeValue) {
570 setAttributeNS(null, attributeName, attributeValue);
571 }
572
573 /**
574 * Sets the value of the attribute specified by namespace and qualified name.
575 *
576 * @param namespaceURI the URI that identifies an XML namespace
577 * @param qualifiedName the qualified name (prefix:local) of the attribute
578 * @param attributeValue the value of the attribute
579 */
580 @Override
581 public void setAttributeNS(final String namespaceURI, final String qualifiedName,
582 final String attributeValue) {
583 setAttributeNS(namespaceURI, qualifiedName, attributeValue, true, true);
584 }
585
586 /**
587 * Sets the value of the attribute specified by namespace and qualified name.
588 *
589 * @param namespaceURI the URI that identifies an XML namespace
590 * @param qualifiedName the qualified name (prefix:local) of the attribute
591 * @param attributeValue the value of the attribute
592 * @param notifyAttributeChangeListeners to notify the associated {@link HtmlAttributeChangeListener}s
593 * @param notifyMutationObservers to notify {@code MutationObserver}s or not
594 */
595 protected void setAttributeNS(final String namespaceURI, final String qualifiedName,
596 final String attributeValue, final boolean notifyAttributeChangeListeners,
597 final boolean notifyMutationObservers) {
598 final DomAttr newAttr = new DomAttr(getPage(), namespaceURI, qualifiedName, attributeValue, true);
599 newAttr.setParentNode(this);
600 attributes_.put(qualifiedName, newAttr);
601
602 if (namespaceURI == null && NONCE_ATTRIBUTE.equals(qualifiedName)) {
603 // the nonce slot follows the attribute again
604 nonce_ = null;
605 }
606
607 if (namespaceURI == null && "style".equals(qualifiedName)) {
608 checkStyleAttribute(attributeValue);
609 }
610
611 if (namespaceURI != null) {
612 if (namespaces_ == null) {
613 namespaces_ = new FastHashMap<>(1, 0.5f);
614 }
615 namespaces_.put(namespaceURI, newAttr.getPrefix());
616 }
617 }
618
619 /**
620 * {@inheritDoc}
621 * Hides the nonce attribute if required by the Content-Security-Policy.
622 */
623 @Override
624 protected void onAddedToPage() {
625 ContentSecurityPolicy.hideNonceIfNeeded(this);
626 super.onAddedToPage();
627 }
628
629 /**
630 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
631 *
632 * Returns the value of the internal nonce slot (the {@code nonce} IDL attribute); this is
633 * the value of the nonce attribute or the value set by script. The nonce slot keeps the
634 * value also if the nonce attribute is hidden (Content-Security-Policy).
635 *
636 * @return the nonce or an empty string
637 */
638 public String getNonce() {
639 if (nonce_ != null) {
640 return nonce_;
641 }
642 final String nonce = getAttributeDirect(NONCE_ATTRIBUTE);
643 if (ATTRIBUTE_NOT_DEFINED == nonce) {
644 return "";
645 }
646 return nonce;
647 }
648
649 /**
650 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
651 *
652 * Sets the value of the internal nonce slot (the {@code nonce} IDL attribute); the
653 * nonce attribute is not changed.
654 *
655 * @param nonce the new nonce
656 */
657 public void setNonce(final String nonce) {
658 nonce_ = nonce;
659 }
660
661 /**
662 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
663 *
664 * Nonce hiding: the value of the nonce attribute is replaced by an empty string,
665 * the nonce slot keeps the value.
666 */
667 public void hideNonce() {
668 final String nonce = getNonce();
669 setAttribute(NONCE_ATTRIBUTE, "");
670 nonce_ = nonce;
671 }
672
673 /**
674 * Indicates if the attribute names are case sensitive.
675 * @return {@code true}
676 */
677 protected boolean isAttributeCaseSensitive() {
678 return true;
679 }
680
681 /**
682 * Returns the value of the attribute specified by namespace and local name or an empty
683 * string. If the attribute wasn't specified, the result is {@link #ATTRIBUTE_NOT_DEFINED}
684 * (check with {@code ==}); any other empty string means the attribute was specified, but it was empty.
685 *
686 * @param namespaceURI the URI that identifies an XML namespace
687 * @param localName the name within the namespace
688 * @return the value of the attribute or {@link #ATTRIBUTE_NOT_DEFINED}
689 */
690 @Override
691 public final String getAttributeNS(final String namespaceURI, final String localName) {
692 final String qualifiedName = getQualifiedName(namespaceURI, localName);
693 if (qualifiedName != null) {
694 return getAttribute(qualifiedName);
695 }
696 return ATTRIBUTE_NOT_DEFINED;
697 }
698
699 /**
700 * {@inheritDoc}
701 */
702 @Override
703 public DomAttr getAttributeNode(final String name) {
704 return attributes_.get(name);
705 }
706
707 /**
708 * {@inheritDoc}
709 */
710 @Override
711 public DomAttr getAttributeNodeNS(final String namespaceURI, final String localName) {
712 final String qualifiedName = getQualifiedName(namespaceURI, localName);
713 if (qualifiedName != null) {
714 return attributes_.get(qualifiedName);
715 }
716 return null;
717 }
718
719 /**
720 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
721 *
722 * @param styleMap the styles
723 */
724 public void writeStyleToElement(final Map<String, StyleElement> styleMap) {
725 if (styleMap.isEmpty()) {
726 setCssomStyleAttribute("");
727 return;
728 }
729
730 final List<StyleElement> styleElements = new ArrayList<>(styleMap.values());
731 styleElements.sort(STYLE_ELEMENT_COMPARATOR);
732
733 final StringBuilder builder = new StringBuilder(styleElements.size() * 32);
734 for (final StyleElement e : styleElements) {
735 if (builder.length() > 0) {
736 builder.append(' ');
737 }
738 builder.append(e.getName())
739 .append(": ")
740 .append(e.getValue());
741
742 final String prio = e.getPriority();
743 if (StringUtils.isNotBlank(prio)) {
744 builder.append(" !").append(prio);
745 }
746 builder.append(';');
747 }
748 setCssomStyleAttribute(builder.toString());
749 }
750
751 /**
752 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
753 *
754 * Sets the style attribute as result of a CSSOM change (e.g. element.style.color = 'red'
755 * or element.style.cssText = '...'). Like the browsers, these changes are not checked
756 * against the Content-Security-Policy (style-src-attr).
757 *
758 * @param value the new value of the style attribute
759 */
760 public void setCssomStyleAttribute(final String value) {
761 writingCssomStyle_ = true;
762 try {
763 setAttribute("style", value);
764 }
765 finally {
766 writingCssomStyle_ = false;
767 }
768 }
769
770 /**
771 * {@inheritDoc}
772 */
773 @Override
774 public DomNodeList<HtmlElement> getElementsByTagName(final String tagName) {
775 return getElementsByTagNameImpl(tagName);
776 }
777
778 /**
779 * This should be {@link #getElementsByTagName(String)}, but is separate because of the type erasure in Java.
780 * @param tagName The name of the tag to match on
781 * @return A list of matching elements.
782 */
783 <E extends HtmlElement> DomNodeList<E> getElementsByTagNameImpl(final String tagName) {
784 return new AbstractDomNodeList<>(this) {
785 @Override
786 @SuppressWarnings("unchecked")
787 protected List<E> provideElements() {
788 final List<E> res = new ArrayList<>();
789 for (final HtmlElement elem : getDomNode().getHtmlElementDescendants()) {
790 if (elem.getLocalName().equalsIgnoreCase(tagName)) {
791 res.add((E) elem);
792 }
793 }
794 return res;
795 }
796 };
797 }
798
799 /**
800 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
801 *
802 * @param <E> the specific HtmlElement type
803 * @param tagName The name of the tag to match on
804 * @return A list of matching elements; this is not a live list
805 */
806 public <E extends HtmlElement> List<E> getStaticElementsByTagName(final String tagName) {
807 final List<E> res = new ArrayList<>();
808 for (final Iterator<HtmlElement> iterator = this.new DescendantHtmlElementsIterator(); iterator.hasNext();) {
809 final HtmlElement elem = iterator.next();
810 if (elem.getLocalName().equalsIgnoreCase(tagName)) {
811 final String prefix = elem.getPrefix();
812 if (prefix == null || prefix.isEmpty()) {
813 res.add((E) elem);
814 }
815 }
816 }
817 return res;
818 }
819
820 /**
821 * {@inheritDoc}
822 * Not yet implemented.
823 */
824 @Override
825 public DomNodeList<HtmlElement> getElementsByTagNameNS(final String namespace, final String localName) {
826 throw new UnsupportedOperationException("DomElement.getElementsByTagNameNS is not yet implemented.");
827 }
828
829 /**
830 * {@inheritDoc}
831 * Not yet implemented.
832 */
833 @Override
834 public TypeInfo getSchemaTypeInfo() {
835 throw new UnsupportedOperationException("DomElement.getSchemaTypeInfo is not yet implemented.");
836 }
837
838 /**
839 * {@inheritDoc}
840 * Not yet implemented.
841 */
842 @Override
843 public void setIdAttribute(final String name, final boolean isId) {
844 throw new UnsupportedOperationException("DomElement.setIdAttribute is not yet implemented.");
845 }
846
847 /**
848 * {@inheritDoc}
849 * Not yet implemented.
850 */
851 @Override
852 public void setIdAttributeNS(final String namespaceURI, final String localName, final boolean isId) {
853 throw new UnsupportedOperationException("DomElement.setIdAttributeNS is not yet implemented.");
854 }
855
856 /**
857 * {@inheritDoc}
858 */
859 @Override
860 public Attr setAttributeNode(final Attr attribute) {
861 return attributes_.setNamedItem(attribute);
862 }
863
864 /**
865 * {@inheritDoc}
866 * Not yet implemented.
867 */
868 @Override
869 public Attr setAttributeNodeNS(final Attr attribute) {
870 throw new UnsupportedOperationException("DomElement.setAttributeNodeNS is not yet implemented.");
871 }
872
873 /**
874 * {@inheritDoc}
875 * Not yet implemented.
876 */
877 @Override
878 public final void setIdAttributeNode(final Attr idAttr, final boolean isId) {
879 throw new UnsupportedOperationException("DomElement.setIdAttributeNode is not yet implemented.");
880 }
881
882 /**
883 * {@inheritDoc}
884 */
885 @Override
886 public DomNode cloneNode(final boolean deep) {
887 final DomElement clone = (DomElement) super.cloneNode(deep);
888 clone.attributes_ = new NamedAttrNodeMapImpl(clone, isAttributeCaseSensitive());
889 clone.attributes_.putAll(attributes_);
890 return clone;
891 }
892
893 /**
894 * Returns the identifier of this element.
895 *
896 * @return the identifier of this element
897 */
898 public final String getId() {
899 return getAttributeDirect(ID_ATTRIBUTE);
900 }
901
902 /**
903 * Sets the identifier this element.
904 *
905 * @param newId the new identifier of this element
906 */
907 public final void setId(final String newId) {
908 setAttribute(ID_ATTRIBUTE, newId);
909 }
910
911 /**
912 * Returns the first child element node of this element. null if this element has no child elements.
913 * @return the first child element node of this element. null if this element has no child elements
914 */
915 public DomElement getFirstElementChild() {
916 final Iterator<DomElement> i = getChildElements().iterator();
917 if (i.hasNext()) {
918 return i.next();
919 }
920 return null;
921 }
922
923 /**
924 * Returns the last child element node of this element. null if this element has no child elements.
925 * @return the last child element node of this element. null if this element has no child elements
926 */
927 public DomElement getLastElementChild() {
928 for (DomNode child = getLastChild(); child != null; child = child.getPreviousSibling()) {
929 if (child instanceof DomElement elem) {
930 return elem;
931 }
932 }
933 return null;
934 }
935
936 /**
937 * Returns the current number of element nodes that are children of this element.
938 * @return the current number of element nodes that are children of this element.
939 */
940 public int getChildElementCount() {
941 int counter = 0;
942 for (DomNode child = getFirstChild(); child != null; child = child.getNextSibling()) {
943 if (child instanceof DomElement) {
944 counter++;
945 }
946 }
947 return counter;
948 }
949
950 /**
951 * Returns an {@link Iterable} over the {@link DomElement} children of this object, i.e. excluding the non-element nodes.
952 *
953 * @return an {@link Iterable} over the {@link DomElement} children of this object, i.e. excluding the non-element nodes
954 */
955 public final Iterable<DomElement> getChildElements() {
956 return new ChildElementsIterable(this);
957 }
958
959 /**
960 * An Iterable over the DomElement children.
961 */
962 private static class ChildElementsIterable implements Iterable<DomElement> {
963 private final Iterator<DomElement> iterator_;
964
965 /**
966 * Constructor.
967 * @param domNode the parent
968 */
969 protected ChildElementsIterable(final DomNode domNode) {
970 iterator_ = new ChildElementsIterator(domNode);
971 }
972
973 @Override
974 public Iterator<DomElement> iterator() {
975 return iterator_;
976 }
977 }
978
979 /**
980 * An iterator over the DomElement children.
981 */
982 protected static class ChildElementsIterator implements Iterator<DomElement> {
983
984 private DomElement currentElement_;
985 private DomElement nextElement_;
986
987 /**
988 * Constructor.
989 * @param domNode the parent
990 */
991 protected ChildElementsIterator(final DomNode domNode) {
992 final DomNode child = domNode.getFirstChild();
993 if (child != null) {
994 if (child instanceof DomElement element) {
995 nextElement_ = element;
996 }
997 else {
998 setNextElement(child);
999 }
1000 }
1001 }
1002
1003 /**
1004 * {@inheritDoc}
1005 */
1006 @Override
1007 public boolean hasNext() {
1008 return nextElement_ != null;
1009 }
1010
1011 /**
1012 * {@inheritDoc}
1013 */
1014 @Override
1015 public DomElement next() {
1016 if (nextElement_ != null) {
1017 currentElement_ = nextElement_;
1018 setNextElement(nextElement_);
1019 return currentElement_;
1020 }
1021 throw new NoSuchElementException();
1022 }
1023
1024 /** Removes the current one. */
1025 @Override
1026 public void remove() {
1027 if (currentElement_ == null) {
1028 throw new IllegalStateException();
1029 }
1030 currentElement_.remove();
1031 currentElement_ = null;
1032 }
1033
1034 private void setNextElement(final DomNode node) {
1035 DomNode next = node.getNextSibling();
1036 while (next != null && !(next instanceof DomElement)) {
1037 next = next.getNextSibling();
1038 }
1039 nextElement_ = (DomElement) next;
1040 }
1041 }
1042
1043 /**
1044 * Returns a string representation of this element.
1045 * @return a string representation of this element
1046 */
1047 @Override
1048 public String toString() {
1049 final StringWriter writer = new StringWriter();
1050 final PrintWriter printWriter = new PrintWriter(writer);
1051
1052 printWriter.print(getClass().getSimpleName());
1053 printWriter.print("[<");
1054 printOpeningTagContentAsXml(printWriter);
1055 printWriter.print(">]");
1056 printWriter.flush();
1057 return writer.toString();
1058 }
1059
1060 /**
1061 * Simulates clicking on this element, returning the page in the window that has the focus
1062 * after the element has been clicked. Note that the returned page may or may not be the same
1063 * as the original page, depending on the type of element being clicked, the presence of JavaScript
1064 * action listeners, etc.<br>
1065 * This only clicks the element if it is visible and enabled (isDisplayed() & !isDisabled()).
1066 * In case the element is not visible and/or disabled, only a log output is generated.
1067 * <br>
1068 * If you circumvent the visible/disabled check use click(shiftKey, ctrlKey, altKey, true, true, false)
1069 *
1070 * @param <P> the page type
1071 * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
1072 * @throws IOException if an IO error occurs
1073 */
1074 public <P extends Page> P click() throws IOException {
1075 return click(false, false, false);
1076 }
1077
1078 /**
1079 * Simulates clicking on this element, returning the page in the window that has the focus
1080 * after the element has been clicked. Note that the returned page may or may not be the same
1081 * as the original page, depending on the type of element being clicked, the presence of JavaScript
1082 * action listeners, etc.<br>
1083 * This only clicks the element if it is visible and enabled (isDisplayed() & !isDisabled()).
1084 * In case the element is not visible and/or disabled, only a log output is generated.
1085 * <br>
1086 * If you circumvent the visible/disabled check use click(shiftKey, ctrlKey, altKey, true, true, false)
1087 *
1088 * @param shiftKey {@code true} if SHIFT is pressed during the click
1089 * @param ctrlKey {@code true} if CTRL is pressed during the click
1090 * @param altKey {@code true} if ALT is pressed during the click
1091 * @param <P> the page type
1092 * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
1093 * @throws IOException if an IO error occurs
1094 */
1095 public <P extends Page> P click(final boolean shiftKey, final boolean ctrlKey, final boolean altKey)
1096 throws IOException {
1097
1098 return click(shiftKey, ctrlKey, altKey, true);
1099 }
1100
1101 /**
1102 * Simulates clicking on this element, returning the page in the window that has the focus
1103 * after the element has been clicked. Note that the returned page may or may not be the same
1104 * as the original page, depending on the type of element being clicked, the presence of JavaScript
1105 * action listeners, etc.<br>
1106 * This only clicks the element if it is visible and enabled (isDisplayed() & !isDisabled()).
1107 * In case the element is not visible and/or disabled, only a log output is generated.
1108 * <br>
1109 * If you circumvent the visible/disabled check use click(shiftKey, ctrlKey, altKey, true, true, false)
1110 *
1111 * @param shiftKey {@code true} if SHIFT is pressed during the click
1112 * @param ctrlKey {@code true} if CTRL is pressed during the click
1113 * @param altKey {@code true} if ALT is pressed during the click
1114 * @param triggerMouseEvents if true trigger the mouse events also
1115 * @param <P> the page type
1116 * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
1117 * @throws IOException if an IO error occurs
1118 */
1119 public <P extends Page> P click(final boolean shiftKey, final boolean ctrlKey, final boolean altKey,
1120 final boolean triggerMouseEvents) throws IOException {
1121 return click(shiftKey, ctrlKey, altKey, triggerMouseEvents, true, false, false);
1122 }
1123
1124 /**
1125 * Returns true if this is an {@link DisabledElement} and disabled.
1126 *
1127 * @return true if this is an {@link DisabledElement} and disabled
1128 */
1129 protected boolean isDisabledElementAndDisabled() {
1130 return this instanceof DisabledElement de && de.isDisabled();
1131 }
1132
1133 /**
1134 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1135 *
1136 * Simulates clicking on this element, returning the page in the window that has the focus
1137 * after the element has been clicked. Note that the returned page may or may not be the same
1138 * as the original page, depending on the type of element being clicked, the presence of JavaScript
1139 * action listeners, etc.
1140 *
1141 * @param shiftKey {@code true} if SHIFT is pressed during the click
1142 * @param ctrlKey {@code true} if CTRL is pressed during the click
1143 * @param altKey {@code true} if ALT is pressed during the click
1144 * @param triggerMouseEvents if true trigger the mouse events also
1145 * @param handleFocus if true set the focus (and trigger the event)
1146 * @param ignoreVisibility whether to ignore visibility or not
1147 * @param disableProcessLabelAfterBubbling ignore label processing
1148 * @param <P> the page type
1149 * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
1150 * @throws IOException if an IO error occurs
1151 */
1152 @SuppressWarnings("unchecked")
1153 public <P extends Page> P click(final boolean shiftKey, final boolean ctrlKey, final boolean altKey,
1154 final boolean triggerMouseEvents, final boolean handleFocus, final boolean ignoreVisibility,
1155 final boolean disableProcessLabelAfterBubbling) throws IOException {
1156
1157 // make enclosing window the current one
1158 final SgmlPage page = getPage();
1159 final WebClient webClient = page.getWebClient();
1160 webClient.setCurrentWindow(page.getEnclosingWindow());
1161
1162 if (!ignoreVisibility) {
1163 if (!(page instanceof HtmlPage)) {
1164 return (P) page;
1165 }
1166
1167 if (!isDisplayed()) {
1168 if (LOG.isWarnEnabled()) {
1169 LOG.warn("Calling click() ignored because the target element '" + this
1170 + "' is not displayed.");
1171 }
1172 return (P) page;
1173 }
1174
1175 if (isDisabledElementAndDisabled()) {
1176 if (LOG.isWarnEnabled()) {
1177 LOG.warn("Calling click() ignored because the target element '" + this + "' is disabled.");
1178 }
1179 return (P) page;
1180 }
1181 }
1182
1183 synchronized (page) {
1184 if (triggerMouseEvents) {
1185 mouseDown(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
1186 }
1187
1188 final AbstractJavaScriptEngine<?> jsEngine = webClient.getJavaScriptEngine();
1189 if (webClient.isJavaScriptEnabled()) {
1190 jsEngine.holdPosponedActions();
1191 }
1192 try {
1193 if (handleFocus) {
1194 // give focus to current element (if possible) or only remove it from previous one
1195 DomElement elementToFocus = null;
1196 if (this instanceof SubmittableElement
1197 || this instanceof HtmlAnchor anchor
1198 && ATTRIBUTE_NOT_DEFINED != anchor.getHrefAttribute()
1199 || this instanceof HtmlArea area
1200 && (ATTRIBUTE_NOT_DEFINED != area.getHrefAttribute()
1201 || webClient.getBrowserVersion().hasFeature(JS_AREA_WITHOUT_HREF_FOCUSABLE))
1202 || this instanceof HtmlElement && ((HtmlElement) this).getTabIndex() != null) {
1203 elementToFocus = this;
1204 }
1205 else if (this instanceof HtmlOption option) {
1206 elementToFocus = option.getEnclosingSelect();
1207 }
1208
1209 if (elementToFocus == null) {
1210 ((HtmlPage) page).setFocusedElement(null);
1211 }
1212 else {
1213 elementToFocus.focus();
1214 }
1215 }
1216
1217 if (triggerMouseEvents) {
1218 mouseUp(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
1219 }
1220
1221 MouseEvent event = null;
1222 if (webClient.isJavaScriptEnabled()) {
1223 event = new PointerEvent(getEventTargetElement(), MouseEvent.TYPE_CLICK, shiftKey,
1224 ctrlKey, altKey, MouseEvent.BUTTON_LEFT, 1);
1225
1226 if (disableProcessLabelAfterBubbling) {
1227 event.disableProcessLabelAfterBubbling();
1228 }
1229 }
1230 click(event, shiftKey, ctrlKey, altKey, ignoreVisibility);
1231 }
1232 finally {
1233 if (webClient.isJavaScriptEnabled()) {
1234 jsEngine.processPostponedActions();
1235 }
1236 }
1237
1238 return (P) webClient.getCurrentWindow().getEnclosedPage();
1239 }
1240 }
1241
1242 /**
1243 * Returns the event target element. This could be overridden by subclasses to have other targets.
1244 * The default implementation returns 'this'.
1245 * @return the event target element.
1246 */
1247 protected DomNode getEventTargetElement() {
1248 return this;
1249 }
1250
1251 /**
1252 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1253 *
1254 * Simulates clicking on this element, returning the page in the window that has the focus
1255 * after the element has been clicked. Note that the returned page may or may not be the same
1256 * as the original page, depending on the type of element being clicked, the presence of JavaScript
1257 * action listeners, etc.
1258 *
1259 * @param event the click event used
1260 * @param shiftKey {@code true} if SHIFT is pressed during the click
1261 * @param ctrlKey {@code true} if CTRL is pressed during the click
1262 * @param altKey {@code true} if ALT is pressed during the click
1263 * @param ignoreVisibility whether to ignore visibility or not
1264 * @param <P> the page type
1265 * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
1266 * @throws IOException if an IO error occurs
1267 */
1268 @SuppressWarnings("unchecked")
1269 public <P extends Page> P click(final Event event,
1270 final boolean shiftKey, final boolean ctrlKey, final boolean altKey,
1271 final boolean ignoreVisibility) throws IOException {
1272 final SgmlPage page = getPage();
1273
1274 if ((!ignoreVisibility && !isDisplayed()) || isDisabledElementAndDisabled()) {
1275 return (P) page;
1276 }
1277
1278 final WebClient webClient = page.getWebClient();
1279 if (!webClient.isJavaScriptEnabled()) {
1280 doClickStateUpdate(shiftKey, ctrlKey);
1281
1282 webClient.loadDownloadedResponses();
1283 return (P) getPage().getWebClient().getCurrentWindow().getEnclosedPage();
1284 }
1285
1286 // may be different from page when working with "orphaned pages"
1287 // (ex: clicking a link in a page that is not active anymore)
1288 final Page contentPage = page.getEnclosingWindow().getEnclosedPage();
1289
1290 boolean stateUpdated = false;
1291 boolean changed = false;
1292 if (isStateUpdateFirst()) {
1293 changed = doClickStateUpdate(shiftKey, ctrlKey);
1294 stateUpdated = true;
1295 }
1296
1297 final ScriptResult scriptResult = doClickFireClickEvent(event);
1298 final boolean eventIsAborted = event.isAborted(scriptResult);
1299
1300 final boolean pageAlreadyChanged = contentPage != page.getEnclosingWindow().getEnclosedPage();
1301 if (!pageAlreadyChanged && !stateUpdated && !eventIsAborted) {
1302 changed = doClickStateUpdate(shiftKey, ctrlKey);
1303 }
1304
1305 if (changed) {
1306 doClickFireChangeEvent();
1307 }
1308
1309 webClient.loadDownloadedResponses();
1310 return (P) getPage().getWebClient().getCurrentWindow().getEnclosedPage();
1311 }
1312
1313 /**
1314 * This method implements the control state update part of the click action.
1315 *
1316 * <p>The default implementation only calls doClickStateUpdate on parent's DomElement (if any).
1317 * Subclasses requiring different behavior (like {@link HtmlSubmitInput}) will override this method.</p>
1318 * @param shiftKey {@code true} if SHIFT is pressed
1319 * @param ctrlKey {@code true} if CTRL is pressed
1320 *
1321 * @return true if doClickFireEvent method has to be called later on (to signal,
1322 * that the value was changed)
1323 * @throws IOException if an IO error occurs
1324 */
1325 protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException {
1326 if (propagateClickStateUpdateToParent()) {
1327 // needed for instance to perform link doClickAction when a nested element is clicked
1328 // it should probably be changed to do this at the event level but currently
1329 // this wouldn't work with JS disabled as events are propagated in the host object tree.
1330 final DomNode parent = getParentNode();
1331 if (parent instanceof DomElement element) {
1332 return element.doClickStateUpdate(false, false);
1333 }
1334 }
1335
1336 return false;
1337 }
1338
1339 /**
1340 * Usually the click is propagated to the parent. Overwrite if you like to disable this.
1341 * @return true or false
1342 * @see #doClickStateUpdate(boolean, boolean)
1343 */
1344 protected boolean propagateClickStateUpdateToParent() {
1345 return true;
1346 }
1347
1348 /**
1349 * This method implements the control onchange handler call during the click action.
1350 */
1351 protected void doClickFireChangeEvent() {
1352 // nothing to do, in the default case
1353 }
1354
1355 /**
1356 * This method implements the control onclick handler call during the click action.
1357 * @param event the click event used
1358 * @return the script result
1359 */
1360 protected ScriptResult doClickFireClickEvent(final Event event) {
1361 return fireEvent(event);
1362 }
1363
1364 /**
1365 * Simulates double-clicking on this element, returning the page in the window that has the focus
1366 * after the element has been clicked. Note that the returned page may or may not be the same
1367 * as the original page, depending on the type of element being clicked, the presence of JavaScript
1368 * action listeners, etc. Note also that {@link #click()} is automatically called first.
1369 *
1370 * @param <P> the page type
1371 * @return the page that occupies this element's window after the element has been double-clicked
1372 * @throws IOException if an IO error occurs
1373 */
1374 public <P extends Page> P dblClick() throws IOException {
1375 return dblClick(false, false, false);
1376 }
1377
1378 /**
1379 * Simulates double-clicking on this element, returning the page in the window that has the focus
1380 * after the element has been clicked. Note that the returned page may or may not be the same
1381 * as the original page, depending on the type of element being clicked, the presence of JavaScript
1382 * action listeners, etc. Note also that {@link #click(boolean, boolean, boolean)} is automatically
1383 * called first.
1384 *
1385 * @param shiftKey {@code true} if SHIFT is pressed during the double click
1386 * @param ctrlKey {@code true} if CTRL is pressed during the double click
1387 * @param altKey {@code true} if ALT is pressed during the double click
1388 * @param <P> the page type
1389 * @return the page that occupies this element's window after the element has been double-clicked
1390 * @throws IOException if an IO error occurs
1391 */
1392 @SuppressWarnings("unchecked")
1393 public <P extends Page> P dblClick(final boolean shiftKey, final boolean ctrlKey, final boolean altKey)
1394 throws IOException {
1395 if (isDisabledElementAndDisabled()) {
1396 return (P) getPage();
1397 }
1398
1399 // call click event first
1400 P clickPage = click(shiftKey, ctrlKey, altKey);
1401 if (clickPage != getPage()) {
1402 LOG.debug("dblClick() is ignored, as click() loaded a different page.");
1403 return clickPage;
1404 }
1405
1406 // call click event a second time
1407 clickPage = click(shiftKey, ctrlKey, altKey);
1408 if (clickPage != getPage()) {
1409 LOG.debug("dblClick() is ignored, as click() loaded a different page.");
1410 return clickPage;
1411 }
1412
1413 final Event event;
1414 event = new MouseEvent(this, MouseEvent.TYPE_DBL_CLICK, shiftKey, ctrlKey, altKey,
1415 MouseEvent.BUTTON_LEFT, 2);
1416
1417 final ScriptResult scriptResult = fireEvent(event);
1418 if (scriptResult == null) {
1419 return clickPage;
1420 }
1421 return (P) getPage().getWebClient().getCurrentWindow().getEnclosedPage();
1422 }
1423
1424 /**
1425 * Simulates moving the mouse over this element, returning the page which this element's window contains
1426 * after the mouse move. The returned page may or may not be the same as the original page, depending
1427 * on JavaScript event handlers, etc.
1428 *
1429 * @return the page which this element's window contains after the mouse move
1430 */
1431 public Page mouseOver() {
1432 return mouseOver(false, false, false, MouseEvent.BUTTON_LEFT);
1433 }
1434
1435 /**
1436 * Simulates moving the mouse over this element, returning the page which this element's window contains
1437 * after the mouse move. The returned page may or may not be the same as the original page, depending
1438 * on JavaScript event handlers, etc.
1439 *
1440 * @param shiftKey {@code true} if SHIFT is pressed during the mouse move
1441 * @param ctrlKey {@code true} if CTRL is pressed during the mouse move
1442 * @param altKey {@code true} if ALT is pressed during the mouse move
1443 * @param button the button code, must be {@link MouseEvent#BUTTON_LEFT}, {@link MouseEvent#BUTTON_MIDDLE}
1444 * or {@link MouseEvent#BUTTON_RIGHT}
1445 * @return the page which this element's window contains after the mouse move
1446 */
1447 public Page mouseOver(final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final int button) {
1448 return doMouseEvent(MouseEvent.TYPE_MOUSE_OVER, shiftKey, ctrlKey, altKey, button);
1449 }
1450
1451 /**
1452 * Simulates moving the mouse over this element, returning the page which this element's window contains
1453 * after the mouse move. The returned page may or may not be the same as the original page, depending
1454 * on JavaScript event handlers, etc.
1455 *
1456 * @return the page which this element's window contains after the mouse move
1457 */
1458 public Page mouseMove() {
1459 return mouseMove(false, false, false, MouseEvent.BUTTON_LEFT);
1460 }
1461
1462 /**
1463 * Simulates moving the mouse over this element, returning the page which this element's window contains
1464 * after the mouse move. The returned page may or may not be the same as the original page, depending
1465 * on JavaScript event handlers, etc.
1466 *
1467 * @param shiftKey {@code true} if SHIFT is pressed during the mouse move
1468 * @param ctrlKey {@code true} if CTRL is pressed during the mouse move
1469 * @param altKey {@code true} if ALT is pressed during the mouse move
1470 * @param button the button code, must be {@link MouseEvent#BUTTON_LEFT}, {@link MouseEvent#BUTTON_MIDDLE}
1471 * or {@link MouseEvent#BUTTON_RIGHT}
1472 * @return the page which this element's window contains after the mouse move
1473 */
1474 public Page mouseMove(final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final int button) {
1475 return doMouseEvent(MouseEvent.TYPE_MOUSE_MOVE, shiftKey, ctrlKey, altKey, button);
1476 }
1477
1478 /**
1479 * Simulates moving the mouse out of this element, returning the page which this element's window contains
1480 * after the mouse move. The returned page may or may not be the same as the original page, depending
1481 * on JavaScript event handlers, etc.
1482 *
1483 * @return the page which this element's window contains after the mouse move
1484 */
1485 public Page mouseOut() {
1486 return mouseOut(false, false, false, MouseEvent.BUTTON_LEFT);
1487 }
1488
1489 /**
1490 * Simulates moving the mouse out of this element, returning the page which this element's window contains
1491 * after the mouse move. The returned page may or may not be the same as the original page, depending
1492 * on JavaScript event handlers, etc.
1493 *
1494 * @param shiftKey {@code true} if SHIFT is pressed during the mouse move
1495 * @param ctrlKey {@code true} if CTRL is pressed during the mouse move
1496 * @param altKey {@code true} if ALT is pressed during the mouse move
1497 * @param button the button code, must be {@link MouseEvent#BUTTON_LEFT}, {@link MouseEvent#BUTTON_MIDDLE}
1498 * or {@link MouseEvent#BUTTON_RIGHT}
1499 * @return the page which this element's window contains after the mouse move
1500 */
1501 public Page mouseOut(final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final int button) {
1502 return doMouseEvent(MouseEvent.TYPE_MOUSE_OUT, shiftKey, ctrlKey, altKey, button);
1503 }
1504
1505 /**
1506 * Simulates clicking the mouse on this element, returning the page which this element's window contains
1507 * after the mouse click. The returned page may or may not be the same as the original page, depending
1508 * on JavaScript event handlers, etc.
1509 *
1510 * @return the page which this element's window contains after the mouse click
1511 */
1512 public Page mouseDown() {
1513 return mouseDown(false, false, false, MouseEvent.BUTTON_LEFT);
1514 }
1515
1516 /**
1517 * Simulates clicking the mouse on this element, returning the page which this element's window contains
1518 * after the mouse click. The returned page may or may not be the same as the original page, depending
1519 * on JavaScript event handlers, etc.
1520 *
1521 * @param shiftKey {@code true} if SHIFT is pressed during the mouse click
1522 * @param ctrlKey {@code true} if CTRL is pressed during the mouse click
1523 * @param altKey {@code true} if ALT is pressed during the mouse click
1524 * @param button the button code, must be {@link MouseEvent#BUTTON_LEFT}, {@link MouseEvent#BUTTON_MIDDLE}
1525 * or {@link MouseEvent#BUTTON_RIGHT}
1526 * @return the page which this element's window contains after the mouse click
1527 */
1528 public Page mouseDown(final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final int button) {
1529 return doMouseEvent(MouseEvent.TYPE_MOUSE_DOWN, shiftKey, ctrlKey, altKey, button);
1530 }
1531
1532 /**
1533 * Simulates releasing the mouse click on this element, returning the page which this element's window contains
1534 * after the mouse click release. The returned page may or may not be the same as the original page, depending
1535 * on JavaScript event handlers, etc.
1536 *
1537 * @return the page which this element's window contains after the mouse click release
1538 */
1539 public Page mouseUp() {
1540 return mouseUp(false, false, false, MouseEvent.BUTTON_LEFT);
1541 }
1542
1543 /**
1544 * Simulates releasing the mouse click on this element, returning the page which this element's window contains
1545 * after the mouse click release. The returned page may or may not be the same as the original page, depending
1546 * on JavaScript event handlers, etc.
1547 *
1548 * @param shiftKey {@code true} if SHIFT is pressed during the mouse click release
1549 * @param ctrlKey {@code true} if CTRL is pressed during the mouse click release
1550 * @param altKey {@code true} if ALT is pressed during the mouse click release
1551 * @param button the button code, must be {@link MouseEvent#BUTTON_LEFT}, {@link MouseEvent#BUTTON_MIDDLE}
1552 * or {@link MouseEvent#BUTTON_RIGHT}
1553 * @return the page which this element's window contains after the mouse click release
1554 */
1555 public Page mouseUp(final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final int button) {
1556 return doMouseEvent(MouseEvent.TYPE_MOUSE_UP, shiftKey, ctrlKey, altKey, button);
1557 }
1558
1559 /**
1560 * Simulates right clicking the mouse on this element, returning the page which this element's window
1561 * contains after the mouse click. The returned page may or may not be the same as the original page,
1562 * depending on JavaScript event handlers, etc.
1563 *
1564 * @return the page which this element's window contains after the mouse click
1565 */
1566 public Page rightClick() {
1567 return rightClick(false, false, false);
1568 }
1569
1570 /**
1571 * Simulates right clicking the mouse on this element, returning the page which this element's window
1572 * contains after the mouse click. The returned page may or may not be the same as the original page,
1573 * depending on JavaScript event handlers, etc.
1574 *
1575 * @param shiftKey {@code true} if SHIFT is pressed during the mouse click
1576 * @param ctrlKey {@code true} if CTRL is pressed during the mouse click
1577 * @param altKey {@code true} if ALT is pressed during the mouse click
1578 * @return the page which this element's window contains after the mouse click
1579 */
1580 public Page rightClick(final boolean shiftKey, final boolean ctrlKey, final boolean altKey) {
1581 final Page mouseDownPage = mouseDown(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_RIGHT);
1582 if (mouseDownPage != getPage()) {
1583 LOG.debug("rightClick() is incomplete, as mouseDown() loaded a different page.");
1584 return mouseDownPage;
1585 }
1586
1587 final Page mouseUpPage = mouseUp(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_RIGHT);
1588 if (mouseUpPage != getPage()) {
1589 LOG.debug("rightClick() is incomplete, as mouseUp() loaded a different page.");
1590 return mouseUpPage;
1591 }
1592
1593 return doMouseEvent(MouseEvent.TYPE_CONTEXT_MENU, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_RIGHT);
1594 }
1595
1596 /**
1597 * Simulates the specified mouse event, returning the page which this element's window contains after the event.
1598 * The returned page may or may not be the same as the original page, depending on JavaScript event handlers, etc.
1599 *
1600 * @param eventType the mouse event type to simulate
1601 * @param shiftKey {@code true} if SHIFT is pressed during the mouse event
1602 * @param ctrlKey {@code true} if CTRL is pressed during the mouse event
1603 * @param altKey {@code true} if ALT is pressed during the mouse event
1604 * @param button the button code, must be {@link MouseEvent#BUTTON_LEFT}, {@link MouseEvent#BUTTON_MIDDLE}
1605 * or {@link MouseEvent#BUTTON_RIGHT}
1606 * @return the page which this element's window contains after the event
1607 */
1608 private Page doMouseEvent(final String eventType, final boolean shiftKey, final boolean ctrlKey,
1609 final boolean altKey, final int button) {
1610 final SgmlPage page = getPage();
1611 final WebClient webClient = getPage().getWebClient();
1612 if (!webClient.isJavaScriptEnabled()) {
1613 return page;
1614 }
1615
1616 final ScriptResult scriptResult;
1617 final Event event;
1618 if (MouseEvent.TYPE_CONTEXT_MENU.equals(eventType)) {
1619 final BrowserVersion browserVersion = webClient.getBrowserVersion();
1620 if (browserVersion.hasFeature(EVENT_CONTEXT_MENU_HAS_DETAIL_1)) {
1621 event = new PointerEvent(this, eventType, shiftKey, ctrlKey, altKey, button, 1);
1622 }
1623 else {
1624 event = new PointerEvent(this, eventType, shiftKey, ctrlKey, altKey, button, 0);
1625 }
1626 }
1627 else if (MouseEvent.TYPE_DBL_CLICK.equals(eventType)) {
1628 event = new MouseEvent(this, eventType, shiftKey, ctrlKey, altKey, button, 2);
1629 }
1630 else {
1631 event = new MouseEvent(this, eventType, shiftKey, ctrlKey, altKey, button, 1);
1632 }
1633 scriptResult = fireEvent(event);
1634
1635 final Page currentPage;
1636 if (scriptResult == null) {
1637 currentPage = page;
1638 }
1639 else {
1640 currentPage = webClient.getCurrentWindow().getEnclosedPage();
1641 }
1642
1643 final boolean mouseOver = !MouseEvent.TYPE_MOUSE_OUT.equals(eventType);
1644 if (mouseOver_ != mouseOver) {
1645 mouseOver_ = mouseOver;
1646
1647 page.clearComputedStyles();
1648 }
1649
1650 return currentPage;
1651 }
1652
1653 /**
1654 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1655 *
1656 * Shortcut for {@link #fireEvent(Event)}.
1657 * @param eventType the event type (like "load", "click")
1658 * @return the execution result, or {@code null} if nothing is executed
1659 */
1660 public ScriptResult fireEvent(final String eventType) {
1661 if (getPage().getWebClient().isJavaScriptEnabled()) {
1662 return fireEvent(new Event(this, eventType));
1663 }
1664 return null;
1665 }
1666
1667 /**
1668 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1669 *
1670 * Fires the event on the element. Nothing is done if JavaScript is disabled.
1671 * @param event the event to fire
1672 * @return the execution result, or {@code null} if nothing is executed
1673 */
1674 public ScriptResult fireEvent(final Event event) {
1675 final WebClient client = getPage().getWebClient();
1676 if (!client.isJavaScriptEnabled()) {
1677 return null;
1678 }
1679
1680 if (!handles(event)) {
1681 return null;
1682 }
1683
1684 if (LOG.isDebugEnabled()) {
1685 LOG.debug("Firing " + event);
1686 }
1687
1688 final EventTarget jsElt = getScriptableObject();
1689 final ScriptResult result = ((JavaScriptEngine) client.getJavaScriptEngine())
1690 .callSecured(cx -> jsElt.fireEvent(event), getHtmlPageOrNull());
1691 if (event.isAborted(result)) {
1692 preventDefault();
1693 }
1694 return result;
1695 }
1696
1697 /**
1698 * This method is called if the current fired event is canceled by <code>preventDefault()</code>.
1699 *
1700 * <p>The default implementation does nothing.</p>
1701 */
1702 protected void preventDefault() {
1703 // Empty by default; override as needed.
1704 }
1705
1706 /**
1707 * Sets the focus on this element.
1708 */
1709 public void focus() {
1710 if (!(this instanceof SubmittableElement
1711 || this instanceof HtmlAnchor anchor && ATTRIBUTE_NOT_DEFINED != anchor.getHrefAttribute()
1712 || this instanceof HtmlArea area
1713 && (ATTRIBUTE_NOT_DEFINED != area.getHrefAttribute()
1714 || getPage().getWebClient().getBrowserVersion().hasFeature(JS_AREA_WITHOUT_HREF_FOCUSABLE))
1715 || this instanceof HtmlElement && ((HtmlElement) this).getTabIndex() != null)) {
1716 return;
1717 }
1718
1719 if (!isDisplayed() || isDisabledElementAndDisabled()) {
1720 return;
1721 }
1722
1723 final HtmlPage page = (HtmlPage) getPage();
1724 page.setFocusedElement(this);
1725 }
1726
1727 /**
1728 * Removes focus from this element.
1729 */
1730 public void blur() {
1731 final HtmlPage page = (HtmlPage) getPage();
1732 if (page.getFocusedElement() != this) {
1733 return;
1734 }
1735
1736 page.setFocusedElement(null);
1737 }
1738
1739 /**
1740 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1741 *
1742 * Gets notified that it has lost the focus.
1743 */
1744 public void removeFocus() {
1745 // nothing
1746 }
1747
1748 /**
1749 * Returns {@code true} if state updates should be done before onclick event handling. This method
1750 * returns {@code false} by default, and is expected to be overridden to return {@code true} by
1751 * derived classes like {@link HtmlCheckBoxInput}.
1752 * @return {@code true} if state updates should be done before onclick event handling
1753 */
1754 protected boolean isStateUpdateFirst() {
1755 return false;
1756 }
1757
1758 /**
1759 * Returns whether the Mouse is currently over this element or not.
1760 * @return whether the Mouse is currently over this element or not
1761 */
1762 public boolean isMouseOver() {
1763 if (mouseOver_) {
1764 return true;
1765 }
1766 for (final DomElement child : getChildElements()) {
1767 if (child.isMouseOver()) {
1768 return true;
1769 }
1770 }
1771 return false;
1772 }
1773
1774 /**
1775 * Returns true if the element would be selected by the specified selector string; otherwise, returns false.
1776 * @param selectorString the selector to test
1777 * @param scope the context scope node used for evaluating relative selectors like :scope
1778 * @return true if the element would be selected by the specified selector string; otherwise, returns false.
1779 */
1780 public boolean matches(final String selectorString, final DomNode scope) {
1781 try {
1782 final WebClient webClient = getPage().getWebClient();
1783 final SelectorList selectorList = getSelectorList(selectorString, webClient);
1784
1785 if (selectorList != null) {
1786 for (final Selector selector : selectorList) {
1787 if (CssStyleSheet.selects(webClient.getBrowserVersion(), this, selector, null, scope, true, true)) {
1788 return true;
1789 }
1790 }
1791 }
1792 return false;
1793 }
1794 catch (final IOException e) {
1795 throw new CSSException("Error parsing CSS selectors from '" + selectorString + "': " + e.getMessage(), e);
1796 }
1797 }
1798
1799 /**
1800 * {@inheritDoc}
1801 */
1802 @Override
1803 public void setNodeValue(final String value) {
1804 // Default behavior is to do nothing, overridden in some subclasses
1805 }
1806
1807 /**
1808 * Callback method which allows different HTML element types to perform custom
1809 * initialization of computed styles. For example, body elements in most browsers
1810 * have default values for their margins.
1811 *
1812 * @param style the style to initialize
1813 */
1814 public void setDefaults(final ComputedCssStyleDeclaration style) {
1815 // Empty by default; override as necessary.
1816 }
1817
1818 /**
1819 * Replaces all child elements of this element with the supplied value parsed as html.
1820 * @param source the new value for the contents of this element
1821 * @throws SAXException in case of error
1822 * @throws IOException in case of error
1823 */
1824 public void setInnerHtml(final String source) throws SAXException, IOException {
1825 removeAllChildren();
1826 getPage().clearComputedStylesUpToRoot(this);
1827
1828 if (source != null) {
1829 parseHtmlSnippet(source);
1830 }
1831 }
1832 }
1833
1834 /**
1835 * The {@link NamedNodeMap} to store the node attributes.
1836 */
1837 class NamedAttrNodeMapImpl implements Map<String, DomAttr>, NamedNodeMap, Serializable {
1838 private final OrderedFastHashMap<String, DomAttr> map_;
1839 private final DomElement domNode_;
1840 private final boolean caseSensitive_;
1841
1842 NamedAttrNodeMapImpl(final DomElement domNode, final boolean caseSensitive) {
1843 super();
1844 if (domNode == null) {
1845 throw new IllegalArgumentException("Provided domNode can't be null.");
1846 }
1847 domNode_ = domNode;
1848 caseSensitive_ = caseSensitive;
1849 map_ = new OrderedFastHashMap<>(0);
1850 }
1851
1852 NamedAttrNodeMapImpl(final DomElement domNode, final boolean caseSensitive,
1853 final Map<String, DomAttr> attributes) {
1854 super();
1855 if (domNode == null) {
1856 throw new IllegalArgumentException("Provided domNode can't be null.");
1857 }
1858 domNode_ = domNode;
1859 caseSensitive_ = caseSensitive;
1860
1861 if (attributes instanceof OrderedFastHashMapWithLowercaseKeys) {
1862 // no need to rework the map at all, we are case sensitive, so
1863 // we keep all attributes, and we got the right map from outside too
1864 map_ = (OrderedFastHashMap) attributes;
1865 }
1866 else if (caseSensitive && attributes instanceof OrderedFastHashMap map) {
1867 // no need to rework the map at all, we are case sensitive, so
1868 // we keep all attributes, and we got the right map from outside too
1869 map_ = map;
1870 }
1871 else {
1872 // this is more expensive but atypical, so we don't have to care that much
1873 map_ = new OrderedFastHashMap<>(attributes.size());
1874 // this will create a new map with all case lowercased and
1875 putAll(attributes);
1876 }
1877 }
1878
1879 /**
1880 * {@inheritDoc}
1881 */
1882 @Override
1883 public int getLength() {
1884 return size();
1885 }
1886
1887 /**
1888 * {@inheritDoc}
1889 */
1890 @Override
1891 public DomAttr getNamedItem(final String name) {
1892 return get(name);
1893 }
1894
1895 private String fixName(final String name) {
1896 if (caseSensitive_) {
1897 return name;
1898 }
1899 return StringUtils.toRootLowerCase(name);
1900 }
1901
1902 /**
1903 * {@inheritDoc}
1904 */
1905 @Override
1906 public Node getNamedItemNS(final String namespaceURI, final String localName) {
1907 if (domNode_ == null) {
1908 return null;
1909 }
1910 return get(domNode_.getQualifiedName(namespaceURI, fixName(localName)));
1911 }
1912
1913 /**
1914 * {@inheritDoc}
1915 */
1916 @Override
1917 public Node item(final int index) {
1918 if (index < 0 || index >= map_.size()) {
1919 return null;
1920 }
1921 return map_.getValue(index);
1922 }
1923
1924 /**
1925 * {@inheritDoc}
1926 */
1927 @Override
1928 public Node removeNamedItem(final String name) throws DOMException {
1929 return remove(name);
1930 }
1931
1932 /**
1933 * {@inheritDoc}
1934 */
1935 @Override
1936 public Node removeNamedItemNS(final String namespaceURI, final String localName) {
1937 if (domNode_ == null) {
1938 return null;
1939 }
1940 return remove(domNode_.getQualifiedName(namespaceURI, fixName(localName)));
1941 }
1942
1943 /**
1944 * {@inheritDoc}
1945 */
1946 @Override
1947 public DomAttr setNamedItem(final Node node) {
1948 return put(node.getLocalName(), (DomAttr) node);
1949 }
1950
1951 /**
1952 * {@inheritDoc}
1953 */
1954 @Override
1955 public Node setNamedItemNS(final Node node) throws DOMException {
1956 return put(node.getNodeName(), (DomAttr) node);
1957 }
1958
1959 /**
1960 * {@inheritDoc}
1961 */
1962 @Override
1963 public DomAttr put(final String key, final DomAttr value) {
1964 final String name = fixName(key);
1965 return map_.put(name, value);
1966 }
1967
1968 /**
1969 * {@inheritDoc}
1970 */
1971 @Override
1972 public DomAttr remove(final Object key) {
1973 if (key instanceof String string) {
1974 return map_.remove(fixName(string));
1975 }
1976 return null;
1977 }
1978
1979 /**
1980 * {@inheritDoc}
1981 */
1982 @Override
1983 public void clear() {
1984 map_.clear();
1985 }
1986
1987 /**
1988 * {@inheritDoc}
1989 */
1990 @Override
1991 public void putAll(final Map<? extends String, ? extends DomAttr> t) {
1992 // add one after the other to save the positions
1993 for (final Map.Entry<? extends String, ? extends DomAttr> entry : t.entrySet()) {
1994 put(entry.getKey(), entry.getValue());
1995 }
1996 }
1997
1998 /**
1999 * {@inheritDoc}
2000 */
2001 @Override
2002 public boolean containsKey(final Object key) {
2003 if (key instanceof String string) {
2004 final String name = fixName(string);
2005 return map_.containsKey(name);
2006 }
2007 return false;
2008 }
2009
2010 /**
2011 * {@inheritDoc}
2012 */
2013 @Override
2014 public DomAttr get(final Object key) {
2015 if (key instanceof String string) {
2016 return map_.get(fixName(string));
2017 }
2018 return null;
2019 }
2020
2021 /**
2022 * Fast access.
2023 * @param key the key
2024 */
2025 protected DomAttr getDirect(final String key) {
2026 return map_.get(key);
2027 }
2028
2029 /**
2030 * {@inheritDoc}
2031 */
2032 @Override
2033 public boolean containsValue(final Object value) {
2034 return map_.containsValue(value);
2035 }
2036
2037 /**
2038 * {@inheritDoc}
2039 */
2040 @Override
2041 public Set<Map.Entry<String, DomAttr>> entrySet() {
2042 return map_.entrySet();
2043 }
2044
2045 /**
2046 * {@inheritDoc}
2047 */
2048 @Override
2049 public boolean isEmpty() {
2050 return map_.isEmpty();
2051 }
2052
2053 /**
2054 * {@inheritDoc}
2055 */
2056 @Override
2057 public Set<String> keySet() {
2058 return map_.keySet();
2059 }
2060
2061 /**
2062 * {@inheritDoc}
2063 */
2064 @Override
2065 public int size() {
2066 return map_.size();
2067 }
2068
2069 /**
2070 * {@inheritDoc}
2071 */
2072 @Override
2073 public Collection<DomAttr> values() {
2074 return map_.values();
2075 }
2076 }