Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/parser/cssParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ export class Parser {
return hasContent ? this.finish(node) : null;
}

public _parseDeclaration(stopTokens?: TokenType[]): nodes.Declaration | null {
const customProperty = this._tryParseCustomPropertyDeclaration(stopTokens);
public _parseDeclaration(stopTokens?: TokenType[], standaloneCustomPropertyValid = false): nodes.Declaration | null {
const customProperty = this._tryParseCustomPropertyDeclaration(stopTokens, standaloneCustomPropertyValid);
if (customProperty) {
return customProperty;
}
Expand Down Expand Up @@ -502,7 +502,7 @@ export class Parser {
return this.finish(node);
}

public _tryParseCustomPropertyDeclaration(stopTokens?: TokenType[]): nodes.CustomPropertyDeclaration | null {
public _tryParseCustomPropertyDeclaration(stopTokens?: TokenType[], standaloneCustomPropertyValid = false): nodes.CustomPropertyDeclaration | null {
if (!this.peekRegExp(TokenType.Ident, /^--/)) {
return null;
}
Expand All @@ -512,6 +512,9 @@ export class Parser {
}

if (!this.accept(TokenType.Colon)) {
if (standaloneCustomPropertyValid) {
return this.finish(node);
}
return this.finish(node, ParseError.ColonExpected, [TokenType.Colon]);
}
if (this.prevToken) {
Expand Down Expand Up @@ -1404,7 +1407,16 @@ export class Parser {
this.consumeToken(); // @container

node.addChild(this._parseIdent()); // optional container name
node.addChild(this._parseContainerQuery());
if (node.addChild(this._parseContainerQuery())) {
while (this.accept(TokenType.Comma)) {
if (this.peek(TokenType.CurlyL)) {
break;
}

node.addChild(this._parseIdent()); // optional container name
node.addChild(this._parseContainerQuery());
}
}

return this._parseBody(node, this._parseContainerDeclaration.bind(this, isNested));
}
Expand All @@ -1416,7 +1428,7 @@ export class Parser {
if (this.acceptIdent('not')) {
node.addChild(this._parseContainerQueryInParens());
} else {
node.addChild(this._parseContainerQueryInParens());
node.addChild(this._parseContainerQueryInParens(true));
if (this.peekIdent('and')) {
while (this.acceptIdent('and')) {
node.addChild(this._parseContainerQueryInParens());
Expand All @@ -1430,7 +1442,7 @@ export class Parser {
return this.finish(node);
}

public _parseContainerQueryInParens(): nodes.Node {
public _parseContainerQueryInParens(optional = false): nodes.Node | null {
// <query-in-parens> = ( <container-query> )
// | ( <size-feature> )
// | style( <style-query> )
Expand All @@ -1454,6 +1466,9 @@ export class Parser {
return this.finish(node, ParseError.RightParenthesisExpected, [], [TokenType.CurlyL]);
}
} else {
if (optional) {
return null;
}
return this.finish(node, ParseError.LeftParenthesisExpected, [], [TokenType.CurlyL]);
}
return this.finish(node);
Expand Down Expand Up @@ -1482,7 +1497,7 @@ export class Parser {
}
}
} else {
node.addChild(this._parseDeclaration([TokenType.ParenthesisR]));
node.addChild(this._parseDeclaration([TokenType.ParenthesisR], true));
}
return this.finish(node);
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/css/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,13 @@ suite('CSS - Parser', () => {

test('@container', function () {
const parser = new Parser();
assertNode(`@container card { #inner { background-color: skyblue; }}`, parser, parser._parseStylesheet.bind(parser));
assertNode(`@container (width <= 150px) { #inner { background-color: skyblue; }}`, parser, parser._parseStylesheet.bind(parser));
assertNode(`@container card (inline-size > 30em) and style(--responsive: true) { }`, parser, parser._parseStylesheet.bind(parser));
assertNode(`@container card style(--responsive) { }`, parser, parser._parseStylesheet.bind(parser));
assertNode(`@container (inline-size > 30em), style(--responsive: true) { }`, parser, parser._parseStylesheet.bind(parser));
assertNode(`@container card (inline-size > 30em), style(--responsive: true) { }`, parser, parser._parseStylesheet.bind(parser));
assertNode(`@container card (inline-size > 30em), summary style(--responsive: true) { }`, parser, parser._parseStylesheet.bind(parser));
assertNode(`@container card (inline-size > 30em) { @container style(--responsive: true) {} }`, parser, parser._parseStylesheet.bind(parser));
});

Expand Down