
org.htmlunit.javascript.host.dom.DocumentFragment Maven / Gradle / Ivy
/*
* Copyright (c) 2002-2024 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.javascript.host.dom;
import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
import org.htmlunit.cssparser.parser.CSSException;
import org.htmlunit.html.DomDocumentFragment;
import org.htmlunit.html.DomNode;
import org.htmlunit.javascript.HtmlUnitScriptable;
import org.htmlunit.javascript.JavaScriptEngine;
import org.htmlunit.javascript.configuration.JsxClass;
import org.htmlunit.javascript.configuration.JsxConstructor;
import org.htmlunit.javascript.configuration.JsxFunction;
import org.htmlunit.javascript.configuration.JsxGetter;
import org.htmlunit.javascript.host.Element;
import org.htmlunit.javascript.host.html.HTMLCollection;
/**
* A JavaScript object for {@code DocumentFragment}.
*
* @author Ahmed Ashour
* @author Frank Danek
* @author Ronald Brill
*
* @see
* W3C Dom Level 1
*/
@JsxClass(domClass = DomDocumentFragment.class)
public class DocumentFragment extends Node {
/**
* Creates an instance.
*/
public DocumentFragment() {
}
/**
* JavaScript constructor.
*/
@Override
@JsxConstructor({CHROME, EDGE, FF, FF_ESR})
public void jsConstructor() {
super.jsConstructor();
}
/**
* Retrieves all element nodes from descendants of the starting element node that match any selector
* within the supplied selector strings.
* The NodeList object returned by the querySelectorAll() method must be static, not live.
* @param selectors the selectors
* @return the static node list
*/
@JsxFunction
public NodeList querySelectorAll(final String selectors) {
try {
return NodeList.staticNodeList(this, getDomNodeOrDie().querySelectorAll(selectors));
}
catch (final CSSException e) {
throw JavaScriptEngine.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
+ selectors + "' error: " + e.getMessage() + ").");
}
}
/**
* Returns the first element within the document that matches the specified group of selectors.
* @param selectors the selectors
* @return null if no matches are found; otherwise, it returns the first matching element
*/
@JsxFunction
public Node querySelector(final String selectors) {
try {
final DomNode node = getDomNodeOrDie().querySelector(selectors);
if (node != null) {
return node.getScriptableObject();
}
return null;
}
catch (final CSSException e) {
throw JavaScriptEngine.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
+ selectors + "' error: " + e.getMessage() + ").");
}
}
/**
* {@inheritDoc}
*/
@Override
public Object getDefaultValue(final Class> hint) {
if (String.class.equals(hint) || hint == null) {
return "[object " + getClassName() + "]";
}
return super.getDefaultValue(hint);
}
/**
* {@inheritDoc}
*/
@Override
@JsxGetter({CHROME, EDGE, FF, FF_ESR})
public int getChildElementCount() {
return super.getChildElementCount();
}
/**
* {@inheritDoc}
*/
@Override
@JsxGetter({CHROME, EDGE, FF, FF_ESR})
public Element getFirstElementChild() {
return super.getFirstElementChild();
}
/**
* {@inheritDoc}
*/
@Override
@JsxGetter({CHROME, EDGE, FF, FF_ESR})
public Element getLastElementChild() {
return super.getLastElementChild();
}
/**
* {@inheritDoc}
*/
@Override
@JsxGetter({CHROME, EDGE, FF, FF_ESR})
public HTMLCollection getChildren() {
return super.getChildren();
}
/**
* Returns the element with the specified ID, or {@code null} if that element could not be found.
* @param id the ID to search for
* @return the element, or {@code null} if it could not be found
*/
@JsxFunction({CHROME, EDGE, FF, FF_ESR})
public HtmlUnitScriptable getElementById(final Object id) {
if (id == null || JavaScriptEngine.isUndefined(id)) {
return null;
}
final String idString = JavaScriptEngine.toString(id);
if (idString == null || idString.length() == 0) {
return null;
}
for (final DomNode child : getDomNodeOrDie().getChildren()) {
final Element elem = child.getScriptableObject();
if (idString.equals(elem.getId())) {
return elem;
}
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public Object getRootNode() {
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy