Basic boilerplate for converting CSS to XPath.

This commit is contained in:
Yorick Peterse 2014-10-16 00:25:31 +02:00
parent 48eb4f83df
commit 073e8fbe5b
2 changed files with 31 additions and 0 deletions

View File

@ -50,3 +50,4 @@ require_relative 'oga/xpath/evaluator'
require_relative 'oga/css/lexer'
require_relative 'oga/css/parser'
require_relative 'oga/css/transformer'

View File

@ -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