Skip to content

Commit

Permalink
Merge pull request #64 from ruby-syntax-tree/split-combinators
Browse files Browse the repository at this point in the history
Split up combinators logic
  • Loading branch information
kddnewton authored Jun 28, 2024
2 parents 5b84ed8 + f410315 commit 223e342
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 144 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ GEM
prettier_print (>= 1.2.0)

PLATFORMS
arm64-darwin-23
x86_64-darwin-21
x86_64-linux

Expand Down
110 changes: 64 additions & 46 deletions lib/syntax_tree/css/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ def visit_delim_token(node)
q.text(node.value)
end

# Visit an IdentToken node.
def visit_ident_token(node)
# Visit a HashToken node.
def visit_hash_token(node)
q.text(node.value)
end

# Visit a HashToken node.
def visit_hash_token(node)
# Visit an IdentToken node.
def visit_ident_token(node)
q.text(node.value)
end

Expand All @@ -70,28 +70,79 @@ def visit_style_rule(node)
end
end

# Visit a Selectors::SubsequentSiblingCombinator node.
def visit_subsequent_sibling_combinator(node)
q.text(" ")
node.value.format(q)
q.text(" ")
end

#-------------------------------------------------------------------------
# Selector nodes
#-------------------------------------------------------------------------

# Visit a Selectors::TypeSelector node.
def visit_type_selector(node)
# Visit a Selectors::ChildCombinator node.
def visit_child_combinator(node)
q.text(" ")
node.value.format(q)
q.text(" ")
end

# Visit a Selectors::ClassSelector node.
def visit_class_selector(node)
q.text(".")
node.value.format(q)
end

# Visit a Selectors::ColumnSiblingCombinator node.
def visit_column_sibling_combinator(node)
q.text(" ")
node.value.each { |value| value.format(q) }
q.text(" ")
end

# Visit a Selectors::ComplexSelector node.
def visit_complex_selector(node)
q.group do
node.prefix.format(q) if node.prefix
node.value.format(q)
node.child_nodes.each do |child_node|
child_node.format(q)
end
end
end

# Visit a Selectors::CompoundSelector node.
def visit_compound_selector(node)
q.group do
node.child_nodes.each do |child_node|
child_node.format(q)
end
end
end

# Visit a Selectors::DescendantCombinator node.
def visit_descendant_combinator(node)
q.text(" ")
end

# Visit a Selectors::IdSelector node.
def visit_id_selector(node)
q.text("#")
node.value.format(q)
end

# Visit a Selectors::ClassSelector node.
def visit_class_selector(node)
q.text(".")
# Visit a Selectors::NextSiblingCombinator node.
def visit_next_sibling_combinator(node)
q.text(" ")
node.value.format(q)
q.text(" ")
end

# Visit a Selectors::TypeSelector node.
def visit_type_selector(node)
q.group do
node.prefix&.format(q)
node.value.format(q)
end
end

# Visit a Selectors::PseudoClassSelector node.
Expand All @@ -116,42 +167,9 @@ def visit_pseudo_element_selector(node)
node.value.format(q)
end

# Visit a Selectors::Combinator node.
def visit_combinator(node)
case node.value
when WhitespaceToken
q.text(" ")
when Array
q.text(" ")
node.value.each { |val| val.format(q) }
q.text(" ")
else
q.text(" ")
node.value.format(q)
q.text(" ")
end
end

# Visit a Selectors::ComplexSelector node.
def visit_complex_selector(node)
q.group do
node.child_nodes.each_with_index do |child_node, j|
child_node.format(q)
end
end
end

# Visit a Selectors::CompoundSelector node.
def visit_compound_selector(node)
q.group do
node.child_nodes.each do |node_|
node_.format(q)
end
end
end

# Visit a Selectors::WqName node.
def visit_wqname(node)
node.prefix.format(q) if node.prefix
node.prefix&.format(q)
node.name.format(q)
end
end
Expand Down
150 changes: 91 additions & 59 deletions lib/syntax_tree/css/pretty_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,73 @@ def visit_whitespace_token(node)
# Selector nodes
#-------------------------------------------------------------------------

# Visit a Selectors::ChildCombinator node.
def visit_child_combinator(node)
token("child-combinator") do
q.breakable
q.pp(node.value)
end
end

# Visit a Selectors::ColumnSiblingCombinator node.
def visit_column_sibling_combinator(node)
token("column-sibling-combinator") do
q.breakable
q.pp(node.value)
end
end

# Visit a Selectors::ComplexSelector node.
def visit_complex_selector(node)
token("complex-selector") do
node.child_nodes.each do |child|
q.breakable
q.pp(child)
end
end
end

# Visit a Selectors::CompoundSelector node.
def visit_compound_selector(node)
token("compound-selector") do
q.breakable
token("type") do
q.breakable
q.pp(node.type)
end

q.breakable
q.text("(subclasses")

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

q.breakable("")
end

q.text(")")

q.breakable("")
q.text("(pseudo-elements")

if node.pseudo_elements.any?
q.nest(2) do
q.breakable
q.seplist(node.pseudo_elements) do |pseudo_element|
q.pp(pseudo_element)
end
end

q.breakable("")
end

q.text(")")
end
end

# Visit a Selectors::ClassSelector node.
def visit_class_selector(node)
token("class-selector") do
Expand All @@ -358,6 +425,14 @@ def visit_class_selector(node)
end
end

# Visit a Selectors::DescendantCombinator node.
def visit_descendant_combinator(node)
token("descendant-combinator") do
q.breakable
q.pp(node.value)
end
end

# Visit a Selectors::IdSelector node.
def visit_id_selector(node)
token("id-selector") do
Expand All @@ -366,6 +441,14 @@ def visit_id_selector(node)
end
end

# Visit a Selectors::NextSiblingCombinator node.
def visit_next_sibling_combinator(node)
token("next-sibling-combinator") do
q.breakable
q.pp(node.value)
end
end

# Visit a Selectors::PseudoClassSelector node.
def visit_pseudo_class_selector(node)
token("pseudo-class-selector") do
Expand Down Expand Up @@ -404,6 +487,14 @@ def visit_pseudo_element_selector(node)
end
end

# Visit a Selectors::SubsequentSiblingCombinator node.
def visit_subsequent_sibling_combinator(node)
token("subsequent-sibling-combinator") do
q.breakable
q.pp(node.value)
end
end

# Visit a Selectors::TypeSelector node.
def visit_type_selector(node)
token("type-selector") do
Expand All @@ -430,65 +521,6 @@ def visit_wqname(node)
end
end

# Visit a Selectors::Combinator node.
def visit_combinator(node)
token(node.class::PP_NAME) do
q.breakable
q.pp(node.value)
end
end

# Visit a Selectors::ComplexSelector node.
def visit_complex_selector(node)
token("complex-selector") do
node.child_nodes.each do |child|
q.breakable
q.pp(child)
end
end
end

# Visit a Selectors::CompoundSelector node.
def visit_compound_selector(node)
token("compound-selector") do
q.breakable
token("type") do
q.breakable
q.pp(node.type)
end

q.breakable
q.text("(subclasses")

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

q.breakable("")
end

q.text(")")

q.breakable("")
q.text("(pseudo-elements")

if node.pseudo_elements.any?
q.nest(2) do
q.breakable
q.seplist(node.pseudo_elements) do |pseudo_element|
q.pp(pseudo_element)
end
end

q.breakable("")
end

q.text(")")
end
end

private

def token(name)
Expand Down
Loading

0 comments on commit 223e342

Please sign in to comment.