Search in sources :

Example 1 with Property

use of com.gargoylesoftware.css.dom.Property in project htmlunit by HtmlUnit.

the class NamedAttrNodeMapImpl method getStyleMap.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Returns a sorted map containing style elements, keyed on style element name. We use a
 * {@link LinkedHashMap} map so that results are deterministic and are thus testable.
 *
 * @return a sorted map containing style elements, keyed on style element name
 */
public Map<String, StyleElement> getStyleMap() {
    final String styleAttribute = getAttributeDirect("style");
    if (styleString_ == styleAttribute) {
        return styleMap_;
    }
    final Map<String, StyleElement> styleMap = new LinkedHashMap<>();
    if (ATTRIBUTE_NOT_DEFINED == styleAttribute || DomElement.ATTRIBUTE_VALUE_EMPTY == styleAttribute) {
        styleMap_ = styleMap;
        styleString_ = styleAttribute;
        return styleMap_;
    }
    final CSSStyleDeclarationImpl cssStyle = new CSSStyleDeclarationImpl(null);
    try {
        // use the configured cssErrorHandler here to do the same error handling during
        // parsing of inline styles like for external css
        cssStyle.setCssText(styleAttribute, getPage().getWebClient().getCssErrorHandler());
    } catch (final Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error while parsing style value '" + styleAttribute + "'", e);
        }
    }
    for (final Property prop : cssStyle.getProperties()) {
        final String key = prop.getName().toLowerCase(Locale.ROOT);
        final StyleElement element = new StyleElement(key, prop.getValue().getCssText(), prop.isImportant() ? StyleElement.PRIORITY_IMPORTANT : "", SelectorSpecificity.FROM_STYLE_ATTRIBUTE);
        styleMap.put(key, element);
    }
    styleMap_ = styleMap;
    styleString_ = styleAttribute;
    // styleString_ = cssStyle.getCssText();
    return styleMap_;
}
Also used : StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) CSSStyleDeclarationImpl(com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl) Property(com.gargoylesoftware.css.dom.Property) CSSException(com.gargoylesoftware.css.parser.CSSException) DOMException(org.w3c.dom.DOMException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Property

use of com.gargoylesoftware.css.dom.Property in project htmlunit by HtmlUnit.

the class CSSStyleSheet method isActive.

private static boolean isActive(final HtmlUnitScriptable scriptable, final MediaQuery mediaQuery) {
    final String mediaType = mediaQuery.getMedia();
    if ("screen".equalsIgnoreCase(mediaType) || "all".equalsIgnoreCase(mediaType)) {
        for (final Property property : mediaQuery.getProperties()) {
            final double val;
            switch(property.getName()) {
                case "max-width":
                    val = pixelValue(property.getValue(), scriptable);
                    if (val == -1 || val < scriptable.getWindow().getWebWindow().getInnerWidth()) {
                        return false;
                    }
                    break;
                case "min-width":
                    val = pixelValue(property.getValue(), scriptable);
                    if (val == -1 || val > scriptable.getWindow().getWebWindow().getInnerWidth()) {
                        return false;
                    }
                    break;
                case "max-device-width":
                    val = pixelValue(property.getValue(), scriptable);
                    if (val == -1 || val < scriptable.getWindow().getScreen().getWidth()) {
                        return false;
                    }
                    break;
                case "min-device-width":
                    val = pixelValue(property.getValue(), scriptable);
                    if (val == -1 || val > scriptable.getWindow().getScreen().getWidth()) {
                        return false;
                    }
                    break;
                case "max-height":
                    val = pixelValue(property.getValue(), scriptable);
                    if (val == -1 || val < scriptable.getWindow().getWebWindow().getInnerWidth()) {
                        return false;
                    }
                    break;
                case "min-height":
                    val = pixelValue(property.getValue(), scriptable);
                    if (val == -1 || val > scriptable.getWindow().getWebWindow().getInnerWidth()) {
                        return false;
                    }
                    break;
                case "max-device-height":
                    val = pixelValue(property.getValue(), scriptable);
                    if (val == -1 || val < scriptable.getWindow().getScreen().getWidth()) {
                        return false;
                    }
                    break;
                case "min-device-height":
                    val = pixelValue(property.getValue(), scriptable);
                    if (val == -1 || val > scriptable.getWindow().getScreen().getWidth()) {
                        return false;
                    }
                    break;
                case "resolution":
                    final CSSValueImpl propValue = property.getValue();
                    val = resolutionValue(propValue);
                    if (propValue == null) {
                        return true;
                    }
                    if (val == -1 || Math.round(val) != scriptable.getWindow().getScreen().getDeviceXDPI()) {
                        return false;
                    }
                    break;
                case "max-resolution":
                    val = resolutionValue(property.getValue());
                    if (val == -1 || val < scriptable.getWindow().getScreen().getDeviceXDPI()) {
                        return false;
                    }
                    break;
                case "min-resolution":
                    val = resolutionValue(property.getValue());
                    if (val == -1 || val > scriptable.getWindow().getScreen().getDeviceXDPI()) {
                        return false;
                    }
                    break;
                case "orientation":
                    final CSSValueImpl cssValue = property.getValue();
                    if (cssValue == null) {
                        if (LOG.isWarnEnabled()) {
                            LOG.warn("CSSValue is null not supported for feature 'orientation'");
                        }
                        return true;
                    }
                    final String orient = cssValue.getCssText();
                    final WebWindow window = scriptable.getWindow().getWebWindow();
                    if ("portrait".equals(orient)) {
                        if (window.getInnerWidth() > window.getInnerHeight()) {
                            return false;
                        }
                    } else if ("landscape".equals(orient)) {
                        if (window.getInnerWidth() < window.getInnerHeight()) {
                            return false;
                        }
                    } else {
                        if (LOG.isWarnEnabled()) {
                            LOG.warn("CSSValue '" + property.getValue().getCssText() + "' not supported for feature 'orientation'.");
                        }
                        return false;
                    }
                    break;
                default:
            }
        }
        return true;
    }
    return false;
}
Also used : CSSValueImpl(com.gargoylesoftware.css.dom.CSSValueImpl) Property(com.gargoylesoftware.css.dom.Property) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Example 3 with Property

use of com.gargoylesoftware.css.dom.Property in project htmlunit by HtmlUnit.

the class ComputedCSSStyleDeclaration method applyStyleFromSelector.

/**
 * Makes a local, "computed", modification to this CSS style.
 *
 * @param declaration the style declaration
 * @param selector the selector determining that the style applies to this element
 */
public void applyStyleFromSelector(final CSSStyleDeclarationImpl declaration, final Selector selector) {
    final SelectorSpecificity specificity = selector.getSelectorSpecificity();
    for (final Property prop : declaration.getProperties()) {
        final String name = prop.getName();
        final String value = declaration.getPropertyValue(name);
        final String priority = declaration.getPropertyPriority(name);
        applyLocalStyleAttribute(name, value, priority, specificity);
    }
}
Also used : SelectorSpecificity(com.gargoylesoftware.css.parser.selector.SelectorSpecificity) AttributedString(java.text.AttributedString) Property(com.gargoylesoftware.css.dom.Property)

Example 4 with Property

use of com.gargoylesoftware.css.dom.Property in project LoboEvolution by LoboEvolution.

the class MediaQuery method toString.

/**
 * {@inheritDoc}
 */
@Override
public String toString() {
    final StringBuilder sb = new StringBuilder();
    boolean hasMedia = false;
    if (isOnly_) {
        sb.append("only ");
        sb.append(getMedia());
        hasMedia = true;
    } else if (isNot_) {
        sb.append("not ");
        sb.append(getMedia());
        hasMedia = true;
    } else {
        if (!implicitAll_) {
            sb.append(getMedia());
            hasMedia = true;
        }
    }
    for (final Property prop : properties_) {
        if (hasMedia) {
            sb.append(" and ");
        } else {
            hasMedia = true;
        }
        sb.append("(");
        sb.append(prop.toString());
        sb.append(')');
    }
    return sb.toString();
}
Also used : Property(com.gargoylesoftware.css.dom.Property)

Example 5 with Property

use of com.gargoylesoftware.css.dom.Property in project LoboEvolution by LoboEvolution.

the class CSS3Parser method mediaQuery.

// 
// media_query
// : [ONLY | NOT]? S* media_type S* [ AND S* expression ]*
// | expression [ AND S* expression ]*
// ;
// 
public final MediaQuery mediaQuery() throws ParseException {
    String s;
    MediaQuery mq;
    Property p;
    boolean only = false;
    boolean not = false;
    switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
        case NOT:
        case ONLY:
        case IDENT:
            {
                switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                    case NOT:
                    case ONLY:
                        {
                            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                                case ONLY:
                                    {
                                        jj_consume_token(ONLY);
                                        only = true;
                                        break;
                                    }
                                case NOT:
                                    {
                                        jj_consume_token(NOT);
                                        not = true;
                                        break;
                                    }
                                default:
                                    jj_la1[22] = jj_gen;
                                    jj_consume_token(-1);
                                    throw new ParseException();
                            }
                            label_13: while (true) {
                                switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                                    case S:
                                        {
                                            ;
                                            break;
                                        }
                                    default:
                                        jj_la1[23] = jj_gen;
                                        break label_13;
                                }
                                jj_consume_token(S);
                            }
                            break;
                        }
                    default:
                        jj_la1[24] = jj_gen;
                        ;
                }
                s = medium();
                mq = new MediaQuery(s, only, not);
                mq.setLocator(createLocator(token));
                label_14: while (true) {
                    switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                        case AND:
                            {
                                ;
                                break;
                            }
                        default:
                            jj_la1[25] = jj_gen;
                            break label_14;
                    }
                    jj_consume_token(AND);
                    label_15: while (true) {
                        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                            case S:
                                {
                                    ;
                                    break;
                                }
                            default:
                                jj_la1[26] = jj_gen;
                                break label_15;
                        }
                        jj_consume_token(S);
                    }
                    p = mediaExpression();
                    mq.addMediaProperty(p);
                }
                break;
            }
        case LROUND:
            {
                p = mediaExpression();
                mq = new MediaQuery(null, only, not);
                mq.setLocator(createLocator(token));
                mq.addMediaProperty(p);
                label_16: while (true) {
                    switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                        case AND:
                            {
                                ;
                                break;
                            }
                        default:
                            jj_la1[27] = jj_gen;
                            break label_16;
                    }
                    jj_consume_token(AND);
                    label_17: while (true) {
                        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                            case S:
                                {
                                    ;
                                    break;
                                }
                            default:
                                jj_la1[28] = jj_gen;
                                break label_17;
                        }
                        jj_consume_token(S);
                    }
                    p = mediaExpression();
                    mq.addMediaProperty(p);
                }
                break;
            }
        default:
            jj_la1[29] = jj_gen;
            jj_consume_token(-1);
            throw new ParseException();
    }
    return mq;
}
Also used : MediaQuery(com.gargoylesoftware.css.parser.media.MediaQuery) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) Property(com.gargoylesoftware.css.dom.Property)

Aggregations

Property (com.gargoylesoftware.css.dom.Property)6 CSSValueImpl (com.gargoylesoftware.css.dom.CSSValueImpl)2 CSSStyleDeclarationImpl (com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl)1 CSSException (com.gargoylesoftware.css.parser.CSSException)1 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)1 LexicalUnit (com.gargoylesoftware.css.parser.LexicalUnit)1 MediaQuery (com.gargoylesoftware.css.parser.media.MediaQuery)1 SelectorSpecificity (com.gargoylesoftware.css.parser.selector.SelectorSpecificity)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)1 IOException (java.io.IOException)1 AttributedString (java.text.AttributedString)1 LinkedHashMap (java.util.LinkedHashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 DOMException (org.w3c.dom.DOMException)1