Search in sources :

Example 21 with CSSNode

use of com.helger.css.parser.CSSNode 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)

Example 22 with CSSNode

use of com.helger.css.parser.CSSNode in project ph-css by phax.

the class CSSNodeToDomainObject method _createExpressionTerm.

@Nonnull
private ICSSExpressionMember _createExpressionTerm(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.EXPRTERM);
    final int nChildCount = aNode.jjtGetNumChildren();
    if (nChildCount > 1)
        _throwUnexpectedChildrenCount(aNode, "Expected 0 or 1 children but got " + nChildCount + "!");
    // Simple value
    if (nChildCount == 0) {
        final CSSExpressionMemberTermSimple ret = new CSSExpressionMemberTermSimple(aNode.getText());
        ret.setSourceLocation(aNode.getSourceLocation());
        return ret;
    }
    final CSSNode aChildNode = aNode.jjtGetChild(0);
    if (ECSSNodeType.URL.isNode(aChildNode, m_eVersion)) {
        // URI value
        return _createExpressionURL(aChildNode);
    } else if (ECSSNodeType.FUNCTION.isNode(aChildNode, m_eVersion)) {
        // function value
        return _createExpressionFunction(aChildNode);
    } else if (ECSSNodeType.MATH.isNode(aChildNode, m_eVersion)) {
        // Math value
        return _createExpressionMathTerm(aChildNode);
    } else if (ECSSNodeType.LINE_NAMES.isNode(aChildNode, m_eVersion)) {
        // Math value
        return _createExpressionLineNamesTerm(aChildNode);
    } else
        throw new IllegalStateException("Expected an expression term but got " + ECSSNodeType.getNodeName(aChildNode, m_eVersion));
}
Also used : CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull)

Example 23 with CSSNode

use of com.helger.css.parser.CSSNode in project ph-css by phax.

the class CSSNodeToDomainObject method _createExpressionMathTerm.

@Nonnull
private CSSExpressionMemberMath _createExpressionMathTerm(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.MATH);
    final CSSExpressionMemberMath ret = new CSSExpressionMemberMath();
    ret.setSourceLocation(aNode.getSourceLocation());
    // read all sums
    for (final CSSNode aChildNode : aNode) {
        if (ECSSNodeType.MATHPRODUCT.isNode(aChildNode, m_eVersion)) {
            ret.addMember(_createExpressionMathProduct(aChildNode));
        } else if (ECSSNodeType.MATHSUMOPERATOR.isNode(aChildNode, m_eVersion)) {
            final String sText = aChildNode.getText();
            final ECSSMathOperator eMathOp = ECSSMathOperator.getFromNameOrNull(sText);
            if (eMathOp == null)
                m_aErrorHandler.onCSSInterpretationError("Failed to parse math operator '" + sText + "'");
            else
                ret.addMember(eMathOp);
        } else
            m_aErrorHandler.onCSSInterpretationError("Unsupported child of " + ECSSNodeType.getNodeName(aNode, m_eVersion) + ": " + ECSSNodeType.getNodeName(aChildNode, m_eVersion));
    }
    return ret;
}
Also used : CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull)

Example 24 with CSSNode

use of com.helger.css.parser.CSSNode in project ph-css by phax.

the class CSSNodeToDomainObject method _createMediaExpr.

@Nonnull
private CSSMediaExpression _createMediaExpr(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.MEDIAEXPR);
    final int nChildCount = aNode.jjtGetNumChildren();
    if (nChildCount != 1 && nChildCount != 2)
        _throwUnexpectedChildrenCount(aNode, "Expected 1 or 2 children but got " + nChildCount + "!");
    final CSSNode aFeatureNode = aNode.jjtGetChild(0);
    if (!ECSSNodeType.MEDIAFEATURE.isNode(aFeatureNode, m_eVersion))
        throw new IllegalStateException("Expected a media feature but got " + ECSSNodeType.getNodeName(aFeatureNode, m_eVersion));
    final String sFeature = aFeatureNode.getText();
    if (ECSSMediaExpressionFeature.getFromNameOrNull(sFeature) == null)
        m_aErrorHandler.onCSSInterpretationWarning("Media expression uses unknown feature '" + sFeature + "'");
    CSSMediaExpression ret;
    if (nChildCount == 1) {
        // Feature only
        ret = new CSSMediaExpression(sFeature);
    } else {
        // Feature + value
        final CSSNode aValueNode = aNode.jjtGetChild(1);
        ret = new CSSMediaExpression(sFeature, _createExpression(aValueNode));
    }
    ret.setSourceLocation(aNode.getSourceLocation());
    return ret;
}
Also used : CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull)

Example 25 with CSSNode

use of com.helger.css.parser.CSSNode in project ph-css by phax.

the class CSSNodeToDomainObject method _createViewportRule.

@Nonnull
private CSSViewportRule _createViewportRule(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.VIEWPORTRULE);
    // Get the identifier (e.g. the default "@viewport" or the non-standard
    // "@-ms-viewport")
    final String sViewportDeclaration = aNode.getText();
    final CSSViewportRule ret = new CSSViewportRule(sViewportDeclaration);
    ret.setSourceLocation(aNode.getSourceLocation());
    for (final CSSNode aChildNode : aNode) {
        if (ECSSNodeType.STYLEDECLARATIONLIST.isNode(aChildNode, m_eVersion)) {
            // Read all contained declarations
            _readStyleDeclarationList(aChildNode, aDeclaration -> ret.addDeclaration(aDeclaration));
        } else if (!ECSSNodeType.isErrorNode(aChildNode, m_eVersion))
            m_aErrorHandler.onCSSInterpretationError("Unsupported viewport rule child: " + ECSSNodeType.getNodeName(aChildNode, m_eVersion));
    }
    return ret;
}
Also used : CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull)

Aggregations

CSSNode (com.helger.css.parser.CSSNode)30 Nonnull (javax.annotation.Nonnull)20 Nullable (javax.annotation.Nullable)9 ECSSVersion (com.helger.css.ECSSVersion)5 CSSCharStream (com.helger.css.parser.CSSCharStream)5 ICSSInterpretErrorHandler (com.helger.css.reader.errorhandler.ICSSInterpretErrorHandler)5 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 EModifier (com.helger.css.decl.CSSMediaQuery.EModifier)3 ICSSParseExceptionCallback (com.helger.css.handler.ICSSParseExceptionCallback)3 ICSSParseErrorHandler (com.helger.css.reader.errorhandler.ICSSParseErrorHandler)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 ValueEnforcer (com.helger.commons.ValueEnforcer)2 Nonempty (com.helger.commons.annotation.Nonempty)2 ICommonsList (com.helger.commons.collection.impl.ICommonsList)2 IHasReader (com.helger.commons.io.IHasReader)2 NonBlockingStringReader (com.helger.commons.io.stream.NonBlockingStringReader)2 com.helger.css.decl (com.helger.css.decl)2 DoNothingCSSParseExceptionCallback (com.helger.css.handler.DoNothingCSSParseExceptionCallback)2 ECSSMediaExpressionFeature (com.helger.css.media.ECSSMediaExpressionFeature)2 ECSSMedium (com.helger.css.media.ECSSMedium)2