Skip to content

Commit

Permalink
Merge pull request #5432 from matthiasblaesing/css_improvements
Browse files Browse the repository at this point in the history
CSS: Support recursive calc definition, improve compatibility for custom properties, support :has pseudo-class
  • Loading branch information
matthiasblaesing authored Feb 8, 2023
2 parents 379a0ab + d4d8c15 commit 795e1d8
Show file tree
Hide file tree
Showing 41 changed files with 14,973 additions and 12,698 deletions.
4 changes: 2 additions & 2 deletions ide/css.editor/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>1.67</specification-version>
<release-version>2</release-version>
<specification-version>2.0</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@
#@uri=url ( [ !string | !identifier ] )
@uri=!uri | <var-fn>

@calc-fn=calc ( [!function-content]+ )
@calc-fn=calc ( <calc-sum> )

@var-fn=var ( !variable [, [!function-content]+]? )
@calc-sum = <calc-product> [ [ '+' | '-' ] <calc-product> ]*

@calc-product = <calc-value> [ [ '*' | '/' ] <calc-value> ]*

@calc-value = <number> | <length> | <percentage> | <calc-constant> | [ ( <calc-sum> ) ]

@calc-constant = e | pi | infinity | -infinity | NaN

@var-fn=var ( !variable [, [<function-content>]+]? )

@function-content= [ !string "(" [<function-content>]+ ")" ] | [ "(" [<function-content>]+ ")" ] | [ "{" [<function-content>]+ "}" ] | [ "[" [<function-content>]+ "]" ] | !nonbrace

@length=[<minus_operator>? !length] | <calc-fn> | <var-fn>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public void testSpecialValues() {

public void testInheritInColor() throws ParseException {
PropertyDefinition p = Properties.getPropertyDefinition( "color");
PRINT_INFO_IN_ASSERT_RESOLVE = true;
GrammarResolver.setLogging(GrammarResolver.Log.DEFAULT, true);
PRINT_INFO_IN_ASSERT_RESOLVE = false;
GrammarResolver.setLogging(GrammarResolver.Log.DEFAULT, false);
assertResolve(p.getGrammarElement(null), "inherit");

// assertCssCode("div { color: inherit }");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Position;
Expand All @@ -50,6 +49,7 @@
import org.netbeans.modules.css.lib.TestUtil;
import org.netbeans.modules.css.lib.api.CssParserResult;
import org.netbeans.modules.css.lib.api.NodeUtil;
import org.netbeans.modules.css.lib.api.properties.GrammarElement;
import org.netbeans.modules.css.lib.api.properties.GroupGrammarElement;
import org.netbeans.modules.css.lib.api.properties.Node;
import org.netbeans.modules.css.lib.api.properties.Properties;
Expand All @@ -66,7 +66,6 @@
import org.netbeans.modules.parsing.spi.Parser.Result;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;

/**
Expand Down Expand Up @@ -139,7 +138,7 @@ protected ResolvedProperty assertResolve(GroupGrammarElement tree, String inputT
System.out.println("Tokens:");
System.out.println(dumpList(pv.getTokens()));
System.out.println("Grammar:");
System.out.println(tree.toString2(0));
System.out.println(dumpGETree(tree));
}
if (PRINT_GRAMMAR_RESOLVE_TIMES) {
System.out.println(String.format("Input '%s' resolved in %s ms.", inputText, c - a));
Expand Down Expand Up @@ -605,4 +604,44 @@ public boolean isCaseSensitive() {
return isCaseSensitive;
}
}

protected static final String dumpGETree(GrammarElement ge) {
StringBuilder result = new StringBuilder();
dumpTree(result, ge, new ArrayList<>());
return result.toString();
}

private static final void dumpTree(StringBuilder sb, GrammarElement ge, List<GrammarElement> parentList) {
int level = parentList.size();
if (ge instanceof GroupGrammarElement) {
List<GrammarElement> newParentList = new ArrayList<>(parentList.size() + 1);
newParentList.addAll(parentList);
newParentList.add(ge);
String heading = ge.toString();
heading = heading.substring(0, heading.length() - 1);
indentString(sb, level);
sb.append(heading);
if (ge.getName() != null) {
sb.append("(").append(ge.getName()).append(") "); //NOI18N
}

sb.append('\n');
for (GrammarElement e : ((GroupGrammarElement) ge).elements()) {
dumpTree(sb, e, newParentList);
sb.append('\n');
}
indentString(sb, level);
sb.append(']');
} else {
sb.append(level);
sb.append(ge.toString());
sb.append("\n");
}
}

private static void indentString(StringBuilder sb, int level) {
for (int i = 0; i < level; i++) {
sb.append('\t');
}
}
}
4 changes: 2 additions & 2 deletions ide/css.lib/manifest.mf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Manifest-Version: 1.0
AutoUpdate-Show-In-Client: false
OpenIDE-Module: org.netbeans.modules.css.lib/1
OpenIDE-Module: org.netbeans.modules.css.lib/2
OpenIDE-Module-Layer: org/netbeans/modules/css/lib/layer.xml
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/css/lib/Bundle.properties
OpenIDE-Module-Specification-Version: 1.103
OpenIDE-Module-Specification-Version: 2.0
23 changes: 14 additions & 9 deletions ide/css.lib/nbproject/org-netbeans-modules-css-lib.sig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Signature file v4.1
#Version 1.101
#Version 2.0

CLSS public abstract interface java.io.Serializable

Expand Down Expand Up @@ -494,9 +494,13 @@ fld public final static org.netbeans.modules.css.lib.api.NodeType atRuleId
fld public final static org.netbeans.modules.css.lib.api.NodeType at_rule
fld public final static org.netbeans.modules.css.lib.api.NodeType body
fld public final static org.netbeans.modules.css.lib.api.NodeType bodyItem
fld public final static org.netbeans.modules.css.lib.api.NodeType braceBlock
fld public final static org.netbeans.modules.css.lib.api.NodeType bracketBlock
fld public final static org.netbeans.modules.css.lib.api.NodeType charSet
fld public final static org.netbeans.modules.css.lib.api.NodeType charSetValue
fld public final static org.netbeans.modules.css.lib.api.NodeType combinator
fld public final static org.netbeans.modules.css.lib.api.NodeType componentValue
fld public final static org.netbeans.modules.css.lib.api.NodeType componentValueOuter
fld public final static org.netbeans.modules.css.lib.api.NodeType counterStyle
fld public final static org.netbeans.modules.css.lib.api.NodeType cp_arg
fld public final static org.netbeans.modules.css.lib.api.NodeType cp_args_list
Expand Down Expand Up @@ -570,6 +574,9 @@ fld public final static org.netbeans.modules.css.lib.api.NodeType namespacePrefi
fld public final static org.netbeans.modules.css.lib.api.NodeType namespaces
fld public final static org.netbeans.modules.css.lib.api.NodeType operator
fld public final static org.netbeans.modules.css.lib.api.NodeType page
fld public final static org.netbeans.modules.css.lib.api.NodeType parenBlock
fld public final static org.netbeans.modules.css.lib.api.NodeType preservedToken
fld public final static org.netbeans.modules.css.lib.api.NodeType preservedTokenTopLevel
fld public final static org.netbeans.modules.css.lib.api.NodeType prio
fld public final static org.netbeans.modules.css.lib.api.NodeType property
fld public final static org.netbeans.modules.css.lib.api.NodeType propertyDeclaration
Expand Down Expand Up @@ -744,7 +751,6 @@ hfds value
CLSS public abstract org.netbeans.modules.css.lib.api.properties.GrammarElement
cons public init(org.netbeans.modules.css.lib.api.properties.GroupGrammarElement,java.lang.String)
fld public final static char INVISIBLE_PROPERTY_PREFIX = '@'
meth protected java.lang.String indentString(int)
meth public abstract void accept(org.netbeans.modules.css.lib.api.properties.GrammarElementVisitor)
meth public boolean equals(java.lang.Object)
meth public boolean isOptional()
Expand All @@ -756,7 +762,6 @@ meth public java.lang.String getVisibleOrigin()
meth public java.lang.String origin()
meth public java.lang.String path()
meth public java.lang.String toString()
meth public java.lang.String toString2(int)
meth public java.util.List<org.netbeans.modules.css.lib.api.properties.GrammarElement> elementsPath()
meth public org.netbeans.modules.css.lib.api.properties.GroupGrammarElement parent()
meth public static boolean isArtificialElementName(java.lang.CharSequence)
Expand All @@ -768,10 +773,10 @@ hfds maximum_occurances,minimum_occurances,name,parent,path

CLSS public abstract org.netbeans.modules.css.lib.api.properties.GrammarElementVisitor
cons public init()
meth public final void visit(org.netbeans.modules.css.lib.api.properties.GrammarElement)
meth public void visit(org.netbeans.modules.css.lib.api.properties.FixedTextGrammarElement)
meth public void visit(org.netbeans.modules.css.lib.api.properties.GroupGrammarElement)
meth public void visit(org.netbeans.modules.css.lib.api.properties.UnitGrammarElement)
meth public boolean visit(org.netbeans.modules.css.lib.api.properties.FixedTextGrammarElement)
meth public boolean visit(org.netbeans.modules.css.lib.api.properties.GroupGrammarElement)
meth public boolean visit(org.netbeans.modules.css.lib.api.properties.UnitGrammarElement)
meth public final boolean visit(org.netbeans.modules.css.lib.api.properties.GrammarElement)
supr java.lang.Object

CLSS public org.netbeans.modules.css.lib.api.properties.GrammarParseTreeConvertor
Expand Down Expand Up @@ -839,7 +844,6 @@ cons public init(org.netbeans.modules.css.lib.api.properties.GroupGrammarElement
innr public final static !enum Type
meth public boolean isVisible()
meth public java.lang.String toString()
meth public java.lang.String toString2(int)
meth public java.util.List<org.netbeans.modules.css.lib.api.properties.GrammarElement> elements()
meth public java.util.List<org.netbeans.modules.css.lib.api.properties.GrammarElement> getAllPossibleValues()
meth public org.netbeans.modules.css.lib.api.properties.GroupGrammarElement$Type getType()
Expand Down Expand Up @@ -973,6 +977,7 @@ fld public final static org.netbeans.modules.css.lib.api.properties.PropertyCate
fld public final static org.netbeans.modules.css.lib.api.properties.PropertyCategory POSITIONING
fld public final static org.netbeans.modules.css.lib.api.properties.PropertyCategory RUBY
fld public final static org.netbeans.modules.css.lib.api.properties.PropertyCategory SAFARI
fld public final static org.netbeans.modules.css.lib.api.properties.PropertyCategory SIZING
fld public final static org.netbeans.modules.css.lib.api.properties.PropertyCategory SPEECH
fld public final static org.netbeans.modules.css.lib.api.properties.PropertyCategory TEXT
fld public final static org.netbeans.modules.css.lib.api.properties.PropertyCategory TRANSFORMATIONS_2D
Expand Down Expand Up @@ -1088,7 +1093,7 @@ meth public static <%0 extends org.netbeans.modules.css.lib.api.properties.Token
meth public static org.netbeans.modules.css.lib.api.properties.TokenAcceptor getAcceptor(java.lang.String)
supr java.lang.Object
hfds INSTANCES,id
hcls GenericFunctionContent
hcls NonBrace

CLSS public static org.netbeans.modules.css.lib.api.properties.TokenAcceptor$Angle
outer org.netbeans.modules.css.lib.api.properties.TokenAcceptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
*/
public abstract class AbstractParseTreeNode extends ParseTree implements Node {

Tree parent;
private Tree parent;

private CharSequence source;
private final CharSequence source;

public AbstractParseTreeNode(CharSequence source) {
super(null);
Expand Down Expand Up @@ -73,6 +73,7 @@ public void setParent(Tree t) {

@Override
public List<Node> children() {
@SuppressWarnings("unchecked")
List<Node> ch = (List<Node>) (List<?>) getChildren();
return ch == null ? Collections.<Node>emptyList() : ch;
}
Expand Down
22 changes: 20 additions & 2 deletions ide/css.lib/src/org/netbeans/modules/css/lib/Css3.g
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
// from the module directory (ide/css.lib)
// 3. Rerun unittests
// 4. Commit Css3.g together with generated Css3Lexer.java and Css3Parser.java
//
// INFO: It is known, that the grammar does not compile without warnings
//
grammar Css3;
Expand Down Expand Up @@ -895,14 +898,15 @@ pseudo
)
| {isScssSource()}? sass_interpolation_expression_var
| ( NOT ws? LPAREN ws? ( selectorsGroup ws?)? RPAREN )
| {tokenNameEquals("is") || tokenNameEquals("where")}? ( IDENT ws? LPAREN ws? ( selectorsGroup ws?)? RPAREN )
| {tokenNameEquals("is") || tokenNameEquals("where") || tokenNameEquals("has")}? ( IDENT ws? LPAREN ws? ( selectorsGroup ws?)? RPAREN )
| ({isLessSource()}? {tokenNameEquals("extend")}? IDENT ws? LPAREN ws? selectorsGroup? RPAREN)
)
;
propertyDeclaration
:
{isCssPreprocessorSource()}? STAR? property ws? COLON ws? cp_propertyValue //cp_expression may contain the IMPORT_SYM
{isCssPreprocessorSource() && !tokenNameStartsWith("--")}? STAR? property ws? COLON ws? cp_propertyValue //cp_expression may contain the IMPORT_SYM
| {tokenNameStartsWith("--")}? property ws? COLON ws? componentValueOuter?
| STAR? property ws? COLON ws? propertyValue (ws? prio)?
;
Expand Down Expand Up @@ -932,6 +936,20 @@ expressionPredicate
( ~ (AT_IDENT | STAR | SOLIDUS | LBRACE | SEMI | RBRACE | SASS_VAR) )+ ( SEMI | RBRACE )
;

preservedToken: ~ (LPAREN | LBRACE | LBRACKET | RPAREN | RBRACE | RBRACKET);

preservedTokenTopLevel: ~ (LPAREN | LBRACE | LBRACKET | RPAREN | RBRACE | RBRACKET | SEMI );

braceBlock: LBRACE componentValue+ RBRACE;

bracketBlock: LBRACKET componentValue+ RBRACKET;

parenBlock: LPAREN componentValue+ RPAREN;

componentValue: parenBlock | braceBlock | bracketBlock | (functionName ws? LPAREN) => function | preservedToken;

componentValueOuter: (parenBlock | braceBlock | bracketBlock | (functionName ws? LPAREN) => function | preservedTokenTopLevel) componentValueOuter*;

//recovery: syncs the parser to the first identifier in the token input stream or the closing curly bracket
//since the rule matches epsilon it will always be entered
syncToDeclarationsRule
Expand Down
Loading

0 comments on commit 795e1d8

Please sign in to comment.