Search in sources :

Example 11 with Element

use of com.gargoylesoftware.htmlunit.javascript.host.Element in project htmlunit by HtmlUnit.

the class ComputedCSSStyleDeclaration method getHeight.

/**
 * {@inheritDoc}
 */
@Override
public String getHeight() {
    if (NONE.equals(getDisplay())) {
        return AUTO;
    }
    final Element elem = getElement();
    if (!elem.getDomNodeOrDie().isAttachedToPage()) {
        if (getBrowserVersion().hasFeature(CSS_STYLE_PROP_DISCONNECTED_IS_EMPTY)) {
            return "";
        }
        if (getStyleAttribute(HEIGHT, true).isEmpty()) {
            return AUTO;
        }
    }
    final int windowHeight = elem.getWindow().getWebWindow().getInnerHeight();
    return pixelString(elem, new CssValue(0, windowHeight) {

        @Override
        public String get(final ComputedCSSStyleDeclaration style) {
            final String offsetHeight = ((HTMLElement) elem).getOffsetHeight() + "px";
            return defaultIfEmpty(style.getStyleAttribute(HEIGHT, true), offsetHeight, AUTO);
        }
    });
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLDataElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement) HTMLDivElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement) HTMLSlotElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement) HTMLImageElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement) HTMLUnknownElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) HTMLTimeElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement) HTMLIFrameElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement) HTMLOutputElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement) HTMLLegendElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) AttributedString(java.text.AttributedString)

Example 12 with Element

use of com.gargoylesoftware.htmlunit.javascript.host.Element in project htmlunit by HtmlUnit.

the class ComputedCSSStyleDeclaration method getWidth.

/**
 * {@inheritDoc}
 */
@Override
public String getWidth() {
    if (NONE.equals(getDisplay())) {
        return AUTO;
    }
    final Element elem = getElement();
    if (!elem.getDomNodeOrDie().isAttachedToPage()) {
        if (getBrowserVersion().hasFeature(CSS_STYLE_PROP_DISCONNECTED_IS_EMPTY)) {
            return "";
        }
        if (getStyleAttribute(WIDTH, true).isEmpty()) {
            return AUTO;
        }
    }
    final int windowWidth = elem.getWindow().getWebWindow().getInnerWidth();
    return pixelString(elem, new CssValue(0, windowWidth) {

        @Override
        public String get(final ComputedCSSStyleDeclaration style) {
            final String value = style.getStyleAttribute(WIDTH, true);
            if (StringUtils.isEmpty(value)) {
                if (ABSOLUTE.equals(getStyleAttribute(POSITION, true))) {
                    final String content = getDomNodeOrDie().getVisibleText();
                    // at least for empty div's this is more correct
                    if (null != content && content.length() < 13) {
                        return (content.length() * 7) + "px";
                    }
                }
                int windowDefaultValue = getWindowDefaultValue();
                if (elem instanceof HTMLBodyElement) {
                    windowDefaultValue -= 16;
                }
                return windowDefaultValue + "px";
            } else if (AUTO.equals(value)) {
                int windowDefaultValue = getWindowDefaultValue();
                if (elem instanceof HTMLBodyElement) {
                    windowDefaultValue -= 16;
                }
                return windowDefaultValue + "px";
            }
            return value;
        }
    });
}
Also used : HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLDataElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement) HTMLDivElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement) HTMLSlotElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement) HTMLImageElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement) HTMLUnknownElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) HTMLTimeElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement) HTMLIFrameElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement) HTMLOutputElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement) HTMLLegendElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) AttributedString(java.text.AttributedString)

Example 13 with Element

use of com.gargoylesoftware.htmlunit.javascript.host.Element in project htmlunit by HtmlUnit.

the class ComputedCSSStyleDeclaration method isScrollable.

/**
 * @param ignoreSize whether to consider the content/calculated width/height
 */
private boolean isScrollable(final boolean horizontal, final boolean ignoreSize) {
    final boolean scrollable;
    final Element node = getElement();
    final String overflow = getStyleAttribute(OVERFLOW, true);
    if (horizontal) {
        // TODO: inherit, overflow-x
        scrollable = (node instanceof HTMLBodyElement || "scroll".equals(overflow) || AUTO.equals(overflow)) && (ignoreSize || getContentWidth() > getCalculatedWidth());
    } else {
        // TODO: inherit, overflow-y
        scrollable = (node instanceof HTMLBodyElement || "scroll".equals(overflow) || AUTO.equals(overflow)) && (ignoreSize || getContentHeight() > getEmptyHeight());
    }
    return scrollable;
}
Also used : HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLDataElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement) HTMLDivElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement) HTMLSlotElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement) HTMLImageElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement) HTMLUnknownElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) HTMLTimeElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement) HTMLIFrameElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement) HTMLOutputElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement) HTMLLegendElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) AttributedString(java.text.AttributedString)

Example 14 with Element

use of com.gargoylesoftware.htmlunit.javascript.host.Element in project htmlunit by HtmlUnit.

the class XMLDocument method makeScriptableFor.

/**
 * {@inheritDoc}
 */
@Override
public HtmlUnitScriptable makeScriptableFor(final DomNode domNode) {
    final HtmlUnitScriptable 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 = 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;
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) SvgElement(com.gargoylesoftware.htmlunit.svg.SvgElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) SvgElement(com.gargoylesoftware.htmlunit.svg.SvgElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) DOMException(com.gargoylesoftware.htmlunit.javascript.host.dom.DOMException) IOException(java.io.IOException) Attr(com.gargoylesoftware.htmlunit.javascript.host.dom.Attr) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 15 with Element

use of com.gargoylesoftware.htmlunit.javascript.host.Element in project htmlunit by HtmlUnit.

the class XMLSerializer method serializeToString.

/**
 * The subtree rooted by the specified element is serialized to a string.
 * @param root the root of the subtree to be serialized (this may be any node, even a document)
 * @return the serialized string
 */
@JsxFunction
public String serializeToString(Node root) {
    if (root == null) {
        return "";
    }
    if (root instanceof DocumentFragment) {
        if (root.getOwnerDocument() instanceof HTMLDocument && getBrowserVersion().hasFeature(JS_XML_SERIALIZER_HTML_DOCUMENT_FRAGMENT_ALWAYS_EMPTY)) {
            return "";
        }
        Node node = root.getFirstChild();
        if (node == null) {
            return "";
        }
        final StringBuilder builder = new StringBuilder();
        while (node != null) {
            builder.append(serializeToString(node));
            node = node.getNextSibling();
        }
        return builder.toString();
    }
    if (root instanceof Document) {
        root = ((Document) root).getDocumentElement();
    }
    if (root instanceof Element) {
        final StringBuilder builder = new StringBuilder();
        final DomNode node = root.getDomNodeOrDie();
        final SgmlPage page = node.getPage();
        final boolean isHtmlPage = page != null && page.isHtmlPage();
        String forcedNamespace = null;
        if (isHtmlPage) {
            forcedNamespace = "http://www.w3.org/1999/xhtml";
        }
        toXml(1, node, builder, forcedNamespace);
        return builder.toString();
    }
    if (root instanceof CDATASection && getBrowserVersion().hasFeature(JS_XML_SERIALIZER_ROOT_CDATA_AS_ESCAPED_TEXT)) {
        final DomCDataSection domCData = (DomCDataSection) root.getDomNodeOrDie();
        final String data = domCData.getData();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(data)) {
            return StringUtils.escapeXmlChars(data);
        }
    }
    return root.getDomNodeOrDie().asXml();
}
Also used : CDATASection(com.gargoylesoftware.htmlunit.javascript.host.dom.CDATASection) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) Node(com.gargoylesoftware.htmlunit.javascript.host.dom.Node) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) Document(com.gargoylesoftware.htmlunit.javascript.host.dom.Document) DocumentFragment(com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

Element (com.gargoylesoftware.htmlunit.javascript.host.Element)15 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)13 StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)11 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)11 HTMLCanvasElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement)11 BaseFrameElement (com.gargoylesoftware.htmlunit.html.BaseFrameElement)10 HTMLBodyElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement)10 HTMLDataElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement)10 HTMLDivElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement)10 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)10 HTMLIFrameElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement)10 HTMLImageElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement)10 HTMLLegendElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement)10 HTMLOutputElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement)10 HTMLSlotElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement)10 HTMLTimeElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement)10 HTMLUnknownElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement)10 AttributedString (java.text.AttributedString)9 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)4 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2