Search in sources :

Example 26 with CSSNode

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

the class CSSNodeToDomainObject method _createPageRule.

@Nonnull
@SuppressFBWarnings("IL_INFINITE_LOOP")
private CSSPageRule _createPageRule(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.PAGERULE);
    final int nChildCount = aNode.jjtGetNumChildren();
    if (m_eVersion == ECSSVersion.CSS30) {
        if (nChildCount < 1)
            _throwUnexpectedChildrenCount(aNode, "Expected at least 1 child but got " + nChildCount + "!");
        // Read page selectors (0-n)
        final ICommonsList<String> aSelectors = new CommonsArrayList<>();
        for (int nIndex = 0; nIndex < nChildCount - 1; ++nIndex) {
            final CSSNode aChildNode = aNode.jjtGetChild(nIndex);
            _expectNodeType(aChildNode, ECSSNodeType.PAGESELECTOR);
            aSelectors.add(aChildNode.getText());
        }
        final CSSPageRule ret = new CSSPageRule(aSelectors);
        ret.setSourceLocation(aNode.getSourceLocation());
        // Read page body
        final CSSNode aBodyNode = aNode.jjtGetChild(nChildCount - 1);
        _expectNodeType(aBodyNode, ECSSNodeType.PAGERULEBLOCK);
        final int nBodyChildren = aBodyNode.jjtGetNumChildren();
        for (int nIndex = 0; nIndex < nBodyChildren; ++nIndex) {
            final CSSNode aBodyChildNode = aBodyNode.jjtGetChild(nIndex);
            if (ECSSNodeType.STYLEDECLARATION.isNode(aBodyChildNode, m_eVersion)) {
                final CSSDeclaration aDeclaration = _createDeclaration(aBodyChildNode);
                if (aDeclaration != null)
                    ret.addMember(aDeclaration);
            } else if (ECSSNodeType.PAGEMARGINSYMBOL.isNode(aBodyChildNode, m_eVersion)) {
                final CSSPageMarginBlock aBlock = new CSSPageMarginBlock(aBodyChildNode.getText());
                final CSSNode aBodyNextChildNode = aBodyNode.jjtGetChild(nIndex + 1);
                _readStyleDeclarationList(aBodyNextChildNode, aDeclaration -> aBlock.addDeclaration(aDeclaration));
                ret.addMember(aBlock);
                // Skip style declaration list
                ++nIndex;
            } else if (!ECSSNodeType.isErrorNode(aBodyChildNode, m_eVersion))
                m_aErrorHandler.onCSSInterpretationError("Unsupported page rule body child: " + ECSSNodeType.getNodeName(aBodyChildNode, m_eVersion));
        }
        return ret;
    }
    String sPseudoPage = null;
    int nStartIndex = 0;
    if (nChildCount > 0) {
        final CSSNode aFirstChild = aNode.jjtGetChild(0);
        if (ECSSNodeType.PSEUDOPAGE.isNode(aFirstChild, m_eVersion)) {
            sPseudoPage = aFirstChild.getText();
            nStartIndex++;
        }
    }
    final CSSPageRule ret = new CSSPageRule(sPseudoPage);
    ret.setSourceLocation(aNode.getSourceLocation());
    for (int nIndex = nStartIndex; nIndex < nChildCount; ++nIndex) {
        final CSSNode aChildNode = aNode.jjtGetChild(nIndex);
        if (ECSSNodeType.STYLEDECLARATIONLIST.isNode(aChildNode, m_eVersion)) {
            // Read all contained declarations
            _readStyleDeclarationList(aChildNode, aDeclaration -> ret.addMember(aDeclaration));
        } else if (!ECSSNodeType.isErrorNode(aChildNode, m_eVersion))
            m_aErrorHandler.onCSSInterpretationError("Unsupported page rule child: " + ECSSNodeType.getNodeName(aChildNode, m_eVersion));
    }
    return ret;
}
Also used : ICSSInterpretErrorHandler(com.helger.css.reader.errorhandler.ICSSInterpretErrorHandler) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ECSSMedium(com.helger.css.media.ECSSMedium) CSSParseHelper(com.helger.css.parser.CSSParseHelper) ECSSVersion(com.helger.css.ECSSVersion) ValueEnforcer(com.helger.commons.ValueEnforcer) Consumer(java.util.function.Consumer) com.helger.css.decl(com.helger.css.decl) CSSNode(com.helger.css.parser.CSSNode) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Nonempty(com.helger.commons.annotation.Nonempty) ECSSMediaExpressionFeature(com.helger.css.media.ECSSMediaExpressionFeature) Nonnull(javax.annotation.Nonnull) EModifier(com.helger.css.decl.CSSMediaQuery.EModifier) Nullable(javax.annotation.Nullable) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 27 with CSSNode

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

the class CSSNodeToDomainObject method _createSelectorMember.

@Nullable
private ICSSSelectorMember _createSelectorMember(final CSSNode aNode) {
    final int nChildCount = aNode.jjtGetNumChildren();
    if (ECSSNodeType.NAMESPACEPREFIX.isNode(aNode, m_eVersion) || ECSSNodeType.ELEMENTNAME.isNode(aNode, m_eVersion) || ECSSNodeType.HASH.isNode(aNode, m_eVersion) || ECSSNodeType.CLASS.isNode(aNode, m_eVersion)) {
        if (nChildCount != 0)
            _throwUnexpectedChildrenCount(aNode, "CSS simple selector member expected 0 children and got " + nChildCount);
        final CSSSelectorSimpleMember ret = new CSSSelectorSimpleMember(aNode.getText());
        ret.setSourceLocation(aNode.getSourceLocation());
        return ret;
    }
    if (ECSSNodeType.ATTRIB.isNode(aNode, m_eVersion))
        return _createSelectorAttribute(aNode);
    if (ECSSNodeType.SELECTORCOMBINATOR.isNode(aNode, m_eVersion)) {
        final String sText = aNode.getText();
        final ECSSSelectorCombinator eCombinator = ECSSSelectorCombinator.getFromNameOrNull(sText);
        if (eCombinator == null)
            m_aErrorHandler.onCSSInterpretationError("Failed to parse CSS selector combinator '" + sText + "'");
        return eCombinator;
    }
    if (ECSSNodeType.NEGATION.isNode(aNode, m_eVersion)) {
        // Note: no children don't make sense but are syntactically allowed!
        final ICommonsList<CSSSelector> aNestedSelectors = new CommonsArrayList<>();
        for (int i = 0; i < nChildCount; ++i) {
            final CSSNode aChildNode = aNode.jjtGetChild(0);
            final CSSSelector aSelector = _createSelector(aChildNode);
            aNestedSelectors.add(aSelector);
        }
        final CSSSelectorMemberNot ret = new CSSSelectorMemberNot(aNestedSelectors);
        ret.setSourceLocation(aNode.getSourceLocation());
        return ret;
    }
    if (ECSSNodeType.PSEUDO.isNode(aNode, m_eVersion)) {
        if (nChildCount == 0) {
            // E.g. ":focus" or ":hover"
            final CSSSelectorSimpleMember ret = new CSSSelectorSimpleMember(aNode.getText());
            ret.setSourceLocation(aNode.getSourceLocation());
            return ret;
        }
        if (nChildCount == 1) {
            final CSSNode aChildNode = aNode.jjtGetChild(0);
            if (ECSSNodeType.NTH.isNode(aChildNode, m_eVersion)) {
                // Handle nth. E.g. ":nth-child(even)" or ":nth-child(3n+1)"
                final CSSSelectorSimpleMember ret = new CSSSelectorSimpleMember(aNode.getText() + aChildNode.getText() + ")");
                ret.setSourceLocation(aNode.getSourceLocation());
                return ret;
            }
            // It's a function (e.g. ":lang(fr)")
            final CSSExpression aExpr = _createExpression(aChildNode);
            final CSSSelectorMemberFunctionLike ret = new CSSSelectorMemberFunctionLike(aNode.getText(), aExpr);
            ret.setSourceLocation(aNode.getSourceLocation());
            return ret;
        }
        throw new UnsupportedOperationException("Not supporting pseudo-selectors with functions and " + nChildCount + " args: " + aNode.toString());
    }
    m_aErrorHandler.onCSSInterpretationError("Unsupported selector child: " + ECSSNodeType.getNodeName(aNode, m_eVersion));
    return null;
}
Also used : CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) CSSNode(com.helger.css.parser.CSSNode) Nullable(javax.annotation.Nullable)

