Renamed node_type to type_test.

This commit is contained in:
Yorick Peterse 2014-08-14 22:35:41 +02:00
parent 05f6fc2f8d
commit a437d67573
9 changed files with 33 additions and 34 deletions

View File

@ -428,23 +428,22 @@ module Oga
# @param [Oga::XML::NodeSet] context # @param [Oga::XML::NodeSet] context
# @return [Oga::XML::NodeSet] # @return [Oga::XML::NodeSet]
# #
def on_node_type(ast_node, context) def on_type_test(ast_node, context)
name, test = *ast_node name, test = *ast_node
handler = name.gsub('-', '_') handler = name.gsub('-', '_')
return send("on_node_type_#{handler}", test, context) return send("on_type_test_#{handler}", test, context)
end end
## ##
# Processes the `node()` node type. This type matcher can be used to match # Processes the `node` type matcher. This matcher matches all node types.
# all node types (text, comment, etc).
# #
# @param [Oga::XPath::Node] ast_node # @param [Oga::XPath::Node] ast_node
# @param [Oga::XML::NodeSet] context # @param [Oga::XML::NodeSet] context
# @return [Oga::XML::NodeSet] # @return [Oga::XML::NodeSet]
# #
def on_node_type_node(ast_node, context) def on_type_test_node(ast_node, context)
nodes = XML::NodeSet.new nodes = XML::NodeSet.new
context.each do |node| context.each do |node|

View File

@ -249,7 +249,7 @@ module Oga
# added on lexer level to make it easier to handle these cases on # added on lexer level to make it easier to handle these cases on
# parser/evaluator level. # parser/evaluator level.
if AXIS_EMIT_NODE.include?(value) if AXIS_EMIT_NODE.include?(value)
add_token(:T_NODE_TYPE, 'node') add_token(:T_TYPE_TEST, 'node')
if AXIS_EMIT_EXTRA_SLASH.include?(value) and te != eof if AXIS_EMIT_EXTRA_SLASH.include?(value) and te != eof
add_token(:T_SLASH) add_token(:T_SLASH)
@ -303,24 +303,24 @@ module Oga
| op_sub | op_sub
; ;
# Node types # Node type tests
# #
# While these look like functions they are actually node tests. For # While these look like functions they are actually node type tests. For
# example, comment() matches all comment nodes. # example, comment() matches all comment nodes.
# #
# See http://www.w3.org/TR/xpath/#NT-NodeType for more information. # See http://www.w3.org/TR/xpath/#NT-NodeType for more information.
node_type = 'comment' | 'text' | 'processing-instruction' | 'node'; type_test = 'comment' | 'text' | 'processing-instruction' | 'node';
action emit_node_type { action emit_type_test {
emit(:T_NODE_TYPE, ts, te - 2) emit(:T_TYPE_TEST, ts, te - 2)
} }
main := |* main := |*
operator; operator;
whitespace | slash | lparen | rparen | comma | colon; whitespace | slash | lparen | rparen | comma | colon;
node_type '()' => emit_node_type; type_test '()' => emit_type_test;
'[' => { add_token(:T_LBRACK) }; '[' => { add_token(:T_LBRACK) };
']' => { add_token(:T_RBRACK) }; ']' => { add_token(:T_RBRACK) };

View File

@ -3,7 +3,7 @@
# #
class Oga::XPath::Parser class Oga::XPath::Parser
token T_AXIS T_COLON T_COMMA T_FLOAT T_INT T_IDENT T_NODE_TYPE token T_AXIS T_COLON T_COMMA T_FLOAT T_INT T_IDENT T_TYPE_TEST
token T_LBRACK T_RBRACK T_LPAREN T_RPAREN T_SLASH T_STRING token T_LBRACK T_RBRACK T_LPAREN T_RPAREN T_SLASH T_STRING
token T_PIPE T_AND T_OR T_ADD T_DIV T_MOD T_EQ T_NEQ T_LT T_GT T_LTE T_GTE token T_PIPE T_AND T_OR T_ADD T_DIV T_MOD T_EQ T_NEQ T_LT T_GT T_LTE T_GTE
token T_SUB T_MUL token T_SUB T_MUL
@ -76,11 +76,11 @@ rule
node_test node_test
: node_name { s(:test, *val[0]) } : node_name { s(:test, *val[0]) }
| node_name predicate { s(:test, *val[0], val[1]) } | node_name predicate { s(:test, *val[0], val[1]) }
| node_type { val[0] } | type_test { val[0] }
; ;
node_type type_test
: T_NODE_TYPE { s(:node_type, val[0]) } : T_TYPE_TEST { s(:type_test, val[0]) }
; ;
node_name node_name

View File

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe Oga::XPath::Evaluator do describe Oga::XPath::Evaluator do
context 'node() tests' do context 'node type test' do
before do before do
@document = parse('<a><b>foo</b></a>') @document = parse('<a><b>foo</b></a>')
@evaluator = described_class.new(@document) @evaluator = described_class.new(@document)

View File

