com.gargoylesoftware.htmlunit.html.DomNode Java Examples
The following examples show how to use
com.gargoylesoftware.htmlunit.html.DomNode.
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: Document.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Returns the value of the {@code embeds} property. * @return the value of the {@code embeds} property */ @JsxGetter({CHROME, IE}) public Object getImages() { return new HTMLCollection(getDomNodeOrDie(), false) { @Override protected boolean isMatching(final DomNode node) { return node instanceof HtmlImage; } @Override public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) { if (getBrowserVersion().hasFeature(JS_DOCUMENT_FORMS_FUNCTION_SUPPORTED)) { return super.call(cx, scope, thisObj, args); } throw Context.reportRuntimeError("TypeError: document.images is not a function"); } }; }
Example #2
Source File: Document.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Returns the value of the {@code embeds} property. * @return the value of the {@code embeds} property */ @JsxGetter({CHROME, IE}) public Object getImages() { return new HTMLCollection(getDomNodeOrDie(), false) { @Override protected boolean isMatching(final DomNode node) { return node instanceof HtmlImage; } @Override public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) { if (getBrowserVersion().hasFeature(JS_DOCUMENT_FORMS_FUNCTION_SUPPORTED)) { return super.call(cx, scope, thisObj, args); } throw Context.reportRuntimeError("TypeError: document.images is not a function"); } }; }
Example #3
Source File: AbstractList.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Helper for {@link #getWithPreemption(String)} when finding by id doesn't get results. * @param name the property name * @param elements the children elements. * @return {@link Scriptable#NOT_FOUND} if not found */ protected Object getWithPreemptionByName(final String name, final List<DomNode> elements) { final List<DomNode> matchingElements = new ArrayList<>(); for (final DomNode next : elements) { if (next instanceof DomElement) { final String nodeName = ((DomElement) next).getAttributeDirect("name"); if (name.equals(nodeName)) { matchingElements.add(next); } } } if (matchingElements.isEmpty()) { return NOT_FOUND; } else if (matchingElements.size() == 1) { return getScriptableForElement(matchingElements.get(0)); } // many elements => build a sub collection final DomNode domNode = getDomNodeOrNull(); final AbstractList collection = create(domNode, matchingElements); collection.setAvoidObjectDetection(true); return collection; }
Example #4
Source File: NodeList.java From htmlunit with Apache License 2.0 | 6 votes |
/** * 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, FF, FF68, FF60}) public void forEach(final Object callback) { final List<DomNode> nodes = getElements(); final WebClient client = getWindow().getWebWindow().getWebClient(); final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory(); final ContextAction<Object> contextAction = new ContextAction<Object>() { @Override public Object run(final Context cx) { final Function function = (Function) callback; final Scriptable scope = getParentScope(); for (int i = 0; i < nodes.size(); i++) { function.call(cx, scope, NodeList.this, new Object[] { nodes.get(i).getScriptableObject(), i, NodeList.this}); } return null; } }; cf.call(contextAction); }
Example #5
Source File: AbstractList.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Gets the HTML elements from cache or retrieve them at first call. * @return the list of {@link HtmlElement} contained in this collection */ public List<DomNode> getElements() { // a bit strange but we like to avoid sync List<DomNode> cachedElements = cachedElements_; if (cachedElements == null) { if (getParentScope() == null) { cachedElements = new ArrayList<>(); } else { cachedElements = computeElements(); } cachedElements_ = cachedElements; } registerListener(); // maybe the cache was cleared in between // then this returns the old state and never null return cachedElements; }
Example #6
Source File: XmlUtil.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Search for the prefix associated with specified namespace URI. * @param element the element to start searching from * @param namespace the namespace prefix * @return the prefix bound to the namespace URI; or null if there is no such namespace */ public static String lookupPrefix(final DomElement element, final String namespace) { final Map<String, DomAttr> attributes = element.getAttributesMap(); for (final Map.Entry<String, DomAttr> entry : attributes.entrySet()) { final String name = entry.getKey(); final DomAttr value = entry.getValue(); if (name.startsWith("xmlns:") && value.getValue().equals(namespace)) { return name.substring(6); } } for (final DomNode child : element.getChildren()) { if (child instanceof DomElement) { final String prefix = lookupPrefix((DomElement) child, namespace); if (prefix != null) { return prefix; } } } return null; }
Example #7
Source File: Document.java From htmlunit with Apache License 2.0 | 6 votes |
private static org.w3c.dom.traversal.NodeFilter createFilterWrapper(final Scriptable filter, final boolean filterFunctionOnly) { org.w3c.dom.traversal.NodeFilter filterWrapper = null; if (filter != null) { filterWrapper = new org.w3c.dom.traversal.NodeFilter() { @Override public short acceptNode(final org.w3c.dom.Node n) { final Object[] args = new Object[] {((DomNode) n).getScriptableObject()}; final Object response; if (filter instanceof Callable) { response = ((Callable) filter).call(Context.getCurrentContext(), filter, filter, args); } else { if (filterFunctionOnly) { throw Context.reportRuntimeError("only a function is allowed as filter"); } response = ScriptableObject.callMethod(filter, "acceptNode", args); } return (short) Context.toNumber(response); } }; } return filterWrapper; }
Example #8
Source File: MouseEvent.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Creates a new event instance. * @param domNode the DOM node that triggered the event * @param type the event type * @param shiftKey true if SHIFT is pressed * @param ctrlKey true if CTRL is pressed * @param altKey true if ALT is pressed * @param button the button code, must be {@link #BUTTON_LEFT}, {@link #BUTTON_MIDDLE} or {@link #BUTTON_RIGHT} */ public MouseEvent(final DomNode domNode, final String type, final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final int button) { super(domNode, type); setShiftKey(shiftKey); setCtrlKey(ctrlKey); setAltKey(altKey); if (button != BUTTON_LEFT && button != BUTTON_MIDDLE && button != BUTTON_RIGHT) { throw new IllegalArgumentException("Invalid button code: " + button); } button_ = button; if (TYPE_DBL_CLICK.equals(type)) { setDetail(2); } else { setDetail(1); } }
Example #9
Source File: Element.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Returns true if the element would be selected by the specified selector string; otherwise, returns false. * @param context the JavaScript context * @param thisObj the scriptable * @param args the arguments passed into the method * @param function the function * @return the value */ @JsxFunction({CHROME, FF, EDGE}) public static boolean matches( final Context context, final Scriptable thisObj, final Object[] args, final Function function) { final String selectorString = (String) args[0]; if (!(thisObj instanceof Element)) { throw ScriptRuntime.typeError("Illegal invocation"); } try { final DomNode domNode = ((Element) thisObj).getDomNodeOrNull(); return domNode != null && ((DomElement) domNode).matches(selectorString); } catch (final CSSException e) { throw ScriptRuntime.constructError("SyntaxError", "An invalid or illegal selector was specified (selector: '" + selectorString + "' error: " + e.getMessage() + ")."); } }
Example #10
Source File: NodeList.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Returns an {@link Iterator} allowing to go through all key/value pairs contained in this object. * @return an {@link Iterator} */ @JsxFunction({CHROME, FF52}) public Iterator entries() { final List<DomNode> elements = getElements(); final Context context = Context.getCurrentContext(); final Scriptable scope = getParentScope(); final List<Scriptable> list = new ArrayList<>(); for (int i = 0; i < elements.size(); i++) { final Object[] array = new Object[] {i, elements.get(i).getScriptableObject()}; list.add(context.newArray(scope, array)); } final Iterator object = new Iterator(ITERATOR_NAME, list.iterator()); object.setParentScope(scope); setIteratorPrototype(object); return object; }
Example #11
Source File: KeyboardEvent.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Creates a new keyboard event instance. * * @param domNode the DOM node that triggered the event * @param type the event type * @param keyCode the key code associated with the event * @param shiftKey true if SHIFT is pressed * @param ctrlKey true if CTRL is pressed * @param altKey true if ALT is pressed */ public KeyboardEvent(final DomNode domNode, final String type, final int keyCode, final boolean shiftKey, final boolean ctrlKey, final boolean altKey) { super(domNode, type); if (isAmbiguousKeyCode(keyCode)) { throw new IllegalArgumentException("Please use the 'char' constructor instead of int"); } setKeyCode(keyCode); if (getType().equals(Event.TYPE_KEY_PRESS)) { which_ = 0; } else { which_ = keyCode; } setShiftKey(shiftKey); setCtrlKey(ctrlKey); setAltKey(altKey); }
Example #12
Source File: HTMLFormElement.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Overridden to allow the retrieval of certain form elements by ID or name. * * @param name {@inheritDoc} * @return {@inheritDoc} */ @Override protected Object getWithPreemption(final String name) { if (getDomNodeOrNull() == null) { return NOT_FOUND; } final List<HtmlElement> elements = findElements(name); if (elements.isEmpty()) { return NOT_FOUND; } if (elements.size() == 1) { return getScriptableFor(elements.get(0)); } final List<DomNode> nodes = new ArrayList<>(elements); return new HTMLCollection(getHtmlForm(), nodes) { @Override protected List<DomNode> computeElements() { return new ArrayList<>(findElements(name)); } }; }
Example #13
Source File: AbstractList.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
private void registerListener() { if (!listenerRegistered_) { final DomNode domNode = getDomNodeOrNull(); if (domNode != null) { final DomHtmlAttributeChangeListenerImpl listener = new DomHtmlAttributeChangeListenerImpl(this); domNode.addDomChangeListener(listener); if (attributeChangeSensitive_) { if (domNode instanceof HtmlElement) { ((HtmlElement) domNode).addHtmlAttributeChangeListener(listener); } else if (domNode instanceof HtmlPage) { ((HtmlPage) domNode).addHtmlAttributeChangeListener(listener); } } listenerRegistered_ = true; } } }
Example #14
Source File: HTMLMapElement.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Returns the value of the JavaScript attribute {@code areas}. * @return the value of this attribute */ @JsxGetter public HTMLCollection getAreas() { if (areas_ == null) { final HtmlMap map = (HtmlMap) getDomNodeOrDie(); areas_ = new HTMLCollection(map, false) { @Override protected List<DomNode> computeElements() { final List<DomNode> list = new ArrayList<>(); for (final DomNode node : map.getChildElements()) { if (node instanceof HtmlArea) { list.add(node); } } return list; } }; } return areas_; }
Example #15
Source File: LabelsHelper.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * This is overridden instead of {@link #computeElements()} in order to prevent caching at all. * * {@inheritDoc} */ @Override public List<DomNode> getElements() { final List<DomNode> response = new ArrayList<>(); final DomElement domElement = (DomElement) getDomNodeOrDie(); for (DomNode parent = domElement.getParentNode(); parent != null; parent = parent.getParentNode()) { if (parent instanceof HtmlLabel) { response.add(parent); } } final String id = domElement.getId(); if (id != DomElement.ATTRIBUTE_NOT_DEFINED) { for (final DomElement label : domElement.getHtmlPageOrNull().getElementsByTagName("label")) { if (id.equals(label.getAttributeDirect("for"))) { response.add(label); } } } return response; }
Example #16
Source File: HTMLFormElement.java From htmlunit with Apache License 2.0 | 6 votes |
private List<HtmlElement> findElements(final String name) { final List<HtmlElement> elements = new ArrayList<>(); addElements(name, getHtmlForm().getHtmlElementDescendants(), elements); addElements(name, getHtmlForm().getLostChildren(), elements); // If no form fields are found, browsers are able to find img elements by ID or name. if (elements.isEmpty()) { for (final DomNode node : getHtmlForm().getHtmlElementDescendants()) { if (node instanceof HtmlImage) { final HtmlImage img = (HtmlImage) node; if (name.equals(img.getId()) || name.equals(img.getNameAttribute())) { elements.add(img); } } } } return elements; }
Example #17
Source File: AbstractList.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * 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 #18
Source File: SimpleRange.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
private static void deleteBefore(final DomNode node, int offset) { if (isOffsetChars(node)) { String text = getText(node); if (offset > -1 && offset < text.length()) { text = text.substring(offset); } else { text = ""; } setText(node, text); } else { final DomNodeList<DomNode> children = node.getChildNodes(); for (int i = 0; i < offset && i < children.getLength(); i++) { final DomNode child = children.get(i); child.remove(); i--; offset--; } } }
Example #19
Source File: LabelsHelper.java From htmlunit with Apache License 2.0 | 6 votes |
/** * This is overridden instead of {@link #computeElements()} in order to prevent caching at all. * * {@inheritDoc} */ @Override public List<DomNode> getElements() { final List<DomNode> response = new ArrayList<>(); final DomElement domElement = (DomElement) getDomNodeOrDie(); for (DomNode parent = domElement.getParentNode(); parent != null; parent = parent.getParentNode()) { if (parent instanceof HtmlLabel) { response.add(parent); } } final String id = domElement.getId(); if (id != DomElement.ATTRIBUTE_NOT_DEFINED) { for (final DomElement label : domElement.getHtmlPageOrNull().getElementsByTagName("label")) { if (id.equals(label.getAttributeDirect("for"))) { response.add(label); } } } return response; }
Example #20
Source File: Window.java From htmlunit with Apache License 2.0 | 6 votes |
@Override protected Object getWithPreemption(final String name) { final List<DomNode> elements = getElements(); for (final Object next : elements) { final BaseFrameElement frameElt = (BaseFrameElement) next; final WebWindow window = frameElt.getEnclosedWindow(); if (name.equals(window.getName())) { if (LOG.isDebugEnabled()) { LOG.debug("Property \"" + name + "\" evaluated (by name) to " + window); } return getScriptableForElement(window); } if (getBrowserVersion().hasFeature(JS_WINDOW_FRAMES_ACCESSIBLE_BY_ID) && frameElt.getId().equals(name)) { if (LOG.isDebugEnabled()) { LOG.debug("Property \"" + name + "\" evaluated (by id) to " + window); } return getScriptableForElement(window); } } return NOT_FOUND; }
Example #21
Source File: XPathResult.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Iterates and returns the next node from the node set or {@code null} if there are no more nodes. * @return the next node */ @JsxFunction public Node iterateNext() { if (resultType_ != UNORDERED_NODE_ITERATOR_TYPE && resultType_ != ORDERED_NODE_ITERATOR_TYPE) { throw Context.reportRuntimeError("Cannot get iterateNext for type: " + resultType_); } if (iteratorIndex_ < result_.size()) { return (Node) ((DomNode) result_.get(iteratorIndex_++)).getScriptableObject(); } return null; }
Example #22
Source File: HTMLDataListElement.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the {@code options} attribute. * @return the {@code options} attribute */ @JsxGetter public Object getOptions() { if (options_ == null) { options_ = new HTMLCollection(getDomNodeOrDie(), false) { @Override protected boolean isMatching(final DomNode node) { return node instanceof HtmlOption; } }; } return options_; }
Example #23
Source File: HTMLDocument.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Returns the value of the {@code all} property. * @return the value of the {@code all} property */ @JsxGetter public HTMLCollection getAll() { return new HTMLAllCollection(getDomNodeOrDie()) { @Override protected boolean isMatching(final DomNode node) { return true; } @Override public boolean avoidObjectDetection() { return true; } }; }
Example #24
Source File: XMLDocument.java From htmlunit with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public SimpleScriptable makeScriptableFor(final DomNode domNode) { final SimpleScriptable scriptable; // TODO: cleanup, getScriptObject() should be used!!! if (domNode instanceof DomElement && !(domNode instanceof HtmlElement)) { if (domNode instanceof SvgElement) { final Class<? extends HtmlUnitScriptable> javaScriptClass = ((JavaScriptEngine) getWindow().getWebWindow().getWebClient() .getJavaScriptEngine()).getJavaScriptClass(domNode.getClass()); try { scriptable = (SimpleScriptable) javaScriptClass.newInstance(); } catch (final Exception e) { throw Context.throwAsScriptRuntimeEx(e); } } else { scriptable = new Element(); } } else if (domNode instanceof DomAttr) { scriptable = new Attr(); } else { return super.makeScriptableFor(domNode); } scriptable.setPrototype(getPrototype(scriptable.getClass())); scriptable.setParentScope(getParentScope()); scriptable.setDomNode(domNode); return scriptable; }
Example #25
Source File: XSLTProcessor.java From htmlunit with Apache License 2.0 | 5 votes |
private static DomNode findOutputNode(final DomNode xsltDomNode) { for (DomNode child : xsltDomNode.getChildren()) { if ("output".equals(child.getLocalName())) { return child; } for (DomNode child1 : child.getChildren()) { if ("output".equals(child1.getLocalName())) { return child1; } } } return null; }
Example #26
Source File: Document.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the value of the JavaScript property {@code applets}. * @see <a href="http://msdn.microsoft.com/en-us/library/ms537436.aspx"> * MSDN documentation</a> * @see <a href="https://developer.mozilla.org/En/DOM:document.applets"> * Gecko DOM reference</a> * @return the value of this property */ @JsxGetter({CHROME, IE}) public Object getApplets() { return new HTMLCollection(getDomNodeOrDie(), false) { @Override protected boolean isMatching(final DomNode node) { return false; } }; }
Example #27
Source File: HTMLCollection.java From htmlunit with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override protected Object getWithPreemptionByName(final String name, final List<DomNode> elements) { final List<DomNode> matchingElements = new ArrayList<>(); final boolean searchName = isGetWithPreemptionSearchName(); for (final DomNode next : elements) { if (next instanceof DomElement && (searchName || next instanceof HtmlInput || next instanceof HtmlForm)) { final String nodeName = ((DomElement) next).getAttributeDirect("name"); if (name.equals(nodeName)) { matchingElements.add(next); } } } if (matchingElements.isEmpty()) { if (getBrowserVersion().hasFeature(HTMLCOLLECTION_ITEM_SUPPORTS_DOUBLE_INDEX_ALSO)) { final Double doubleValue = Context.toNumber(name); if (!doubleValue.isNaN()) { final Object object = get(doubleValue.intValue(), this); if (object != NOT_FOUND) { return object; } } } return NOT_FOUND; } else if (matchingElements.size() == 1) { return getScriptableForElement(matchingElements.get(0)); } // many elements => build a sub collection final DomNode domNode = getDomNodeOrNull(); final HTMLCollection collection = new HTMLCollection(domNode, matchingElements); collection.setAvoidObjectDetection(true); return collection; }
Example #28
Source File: HTMLDocument.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Gets the node that is the last one when exploring following nodes, depth-first. * @param node the node to search * @return the searched node */ HtmlElement getLastHtmlElement(final HtmlElement node) { final DomNode lastChild = node.getLastChild(); if (lastChild == null || !(lastChild instanceof HtmlElement) || lastChild instanceof HtmlScript) { return node; } return getLastHtmlElement((HtmlElement) lastChild); }
Example #29
Source File: HTMLOptionElement.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Returns the value of the {@code selected} property. * @return the text property */ @JsxGetter public boolean isSelected() { final DomNode dom = getDomNodeOrNull(); if (dom instanceof HtmlOption) { return ((HtmlOption) dom).isSelected(); } return false; }
Example #30
Source File: DocumentFragment.java From htmlunit with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override @JsxGetter({CHROME, FF, FF68, FF60}) public Element getFirstElementChild() { for (DomNode child : getDomNodeOrDie().getChildren()) { return (Element) child.getScriptableObject(); } return null; }