Java Code Examples for com.gargoylesoftware.htmlunit.html.DomNode#getScriptableObject()

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#getScriptableObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Range.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves a collection of rectangles that describes the layout of the contents of an object
 * or range within the client. Each rectangle describes a single line.
 * @return a collection of rectangles that describes the layout of the contents
 */
@JsxFunction
public ClientRectList getClientRects() {
    final Window w = getWindow();
    final ClientRectList rectList = new ClientRectList();
    rectList.setParentScope(w);
    rectList.setPrototype(getPrototype(rectList.getClass()));

    // simple impl for now
    for (DomNode node : toW3C().containedNodes()) {
        final ScriptableObject scriptable = node.getScriptableObject();
        if (scriptable instanceof HTMLElement) {
            final ClientRect rect = new ClientRect(0, 0, 1, 1);
            rect.setParentScope(w);
            rect.setPrototype(getPrototype(rect.getClass()));
            rectList.add(rect);
        }
    }

    return rectList;
}
 
Example 2
Source File: AbstractList.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * 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)
 * @param initialElements the initial content for the cache
 */
private AbstractList(final DomNode domNode, final boolean attributeChangeSensitive,
        final List<DomNode> initialElements) {
    if (domNode != null) {
        setDomNode(domNode, false);
        final ScriptableObject parentScope = domNode.getScriptableObject();
        if (parentScope != null) {
            setParentScope(parentScope);
            setPrototype(getPrototype(getClass()));
        }
    }
    attributeChangeSensitive_ = attributeChangeSensitive;
    cachedElements_ = initialElements;
    if (initialElements != null) {
        registerListener();
    }
}
 
Example 3
Source File: NamedNodeMap.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: SimpleScriptable.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the JavaScript object that corresponds to the specified object.
 * New JavaScript objects will be created as needed. If a JavaScript object
 * cannot be created for a domNode then NOT_FOUND will be returned.
 *
 * @param object a {@link DomNode} or a {@link WebWindow}
 * @return the JavaScript object or NOT_FOUND
 */
protected SimpleScriptable getScriptableFor(final Object object) {
    if (object instanceof WebWindow) {
        return (SimpleScriptable) ((WebWindow) object).getScriptableObject();
    }

    final DomNode domNode = (DomNode) object;

    final Object scriptObject = domNode.getScriptableObject();
    if (scriptObject != null) {
        return (SimpleScriptable) scriptObject;
    }
    return makeScriptableFor(domNode);
}
 
Example 5
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 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) node.getScriptableObject();
        }
        return null;
    }
    catch (final CSSException e) {
        throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
                + selectors + "' error: " + e.getMessage() + ").");
    }
}
 
Example 6
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object get(final String name, final Scriptable start) {
    final Object response = super.get(name, start);

    // IE support .querySelector(All) but not in quirks mode
    // => TODO: find a better way to handle this!
    if (response instanceof FunctionObject
        && ("querySelectorAll".equals(name) || "querySelector".equals(name))
        && getBrowserVersion().hasFeature(QUERYSELECTORALL_NOT_IN_QUIRKS)) {
        Document document = null;
        if (start instanceof DocumentProxy) {
            // if in prototype no domNode is set -> use start
            document = ((DocumentProxy) start).getDelegee();
        }
        else if (start instanceof HTMLDocument) {
            final DomNode page = ((HTMLDocument) start).getDomNodeOrNull();
            if (page != null) {
                document = (Document) page.getScriptableObject();
            }
        }
        if (document instanceof HTMLDocument && ((HTMLDocument) document).getDocumentMode() < 8) {
            return NOT_FOUND;
        }
    }

    return response;
}
 
Example 7
Source File: DocumentFragment.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * 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) node.getScriptableObject();
        }
        return null;
    }
    catch (final CSSException e) {
        throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
                + selectors + "' error: " + e.getMessage() + ").");
    }
}
 
