Skip to content

Commit a85203e

Browse files
authored
Raise appropriate exception when failing to match start tag in DOCTYPE (#247)
## Why? Added exception to make the process easier to understand.
1 parent a5f31c4 commit a85203e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/rexml/parsers/baseparser.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,15 @@ def pull_event
412412
return [:notationdecl, name, *id]
413413
elsif @source.match?("--", true)
414414
return [ :comment, process_comment ]
415+
else
416+
raise REXML::ParseException.new("Malformed node: Started with '<!' but not a comment nor ELEMENT,ENTITY,ATTLIST,NOTATION", @source)
415417
end
416418
elsif match = @source.match(/(%.*?;)\s*/um, true)
417419
return [ :externalentity, match[1] ]
418420
elsif @source.match?(/\]\s*>/um, true)
419421
@document_status = :after_doctype
420422
return [ :end_doctype ]
421-
end
422-
if @document_status == :in_doctype
423+
else
423424
raise ParseException.new("Malformed DOCTYPE: invalid declaration", @source)
424425
end
425426
end

test/parse/test_comment.rb

+13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ def test_toplevel_malformed_comment_end
4848
DETAIL
4949
end
5050

51+
def test_doctype_malformed_node
52+
exception = assert_raise(REXML::ParseException) do
53+
parse("<!DOCTYPE foo [<!a")
54+
end
55+
assert_equal(<<~DETAIL.chomp, exception.to_s)
56+
Malformed node: Started with '<!' but not a comment nor ELEMENT,ENTITY,ATTLIST,NOTATION
57+
Line: 1
58+
Position: 18
59+
Last 80 unconsumed characters:
60+
a
61+
DETAIL
62+
end
63+
5164
def test_doctype_unclosed_comment
5265
exception = assert_raise(REXML::ParseException) do
5366
parse("<!DOCTYPE foo [<!--")

0 commit comments

Comments
 (0)