Skip to content

Parse pseudo-class function arguments #62

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Parse arguments for pseudo-class functions
so that the AST doesn't just contain tokens. Previously the arguments
were something like

```
[(ident-token "div"),
 (comma-token),
 (whitespace-token " "),
 (ident-token "span"),
 (delim-token "."),
 (ident-token "wide"),
 (comma-token),
 (whitespace-token " "),
 (delim-token "."),
 (ident-token "hidden")]
```

Now those tokens will be represented in the AST as

```
[(type-selector (wqname (ident-token "div"))),
 (compound-selector
   (type (type-selector (wqname (ident-token "span"))))
   (subclasses
     (class-selector (ident-token "wide"))
   )
   (pseudo-elements)
 ),
 (class-selector (ident-token "hidden"))]
```
  • Loading branch information
flavorjones committed Jun 5, 2024
commit 5b4571f786cef0e5828657a6ba8c6c0848739064
3 changes: 2 additions & 1 deletion lib/syntax_tree/css/selectors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ def pseudo_class_selector
PseudoClassSelector.new(value: consume(IdentToken))
in Function
node = consume(Function)
function = PseudoClassFunction.new(name: node.name, arguments: node.value)
arguments = Selectors.new(node.value).parse
function = PseudoClassFunction.new(name: node.name, arguments: arguments)
PseudoClassSelector.new(value: function)
else
raise MissingTokenError, "Expected pseudo class selector to produce something"
Expand Down
30 changes: 28 additions & 2 deletions test/selectors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class SelectorsTest < Minitest::Spec
end
end

it "parses a compound selector with a pseudo-class" do
it "parses a compound selector with a pseudo-class selector" do
actual = parse_selectors("div.flex:hover")

assert_pattern do
Expand All @@ -98,6 +98,33 @@ class SelectorsTest < Minitest::Spec
end
end

it "parses a compound selector with a pseudo-class function" do
actual = parse_selectors(".flex:not(div, span.wide, .hidden)")

assert_pattern do
actual => [
Selectors::CompoundSelector[
type: nil,
subclasses: [
Selectors::ClassSelector[value: { value: "flex" }],
Selectors::PseudoClassSelector[
value: Selectors::PseudoClassFunction[
name: "not",
arguments: [
Selectors::TypeSelector[value: { name: { value: "div" } }],
Selectors::CompoundSelector[
Selectors::TypeSelector[value: { name: { value: "span" } }],
Selectors::ClassSelector[value: { value: "wide" }],
],
],
],
],
],
]
]
end
end

it "parses a compound selector with pseudo-elements and pseudo-classes" do
actual = parse_selectors("div.flex:hover::first-line:last-child:active::first-letter")

Expand Down Expand Up @@ -208,7 +235,6 @@ class SelectorsTest < Minitest::Spec
]
end
end

end

describe "formatting" do
Expand Down