Search in sources :

Example 1 with CSSNode

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

the class CSSNodeToDomainObject method _createDeclaration.

@Nullable
private CSSDeclaration _createDeclaration(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.STYLEDECLARATION);
    final int nChildCount = aNode.jjtGetNumChildren();
    if (nChildCount < 1 || nChildCount > 3)
        _throwUnexpectedChildrenCount(aNode, "Expected 1-3 children but got " + nChildCount + "!");
    if (nChildCount == 1) {
        // Syntax error. E.g. "color:;"
        return null;
    }
    if (!ECSSNodeType.EXPR.isNode(aNode.jjtGetChild(1), m_eVersion)) {
        // Syntax error. E.g. "color: !important;"
        return null;
    }
    final String sProperty = aNode.jjtGetChild(0).getText();
    final CSSExpression aExpression = _createExpression(aNode.jjtGetChild(1));
    boolean bImportant = false;
    if (nChildCount == 3) {
        // Must be an "!important" node
        final CSSNode aChildNode = aNode.jjtGetChild(2);
        if (ECSSNodeType.IMPORTANT.isNode(aChildNode, m_eVersion))
            bImportant = true;
        else
            m_aErrorHandler.onCSSInterpretationError("Expected an " + ECSSNodeType.IMPORTANT.getNodeName(m_eVersion) + " token but got a " + ECSSNodeType.getNodeName(aChildNode, m_eVersion));
    }
    final CSSDeclaration ret = new CSSDeclaration(sProperty, aExpression, bImportant);
    ret.setSourceLocation(aNode.getSourceLocation());
    return ret;
}
Also used : CSSNode(com.helger.css.parser.CSSNode) Nullable(javax.annotation.Nullable)

Example 2 with CSSNode

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

the class CSSNodeToDomainObject method _createExpression.

@Nonnull
private CSSExpression _createExpression(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.EXPR);
    final CSSExpression ret = new CSSExpression();
    ret.setSourceLocation(aNode.getSourceLocation());
    for (final CSSNode aChildNode : aNode) {
        if (ECSSNodeType.EXPRTERM.isNode(aChildNode, m_eVersion))
            ret.addMember(_createExpressionTerm(aChildNode));
        else if (ECSSNodeType.EXPROPERATOR.isNode(aChildNode, m_eVersion)) {
            final String sText = aChildNode.getText();
            final ECSSExpressionOperator eOp = ECSSExpressionOperator.getFromNameOrNull(sText);
            if (eOp == null)
                m_aErrorHandler.onCSSInterpretationError("Failed to parse expression operator '" + sText + "'");
            else
                ret.addMember(eOp);
        } 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 3 with CSSNode

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

the class CSSNodeToDomainObject method _createSelector.

@Nonnull
private CSSSelector _createSelector(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.SELECTOR);
    final CSSSelector ret = new CSSSelector();
    ret.setSourceLocation(aNode.getSourceLocation());
    for (final CSSNode aChildNode : aNode) {
        final ICSSSelectorMember aMember = _createSelectorMember(aChildNode);
        if (aMember != null)
            ret.addMember(aMember);
    }
    return ret;
}
Also used : CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull)

Example 4 with CSSNode

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

the class CSSNodeToDomainObject method _createFontFaceRule.

@Nonnull
private CSSFontFaceRule _createFontFaceRule(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.FONTFACERULE);
    final String sFontFaceDeclaration = aNode.getText();
    final CSSFontFaceRule ret = new CSSFontFaceRule(sFontFaceDeclaration);
    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 font-face rule child: " + ECSSNodeType.getNodeName(aChildNode, m_eVersion));
    }
    return ret;
}
Also used : CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull)

Example 5 with CSSNode

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

the class CSSNodeToDomainObject method _createSupportsConditionMemberRecursive.

@Nullable
private ICSSSupportsConditionMember _createSupportsConditionMemberRecursive(@Nonnull final CSSNode aNode) {
    final int nChildCount = aNode.jjtGetNumChildren();
    if (ECSSNodeType.SUPPORTSCONDITIONOPERATOR.isNode(aNode, m_eVersion)) {
        if (nChildCount != 0)
            _throwUnexpectedChildrenCount(aNode, "Expected no children but got " + nChildCount + "!");
        return ECSSSupportsConditionOperator.getFromNameCaseInsensitiveOrNull(aNode.getText());
    }
    if (ECSSNodeType.SUPPORTSNEGATION.isNode(aNode, m_eVersion)) {
        if (nChildCount != 1)
            _throwUnexpectedChildrenCount(aNode, "Expected at exactly 1 child but got " + nChildCount + "!");
        final ICSSSupportsConditionMember aNestedMember = _createSupportsConditionMemberRecursive(aNode.jjtGetChild(0));
        if (aNestedMember == null)
            return null;
        final CSSSupportsConditionNegation ret = new CSSSupportsConditionNegation(aNestedMember);
        ret.setSourceLocation(aNode.getSourceLocation());
        return ret;
    }
    if (ECSSNodeType.SUPPORTSCONDITIONINPARENS.isNode(aNode, m_eVersion)) {
        if (nChildCount != 1)
            _throwUnexpectedChildrenCount(aNode, "Expected at exactly 1 child but got " + nChildCount + "!");
        final CSSNode aChildNode = aNode.jjtGetChild(0);
        if (ECSSNodeType.STYLEDECLARATION.isNode(aChildNode, m_eVersion)) {
            final CSSDeclaration aDeclaration = _createDeclaration(aChildNode);
            if (aDeclaration == null)
                throw new CSSHandlingException(aChildNode, "The style declaration in the @supports rule is invalid!");
            final CSSSupportsConditionDeclaration ret = new CSSSupportsConditionDeclaration(aDeclaration);
            ret.setSourceLocation(aNode.getSourceLocation());
            return ret;
        }
        if (ECSSNodeType.SUPPORTSCONDITION.isNode(aChildNode, m_eVersion)) {
            final CSSSupportsConditionNested ret = new CSSSupportsConditionNested();
            for (final CSSNode aChildChildNode : aChildNode) {
                final ICSSSupportsConditionMember aMember = _createSupportsConditionMemberRecursive(aChildChildNode);
                if (aMember != null)
                    ret.addMember(aMember);
            }
            return ret;
        }
        m_aErrorHandler.onCSSInterpretationError("Unsupported supportsConditionInParents child: " + ECSSNodeType.getNodeName(aChildNode, m_eVersion));
        return null;
    }
    if (!ECSSNodeType.isErrorNode(aNode, m_eVersion))
        m_aErrorHandler.onCSSInterpretationError("Unsupported supports-condition child: " + ECSSNodeType.getNodeName(aNode, m_eVersion));
    return null;
}
Also used : CSSNode(com.helger.css.parser.CSSNode) Nullable(javax.annotation.Nullable)

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