Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css_semantic): resolve nested selectors #4658

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

togami2864
Copy link
Contributor

@togami2864 togami2864 commented Nov 29, 2024

Summary

The actual result would be like

.button {
  &:hover {
    background: yellow;
  }
}
// parent selector
Selector {
    name: ".button",
    range: 0..7,
    specificity: Specificity(0, 1, 0),
}

// child selector
Selector {
    name: ".button:hover",
    range: 12..19,
    specificity: Specificity(0, 2, 0),
}

Example

  1. Basic nesting:
.parent {
  .child {
    color: blue;
  }
}

Resolves to:

.parent .child {
  color: blue;
}
  1. Parent selector reference (&):
.button {
  &:hover {
    background: yellow;
  }
}

Resolves to:

.button:hover {
  background: yellow;
}
  1. Complex nesting with parent reference:
.header {
  nav {
    ul {
      &.primary {
        li {
          a {
            color: black;
          }
        }
      }
    }
  }
}

Resolves to:

.header nav ul.primary li a {
  color: black;
}
  1. Multiple selectors:
.sidebar, .content {
  h2 {
    font-size: 18px;
  }
}

Resolves to:

.sidebar h2, .content h2 {
  font-size: 18px;
}

Test Plan

Added tests

@togami2864 togami2864 self-assigned this Nov 29, 2024
@github-actions github-actions bot added the L-CSS Language: CSS label Nov 29, 2024
@togami2864 togami2864 marked this pull request as ready for review December 1, 2024 13:05
Comment on lines +123 to +125
for name in resolved_selectors.iter() {
current_rule.selectors.push(Selector {
name: name.to_string(),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for name in resolved_selectors.iter() {
current_rule.selectors.push(Selector {
name: name.to_string(),
for selector_name in resolved_selectors.iter() {
current_rule.selectors.push(Selector {
name: selector_name.to_string(),

name is somewhat ambiguous, and selector_name would improve readability IMHO

@@ -173,3 +185,23 @@ impl SemanticModelBuilder {
}
}
}

fn resolve_selector(current: &str, parent: &[Selector]) -> Vec<String> {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fn resolve_selector(current: &str, parent: &[Selector]) -> Vec<String> {
fn resolve_selector(current: &str, parents: &[Selector]) -> Vec<String> {

nit: parents is more accurate since it's a slice of selectors?

Comment on lines +190 to +206
let mut resolved = Vec::new();

if parent.is_empty() {
return vec![current.to_string()];
}

for parent in parent {
let parent = parent.name.to_string();
if current.contains('&') {
let current = current.replace('&', &parent);
resolved.push(current);
} else {
resolved.push(format!("{} {}", parent, current));
}
}

resolved
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let mut resolved = Vec::new();
if parent.is_empty() {
return vec![current.to_string()];
}
for parent in parent {
let parent = parent.name.to_string();
if current.contains('&') {
let current = current.replace('&', &parent);
resolved.push(current);
} else {
resolved.push(format!("{} {}", parent, current));
}
}
resolved
if parents.is_empty() {
return vec![current.to_string()];
}
parents
.iter()
.map(|parent| {
let parent_name = &parent.name;
if current.contains('&') {
current.replace('&', parent_name)
} else {
format!("{} {}", parent_name, current)
}
})
.collect()

let current_rule = self.rules_by_id.get_mut(current_rule_id).unwrap();
for name in resolved_selectors.iter() {
current_rule.selectors.push(Selector {
name: name.to_string(),
Copy link
Member

Choose a reason for hiding this comment

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

We already have a String, we shouldn't create a new one

@@ -173,3 +185,23 @@ impl SemanticModelBuilder {
}
}
}

fn resolve_selector(current: &str, parent: &[Selector]) -> Vec<String> {
Copy link
Member

Choose a reason for hiding this comment

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

It's possible that I am late to the party, so I am sorry if this questions come too late.

Is there a particular reason we need to track String inside the semantic model? I wonder if we can avoid that and store only TextRange and syntax nodes (syntax nodes are cheaper than strings) inside the model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L-CSS Language: CSS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants