Skip to content

Commit

Permalink
(ast/parser) move comma from compoundselector to selectorlist
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Jan 1, 2025
1 parent b50e5ac commit 49b0c8d
Show file tree
Hide file tree
Showing 37 changed files with 48,249 additions and 48,187 deletions.
32 changes: 18 additions & 14 deletions crates/css_ast/src/selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ pub use webkit::*;

use super::{Visit, Visitable};

/// Represents a list of [CompoundSelectors][CompoundSelector], such as `body, dialog:modal`.
///
/// ```md
/// <selector-list>
/// │├─╭─ <compound-selector> ─╮─ "," ─╭─╮─┤│
/// │ ╰───────╯ │
/// ╰─────────────────────────────────╯
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[visit]
pub struct SelectorList<'a>(pub Vec<'a, CompoundSelector<'a>>);
pub struct SelectorList<'a>(pub Vec<'a, (CompoundSelector<'a>, Option<T![,]>)>);

impl<'a> SelectorListTrait<'a> for SelectorList<'a> {
type CompoundSelector = CompoundSelector<'a>;
Expand All @@ -55,16 +63,19 @@ impl<'a> Parse<'a> for SelectorList<'a> {

impl<'a> ToCursors for SelectorList<'a> {
fn to_cursors(&self, s: &mut impl CursorSink) {
for selector in &self.0 {
for (selector, comma) in &self.0 {
ToCursors::to_cursors(selector, s);
if let Some(comma) = comma {
s.append(comma.into());
}
}
}
}

impl<'a> Visitable<'a> for SelectorList<'a> {
fn accept<V: Visit<'a>>(&self, v: &mut V) {
v.visit_selector_list(&self);
for selector in &self.0 {
for (selector, _) in &self.0 {
Visitable::accept(selector, v);
}
}
Expand All @@ -73,37 +84,30 @@ impl<'a> Visitable<'a> for SelectorList<'a> {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[visit]
pub struct CompoundSelector<'a> {
pub components: Vec<'a, SelectorComponent<'a>>,
pub comma: Option<T![,]>,
}
pub struct CompoundSelector<'a>(pub Vec<'a, SelectorComponent<'a>>);

impl<'a> CompoundSelectorTrait<'a> for CompoundSelector<'a> {
type SelectorComponent = SelectorComponent<'a>;
}

impl<'a> Parse<'a> for CompoundSelector<'a> {
fn parse(p: &mut Parser<'a>) -> ParserResult<Self> {
let (components, comma) = Self::parse_compound_selector(p)?;
Ok(Self { components, comma })
Ok(Self(Self::parse_compound_selector(p)?))
}
}

impl<'a> ToCursors for CompoundSelector<'a> {
fn to_cursors(&self, s: &mut impl CursorSink) {
for component in &self.components {
for component in &self.0 {
ToCursors::to_cursors(component, s);
}
if let Some(comma) = self.comma {
s.append(comma.into())
}
}
}

impl<'a> Visitable<'a> for CompoundSelector<'a> {
fn accept<V: Visit<'a>>(&self, v: &mut V) {
v.visit_compound_selector(&self);
for component in &self.components {
for component in &self.0 {
Visitable::accept(component, v);
}
}
Expand Down
15 changes: 14 additions & 1 deletion crates/css_ast/src/stylerule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@ use hdx_proc_macro::visit;

use super::{rules, UnknownAtRule, UnknownQualifiedRule, Visit, Visitable};

// https://drafts.csswg.org/cssom-1/#the-cssstylerule-interface
/// Represents a "Style Rule", such as `body { width: 100% }`. See also the CSS-OM [CSSStyleRule][1] interface.
///
/// The Style Rule is comprised of two child nodes: the [SelectorList] represents the selectors of the rule, and the
/// [StyleDeclaration] represents the block, including the `{` and `}`.
///
/// ```md
/// <style-rule>
/// │├─ <selector-list> ─ <style-declaration> ─┤│
///
/// ```
///
/// [1]: https://drafts.csswg.org/cssom-1/#the-cssstylerule-interface
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(tag = "type", rename = "stylerule"))]
#[visit]
pub struct StyleRule<'a> {
/// The selectors this rule applies to.
pub selectors: SelectorList<'a>,
#[cfg_attr(feature = "serde", serde(flatten))]
/// The declaration of this rule.
pub style: StyleDeclaration<'a>,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/css_ast/tests/basic_snapshots.rs
expression: result.output.unwrap()
snapshot_kind: text
---
{
"type": "stylesheet",
Expand Down Expand Up @@ -36,8 +37,8 @@ expression: result.output.unwrap()
{
"type": "stylerule",
"selectors": [
{
"components": [
[
[
{
"type": "tag",
"value": {
Expand All @@ -51,8 +52,8 @@ expression: result.output.unwrap()
}
}
],
"comma": null
}
null
]
],
"type": "style-declaration",
"open": {
Expand Down
11 changes: 6 additions & 5 deletions crates/css_ast/tests/snapshots/basic_snapshots__basic_nth.snap
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
---
source: crates/hdx_ast/tests/basic_snapshots.rs
source: crates/css_ast/tests/basic_snapshots.rs
expression: result.output.unwrap()
snapshot_kind: text
---
{
"type": "stylesheet",
"rules": [
{
"type": "stylerule",
"selectors": [
{
"components": [
[
[
{
"type": "functional-pseudo-class",
"value": {
Expand Down Expand Up @@ -52,8 +53,8 @@ expression: result.output.unwrap()
}
}
],
"comma": null
}
null
]
],
"type": "style-declaration",
"open": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
---
source: crates/css_ast/tests/basic_snapshots.rs
expression: result.output.unwrap()
snapshot_kind: text
---
{
"type": "stylesheet",
"rules": [
{
"type": "stylerule",
"selectors": [
{
"components": [
[
[
{
"type": "tag",
"value": {
Expand All @@ -23,8 +24,8 @@ expression: result.output.unwrap()
}
}
],
"comma": null
}
null
]
],
"type": "style-declaration",
"open": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
---
source: crates/css_ast/tests/basic_snapshots.rs
expression: result.output.unwrap()
snapshot_kind: text
---
{
"type": "stylesheet",
"rules": [
{
"type": "stylerule",
"selectors": [
{
"components": [
[
[
{
"type": "tag",
"value": {
Expand All @@ -23,8 +24,8 @@ expression: result.output.unwrap()
}
}
],
"comma": null
}
null
]
],
"type": "style-declaration",
"open": {
Expand Down
Loading

0 comments on commit 49b0c8d

Please sign in to comment.