From 59c0fb0b4860ff244345171bfc35f589e5e5ded7 Mon Sep 17 00:00:00 2001 From: Wleeaf <179125857+wleeaf@users.noreply.github.com> Date: Sat, 24 Oct 2026 22:46:06 +0000 Subject: [PATCH] Accept processing instructions in the internal DTD subset Since 3.3.1, a processing instruction in the internal subset raises "Malformed DOCTYPE: invalid declaration", although [28b] intSubset allows PIs (markupdecl | DeclSep, [29] markupdecl includes PI). 3.3.0 and earlier accepted them. Parse them with process_instruction, as for PIs outside the DOCTYPE; an XML declaration there is still rejected. --- lib/rexml/parsers/baseparser.rb | 2 ++ test/parse/test_document_type_declaration.rb | 9 ++++++ test/parse/test_processing_instruction.rb | 31 ++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/lib/rexml/parsers/baseparser.rb b/lib/rexml/parsers/baseparser.rb index d87f281d..09dae861 100644 --- a/lib/rexml/parsers/baseparser.rb +++ b/lib/rexml/parsers/baseparser.rb @@ -425,6 +425,8 @@ def pull_event else raise REXML::ParseException.new("Malformed node: Started with '/um, true) diff --git a/test/parse/test_document_type_declaration.rb b/test/parse/test_document_type_declaration.rb index cc37ad3f..b0a4551e 100644 --- a/test/parse/test_document_type_declaration.rb +++ b/test/parse/test_document_type_declaration.rb @@ -310,6 +310,15 @@ def test_notation_attlist doctype.children.collect(&:class)) end + def test_instruction_element + doctype = parse(<<-INTERNAL_SUBSET) + + + INTERNAL_SUBSET + assert_equal([REXML::Instruction, REXML::ElementDecl], + doctype.children.collect(&:class)) + end + private def parse(internal_subset) super(<<-DOCTYPE) diff --git a/test/parse/test_processing_instruction.rb b/test/parse/test_processing_instruction.rb index 723a8cd0..1d0b8452 100644 --- a/test/parse/test_processing_instruction.rb +++ b/test/parse/test_processing_instruction.rb @@ -81,6 +81,24 @@ def test_xml_declaration_not_at_document_start DETAIL end + def test_xml_declaration_in_internal_subset + exception = assert_raise(REXML::ParseException) do + parse(<<-XML) + +]> + + XML + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed XML: XML declaration is not at the start +Line: 2 +Position: 35 +Last 80 unconsumed characters: + version="1.0"?> + DETAIL + end + def test_xml_declaration_missing_spaces exception = assert_raise(REXML::ParseException) do parser = REXML::Parsers::BaseParser.new('') @@ -229,6 +247,19 @@ def test_after_root assert_equal("abc", events[:processing_instruction]) end + def test_in_internal_subset + doc = parse(<<-XML) + + +]> + + XML + instruction = doc.doctype.children.first + assert_equal(["x", "y"], + [instruction.target, instruction.content]) + end + def test_content_question document = REXML::Document.new("") assert_equal("con?tent", document.root.children.first.content)