Skip to content

Commit 2b47b16

Browse files
committed
parser: move duplicated end tag check to BaseParser
1 parent 35e1681 commit 2b47b16

File tree

4 files changed

+5
-16
lines changed

4 files changed

+5
-16
lines changed

lib/rexml/parsers/baseparser.rb

+4
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ def pull_event
249249
if @document_status == :in_doctype
250250
raise ParseException.new("Malformed DOCTYPE: unclosed", @source)
251251
end
252+
unless @tags.empty?
253+
path = "/" + @tags.join("/")
254+
raise ParseException.new("Missing end tag for '#{path}'", @source)
255+
end
252256
return [ :end_document ]
253257
end
254258
return @stack.shift if @stack.size > 0

lib/rexml/parsers/streamparser.rb

-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class StreamParser
77
def initialize source, listener
88
@listener = listener
99
@parser = BaseParser.new( source )
10-
@tag_stack = []
1110
end
1211

1312
def add_listener( listener )
@@ -20,21 +19,14 @@ def parse
2019
event = @parser.pull
2120
case event[0]
2221
when :end_document
23-
unless @tag_stack.empty?
24-
tag_path = "/" + @tag_stack.join("/")
25-
raise ParseException.new("Missing end tag for '#{tag_path}'",
26-
@parser.source)
27-
end
2822
return
2923
when :start_element
30-
@tag_stack << event[1]
3124
attrs = event[2].each do |n, v|
3225
event[2][n] = @parser.unnormalize( v )
3326
end
3427
@listener.tag_start( event[1], attrs )
3528
when :end_element
3629
@listener.tag_end( event[1] )
37-
@tag_stack.pop
3830
when :text
3931
unnormalized = @parser.unnormalize( event[1] )
4032
@listener.text( unnormalized )

lib/rexml/parsers/treeparser.rb

-7
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,20 @@ def add_listener( listener )
1515
end
1616

1717
def parse
18-
tag_stack = []
1918
entities = nil
2019
begin
2120
while true
2221
event = @parser.pull
2322
#STDERR.puts "TREEPARSER GOT #{event.inspect}"
2423
case event[0]
2524
when :end_document
26-
unless tag_stack.empty?
27-
raise ParseException.new("No close tag for #{@build_context.xpath}",
28-
@parser.source, @parser)
29-
end
3025
return
3126
when :start_element
32-
tag_stack.push(event[1])
3327
el = @build_context = @build_context.add_element( event[1] )
3428
event[2].each do |key, value|
3529
el.attributes[key]=Attribute.new(key,value,self)
3630
end
3731
when :end_element
38-
tag_stack.pop
3932
@build_context = @build_context.parent
4033
when :text
4134
if @build_context[-1].instance_of? Text

test/parser/test_tree.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_no_close_tag
3131
parse(xml)
3232
end
3333
assert_equal(<<-MESSAGE, exception.to_s)
34-
No close tag for /root
34+
Missing end tag for '/root'
3535
Line: 1
3636
Position: #{xml.bytesize}
3737
Last 80 unconsumed characters:

0 commit comments

Comments
 (0)