All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gargoylesoftware.htmlunit.javascript.NamedNodeMap Maven / Gradle / Ivy

Go to download

XLT (Xceptance LoadTest) is an extensive load and performance test tool developed and maintained by Xceptance.

There is a newer version: 8.1.0
Show newest version
/*
 * Copyright (c) 2002-2021 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 com.gargoylesoftware.htmlunit.javascript;

import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF78;

import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
import com.gargoylesoftware.htmlunit.javascript.host.dom.Node;

import net.sourceforge.htmlunit.corejs.javascript.Scriptable;

/**
 * A collection of nodes that can be accessed by name. String comparisons in this class are case-insensitive when
 * used with an {@link com.gargoylesoftware.htmlunit.html.HtmlElement},
 * but case-sensitive when used with a {@link com.gargoylesoftware.htmlunit.html.DomElement}.
 *
 * @author Daniel Gredler
 * @author Ahmed Ashour
 * @author Marc Guillemot
 * @author Ronald Brill
 * @author Frank Danek
 * @see DOM Level 2 Core Spec
 * @see IXMLDOMNamedNodeMap
 */
@JsxClass
public class NamedNodeMap extends SimpleScriptable {

    private final org.w3c.dom.NamedNodeMap attributes_;

    /**
     * We need default constructors to build the prototype instance.
     */
    @JsxConstructor({CHROME, EDGE, FF, FF78})
    public NamedNodeMap() {
        attributes_ = null;
    }

    /**
     * Creates a new named node map for the specified element.
     *
     * @param element the owning element
     */
    public NamedNodeMap(final DomElement element) {
        setParentScope(element.getScriptableObject());
        setPrototype(getPrototype(getClass()));

        attributes_ = element.getAttributes();
        setDomNode(element, false);
    }

    /**
     * Returns the element at the specified index, or {@link #NOT_FOUND} if the index is invalid.
     *
     * {@inheritDoc}
     */
    @Override
    public final Object get(final int index, final Scriptable start) {
        final NamedNodeMap startMap = (NamedNodeMap) start;
        final Object response = startMap.item(index);
        if (response != null) {
            return response;
        }
        return NOT_FOUND;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object get(final String name, final Scriptable start) {
        Object response = super.get(name, start);
        if (response != NOT_FOUND) {
            return response;
        }

        response = getNamedItem(name);
        if (response != null) {
            return response;
        }

        return NOT_FOUND;
    }

    /**
     * INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
* * Gets the specified attribute but does not handle the synthetic class attribute for IE. * @see #getNamedItem(String) * * @param name attribute name * @return the attribute node, {@code null} if the attribute is not defined */ public Object getNamedItemWithoutSytheticClassAttr(final String name) { if (attributes_ != null) { final DomNode attr = (DomNode) attributes_.getNamedItem(name); if (attr != null) { return attr.getScriptableObject(); } } return null; } /** * Gets the specified attribute. * @param name attribute name * @return the attribute node, {@code null} if the attribute is not defined */ @JsxFunction public Object getNamedItem(final String name) { final Object attr = getNamedItemWithoutSytheticClassAttr(name); if (null != attr) { return attr; } return null; } /** * Sets the specified attribute. * @param node the attribute */ @JsxFunction public void setNamedItem(final Node node) { attributes_.setNamedItem(node.getDomNodeOrDie()); } /** * Removes the specified attribute. * @param name the name of the item to remove */ @JsxFunction public void removeNamedItem(final String name) { attributes_.removeNamedItem(name); } /** * Returns the item at the specified index. * @param index the index * @return the item at the specified index */ @JsxFunction public Object item(final int index) { final DomNode attr = (DomNode) attributes_.item(index); if (attr != null) { return attr.getScriptableObject(); } return null; } /** * Returns the number of attributes in this named node map. * @return the number of attributes in this named node map */ @JsxGetter public int getLength() { return attributes_.getLength(); } /** * {@inheritDoc} */ @Override public boolean has(final int index, final Scriptable start) { return index >= 0 && index < getLength(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy