Search in sources :

Example 11 with CommonsArrayList

use of com.helger.commons.collection.impl.CommonsArrayList 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 12 with CommonsArrayList

use of com.helger.commons.collection.impl.CommonsArrayList 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 13 with CommonsArrayList

use of com.helger.commons.collection.impl.CommonsArrayList in project ph-css by phax.

the class WikiVisitSelectors method readAllSelectors.

public void readAllSelectors() {
    final String sStyle = "blockquote p,\r\n" + "blockquote ul,\r\n" + "blockquote ol {\r\n" + "  line-height:normal;\r\n" + "  font-style:italic;\r\n" + "}\r\n" + "\r\n" + "a { color:#FFEA6F; }\r\n" + "\r\n" + "a:hover { text-decoration:none; }\r\n" + "\r\n" + "img { border:none; }";
    final CascadingStyleSheet aCSS = CSSReader.readFromString(sStyle, ECSSVersion.CSS30);
    final ICommonsList<String> aAllSelectors = new CommonsArrayList<>();
    CSSVisitor.visitCSS(aCSS, new DefaultCSSVisitor() {

        @Override
        public void onStyleRuleSelector(@Nonnull final CSSSelector aSelector) {
            aAllSelectors.add(aSelector.getAsCSSString(new CSSWriterSettings(ECSSVersion.CSS30)));
        }
    });
    System.out.println(aAllSelectors);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) DefaultCSSVisitor(com.helger.css.decl.visit.DefaultCSSVisitor) CSSSelector(com.helger.css.decl.CSSSelector) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList)

Aggregations

CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)13 Nullable (javax.annotation.Nullable)6 Nonnull (javax.annotation.Nonnull)5 ICommonsList (com.helger.commons.collection.impl.ICommonsList)4 ValueEnforcer (com.helger.commons.ValueEnforcer)3 Nonempty (com.helger.commons.annotation.Nonempty)3 CSSNode (com.helger.css.parser.CSSNode)3 File (java.io.File)3 CommandResult (com.helger.as2.cmd.CommandResult)2 ICommand (com.helger.as2.cmd.ICommand)2 CommandTokenizer (com.helger.as2.util.CommandTokenizer)2 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)2 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)2 IReadableResource (com.helger.commons.io.resource.IReadableResource)2 StringHelper (com.helger.commons.string.StringHelper)2 ECSSVersion (com.helger.css.ECSSVersion)2 com.helger.css.decl (com.helger.css.decl)2 EModifier (com.helger.css.decl.CSSMediaQuery.EModifier)2 ECSSMediaExpressionFeature (com.helger.css.media.ECSSMediaExpressionFeature)2 ECSSMedium (com.helger.css.media.ECSSMedium)2