Search in sources :

Example 1 with HtmlBody

use of com.gargoylesoftware.htmlunit.html.HtmlBody in project htmlunit by HtmlUnit.

the class HTMLElement method getOffsetParentInternal.

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;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlBody(com.gargoylesoftware.htmlunit.html.HtmlBody) HtmlTable(com.gargoylesoftware.htmlunit.html.HtmlTable) HtmlTableDataCell(com.gargoylesoftware.htmlunit.html.HtmlTableDataCell) ComputedCSSStyleDeclaration(com.gargoylesoftware.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration)

Example 2 with HtmlBody

use of com.gargoylesoftware.htmlunit.html.HtmlBody in project htmlunit by HtmlUnit.

the class HtmlUnitXPathTest method xpathFromElement.

/**
 * Test evaluation relative from elements other than the whole page.
 * @throws Exception if test fails
 */
@Test
public void xpathFromElement() throws Exception {
    final String content = "<html><head><title>Test page</title></head>\n" + "<body><a href='foo.html' id='myLink'>foo</a></body>\n" + "</html>";
    final HtmlPage page = loadPage(content);
    final HtmlBody body = page.getFirstByXPath("/html/body");
    assertEquals(page.getHtmlElementById("myLink"), body.getFirstByXPath("./a"));
}
Also used : HtmlBody(com.gargoylesoftware.htmlunit.html.HtmlBody) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Test(org.junit.Test)

Example 3 with HtmlBody

use of com.gargoylesoftware.htmlunit.html.HtmlBody in project htmlunit by HtmlUnit.

the class HtmlSerializerNormalizedText method appendDomNode.

/**
 * Process {@link HtmlHiddenInput}.
 *
 * @param builder the StringBuilder to add to
 * @param domNode the target to process
 */
