nokogiri-streaming-reader 0.0.4 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nokogiri/streaming/reader.rb +22 -10
- data/lib/nokogiri/streaming/version.rb +1 -1
- data/spec/reader_spec.rb +70 -46
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37fc22b99e9fd94795e988f57082726da98ef508
|
4
|
+
data.tar.gz: e3bb119767f3a94f69f6f9bad22c5a77c515d58d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb074d9be0e846dd89a034f026ba01c38224426a94f7cbba99dfd623d1a33dd804b53c52ff3a5300c2150ddbd4cc23c7b7e58f56dec73dfabb168841800fbb92
|
7
|
+
data.tar.gz: 79d289710b6dc9869e59639bfafb33806299021f0ec8fa42429cb7d7c01600b962ba9bc0682aad1950936a0ac5438c5005acab61521e7c695f1543fbc4f0e54a
|
@@ -34,7 +34,8 @@ module Nokogiri
|
|
34
34
|
@source = IOWrapper.new(source)
|
35
35
|
@parser = Nokogiri::XML::SAX::Parser.new(self)
|
36
36
|
@stack = []
|
37
|
-
@
|
37
|
+
@end_triggers = {}
|
38
|
+
@start_triggers = {}
|
38
39
|
end
|
39
40
|
|
40
41
|
def run
|
@@ -42,7 +43,11 @@ module Nokogiri
|
|
42
43
|
end
|
43
44
|
|
44
45
|
def on(path, &block)
|
45
|
-
(@
|
46
|
+
(@end_triggers[path] ||= []).push(block)
|
47
|
+
end
|
48
|
+
|
49
|
+
def on_start(path, &block)
|
50
|
+
(@start_triggers[path] ||= []).push(block)
|
46
51
|
end
|
47
52
|
|
48
53
|
def current_path
|
@@ -70,19 +75,27 @@ module Nokogiri
|
|
70
75
|
def start_element(name, attrs = [])
|
71
76
|
if @current
|
72
77
|
element = @current.document.create_element(name)
|
73
|
-
attrs.each do |
|
74
|
-
element[
|
78
|
+
attrs.each do |attrname, value|
|
79
|
+
element[attrname] = value
|
75
80
|
end
|
76
81
|
@current.add_child(element)
|
77
82
|
@current = element
|
78
|
-
elsif @
|
83
|
+
elsif @end_triggers.include?(current_path + '/' + name)
|
79
84
|
fragment = Nokogiri::XML::DocumentFragment.new(Nokogiri::XML::Document.new)
|
80
|
-
|
81
85
|
element = fragment.document.create_element(name)
|
82
|
-
attrs.each do |
|
83
|
-
element[
|
86
|
+
attrs.each do |attrname, value|
|
87
|
+
element[attrname] = value
|
84
88
|
end
|
85
89
|
@current = element
|
90
|
+
elsif (triggers = @start_triggers[current_path + '/' + name])
|
91
|
+
fragment = Nokogiri::XML::DocumentFragment.new(Nokogiri::XML::Document.new)
|
92
|
+
element = fragment.document.create_element(name)
|
93
|
+
attrs.each do |attrname, value|
|
94
|
+
element[attrname] = value
|
95
|
+
end
|
96
|
+
triggers.each do |proc|
|
97
|
+
proc.call(element)
|
98
|
+
end
|
86
99
|
end
|
87
100
|
@stack.push(name)
|
88
101
|
end
|
@@ -97,8 +110,7 @@ module Nokogiri
|
|
97
110
|
@current = @current.parent
|
98
111
|
end
|
99
112
|
|
100
|
-
triggers = @
|
101
|
-
if triggers
|
113
|
+
if (triggers = @end_triggers[path])
|
102
114
|
triggers.each do |proc|
|
103
115
|
proc.call(element)
|
104
116
|
end
|
data/spec/reader_spec.rb
CHANGED
@@ -6,62 +6,86 @@ describe Nokogiri::Streaming::Reader do
|
|
6
6
|
Nokogiri::Streaming::Reader
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
<
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
describe "#on" do
|
10
|
+
it 'parses registered paths' do
|
11
|
+
doc = StringIO.new %{
|
12
|
+
<root>
|
13
|
+
<fruit/>
|
14
|
+
<fruit/>
|
15
|
+
<vegetable id='1'><seed/></vegetable>
|
16
|
+
<meat><type>beef</type></meat>
|
17
|
+
</root>
|
18
|
+
}
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
fruits = []
|
21
|
+
vegetables = []
|
22
|
+
meats = []
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
reader = subject.new(doc)
|
25
|
+
reader.on('/root/fruit') do |e|
|
26
|
+
fruits.push(e.to_xml(indent: 0))
|
27
|
+
end
|
28
|
+
reader.on('/root/vegetable') do |e|
|
29
|
+
vegetables.push(e.to_xml(indent: 0))
|
30
|
+
end
|
31
|
+
reader.on('/root/meat/type') do |e|
|
32
|
+
meats.push(e.to_xml(indent: 0))
|
33
|
+
end
|
34
|
+
reader.run
|
35
|
+
|
36
|
+
expect(fruits).to eq ['<fruit/>', '<fruit/>']
|
37
|
+
expect(vegetables).to eq ["<vegetable id=\"1\">\n<seed/>\n</vegetable>"]
|
38
|
+
expect(meats).to eq ["<type>beef</type>"]
|
32
39
|
end
|
33
|
-
reader.run
|
34
40
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
41
|
+
it 'preserves CDATA nodes' do
|
42
|
+
doc = StringIO.new %{
|
43
|
+
<root><![CDATA[banana]]></root>
|
44
|
+
}
|
45
|
+
|
46
|
+
reader = subject.new(doc)
|
47
|
+
reader.on('/root') do |e|
|
48
|
+
expect(e.children.length).to eq 1
|
49
|
+
expect(e.children.first.cdata?).to be true
|
50
|
+
end
|
51
|
+
reader.run
|
52
|
+
end
|
39
53
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
54
|
+
it 'preserves text nodes' do
|
55
|
+
doc = StringIO.new %{
|
56
|
+
<root>banana</root>
|
57
|
+
}
|
44
58
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
59
|
+
reader = subject.new(doc)
|
60
|
+
reader.on('/root') do |e|
|
61
|
+
expect(e.children.length).to eq 1
|
62
|
+
expect(e.children.first.text?).to be true
|
63
|
+
expect(e.children.first.cdata?).to be false
|
64
|
+
end
|
65
|
+
reader.run
|
49
66
|
end
|
50
|
-
reader.run
|
51
67
|
end
|
52
68
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
69
|
+
describe "#on_start" do
|
70
|
+
it 'parses registered paths' do
|
71
|
+
doc = StringIO.new %{
|
72
|
+
<root>
|
73
|
+
<fruit name="apple"/>
|
74
|
+
<fruit/>
|
75
|
+
<fruit>something</fruit>
|
76
|
+
<vegetable id='1'><seed/></vegetable>
|
77
|
+
<meat><type>beef</type></meat>
|
78
|
+
</root>
|
79
|
+
}
|
80
|
+
fruits = []
|
81
|
+
reader = subject.new(doc)
|
82
|
+
reader.on_start('/root/fruit') do |e|
|
83
|
+
fruits.push(e.to_xml(indent: 0))
|
84
|
+
end
|
85
|
+
reader.run
|
57
86
|
|
58
|
-
|
59
|
-
reader.on('/root') do |e|
|
60
|
-
expect(e.children.length).to eq 1
|
61
|
-
expect(e.children.first.text?).to be true
|
62
|
-
expect(e.children.first.cdata?).to be false
|
87
|
+
expect(fruits).to eq ['<fruit name="apple"/>', '<fruit/>', '<fruit/>']
|
63
88
|
end
|
64
|
-
reader.run
|
65
89
|
end
|
66
90
|
|
67
|
-
end
|
91
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nokogiri-streaming-reader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: '1.1'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Staubo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -103,11 +103,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
103
|
version: '0'
|
104
104
|
requirements: []
|
105
105
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.2
|
106
|
+
rubygems_version: 2.5.2
|
107
107
|
signing_key:
|
108
108
|
specification_version: 4
|
109
109
|
summary: Simple streaming reader for Nokogiri.
|
110
110
|
test_files:
|
111
111
|
- spec/reader_spec.rb
|
112
112
|
- spec/spec_helper.rb
|
113
|
-
has_rdoc:
|