File tree Expand file tree Collapse file tree 6 files changed +99
-7
lines changed Expand file tree Collapse file tree 6 files changed +99
-7
lines changed Original file line number Diff line number Diff line change @@ -460,15 +460,24 @@ def pull_event
460
460
@closed = tag
461
461
@nsstack . shift
462
462
else
463
+ if @tags . empty? and @have_root
464
+ raise ParseException . new ( "Malformed XML: Extra tag at the end of the document (got '<#{ tag } ')" , @source )
465
+ end
463
466
@tags . push ( tag )
464
467
end
468
+ @have_root = true
465
469
return [ :start_element , tag , attributes ]
466
470
end
467
471
else
468
472
text = @source . read_until ( "<" )
469
473
if text . chomp! ( "<" )
470
474
@source . position -= "<" . bytesize
471
475
end
476
+ if @tags . empty? and @have_root
477
+ unless /\A \s *\z / . match? ( text )
478
+ raise ParseException . new ( "Malformed XML: Extra content at the end of the document (got '#{ text } ')" , @source )
479
+ end
480
+ end
472
481
return [ :text , text ]
473
482
end
474
483
rescue REXML ::UndefinedNamespaceException
Original file line number Diff line number Diff line change @@ -105,5 +105,17 @@ def test_after_doctype_malformed_comment_end
105
105
DETAIL
106
106
end
107
107
end
108
+
109
+ def test_after_root
110
+ parser = REXML ::Parsers ::BaseParser . new ( '<a></a><!-- ok comment -->' )
111
+
112
+ events = { }
113
+ while parser . has_next?
114
+ event = parser . pull
115
+ events [ event [ 0 ] ] = event [ 1 ]
116
+ end
117
+
118
+ assert_equal ( " ok comment " , events [ :comment ] )
119
+ end
108
120
end
109
121
end
Original file line number Diff line number Diff line change @@ -85,6 +85,40 @@ def test_garbage_less_than_slash_before_end_tag_at_line_start
85
85
</ </x>
86
86
DETAIL
87
87
end
88
+
89
+ def test_after_root
90
+ exception = assert_raise ( REXML ::ParseException ) do
91
+ parser = REXML ::Parsers ::BaseParser . new ( '<a></a><b>' )
92
+ while parser . has_next?
93
+ parser . pull
94
+ end
95
+ end
96
+
97
+ assert_equal ( <<~DETAIL . chomp , exception . to_s )
98
+ Malformed XML: Extra tag at the end of the document (got '<b')
99
+ Line: 1
100
+ Position: 10
101
+ Last 80 unconsumed characters:
102
+
103
+ DETAIL
104
+ end
105
+
106
+ def test_after_empty_element_tag_root
107
+ exception = assert_raise ( REXML ::ParseException ) do
108
+ parser = REXML ::Parsers ::BaseParser . new ( '<a/><b>' )
109
+ while parser . has_next?
110
+ parser . pull
111
+ end
112
+ end
113
+
114
+ assert_equal ( <<~DETAIL . chomp , exception . to_s )
115
+ Malformed XML: Extra tag at the end of the document (got '<b')
116
+ Line: 1
117
+ Position: 7
118
+ Last 80 unconsumed characters:
119
+
120
+ DETAIL
121
+ end
88
122
end
89
123
end
90
124
end
Original file line number Diff line number Diff line change @@ -40,5 +40,17 @@ def test_garbage_text
40
40
] )
41
41
end
42
42
end
43
+
44
+ def test_after_root
45
+ parser = REXML ::Parsers ::BaseParser . new ( '<a></a><?abc version="1.0" ?>' )
46
+
47
+ events = { }
48
+ while parser . has_next?
49
+ event = parser . pull
50
+ events [ event [ 0 ] ] = event [ 1 ]
51
+ end
52
+
53
+ assert_equal ( "abc" , events [ :processing_instruction ] )
54
+ end
43
55
end
44
56
end
Original file line number Diff line number Diff line change
1
+ require "test/unit"
2
+ require 'rexml/parsers/baseparser'
3
+
4
+ module REXMLTests
5
+ class TestParseText < Test ::Unit ::TestCase
6
+ class TestInvalid < self
7
+ def test_after_root
8
+ exception = assert_raise ( REXML ::ParseException ) do
9
+ parser = REXML ::Parsers ::BaseParser . new ( '<a></a>c' )
10
+ while parser . has_next?
11
+ parser . pull
12
+ end
13
+ end
14
+
15
+ assert_equal ( <<~DETAIL . chomp , exception . to_s )
16
+ Malformed XML: Extra content at the end of the document (got 'c')
17
+ Line: 1
18
+ Position: 8
19
+ Last 80 unconsumed characters:
20
+
21
+ DETAIL
22
+ end
23
+ end
24
+ end
25
+ end
Original file line number Diff line number Diff line change @@ -63,23 +63,23 @@ def test_entity_replacement
63
63
end
64
64
65
65
def test_character_references
66
- source = '<a>A</a><b>B</b>'
66
+ source = '<root>< a>A</a><b>B</b></root >'
67
67
parser = REXML ::Parsers ::PullParser . new ( source )
68
+
69
+ events = { }
68
70
element_name = ''
69
71
while parser . has_next?
70
72
event = parser . pull
71
73
case event . event_type
72
74
when :start_element
73
75
element_name = event [ 0 ]
74
76
when :text
75
- case element_name
76
- when 'a'
77
- assert_equal ( 'A' , event [ 1 ] )
78
- when 'b'
79
- assert_equal ( 'B' , event [ 1 ] )
80
- end
77
+ events [ element_name ] = event [ 1 ]
81
78
end
82
79
end
80
+
81
+ assert_equal ( 'A' , events [ 'a' ] )
82
+ assert_equal ( "B" , events [ 'b' ] )
83
83
end
84
84
85
85
def test_text_content_with_line_breaks
You can’t perform that action at this time.
0 commit comments