Posted by
Aaron Patterson – November 28, 2007
CSSpool (pronounced “cesspool”) is a validating SAC parser for CSS. The parser
calls methods on a document handler depending on what it has found. CSSPool
currently only supports CSS 2.1. CSSPool will not yield invalid properties or
selectors.
Changes:
## 0.2.0
* Added CSS::SAC::Parser#parse_rule to parse a single rule.
* Added CSS::StyleSheet#find_rule for finding a particular rule.
* Added CSS::StyleSheet#rules_matching for finding all rules matching a node.
* Added CSS::StyleSheet#create_rule for creating a new rule.
* Added CSS::StyleSheet#find_all_rules_matching for finding all rules that match
any node in the passed in document.
* Added .eql? to selector AST
* Added .hash to selector AST
* Added .eql? to LexicalUnits
* Added .hash to LexicalUnits
* Added CSS::StyleSheet#to_css
* Added CSS::StyleSheet#reduce!
* CSS::StyleSheet is now the default document handler
*
Posted by
Aaron Patterson – November 17, 2007
CSSpool (pronounced “cesspool”) is a validating SAC parser for CSS. The parser
calls methods on a document handler depending on what it has found. CSSPool
currently only supports CSS 2.1. CSSPool will not yield invalid properties or
selectors.
Changes:
## 0.1.1
* Added specificity to selectors.
* Added == to selector ASTs.
* Added =~ to selector ASTs. The object passed in to =~ must quack like an
hpricot node. It must implement the following methods: name, parent, child,
next_sibling, attributes.
* Added .to_xpath to the selector AST.
* Fixed a bug where functions as pseudo classes didn’t parse.
*
Posted by
Aaron Patterson – November 15, 2007
I just finished up a matching method on CSSPool. So you can pass an hpricot node to a selector AST, and determine whether the node matches or not.
Lets say for example that you had an html document and a css document, and you wanted to know what selectors matched a given hpricot node, you could do something like this:
class DH < CSS::SAC::DocumentHandler
def initialize(node)
@node = node
end
def start_selector(selectors)
selectors.each { |sel| p sel if sel =~ @node }
end
end
parser = CSS::SAC::Parser.new(DH.new(some_hpricot_node))
parser.parse(File.read(ARGV[0]))
I think I will probably release what I have in trunk soon. I will probably add a new document handler for 0.2.0 that collects all selectors and properties together, and give a nice interface for finding styles that apply to an hpricot node.
Posted by
Aaron Patterson – November 3, 2007
I forgot to mention. I am going to give a presentation at RejectConf tonight, and here are the slides.
See the CSSPool and Hpricot integration after the jump.
More…
Posted by
Aaron Patterson – November 3, 2007
I just released csspool version 0.1.0 last night, so I thought I’d write a little about it.
CSSPool is a CSS SAC parser. Basically it parses a CSS document calling methods on a document handler object. The parser will send the document handler lots of information about the CSS, like the selectors and properties. The selectors you get are ASTs, and implement .to_css methods so you can get back css.
Here is a quick example:
require 'css/sac/parser'
class SimpleDocHandler < CSS::SAC::DocumentHandler
def start_selector(selectors)
p selectors.map { |sel| sel.to_css }.join(', ')
end
def property(name, value, important)
puts "#{name}: #{value} #{important ? '!important' : ''}".strip
end
end
csspool = CSS::SAC::Parser.new(SimpleDocHandler.new)
csspool.parse(File.read(ARGV[0]))