Example 8
Source File: DocumentFragment.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 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) node.getScriptableObject();
        }
        return null;
    }
    catch (final CSSException e) {
        throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
                + selectors + "' error: " + e.getMessage() + ").");
    }
}
 
Example 9
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Imports a node from another document to this document.
 * The source node is not altered or removed from the original document;
 * this method creates a new copy of the source node.
 *
 * @param importedNode the node to import
 * @param deep Whether to recursively import the subtree under the specified node; or not
 * @return the imported node that belongs to this Document
 */
@JsxFunction
public Object importNode(final Node importedNode, final boolean deep) {
    DomNode domNode = importedNode.getDomNodeOrDie();
    domNode = domNode.cloneNode(deep);
    domNode.processImportNode(this);
    for (final DomNode childNode : domNode.getDescendants()) {
        childNode.processImportNode(this);
    }
    return domNode.getScriptableObject();
}
 
Example 10
Source File: Range.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
Range(final org.w3c.dom.ranges.Range w3cRange) {
    final DomNode domNodeStartContainer = (DomNode) w3cRange.getStartContainer();
    startContainer_ = domNodeStartContainer.getScriptableObject();
    startOffset_ = w3cRange.getStartOffset();

    final DomNode domNodeEndContainer = (DomNode) w3cRange.getEndContainer();
    endContainer_ = domNodeEndContainer.getScriptableObject();
    endOffset_ = w3cRange.getEndOffset();
}
 
Example 11
Source File: Document.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object get(final String name, final Scriptable start) {
    final Object response = super.get(name, start);

    // IE support .querySelector(All) but not in quirks mode
    // => TODO: find a better way to handle this!
    if (response instanceof FunctionObject
        && ("querySelectorAll".equals(name) || "querySelector".equals(name))
        && getBrowserVersion().hasFeature(QUERYSELECTORALL_NOT_IN_QUIRKS)) {
        Document document = null;
        if (start instanceof DocumentProxy) {
            // if in prototype no domNode is set -> use start
            document = ((DocumentProxy) start).getDelegee();
        }
        else if (start instanceof HTMLDocument) {
            final DomNode page = ((HTMLDocument) start).getDomNodeOrNull();
            if (page != null) {
                document = page.getScriptableObject();
            }
        }
        if (document instanceof HTMLDocument && ((HTMLDocument) document).getDocumentMode() < 8) {
            return NOT_FOUND;
        }
    }

    return response;
}
 
Example 12
Source File: NamedNodeMap.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * 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;
}
 
Example 13
Source File: XMLDOMNamedNodeMap.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * 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 getNamedItemWithoutSyntheticClassAttr(final String name) {
    if (attributes_ != null) {
        final DomNode attr = (DomNode) attributes_.getNamedItem(name);
        if (attr != null) {
            return attr.getScriptableObject();
        }
    }
    return null;
}
 
Example 14
Source File: SimpleScriptable.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the JavaScript object that corresponds to the specified object.
 * New JavaScript objects will be created as needed. If a JavaScript object
 * cannot be created for a domNode then NOT_FOUND will be returned.
 *
 * @param object a {@link DomNode} or a {@link WebWindow}
 * @return the JavaScript object or NOT_FOUND
 */
protected SimpleScriptable getScriptableFor(final Object object) {
    if (object instanceof WebWindow) {
        return (SimpleScriptable) ((WebWindow) object).getScriptableObject();
    }

    final DomNode domNode = (DomNode) object;

    final Object scriptObject = domNode.getScriptableObject();
    if (scriptObject != null) {
        return (SimpleScriptable) scriptObject;
    }
    return makeScriptableFor(domNode);
}
 
Example 15
Source File: NamedNodeMap.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * 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;
}
 
Example 16
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces a child DOM node with another DOM node.
 * @param newChildObject the node to add as a child of this node
 * @param oldChildObject the node to remove as a child of this node
 * @return the removed child node
 */