Example 28 with CSSNode

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

the class CSSNodeToDomainObject method _createKeyframesRule.

@Nonnull
private CSSKeyframesRule _createKeyframesRule(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.KEYFRAMESRULE);
    final int nChildCount = aNode.jjtGetNumChildren();
    if (nChildCount == 0)
        _throwUnexpectedChildrenCount(aNode, "Expected at least 1 child but got " + nChildCount + "!");
    // Get the identifier (e.g. the default "@keyframes" or the non-standard
    // "@-webkit-keyframes")
    final String sKeyframesDeclaration = aNode.getText();
    // get the name of the animation
    final CSSNode aAnimationNameNode = aNode.jjtGetChild(0);
    _expectNodeType(aAnimationNameNode, ECSSNodeType.KEYFRAMESIDENTIFIER);
    final String sAnimationName = aAnimationNameNode.getText();
    final CSSKeyframesRule ret = new CSSKeyframesRule(sKeyframesDeclaration, sAnimationName);
    ret.setSourceLocation(aNode.getSourceLocation());
    // Get the key frame blocks
    int nIndex = 1;
    CSSKeyframesBlock aBlock = null;
    while (nIndex < nChildCount) {
        final CSSNode aChildNode = aNode.jjtGetChild(nIndex);
        if (ECSSNodeType.KEYFRAMESSELECTOR.isNode(aChildNode, m_eVersion)) {
            // Read all single selectors
            final ICommonsList<String> aKeyframesSelectors = new CommonsArrayList<>();
            for (final CSSNode aSelectorChild : aChildNode) {
                _expectNodeType(aSelectorChild, ECSSNodeType.SINGLEKEYFRAMESELECTOR);
                aKeyframesSelectors.add(aSelectorChild.getText());
            }
            aBlock = new CSSKeyframesBlock(aKeyframesSelectors);
            aBlock.setSourceLocation(aChildNode.getSourceLocation());
            ret.addBlock(aBlock);
        } else if (ECSSNodeType.STYLEDECLARATIONLIST.isNode(aChildNode, m_eVersion)) {
            if (aBlock == null)
                throw new IllegalStateException("No keyframes block present!");
            // Read all contained declarations
            final CSSKeyframesBlock aFinalBlock = aBlock;
            _readStyleDeclarationList(aChildNode, aDeclaration -> aFinalBlock.addDeclaration(aDeclaration));
        } else if (!ECSSNodeType.isErrorNode(aChildNode, m_eVersion))
            m_aErrorHandler.onCSSInterpretationError("Unsupported keyframes rule child: " + ECSSNodeType.getNodeName(aChildNode, m_eVersion));
        ++nIndex;
    }
    return ret;
}
Also used : ICSSInterpretErrorHandler(com.helger.css.reader.errorhandler.ICSSInterpretErrorHandler) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ECSSMedium(com.helger.css.media.ECSSMedium) CSSParseHelper(com.helger.css.parser.CSSParseHelper) ECSSVersion(com.helger.css.ECSSVersion) ValueEnforcer(com.helger.commons.ValueEnforcer) Consumer(java.util.function.Consumer) com.helger.css.decl(com.helger.css.decl) CSSNode(com.helger.css.parser.CSSNode) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Nonempty(com.helger.commons.annotation.Nonempty) ECSSMediaExpressionFeature(com.helger.css.media.ECSSMediaExpressionFeature) Nonnull(javax.annotation.Nonnull) EModifier(com.helger.css.decl.CSSMediaQuery.EModifier) Nullable(javax.annotation.Nullable) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull)

