Contextual pull parsing.

This adds the ability to more easily act upon specific node types and nestings
when using the pull parsing API.

A basic example of this API looks like the following (only including relevant
code):

    parser.parse do |node|
      parser.on(:element, %w{people person}) do
        people << {:name => nil, :age => nil}
      end

      parser.on(:text, %w{people person name}) do
        people.last[:name] = node.text
      end

      parser.on(:text, %w{people person age}) do
        people.last[:age] = node.text.to_i
      end
    end

This fixes #6.
This commit is contained in:
Yorick Peterse committed 2014-04-29 23:05:49 +02:00
1 parent 1a413998a3
commit 83f6d5437e
16 files changed
+164

No files matched your search

+6
View File
@@ -33,4 +33,10 @@ describe Oga::XML::Cdata do
@instance.inspect.should == 'Cdata(text: "foo")'
end
end
context '#type' do
example 'return the type of the node' do
described_class.new.node_type.should == :cdata
end
end
end
+6
View File
@@ -33,4 +33,10 @@ describe Oga::XML::Comment do
@instance.inspect.should == 'Comment(text: "foo")'
end
end
context '#type' do
example 'return the type of the node' do
described_class.new.node_type.should == :comment
end
end
end
+6
View File
@@ -63,4 +63,10 @@ Doctype(
EOF
end
end
context '#type' do
example 'return the type of the node' do
described_class.new.node_type.should == :doctype
end
end
end
+6
View File
@@ -80,4 +80,10 @@ Element(
EOF
end
end
context '#type' do
example 'return the type of the node' do
described_class.new.node_type.should == :element
end
end
end
+6
View File
@@ -13,4 +13,10 @@ describe Oga::XML::Node do
described_class.new.children.should == []
end
end
context '#type' do
example 'return the type of the node' do
described_class.new.node_type.should == :node
end
end
end
@@ -0,0 +1,29 @@
require 'spec_helper'
describe Oga::XML::PullParser do
context '#on' do
before do
@parser = Oga::XML::PullParser.new('<a><b></b></a>')
@parser.stub(:node).and_return(Oga::XML::Text.new)
end
example 'do not yield if the node types do not match' do
expect { |b| @parser.on(:element, &b) }.to_not yield_control
end
example 'yield if the node type matches and the nesting is empty' do
expect { |b| @parser.on(:text, &b) }.to yield_control
end
example 'do not yield if the node type matches but the nesting does not' do
expect { |b| @parser.on(:text, %w{foo}, &b) }.to_not yield_control
end
example 'yield if the node type and the nesting matches' do
@parser.stub(:nesting).and_return(%w{a b})
expect { |b| @parser.on(:text, %w{a b}, &b) }.to yield_control
end
end
end
+6
View File
@@ -33,4 +33,10 @@ describe Oga::XML::Text do
@instance.inspect.should == 'Text(text: "foo")'
end
end
context '#type' do
example 'return the type of the node' do
described_class.new.node_type.should == :text
end
end
end
+6
View File
@@ -58,4 +58,10 @@ XmlDeclaration(
EOF
end
end
context '#type' do
example 'return the type of the node' do
described_class.new.node_type.should == :xml_decl
end
end
end