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;
}
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;
}
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);
}
Aggregations