Support for the XPath lang() function.
This commit is contained in:
parent
10e82de87b
commit
585b3535b2
|
@ -1103,6 +1103,35 @@ module Oga
|
|||
return false
|
||||
end
|
||||
|
||||
##
|
||||
# Processes the `lang()` function call.
|
||||
#
|
||||
# This function returns `true` if the current context node is in the given
|
||||
# language, `false` otherwise.
|
||||
#
|
||||
# The language is based on the value of the "xml:lang" attribute of either
|
||||
# the context node or an ancestor node (in case the context node has no
|
||||
# such attribute).
|
||||
#
|
||||
# @param [Oga::XML::NodeSet] context
|
||||
# @param [Oga::XPath::Node] language
|
||||
# @return [TrueClass|FalseClass]
|
||||
#
|
||||
def on_call_lang(context, language)
|
||||
lang_str = on_call_string(context, language)
|
||||
node = current_node
|
||||
|
||||
while node.respond_to?(:attribute)
|
||||
found = node.attribute('xml:lang')
|
||||
|
||||
return found.value == lang_str if found
|
||||
|
||||
node = node.parent
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
##
|
||||
# Processes an `(int)` node.
|
||||
#
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
context 'lang() function' do
|
||||
before do
|
||||
@document = parse('<root xml:lang="en"><a></a><a xml:lang="nl"></a></root>')
|
||||
@evaluator = described_class.new(@document)
|
||||
end
|
||||
|
||||
context 'selecting nodes with lang="en"' do
|
||||
before do
|
||||
@set = @evaluator.evaluate('root[lang("en")]')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'return the <root> node' do
|
||||
@set[0].should == @document.children[0]
|
||||
end
|
||||
end
|
||||
|
||||
context 'selecting nodes with lang="nl"' do
|
||||
before do
|
||||
@set = @evaluator.evaluate('root/a[lang("nl")]')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'return the second <a> node' do
|
||||
@set[0].should == @document.children[0].children[1]
|
||||
end
|
||||
end
|
||||
|
||||
context 'inheriting the language from ancestor nodes' do
|
||||
before do
|
||||
@set = @evaluator.evaluate('root/a[lang("en")]')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'return the first <a> node' do
|
||||
@set[0].should == @document.children[0].children[0]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue