Search in sources :

Example 1 with HTMLHtmlElement

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

the class CSSStyleDeclaration method pixelValue.

private static int pixelValue(final Element element, final CssValue value, final boolean percentMode) {
    final String s = value.get(element);
    if (s.endsWith("%") || (s.isEmpty() && element instanceof HTMLHtmlElement)) {
        final float i = NumberUtils.toFloat(TO_FLOAT_PATTERN.matcher(s).replaceAll("$1"), 100);
        final Element parent = element.getParentElement();
        final int absoluteValue = (parent == null) ? value.getWindowDefaultValue() : pixelValue(parent, value, true);
        return Math.round((i / 100f) * absoluteValue);
    }
    if (AUTO.equals(s)) {
        return value.getDefaultValue();
    }
    if (s.isEmpty()) {
        if (element instanceof HTMLCanvasElement) {
            return value.getWindowDefaultValue();
        }
        // we can provide some kind of base value for percent calculation
        if (percentMode) {
            final Element parent = element.getParentElement();
            if (parent == null || parent instanceof HTMLHtmlElement) {
                return value.getWindowDefaultValue();
            }
            return pixelValue(parent, value, true);
        }
        return 0;
    }
    return pixelValue(s);
}
Also used : HTMLHtmlElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLHtmlElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) HTMLHtmlElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLHtmlElement)

Aggregations

StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)1 Element (com.gargoylesoftware.htmlunit.javascript.host.Element)1 HTMLCanvasElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement)1 HTMLHtmlElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLHtmlElement)1