
org.htmlunit.javascript.host.dom.NodeList 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.BrowserVersionFeatures.HTMLCOLLECTION_NULL_IF_NOT_FOUND;
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 java.util.ArrayList;
import java.util.List;
import org.htmlunit.WebClient;
import org.htmlunit.corejs.javascript.Callable;
import org.htmlunit.corejs.javascript.Context;
import org.htmlunit.corejs.javascript.ContextAction;
import org.htmlunit.corejs.javascript.ES6Iterator;
import org.htmlunit.corejs.javascript.Function;
import org.htmlunit.corejs.javascript.NativeArrayIterator;
import org.htmlunit.corejs.javascript.Scriptable;
import org.htmlunit.corejs.javascript.ScriptableObject;
import org.htmlunit.corejs.javascript.Undefined;
import org.htmlunit.html.DomNode;
import org.htmlunit.javascript.HtmlUnitContextFactory;
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.configuration.JsxSymbol;
/**
* An array of elements. Used for the element arrays returned by document.all
,
* document.all.tags('x')
, document.forms
, window.frames
, etc.
* Note that this class must not be used for collections that can be modified, for example
* map.areas
and select.options
.
*
* @author Daniel Gredler
* @author Marc Guillemot
* @author Chris Erskine
* @author Ahmed Ashour
* @author Frank Danek
* @author Ronald Brill
* @author Lai Quang Duong
*/
@JsxClass
public class NodeList extends AbstractList implements Callable {
/**
* Creates an instance.
*/
public NodeList() {
}
/**
* JavaScript constructor.
*/
@JsxConstructor({CHROME, EDGE, FF, FF_ESR})
public void jsConstructor() {
}
/**
* Creates an instance.
*
* @param domNode the {@link DomNode}
* @param attributeChangeSensitive indicates if the content of the collection may change when an attribute
* of a descendant node of parentScope changes (attribute added, modified or removed)
*/
public NodeList(final DomNode domNode, final boolean attributeChangeSensitive) {
super(domNode, attributeChangeSensitive, null);
}
/**
* Constructs an instance with an initial cache value.
* @param domNode the parent scope, on which we listen for changes
* @param initialElements the initial content for the cache
*/
public NodeList(final DomNode domNode, final List initialElements) {
super(domNode, true, new ArrayList<>(initialElements));
}
/**
* Creates an instance.
* @param parentScope the parent scope
*/
NodeList(final ScriptableObject parentScope) {
setParentScope(parentScope);
setPrototype(getPrototype(getClass()));
setExternalArrayData(this);
}
/**
* Gets a static NodeList.
*
* @param parentScope the parent scope
* @param elements the elements
* @return an empty collection
*/
public static NodeList staticNodeList(final HtmlUnitScriptable parentScope, final List elements) {
return new NodeList(parentScope) {
@Override
public List getElements() {
return elements;
}
};
}
/**
* Returns an Iterator allowing to go through all keys contained in this object.
* @return an {@link NativeArrayIterator}
*/
@JsxFunction({CHROME, EDGE, FF, FF_ESR})
public ES6Iterator keys() {
return new NativeArrayIterator(getParentScope(), this, NativeArrayIterator.ARRAY_ITERATOR_TYPE.KEYS);
}
/**
* Returns an Iterator allowing to go through all keys contained in this object.
* @return an {@link NativeArrayIterator}
*/
@JsxFunction({CHROME, EDGE, FF, FF_ESR})
@JsxSymbol(value = {CHROME, EDGE, FF, FF_ESR}, symbolName = "iterator")
public ES6Iterator values() {
return new NativeArrayIterator(getParentScope(), this, NativeArrayIterator.ARRAY_ITERATOR_TYPE.VALUES);
}
/**
* Returns an Iterator allowing to go through all key/value pairs contained in this object.
* @return an {@link NativeArrayIterator}
*/
@JsxFunction({CHROME, EDGE, FF, FF_ESR})
public ES6Iterator entries() {
return new NativeArrayIterator(getParentScope(), this, NativeArrayIterator.ARRAY_ITERATOR_TYPE.ENTRIES);
}
/**
* Calls the {@code callback} given in parameter once for each value pair in the list, in insertion order.
* @param callback function to execute for each element
*/
@JsxFunction({CHROME, EDGE, FF, FF_ESR})
public void forEach(final Object callback) {
final List nodes = getElements();
final WebClient client = getWindow().getWebWindow().getWebClient();
final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
final ContextAction
© 2015 - 2025 Weber Informatics LLC | Privacy Policy