/** * Returns the first element with the specified tag name that is an ancestor to this element, or * {@code null} if no such element is found. * @param tagName the name of the tag searched (case insensitive) * @return the first element with the specified tag name that is an ancestor to this element */ public HtmlElement getEnclosingElement(final String tagName) { final String tagNameLC = tagName.toLowerCase(Locale.ROOT); for (DomNode currentNode = getParentNode(); currentNode != null; currentNode = currentNode.getParentNode()) { if (currentNode instanceof HtmlElement && currentNode.getNodeName().equals(tagNameLC)) { return (HtmlElement) currentNode; } } return null; }
List<DomNode> companies = (List<DomNode>) page.getByXPath("//li[contains(@class, 'featured block']"); for (DomNode node : companies) { // retrieve telephone number DomNode telephone = (DomNode) node.getByXPath( "div[@class='listingWrapper']/div[@class='itemInfo']/span[@class='tel']").get(0);
/** * Evaluates the specified XPath expression from this node, returning the first matching element, * or {@code null} if no node matches the specified XPath expression. * * @param xpathExpr the XPath expression * @param <X> the expression type * @return the first element matching the specified XPath expression * @see #getByXPath(String) * @see #getCanonicalXPath() */ public <X> X getFirstByXPath(final String xpathExpr) { return getFirstByXPath(xpathExpr, null); }
/** * {@inheritDoc} */ @Override public DomNode appendChild(final org.w3c.dom.Node node) { final DomNode domNode = (DomNode) node; if (append_) { return target_.appendChild(domNode); } target_.insertBefore(domNode); return domNode; }
private void getPreformattedText(DomNode node, StringBuffer toReturn) { if (node.isDisplayed()) { toReturn.append(node.getTextContent()); } }
/** * {@inheritDoc} */ @Override public void setTextContent(final String textContent) { removeAllChildren(); if (textContent != null && !textContent.isEmpty()) { appendChild(new DomText(getPage(), textContent)); } }
/** * Replace all children elements of this element with the supplied value. * @param value - the new value for the contents of this node */ public void jsxSet_innerText(final String value) { final DomNode domNode = getDomNodeOrDie(); if (INNER_TEXT_READONLY.contains(domNode.getNodeName())) { throw Context.reportRuntimeError("innerText is read-only for tag " + domNode.getNodeName()); } domNode.removeAllChildren(); final DomNode node = new DomText(getDomNodeOrDie().getPage(), value); domNode.appendChild(node); //if the parentNode has null parentNode in IE, //create a DocumentFragment to be the parentNode's parentNode. if (domNode.getParentNode() == null && getBrowserVersion().isIE()) { final DomDocumentFragment fragment = ((HtmlPage) domNode.getPage()).createDomDocumentFragment(); fragment.appendChild(domNode); } }
if (!page.getWebClient().getOptions().isJavaScriptEnabled()) { return false; if (htmlPage != null && htmlPage.isParsingHtmlSnippet()) { return false; for (DomNode o = element; o != null; o = o.getParentNode()) { if (o instanceof HtmlInlineFrame || o instanceof HtmlNoFrames) { return false; if (page.getEnclosingWindow() != null && page.getEnclosingWindow().getEnclosedPage() != page) { return false;
/** * Replace all children elements of this element with the supplied value. * @param value - the new value for the contents of this node */ public void jsxSet_innerHTML(final Object value) { final DomNode domNode = getDomNodeOrDie(); final boolean ie = getBrowserVersion().isIE(); if (ie && INNER_HTML_READONLY_IN_IE.contains(domNode.getNodeName())) { throw Context.reportRuntimeError("innerHTML is read-only for tag " + domNode.getNodeName()); } domNode.removeAllChildren(); // null && IE -> add child // null && non-IE -> Don't add // '' -> Don't add if ((value == null && ie) || (value != null && !"".equals(value))) { final String valueAsString = Context.toString(value); parseHtmlSnippet(domNode, true, valueAsString); //if the parentNode has null parentNode in IE, //create a DocumentFragment to be the parentNode's parentNode. if (domNode.getParentNode() == null && ie) { final DomDocumentFragment fragment = ((HtmlPage) domNode.getPage()).createDomDocumentFragment(); fragment.appendChild(domNode); } } }
/** @inheritDoc ContentHandler#characters(char,int,int) */ public void characters(final char[] ch, final int start, final int length) throws SAXException { if ((characters_ == null || characters_.length() == 0) && new String(ch, start, length).trim().length() == 0 && page_.getWebClient().getBrowserVersion().isIE()) { DomNode node = currentNode_.getLastChild(); if (currentNode_ instanceof HTMLElement.ProxyDomNode) { final HTMLElement.ProxyDomNode proxyNode = (HTMLElement.ProxyDomNode) currentNode_; node = proxyNode.getDomNode(); if (!proxyNode.isAppend()) { node = node.getPreviousSibling(); if (node == null) { node = proxyNode.getDomNode().getParentNode(); } } } if (removeEmptyCharacters(node)) { return; } } if (characters_ == null) { characters_ = new StringBuilder(); } characters_.append(ch, start, length); }
for (DomNode child = getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof DomText) { final boolean ie = getPage().getWebClient().getBrowserVersion().isIE(); final StringBuilder dataBuilder = new StringBuilder(); DomNode toRemove = child; final DomNode nextChild = toRemove.getNextSibling(); dataBuilder.append(toRemove.getTextContent()); if (ie || firstText != null) { toRemove.remove(); final DomText newText = new DomText(getPage(), dataBuilder.toString()); insertBefore(newText, toRemove);
if (!page.getWebClient().isJavaScriptEnabled()) { return false; if (page instanceof HtmlPage && ((HtmlPage) page).isParsingHtmlSnippet()) { return false; for (DomNode o = this; o != null; o = o.getParentNode()) { if (o instanceof HtmlInlineFrame || o instanceof HtmlNoFrames || o instanceof HtmlNoScript) { return false; if (page.getEnclosingWindow() != null && page.getEnclosingWindow().getEnclosedPage() != page) { return false; while (root.getParentNode() != null) { root = root.getParentNode();
/** * Replace all children elements of this element with the supplied value. * Sets the outerHTML of the node. * @see <a href="http://msdn.microsoft.com/en-us/library/ms534310.aspx">MSDN documentation</a> * @param value - the new value for replacing this node */ public void jsxSet_outerHTML(final String value) { final DomNode domNode = getDomNodeOrDie(); if (OUTER_HTML_READONLY.contains(domNode.getNodeName())) { throw Context.reportRuntimeError("outerHTML is read-only for tag " + domNode.getNodeName()); } final DomNode proxyNode = new ProxyDomNode(domNode.getPage(), domNode, false); parseHtmlSnippet(proxyNode, false, value); domNode.remove(); }
/** * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br/> * * @param node the node that has just been added to the document */ void notifyNodeAdded(final DomNode node) { if (node instanceof HtmlElement) { boolean insideNoScript = false; if (getWebClient().isJavaScriptEnabled()) { for (DomNode parent = node.getParentNode(); parent != null; parent = parent.getParentNode()) { if (parent instanceof HtmlNoScript) { insideNoScript = true; break; } } } if (!insideNoScript) { addMappedElement((HtmlElement) node, true); } if (node.getNodeName().equals("base")) { calculateBase(); } } node.onAddedToPage(); }
/** * Submits the form by clikcing the submit button of the given name. * * @param name * This corresponds to the @name of {@code <f:submit />} */ public HtmlPage submit(HtmlForm form, String name) throws Exception { for( HtmlElement e : form.getElementsByTagName("button")) { HtmlElement p = (HtmlElement)e.getParentNode().getParentNode(); if (e instanceof HtmlButton && p.getAttribute("name").equals(name)) { return (HtmlPage)HtmlFormUtil.submit(form, (HtmlButton) e); } } throw new AssertionError("No such submit button with the name "+name); }
/** {@inheritDoc} */ public void comment(final char[] ch, final int start, final int length) { handleCharacters(); final String data = String.valueOf(ch, start, length); if (!data.startsWith("[CDATA") || !page_.getWebClient().getBrowserVersion().isIE()) { final DomComment comment = new DomComment(page_, data); currentNode_.appendChild(comment); } }
/** * Gets the associated sheet. * @see <a href="http://www.xulplanet.com/references/objref/HTMLStyleElement.html">Mozilla doc</a> * @return the sheet */ public Stylesheet jsxGet_sheet() { if (sheet_ != null) { return sheet_; } String css = ""; final HtmlStyle style = (HtmlStyle) getDomNodeOrDie(); if (style.getFirstChild() != null) { css = style.getFirstChild().asText(); } final Cache cache = getWindow().getWebWindow().getWebClient().getCache(); final CSSStyleSheet cached = cache.getCachedStyleSheet(css); final String uri = getDomNodeOrDie().getPage().getWebResponse().getRequestSettings() .getUrl().toExternalForm(); if (cached != null) { sheet_ = new Stylesheet(this, cached, uri); } else { final InputSource source = new InputSource(new StringReader(css)); sheet_ = new Stylesheet(this, source, uri); cache.cache(css, sheet_.getWrappedSheet()); } return sheet_; }
/** * Returns the row element which contains this cell's HTML element; may return {@code null}. * @return the row element which contains this cell's HTML element */ private HtmlTableRow getRow() { DomNode node = getDomNodeOrDie(); while (node != null && !(node instanceof HtmlTableRow)) { node = node.getParentNode(); } return (HtmlTableRow) node; }
if (domNode instanceof HtmlImage && "image".equals(((HtmlImage) domNode).getOriginalQualifiedName()) && ((HtmlImage) domNode).wasCreatedByJavascript()) { if (domNode.hasFeature(HTMLIMAGE_HTMLELEMENT)) { javaScriptClass = HTMLElement.class; else if (domNode.hasFeature(HTMLIMAGE_HTMLUNKNOWNELEMENT)) { javaScriptClass = HTMLUnknownElement.class; (JavaScriptEngine) getWindow().getWebWindow().getWebClient().getJavaScriptEngine(); for (Class<?> c = domNode.getClass(); javaScriptClass == null && c != null; c = c.getSuperclass()) { javaScriptClass = (Class<? extends SimpleScriptable>) javaScriptEngine.getJavaScriptClass(c); LOG.debug("No JavaScript class found for element <" + domNode.getNodeName() + ">. Using HTMLElement"); throw Context.throwAsScriptRuntimeEx(e);
/** * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br/> * * @param node the node that has just been removed from the tree */ void notifyNodeRemoved(final DomNode node) { if (node instanceof HtmlElement) { removeMappedElement((HtmlElement) node, true, true); if (node.getNodeName().equals("base")) { calculateBase(); } } }