julien51-sax-machine 0.0.20 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,6 +18,7 @@ module SAXMachine
18
18
  if @default_xmlns && @xmlns && !@xmlns.include?('')
19
19
  @xmlns << ''
20
20
  end
21
+ @record_events = options[:events]
21
22
  end
22
23
 
23
24
  def handler(nsstack)
@@ -25,7 +26,11 @@ module SAXMachine
25
26
  nsstack = NSStack.new(nsstack, nsstack)
26
27
  nsstack[''] = @default_xmlns
27
28
  end
28
- SAXHandler.new(@class.new, nsstack)
29
+ unless @record_events
30
+ SAXHandler.new(@class.new, nsstack)
31
+ else
32
+ SAXEventRecorder.new(nsstack)
33
+ end
29
34
  end
30
35
 
31
36
  def accessor
@@ -68,7 +68,7 @@ module SAXMachine
68
68
 
69
69
  def elements(name, options = {})
70
70
  options[:as] ||= name
71
- if options[:class]
71
+ if options[:class] || options[:events]
72
72
  sax_config.add_collection_element(name, options)
73
73
  else
74
74
  class_eval <<-SRC
@@ -33,8 +33,9 @@ module SAXMachine
33
33
 
34
34
  elsif @collection_config = sax_config.collection_config(@name, @nsstack)
35
35
  @collection_handler = @collection_config.handler(@nsstack)
36
- @collection_handler.start_element(@name, @attrs)
37
-
36
+ if @object.class != @collection_handler.object.class
37
+ @collection_handler.start_element(@name, @attrs)
38
+ end
38
39
  elsif (element_configs = sax_config.element_configs_for_attribute(@name, @attrs)).any?
39
40
  parse_element_attributes(element_configs)
40
41
  set_element_config_for_element_value
data/lib/sax-machine.rb CHANGED
@@ -5,7 +5,8 @@ require "cgi"
5
5
  require "sax-machine/sax_document"
6
6
  require "sax-machine/sax_handler"
7
7
  require "sax-machine/sax_config"
8
+ require "sax-machine/sax_event_recorder"
8
9
 
9
10
  module SAXMachine
10
- VERSION = "0.0.20"
11
+ VERSION = "0.0.21"
11
12
  end
@@ -344,7 +344,7 @@ describe "SAXMachine" do
344
344
  end
345
345
 
346
346
  context "when passing a default namespace" do
347
- before :each do
347
+ before :all do
348
348
  @xmlns = 'urn:test'
349
349
  class Inner
350
350
  include SAXMachine
@@ -373,7 +373,7 @@ describe "SAXMachine" do
373
373
 
374
374
  describe "elements" do
375
375
  describe "when parsing multiple elements" do
376
- before :each do
376
+ before :all do
377
377
  @klass = Class.new do
378
378
  include SAXMachine
379
379
  elements :entry, :as => :entries
@@ -447,6 +447,50 @@ describe "SAXMachine" do
447
447
  document.entries.first.url.should == "http://pauldix.net"
448
448
  end
449
449
  end
450
+
451
+ describe "when desiring sax events" do
452
+ XHTML_XMLNS = "http://www.w3.org/1999/xhtml"
453
+
454
+ before :all do
455
+ @klass = Class.new do
456
+ include SAXMachine
457
+ elements :body, :events => true
458
+ end
459
+ end
460
+
461
+ it "should parse a simple child" do
462
+ document = @klass.parse("<body><p/></body>")
463
+ document.body[0].should == [[:start_element, "", "p", []],
464
+ [:end_element, "", "p"]]
465
+ end
466
+ it "should parse a simple child with text" do
467
+ document = @klass.parse("<body><p>Hello</p></body>")
468
+ document.body[0].should == [[:start_element, "", "p", []],
469
+ [:chars, "Hello"],
470
+ [:end_element, "", "p"]]
471
+ end
472
+ it "should parse nested children" do
473
+ document = @klass.parse("<body><p><span/></p></body>")
474
+ document.body[0].should == [[:start_element, "", "p", []],
475
+ [:start_element, "", "span", []],
476
+ [:end_element, "", "span"],
477
+ [:end_element, "", "p"]]
478
+ end
479
+ it "should parse multiple children" do
480
+ document = @klass.parse("<body><p>Hello</p><p>World</p></body>")
481
+ document.body[0].should == [[:start_element, "", "p", []],
482
+ [:chars, "Hello"],
483
+ [:end_element, "", "p"],
484
+ [:start_element, "", "p", []],
485
+ [:chars, "World"],
486
+ [:end_element, "", "p"]]
487
+ end
488
+ it "should pass namespaces" do
489
+ document = @klass.parse("<body xmlns='#{XHTML_XMLNS}'><p/></body>")
490
+ document.body[0].should == [[:start_element, XHTML_XMLNS, "p", []],
491
+ [:end_element, XHTML_XMLNS, "p"]]
492
+ end
493
+ end
450
494
  end
451
495
 
452
496
  describe "full example" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: julien51-sax-machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.20
4
+ version: 0.0.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix