Skip to content

css nesting : increase support#345

Merged
aeschli merged 1 commit into
microsoft:mainfrom
romainmenke:css-nesting-1--handle-at-rules--courteous-komodo-dragon-4379500d15
May 25, 2023
Merged

css nesting : increase support#345
aeschli merged 1 commit into
microsoft:mainfrom
romainmenke:css-nesting-1--handle-at-rules--courteous-komodo-dragon-4379500d15

Conversation

@romainmenke
Copy link
Copy Markdown
Contributor

@romainmenke romainmenke commented Apr 29, 2023

  • handle nested at rules
  • improve autocomplete and diagnostics when starting a nested rule
  • improve parsing behaviour when a rule doesn't start with an ident

- handle nested at rules
- improve parsing behaviour when a rule doesn't start with an ident
@romainmenke romainmenke force-pushed the css-nesting-1--handle-at-rules--courteous-komodo-dragon-4379500d15 branch from 76a2698 to 040b46e Compare April 29, 2023 22:27
Comment thread src/test/css/lint.test.ts
Comment on lines +233 to +235
// IE star hacks are incompatible with CSS nesting.
// assertRuleSet('selector { display: inline-block; *display: inline; }', Rules.IEStarHack);
// assertRuleSet('selector { background: #00f; /* all browsers including Mac IE */ *background: #f00; /* IE 7 and below */ _background: #f60; /* IE 6 and below */ }', Rules.IEStarHack, Rules.IEStarHack);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see : #344

IE star hacks aren't compatible with CSS Nesting. * triggers rule parsing.

Comment on lines +32 to +47
let expectedSegment = segments.shift()!;
let actualSegment = actual.shift()!;

if (expectedSegment === '...') {
let nextExpectedSegment = segments[0];
let nextActualSegment = actual[0];

while (actual.length > 0) {
if (nextExpectedSegment === nextActualSegment) {
break;
}

actualSegment = actual.shift()!;
nextActualSegment = actual[0];
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test helper was broken, it didn't actually test anything.
Adding any ... in a expect string would cause the previous algorithm to just skip to the end in case of issues.

This new algorithm is a bit stricter, making it easier to write reliable tests for nested and repeating structures.


test('RuleSet', function () {
assertNodes(ruleset, 'selector { prop: value }', 'ruleset,...,selector,...,declaration,...,property,...,expression');
assertNodes(ruleset, 'selector { prop; }', 'ruleset,...,selector,...,selector');
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a false positive from the old test helper.
There is only one selector in this source, but the test still passed.

@romainmenke romainmenke changed the title css nesting css nesting : increase support Apr 29, 2023
Comment thread src/parser/cssParser.ts
Comment on lines +376 to +380
if (!this.peek(TokenType.Ident)) {
return this._parseRuleset(true);
}

return this._tryParseRuleset(true) || this._parseDeclaration();
Copy link
Copy Markdown
Contributor Author

@romainmenke romainmenke Apr 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still isn't fully correct for the CSS specification, but it does work ok in practice.

It is mainly important to have strict rule parsing when the current token isn't an ident. There must not a be a fallback to declaration parsing.

Doing the fallback to declaration parsing causes the wrong auto complete and diagnostics to be shown to users.


To be fully correct for the specification it would be better to do this :

  1. parse as a declaration
    1. if this passes, return the declaration
    2. if this fails, record the diagnostics and reset the parser
  2. parse as a rule
    1. if this passes, return the rule
    2. if this fails, record the diagnostics
  3. report all diagnostics

It's a bit chicken vs. egg when a user is starting to write something, you can't know what it will be come until they get to the end. So autocomplete and diagnostics should be given for both declarations and rules.

I think it is ok to add this later, given that it is a very large refactor for an edge case.

@romainmenke
Copy link
Copy Markdown
Contributor Author

@aeschli friendly ping in case you missed these changes :)

@aeschli
Copy link
Copy Markdown
Collaborator

aeschli commented May 24, 2023

@romainmenke Oh sorry, I missed that. PR look good, thanks a lot!

@aeschli aeschli self-assigned this May 24, 2023
@aeschli aeschli added this to the May 2023 milestone May 24, 2023
@aeschli aeschli merged commit 984942a into microsoft:main May 25, 2023
@romainmenke romainmenke deleted the css-nesting-1--handle-at-rules--courteous-komodo-dragon-4379500d15 branch May 25, 2023 08:18
@romainmenke
Copy link
Copy Markdown
Contributor Author

Thank you @aeschli and @joyceerhl for reviewing this 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants