Search in sources :

Example 1 with EModifier

use of com.helger.css.decl.CSSMediaQuery.EModifier in project ph-css by phax.

the class CSSNodeToDomainObject method _createMediaQuery.

@Nonnull
@SuppressFBWarnings("IL_INFINITE_LOOP")
private CSSMediaQuery _createMediaQuery(@Nonnull final CSSNode aNode) {
    if (ECSSNodeType.MEDIUM.isNode(aNode, m_eVersion)) {
        // CSS 2.1 compatibility
        final String sMedium = aNode.getText();
        if (ECSSMedium.getFromNameOrNull(sMedium) == null)
            m_aErrorHandler.onCSSInterpretationWarning("CSS " + m_eVersion.getVersionString() + " Media query uses unknown medium '" + sMedium + "'");
        final CSSMediaQuery ret = new CSSMediaQuery(EModifier.NONE, sMedium);
        ret.setSourceLocation(aNode.getSourceLocation());
        return ret;
    }
    // CSS 3.0 media query
    _expectNodeType(aNode, ECSSNodeType.MEDIAQUERY);
    final int nChildCount = aNode.jjtGetNumChildren();
    int nStartIndex = 0;
    EModifier eModifier = EModifier.NONE;
    // Check if a media modifier is present
    if (nChildCount > 0) {
        final CSSNode aFirstChildNode = aNode.jjtGetChild(0);
        if (ECSSNodeType.MEDIAMODIFIER.isNode(aFirstChildNode, m_eVersion)) {
            final String sMediaModifier = aFirstChildNode.getText();
            // The "mediaModifier" token might be present, but without text!!!
            if (sMediaModifier != null) {
                if ("not".equalsIgnoreCase(sMediaModifier))
                    eModifier = EModifier.NOT;
                else if ("only".equalsIgnoreCase(sMediaModifier))
                    eModifier = EModifier.ONLY;
                else
                    m_aErrorHandler.onCSSInterpretationError("Unsupported media modifier '" + sMediaModifier + "' found!");
            }
            ++nStartIndex;
        }
    }
    // Next check if a medium is present
    String sMedium = null;
    if (nChildCount > nStartIndex) {
        final CSSNode aNextChild = aNode.jjtGetChild(nStartIndex);
        if (ECSSNodeType.MEDIUM.isNode(aNextChild, m_eVersion)) {
            sMedium = aNextChild.getText();
            if (ECSSMedium.getFromNameOrNull(sMedium) == null)
                m_aErrorHandler.onCSSInterpretationWarning("CSS " + m_eVersion.getVersionString() + " media query uses unknown medium '" + sMedium + "'");
            ++nStartIndex;
        }
    }
    final CSSMediaQuery ret = new CSSMediaQuery(eModifier, sMedium);
    ret.setSourceLocation(aNode.getSourceLocation());
    for (int i = nStartIndex; i < nChildCount; ++i) {
        final CSSNode aChildNode = aNode.jjtGetChild(i);
        if (ECSSNodeType.MEDIAEXPR.isNode(aChildNode, m_eVersion))
            ret.addMediaExpression(_createMediaExpr(aChildNode));
        else if (!ECSSNodeType.isErrorNode(aChildNode, m_eVersion))
            m_aErrorHandler.onCSSInterpretationError("Unsupported media query child: " + ECSSNodeType.getNodeName(aChildNode, m_eVersion));
    }
    return ret;
}
Also used : EModifier(com.helger.css.decl.CSSMediaQuery.EModifier) CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

EModifier (com.helger.css.decl.CSSMediaQuery.EModifier)1 CSSNode (com.helger.css.parser.CSSNode)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Nonnull (javax.annotation.Nonnull)1