@JsxFunction
public Object replaceChild(final Object newChildObject, final Object oldChildObject) {
    Object removedChild = null;

    if (newChildObject instanceof DocumentFragment) {
        final DocumentFragment fragment = (DocumentFragment) newChildObject;
        Node firstNode = null;
        final Node refChildObject = ((Node) oldChildObject).getNextSibling();
        for (final DomNode node : fragment.getDomNodeOrDie().getChildren()) {
            if (firstNode == null) {
                replaceChild(node.getScriptableObject(), oldChildObject);
                firstNode = (Node) node.getScriptableObject();
            }
            else {
                insertBeforeImpl(new Object[] {node.getScriptableObject(), refChildObject});
            }
        }
        if (firstNode == null) {
            removeChild(oldChildObject);
        }
        removedChild = oldChildObject;
    }
    else if (newChildObject instanceof Node && oldChildObject instanceof Node) {
        final Node newChild = (Node) newChildObject;

        // is the node allowed here?
        if (!isNodeInsertable(newChild)) {
            throw Context.reportRuntimeError("Node cannot be inserted at the specified point in the hierarchy");
        }

        // Get XML nodes for the DOM nodes passed in
        final DomNode newChildNode = newChild.getDomNodeOrDie();
        final DomNode oldChildNode = ((Node) oldChildObject).getDomNodeOrDie();

        // Replace the old child with the new child.
        oldChildNode.replace(newChildNode);
        removedChild = oldChildObject;
    }

    return removedChild;
}
 
Example 17
Source File: HTMLElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private Object getOffsetParentInternal(final boolean returnNullIfFixed) {
    DomNode currentElement = getDomNodeOrDie();

    if (currentElement.getParentNode() == null) {
        return null;
    }

    final HTMLElement htmlElement = currentElement.getScriptableObject();
    if (returnNullIfFixed && "fixed".equals(htmlElement.getStyle().getStyleAttribute(
            StyleAttributes.Definition.POSITION, true))) {
        return null;
    }

    final ComputedCSSStyleDeclaration style = htmlElement.getWindow().getComputedStyle(htmlElement, null);
    final String position = style.getPositionWithInheritance();
    final boolean staticPos = "static".equals(position);

    while (currentElement != null) {

        final DomNode parentNode = currentElement.getParentNode();
        if (parentNode instanceof HtmlBody
            || (staticPos && parentNode instanceof HtmlTableDataCell)
            || (staticPos && parentNode instanceof HtmlTable)) {
            return parentNode.getScriptableObject();
        }

        if (parentNode != null && parentNode.getScriptableObject() instanceof HTMLElement) {
            final HTMLElement parentElement = parentNode.getScriptableObject();
            final ComputedCSSStyleDeclaration parentStyle =
                        parentElement.getWindow().getComputedStyle(parentElement, null);
            final String parentPosition = parentStyle.getPositionWithInheritance();
            if (!"static".equals(parentPosition)) {
                return parentNode.getScriptableObject();
            }
        }

        currentElement = currentElement.getParentNode();
    }

    return null;
}
 
Example 18
Source File: NodeIterator.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private static Node getNodeOrNull(final DomNode domNode) {
    if (domNode == null) {
        return null;
    }
    return (Node) domNode.getScriptableObject();
}
 
Example 19
Source File: XMLDOMNodeList.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs an instance with an initial cache value.
 * @param parentScope the parent scope, on which we listen for changes
 * @param initialElements the initial content for the cache
 */
protected XMLDOMNodeList(final DomNode parentScope, final List<DomNode> initialElements) {
    this((ScriptableObject) parentScope.getScriptableObject(), true, null);
    cachedElements_ = new ArrayList<>(initialElements);
}
 
Example 20
Source File: Event.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new event instance.
 * @param domNode the DOM node that triggered the event
 * @param type the event type
 */
public Event(final DomNode domNode, final String type) {
    this((EventTarget) domNode.getScriptableObject(), type);
    setDomNode(domNode, false);
}