Track the current node in the pull parser.

The current node is tracked in the instance method `node`.
This commit is contained in:
Yorick Peterse 2014-04-29 21:21:05 +02:00
parent d0b3653785
commit 1a413998a3
2 changed files with 32 additions and 5 deletions

View File

@ -19,12 +19,16 @@ module Oga
# This parses yields proper XML instances such as {Oga::XML::Element}.
# Doctypes and XML declarations are ignored by this parser.
#
# @!attribute [r] node
# The current node.
# @return [Oga::XML::Node]
#
# @!attribute [r] nesting
# Array containing the names of the currently nested elements.
# @return [Array]
#
class PullParser < Parser
attr_reader :nesting
attr_reader :node, :nesting
##
# @return [Array]
@ -53,6 +57,7 @@ module Oga
@block = nil
@nesting = []
@node = nil
end
##
@ -83,7 +88,8 @@ module Oga
BLOCK_CALLBACKS.each do |method|
eval <<-EOF, nil, __FILE__, __LINE__ + 1
def #{method}(*args)
@block.call(super)
@node = super
@block.call(@node)
return
end
EOF
@ -93,11 +99,11 @@ module Oga
# @see Oga::XML::Parser#on_element
#
def on_element(*args)
element = super
@node = super
nesting << element.name
nesting << @node.name
@block.call(element)
@block.call(@node)
return
end

View File

@ -0,0 +1,21 @@
require 'spec_helper'
describe Oga::XML::PullParser do
context 'tracking nodes' do
before :all do
@parser = described_class.new('<a></a>')
end
example 'track the current node' do
@parser.parse do
@parser.node.is_a?(Oga::XML::Element).should == true
end
end
example 'reset the current node after parsing' do
@parser.parse { }
@parser.node.nil?.should == true
end
end
end