protected void appendDomNode(final HtmlSerializerTextBuilder builder, final DomNode domNode) {
    boolean block = false;
    if (!(domNode instanceof HtmlBody)) {
        final SgmlPage page = domNode.getPage();
        final WebWindow window = page.getEnclosingWindow();
        if (page != null && // TODO
        page.getWebClient().isJavaScriptEngineEnabled() && window.getWebClient().getOptions().isCssEnabled()) {
            if (domNode instanceof DomElement) {
                final String display = window.getComputedStyle((DomElement) domNode, null).getDisplay();
                block = "block".equals(display);
            }
        } else if (domNode instanceof HtmlElement) {
            block = DisplayStyle.BLOCK == ((HtmlElement) domNode).getDefaultStyleDisplay();
        }
    }
    if (block) {
        builder.appendBlockSeparator();
    }
    appendChildren(builder, domNode);
    if (block) {
        builder.appendBlockSeparator();
    }
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlBody(com.gargoylesoftware.htmlunit.html.HtmlBody) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Example 4 with HtmlBody

use of com.gargoylesoftware.htmlunit.html.HtmlBody in project htmlunit by HtmlUnit.

the class HtmlUnitNekoDOMBuilder method startElement.

/**
 * {@inheritDoc}
 */
@Override
public void startElement(String namespaceURI, final String localName, final String qName, final Attributes atts) throws SAXException {
    if (snippetStartNodeOverwritten_) {
        snippetStartNodeOverwritten_ = false;
        return;
    }
    handleCharacters();
    final String tagLower = localName.toLowerCase(Locale.ROOT);
    if (page_.isParsingHtmlSnippet() && ("html".equals(tagLower) || "body".equals(tagLower))) {
        return;
    }
    if ("head".equals(tagLower)) {
        if (headParsed_ == HeadParsed.YES || page_.isParsingHtmlSnippet()) {
            return;
        }
        headParsed_ = lastTagWasSynthesized_ ? HeadParsed.SYNTHESIZED : HeadParsed.YES;
    }
    if (namespaceURI != null) {
        namespaceURI = namespaceURI.trim();
    } else // add a head if none was there
    if (headParsed_ == HeadParsed.NO && ("body".equals(tagLower) || "frameset".equals(tagLower))) {
        final ElementFactory factory = htmlParser_.getElementFactory(page_, null, "head", insideSvg_, false);
        final DomElement newElement = factory.createElement(page_, "head", null);
        appendChild(currentNode_, newElement);
        headParsed_ = HeadParsed.SYNTHESIZED;
    }
    // If we're adding a body element, keep track of any temporary synthetic ones
    // that we may have had to create earlier (for document.write(), for example).
    HtmlBody oldBody = null;
    if ("body".equals(qName) && page_.getBody() instanceof HtmlBody) {
        oldBody = (HtmlBody) page_.getBody();
    }
    // end tag.
    if ("form".equals(tagLower)) {
        formWaitingForLostChildren_ = null;
    }
    // Add the new node.
    if (!(page_ instanceof XHtmlPage) && Html.XHTML_NAMESPACE.equals(namespaceURI)) {
        namespaceURI = null;
    }
    final ElementFactory factory = htmlParser_.getElementFactory(page_, namespaceURI, qName, insideSvg_, false);
    if (factory == HtmlUnitNekoHtmlParser.SVG_FACTORY) {
        namespaceURI = Html.SVG_NAMESPACE;
    }
    final DomElement newElement = factory.createElementNS(page_, namespaceURI, qName, atts, true);
    newElement.setStartLocation(locator_.getLineNumber(), locator_.getColumnNumber());
    // parse can't replace everything as it does not buffer elements while parsing
    addNodeToRightParent(currentNode_, newElement);
    if ("svg".equals(tagLower)) {
        insideSvg_ = true;
    }
    // remove the old body and move its children to the real body element we just added.
    if (oldBody != null) {
        oldBody.quietlyRemoveAndMoveChildrenTo(newElement);
    }
    if ("body".equals(tagLower)) {
        body_ = (HtmlElement) newElement;
    } else if ("meta".equals(tagLower) && page_.hasFeature(META_X_UA_COMPATIBLE)) {
        final HtmlMeta meta = (HtmlMeta) newElement;
        if ("X-UA-Compatible".equals(meta.getHttpEquivAttribute())) {
            final String content = meta.getContentAttribute();
            if (content.startsWith("IE=")) {
                final String mode = content.substring(3).trim();
                final int version = page_.getWebClient().getBrowserVersion().getBrowserVersionNumeric();
                try {
                    int value = Integer.parseInt(mode);
                    if (value > version) {
                        value = version;
                    }
                    ((HTMLDocument) page_.getScriptableObject()).forceDocumentMode(value);
                } catch (final Exception e) {
                // ignore
                }
            }
        }
    } else if (createdByJavascript_ && "script".equals(tagLower)) {
        final ScriptElement script = (ScriptElement) newElement;
        script.markAsCreatedByDomParser();
    }
    currentNode_ = newElement;
    stack_.push(currentNode_);
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlBody(com.gargoylesoftware.htmlunit.html.HtmlBody) XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) ScriptElement(com.gargoylesoftware.htmlunit.html.ScriptElement) HtmlMeta(com.gargoylesoftware.htmlunit.html.HtmlMeta) XNIException(org.apache.xerces.xni.XNIException) SAXException(org.xml.sax.SAXException) ObjectInstantiationException(com.gargoylesoftware.htmlunit.ObjectInstantiationException) IOException(java.io.IOException) ElementFactory(com.gargoylesoftware.htmlunit.html.ElementFactory)

Aggregations

HtmlBody (com.gargoylesoftware.htmlunit.html.HtmlBody)4 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 ObjectInstantiationException (com.gargoylesoftware.htmlunit.ObjectInstantiationException)1 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 ElementFactory (com.gargoylesoftware.htmlunit.html.ElementFactory)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 HtmlMeta (com.gargoylesoftware.htmlunit.html.HtmlMeta)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 HtmlTable (com.gargoylesoftware.htmlunit.html.HtmlTable)1 HtmlTableDataCell (com.gargoylesoftware.htmlunit.html.HtmlTableDataCell)1 ScriptElement (com.gargoylesoftware.htmlunit.html.ScriptElement)1 XHtmlPage (com.gargoylesoftware.htmlunit.html.XHtmlPage)1 ComputedCSSStyleDeclaration (com.gargoylesoftware.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration)1 IOException (java.io.IOException)1 XNIException (org.apache.xerces.xni.XNIException)1 Test (org.junit.Test)1 SAXException (org.xml.sax.SAXException)1