@ -120,7 +120,7 @@ describe Oga::XPath::Lexer do
lex_xpath('//A').should == [ lex_xpath('//A').should == [
[:T_SLASH, nil], [:T_SLASH, nil],
[:T_AXIS, 'descendant-or-self'], [:T_AXIS, 'descendant-or-self'],
[:T_NODE_TYPE, 'node'], [:T_TYPE_TEST, 'node'],
[:T_SLASH, nil], [:T_SLASH, nil],
[:T_IDENT, 'A'] [:T_IDENT, 'A']
] ]
@ -130,7 +130,7 @@ describe Oga::XPath::Lexer do
lex_xpath('/..').should == [ lex_xpath('/..').should == [
[:T_SLASH, nil], [:T_SLASH, nil],
[:T_AXIS, 'parent'], [:T_AXIS, 'parent'],
[:T_NODE_TYPE, 'node'] [:T_TYPE_TEST, 'node']
] ]
end end
@ -138,14 +138,14 @@ describe Oga::XPath::Lexer do
lex_xpath('/.').should == [ lex_xpath('/.').should == [
[:T_SLASH, nil], [:T_SLASH, nil],
[:T_AXIS, 'self'], [:T_AXIS, 'self'],
[:T_NODE_TYPE, 'node'] [:T_TYPE_TEST, 'node']
] ]
end end
example 'lex the . axis followed by a path' do example 'lex the . axis followed by a path' do
lex_xpath('./foo').should == [ lex_xpath('./foo').should == [
[:T_AXIS, 'self'], [:T_AXIS, 'self'],
[:T_NODE_TYPE, 'node'], [:T_TYPE_TEST, 'node'],
[:T_SLASH, nil], [:T_SLASH, nil],
[:T_IDENT, 'foo'] [:T_IDENT, 'foo']
] ]

View File

@ -37,7 +37,7 @@ describe Oga::XPath::Lexer do
[:T_IDENT, 'wikimedia'], [:T_IDENT, 'wikimedia'],
[:T_SLASH, nil], [:T_SLASH, nil],
[:T_AXIS, 'descendant-or-self'], [:T_AXIS, 'descendant-or-self'],
[:T_NODE_TYPE, 'node'], [:T_TYPE_TEST, 'node'],
[:T_SLASH, nil], [:T_SLASH, nil],
[:T_IDENT, 'editions'] [:T_IDENT, 'editions']
] ]
@ -64,7 +64,7 @@ describe Oga::XPath::Lexer do
[:T_SLASH, nil], [:T_SLASH, nil],
[:T_IDENT, 'edition'], [:T_IDENT, 'edition'],
[:T_SLASH, nil], [:T_SLASH, nil],
[:T_NODE_TYPE, 'text'] [:T_TYPE_TEST, 'text']
] ]
end end
end end

View File

@ -3,20 +3,20 @@ require 'spec_helper'
describe Oga::XPath::Lexer do describe Oga::XPath::Lexer do
context 'node types' do context 'node types' do
example 'lex the "node" type' do example 'lex the "node" type' do
lex_xpath('node()').should == [[:T_NODE_TYPE, 'node']] lex_xpath('node()').should == [[:T_TYPE_TEST, 'node']]
end end
example 'lex the "comment" type' do example 'lex the "comment" type' do
lex_xpath('comment()').should == [[:T_NODE_TYPE, 'comment']] lex_xpath('comment()').should == [[:T_TYPE_TEST, 'comment']]
end end
example 'lex the "text" type' do example 'lex the "text" type' do
lex_xpath('text()').should == [[:T_NODE_TYPE, 'text']] lex_xpath('text()').should == [[:T_TYPE_TEST, 'text']]
end end
example 'lex the "processing-instruction" type' do example 'lex the "processing-instruction" type' do
lex_xpath('processing-instruction()').should == [ lex_xpath('processing-instruction()').should == [
[:T_NODE_TYPE, 'processing-instruction'] [:T_TYPE_TEST, 'processing-instruction']
] ]
end end
end end

View File

@ -105,7 +105,7 @@ describe Oga::XPath::Parser do
example 'parse the // axis' do example 'parse the // axis' do
parse_xpath('//A').should == s( parse_xpath('//A').should == s(
:absolute_path, :absolute_path,
s(:axis, 'descendant-or-self', s(:node_type, 'node')), s(:axis, 'descendant-or-self', s(:type_test, 'node')),
s(:axis, 'child', s(:test, nil, 'A')) s(:axis, 'child', s(:test, nil, 'A'))
) )
end end
@ -113,14 +113,14 @@ describe Oga::XPath::Parser do
example 'parse the .. axis' do example 'parse the .. axis' do
parse_xpath('/..').should == s( parse_xpath('/..').should == s(
:absolute_path, :absolute_path,
s(:axis, 'parent', s(:node_type, 'node')) s(:axis, 'parent', s(:type_test, 'node'))
) )
end end
example 'parse the . axis' do example 'parse the . axis' do
parse_xpath('/.').should == s( parse_xpath('/.').should == s(
:absolute_path, :absolute_path,
s(:axis, 'self', s(:node_type, 'node')) s(:axis, 'self', s(:type_test, 'node'))
) )
end end
end end

View File

@ -3,21 +3,21 @@ require 'spec_helper'
describe Oga::XPath::Parser do describe Oga::XPath::Parser do
context 'node types' do context 'node types' do
example 'parse the "node" type' do example 'parse the "node" type' do
parse_xpath('node()').should == s(:axis, 'child', s(:node_type, 'node')) parse_xpath('node()').should == s(:axis, 'child', s(:type_test, 'node'))
end end
example 'parse the "comment" type' do example 'parse the "comment" type' do
parse_xpath('comment()') parse_xpath('comment()')
.should == s(:axis, 'child', s(:node_type, 'comment')) .should == s(:axis, 'child', s(:type_test, 'comment'))
end end
example 'parse the "text" type' do example 'parse the "text" type' do
parse_xpath('text()').should == s(:axis, 'child', s(:node_type, 'text')) parse_xpath('text()').should == s(:axis, 'child', s(:type_test, 'text'))
end end
example 'parse the "processing-instruction" type' do example 'parse the "processing-instruction" type' do
parse_xpath('processing-instruction()') parse_xpath('processing-instruction()')
.should == s(:axis, 'child', s(:node_type, 'processing-instruction')) .should == s(:axis, 'child', s(:type_test, 'processing-instruction'))
end end
end end
end end