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