Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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 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
11 changes: 10 additions & 1 deletion lib/syntax_tree/css/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ def visit_pseudo_class_selector(node)
node.value.format(q)
end

# Visit a Selectors::PseudoClassFunction node.
def visit_pseudo_class_function(node)
q.text(node.name)
q.text("(")
q.seplist(node.arguments, -> { q.text(", ") }) do |selector|
selector.format(q)
end
q.text(")")
end

# Visit a Selectors::PseudoElementSelector node.
def visit_pseudo_element_selector(node)
q.text(":")
Expand Down Expand Up @@ -127,7 +137,6 @@ def visit_compound_selector(node)
node.child_nodes.each do |node_|
node_.format(q)
end
# TODO: pseudo-elements
end
end

Expand Down
18 changes: 15 additions & 3 deletions lib/syntax_tree/css/pretty_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,19 @@ def visit_pseudo_class_function(node)
q.breakable
q.pp(node.name)

q.breakable
q.text("(arguments")

if node.arguments.any?
q.breakable
q.seplist(node.arguments) { |argument| q.pp(argument) }
q.nest(2) do
q.breakable
q.seplist(node.arguments) { |argument| q.pp(argument) }
end

q.breakable("")
end

q.text(")")
end
end

Expand Down Expand Up @@ -443,7 +452,10 @@ def visit_complex_selector(node)
def visit_compound_selector(node)
token("compound-selector") do
q.breakable
q.pp(node.type)
token("type") do
q.breakable
q.pp(node.type)
end

q.breakable
q.text("(subclasses")
Expand Down
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
37 changes: 35 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 All @@ -227,6 +253,13 @@ class SelectorsTest < Minitest::Spec
)
end

it "with a pseudo-class function" do
assert_selector_format(
".flex:not(div, span.wide, .hidden)",
".flex:not(div, span.wide, .hidden)",
)
end

it "with class selectors" do
assert_selector_format(
"div.flex.text-xl",
Expand Down