Talius
Talius is a parser for CSS selectors. It does not parse CSS, just CSS selectors.
Talius.new
accepts a single string as the selector. The Talius
object provides an interface with which to access the properties of that
selector.
In this example, the selector consists of just a
, meaning it selects
<a>
tags:
```ruby line-numbers raw = 'a' selector = Talius.new(raw) rule = selector.rules[0] rule.tags # => "a"=>{"name"=>"a"}
Line 1 creates the raw CSS selector like you might find in a CSS file. Line 2
creates a `Talius` object.
Each Talius has one or more rules. In this example the object will have one
rule. Line 3 gets that rule. Each rule has a hash of tag names in the `tags`
property. Line 4 displays that hash. See below for more about the `tags` hash.
The following sections will describe how Talius provides information about
different elements of a selector. Talius is in its infancy, so there are
some important aspects of CSS selectors that it doesn't support. Those aspects
are detailed below.
## Tags
If a selector contains a tag name, that information will be put into the
`tags` hash of the rule. Consider this example.
```ruby
raw = 'a'
selector = Talius.new(raw)
rule = selector.rules[0]
rule.tags # => {"a"=>{"name"=>"a"}}
rule.tags['a'].class # => Talius::Node::Tag
rule.tags['a'].name # => "a"
rule.tags['a'].namespace # => nil
That selector consists of just a single tag name, so the selector
object has
just one rule. That rule has a property called tags
, which is a hash of the
tags in the rule. The key for each hash element is the name of the tag. The
value of the element is a Talius::Node::Tag
object. That object has two
properties, name
and namespace
.
To indicate the namespace for the tag, put the namespace, followed by |
,
followed by the name of the tag. For example, the following code has a selector
for tags in the mml
namespace with the name a
. The keys in the tags
hash
are formatted in the same way.
raw = 'mml|a'
selector = Talius.new(raw)
rule = selector.rules[0]
rule. # => {"mml|a"=>{"name"=>"a", "namespace"=>"mml"}}
rule.['mml|a'].class # => Talius::Node::Tag
rule.['mml|a'].name # => "a"
rule.['mml|a'].namespace # => "mml"
Multiple rules
For multiple rules for a selector, separate the rules with a comma. For example,
the following code parses a selector with two rules, one for the section
tag
and one for the div
tag.
raw = 'section, div'
selector = Talius.new(raw)
selector.rules.length # => 2
selector.rules # => [{"tags"=>{"section"=>{"name"=>"section"}}}, {"tags"=>{"div"=>{"name"=>"div"}}}]
IDs
If any ID descriptions are given, those IDs can be found in the rule's ids
hash. The keys are the names of the IDs, the values are always true
.
raw = '#overview'
selector = Talius.new(raw)
rule = selector.rules[0]
rule.ids # => {"overview"=>true}
Classes
Classes are available in the classes
property of the rule. classes
is a
simple hash in which the value of each class is true.
raw = 'section.overview.current'
selector = Talius.new(raw)
rule = selector.rules[0]
rule.classes # => {"overview"=>true, "current"=>true}
Attributes
Attribute rules are provided in the rule's atts
hash. The hash consists of the
key of each attribute and a Talius::Node::Att
object.
In this simple example, the selector looks for tags with an rel
attribute.
raw = '[rel]'
selector = Talius.new(raw)
rule = selector.rules[0]
att = rule.atts['rel']
att.class # => Talius::Node::Att
att.name # => rel
If you assign a value to the attribute, that value will be in value
property.
raw = '[rel=license]'
selector = Talius.new(raw)
rule = selector.rules[0]
att = rule.atts['rel']
att.name # => href
att.value # => license
Attribute namespaces are indicated in the same way as with tags. You can access
the namespace with the namespace
property.
raw = '[mml|rel]'
selector = Talius.new(raw)
rule = selector.rules[0]
att = rule.atts['mml|rel']
att.name # => rel
att.namespace # => mml
Not implemented
There are a few aspects of CSS selectors that have not yet been implemented.
- Combinators are not parsed.
- The
:not()
pseudo-class is not understood.
Install
gem install talius
Author
Mike O'Sullivan [email protected]
Name
"Talius" doesn't mean anything in particular. It just sounded like a good name and it was available on rubygems.rb.
History
version | date | notes |
---|---|---|
0.5 | May 29, 2020 | Initial upload. |