rgrove / crass

A Ruby CSS parser that's fully compliant with the CSS Syntax Level 3 specification.
MIT License
138 stars 14 forks source link

Expose start and end token for simple_rule, needed for sourcemaps #19

Open stoivo opened 1 month ago

stoivo commented 1 month ago

I think it would be good to add two more values to style_rule node. block_start amd block_end

css = <<~CSS
body {font-size:small;}
CSS
# body {font-size:small;}
# 01234567891111111111222
#           0123456789012

pp tree = Crass.parse(css, :preserve_comments => false)
[{:node=>:style_rule,
  :selector=>
   {:node=>:selector,
    :value=>"body",
    :tokens=>
     [{:node=>:ident, :pos=>0, :raw=>"body", :value=>"body"},
      {:node=>:whitespace, :pos=>4, :raw=>" "}]},
  :children=>
   [{:node=>:property,
     :name=>"font-size",
     :value=>"small",
     :children=>
      [{:node=>:ident, :pos=>16, :raw=>"small", :value=>"small"}],
     :important=>false,
     :tokens=>
      [{:node=>:ident,
        :pos=>6,
        :raw=>"font-size",
        :value=>"font-size"},
       {:node=>:colon, :pos=>15, :raw=>":"},
       {:node=>:ident, :pos=>16, :raw=>"small", :value=>"small"}]},
    {:node=>:semicolon, :pos=>21, :raw=>";"}]},
 {:node=>:whitespace, :pos=>23, :raw=>"\n"}]
stoivo commented 1 month ago

One option might be to expose tokens

flavorjones commented 1 month ago

@stoivo Can you explain about about the use case, to help readers understand why you're asking for this feature?

stoivo commented 1 month ago

Yes, I just briefly mention it in the title. Good to expand on that. I am working on refactoring css_parser. One of the features is to see where the rules are defined. Something like source maps for css and javascript.

Does that explain it?