Example 29 with CSSNode

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

the class CSSNodeToDomainObject method _createExpressionMathProduct.

@Nonnull
private CSSExpressionMemberMathProduct _createExpressionMathProduct(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.MATHPRODUCT);
    final CSSExpressionMemberMathProduct ret = new CSSExpressionMemberMathProduct();
    ret.setSourceLocation(aNode.getSourceLocation());
    // read all sums
    for (final CSSNode aChildNode : aNode) {
        if (ECSSNodeType.MATHUNIT.isNode(aChildNode, m_eVersion)) {
            final int nChildCount = aChildNode.jjtGetNumChildren();
            if (nChildCount == 0) {
                final CSSExpressionMemberMathUnitSimple aMember = new CSSExpressionMemberMathUnitSimple(aChildNode.getText());
                aMember.setSourceLocation(aChildNode.getSourceLocation());
                ret.addMember(aMember);
            } else if (nChildCount == 1 && ECSSNodeType.FUNCTION.isNode(aChildNode.jjtGetChild(0), m_eVersion)) {
                // Source location is taken from aNestedProduct
                ret.addMember(_createExpressionFunction(aChildNode.jjtGetChild(0)));
            } else {
                if ((nChildCount % 2) == 0)
                    _throwUnexpectedChildrenCount(aChildNode, "CSS math unit expected odd child count and got " + nChildCount);
                final CSSExpressionMemberMathProduct aNestedProduct = new CSSExpressionMemberMathProduct();
                for (int i = 0; i < nChildCount; ++i) {
                    final CSSNode aChildChildNode = aChildNode.jjtGetChild(i);
                    if (ECSSNodeType.MATHPRODUCT.isNode(aChildChildNode, m_eVersion)) {
                        // Source location is taken from aNestedProduct
                        aNestedProduct.addMember(_createExpressionMathProduct(aChildChildNode));
                    } else if (ECSSNodeType.MATHSUMOPERATOR.isNode(aChildChildNode, m_eVersion)) {
                        final String sText = aChildChildNode.getText();
                        final ECSSMathOperator eMathOp = ECSSMathOperator.getFromNameOrNull(sText);
                        if (eMathOp == null)
                            m_aErrorHandler.onCSSInterpretationError("Failed to parse math operator '" + sText + "'");
                        else
                            aNestedProduct.addMember(eMathOp);
                    } else
                        m_aErrorHandler.onCSSInterpretationError("Unsupported child of " + ECSSNodeType.getNodeName(aChildNode, m_eVersion) + ": " + ECSSNodeType.getNodeName(aChildChildNode, m_eVersion));
                }
                ret.addMember(new CSSExpressionMemberMathUnitProduct(aNestedProduct));
            }
        } else if (ECSSNodeType.MATHPRODUCTOPERATOR.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 product 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 30 with CSSNode

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

the class CSSNodeToDomainObject method _createExpressionLineNamesTerm.

@Nonnull
private CSSExpressionMemberLineNames _createExpressionLineNamesTerm(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.LINE_NAMES);
    final CSSExpressionMemberLineNames ret = new CSSExpressionMemberLineNames();
    ret.setSourceLocation(aNode.getSourceLocation());
    // read all sums
    for (final CSSNode aChildNode : aNode) {
        if (ECSSNodeType.LINE_NAME.isNode(aChildNode, m_eVersion)) {
            ret.addMember(aChildNode.getText());
        } 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)

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