Skip to content

ydah/parselly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Parselly Gem Version CI

Pure Ruby CSS selector parser.

Installation

gem 'parselly'
bundle install

Or install it directly:

gem install parselly

Requires Ruby 2.7 or newer.

Usage

require 'parselly'

ast = Parselly.parse('article#main.content[data-state="open"] > a:hover')

ast.ids
#=> ["main"]

ast.attributes
#=> [{ name: "data-state", operator: "=", value: "open" }]

ast.pseudo_class_names
#=> ["hover"]

ast.specificity
#=> [1, 3, 2]

Strict parsing raises Parselly::LexError or Parselly::SyntaxError for invalid selectors:

Parselly.parse('div >')

Use tolerant mode when you want a Parselly::ParseResult instead:

result = Parselly.parse('div >', tolerant: true)

result.success?
#=> false

result.errors.first[:message]
#=> "Parse error: unexpected $end '' at 1:6"

Use Parselly.sanitize to escape text for a CSS identifier:

Parselly.sanitize('1st item')
#=> "\\31 st\\ item"

Development

bin/setup
bundle exec rake

License

MIT

About

Parselly is a module providing a simple way to parse and extract data from a css selector 🌿

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors