Use a helper method for transforming CSS ASTs.

This commit is contained in:
Yorick Peterse 2014-10-16 23:01:56 +02:00
parent a85cd7cbd1
commit 7ccd685acb
2 changed files with 14 additions and 24 deletions

View File

@ -2,34 +2,12 @@ require 'spec_helper'
describe Oga::CSS::Transformer do
context 'classes' do
before do
@transformer = described_class.new
end
example 'convert a class node without a node test' do
@transformer.process(parse_css('.y')).should == s(
:axis,
'child',
s(
:test,
nil,
'*',
s(:eq, s(:axis, 'attribute', s(:test, nil, 'class')), s(:string, 'y'))
)
)
transform_css('.y').should == parse_xpath('*[@class="y"]')
end
example 'convert a class node with a node test' do
@transformer.process(parse_css('x.y')).should == s(
:axis,
'child',
s(
:test,
nil,
'x',
s(:eq, s(:axis, 'attribute', s(:test, nil, 'class')), s(:string, 'y'))
)
)
transform_css('x.y').should == parse_xpath('x[@class="y"]')
end
end
end

View File

@ -89,5 +89,17 @@ module Oga
rescue Racc::ParseError => error
return error.message
end
##
# Parses and transforms a CSS AST into an XPath AST.
#
# @param [String] css
# @return [AST::Node]
#
def transform_css(css)
ast = parse_css(css)
return Oga::CSS::Transformer.new.process(ast)
end
end # ParsingHelpers
end # Oga