Basic boilerplate for converting CSS to XPath.
This commit is contained in:
parent
48eb4f83df
commit
073e8fbe5b
|
@ -50,3 +50,4 @@ require_relative 'oga/xpath/evaluator'
|
|||
|
||||
require_relative 'oga/css/lexer'
|
||||
require_relative 'oga/css/parser'
|
||||
require_relative 'oga/css/transformer'
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
module Oga
|
||||
module CSS
|
||||
##
|
||||
# Transforms an CSS AST into a corresponding XPath AST.
|
||||
#
|
||||
class Transformer < AST::Processor
|
||||
def on_class(node)
|
||||
name, test = node.to_a
|
||||
|
||||
unless test
|
||||
test = s(:test, nil, '*')
|
||||
end
|
||||
|
||||
predicate = s(
|
||||
:eq,
|
||||
s(:axis, 'attribute', s(:test, nil, 'class')),
|
||||
s(:string, name)
|
||||
)
|
||||
|
||||
return s(:axis, 'child', test.updated(nil, test.children + [predicate]))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def s(type, *children)
|
||||
return AST::Node.new(type, children)
|
||||
end
|
||||
end # Transformer
|
||||
end # CSS
|
||||
end # Oga
|
Loading…
Reference in New Issue