From 6ee62acc6888ccdfeace7f68013c9b2f3a8aa3e6 Mon Sep 17 00:00:00 2001 From: Wleeaf <179125857+wleeaf@users.noreply.github.com> Date: Sat, 24 Oct 2026 22:54:05 +0000 Subject: [PATCH] Accept non-ASCII names in DOCTYPE, processing instructions and DTD declarations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BaseParser::NAME used \w, which is ASCII-only in Ruby, so , , , &été;, and non-ASCII ATTLIST names raised ParseException, although element and attribute names already accept non-ASCII letters. Use the XML Name production from XMLTokens (NameStartChar and NameChar), which REXML::Entity and REXML::Text already use. Names starting with a digit, which [5] Name does not allow, are now rejected in these places too. --- lib/rexml/parsers/baseparser.rb | 5 +++-- test/parse/test_document_type_declaration.rb | 7 +++++++ test/parse/test_entity_declaration.rb | 8 ++++++++ test/parse/test_notation_declaration.rb | 5 +++++ test/parse/test_processing_instruction.rb | 5 +++++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/rexml/parsers/baseparser.rb b/lib/rexml/parsers/baseparser.rb index d87f281d..5d9a6f2e 100644 --- a/lib/rexml/parsers/baseparser.rb +++ b/lib/rexml/parsers/baseparser.rb @@ -4,6 +4,7 @@ require_relative '../security' require_relative '../source' require_relative '../text' +require_relative '../xmltokens' require 'set' require "strscan" @@ -70,8 +71,8 @@ class BaseParser # It's not used in REXML. UNAME_STR= "(?:#{NCNAME_STR}:)?#{NCNAME_STR}" - NAMECHAR = '[\-\w\.:]' - NAME = "([\\w:]#{NAMECHAR}*)" + NAMECHAR = XMLTokens::NAME_CHAR + NAME = XMLTokens::NAME NMTOKEN = "(?:#{NAMECHAR})+" NMTOKENS = "#{NMTOKEN}(\\s+#{NMTOKEN})*" REFERENCE = "&(?:#{NAME};|#\\d+;|#x[0-9a-fA-F]+;)" diff --git a/test/parse/test_document_type_declaration.rb b/test/parse/test_document_type_declaration.rb index cc37ad3f..444e4729 100644 --- a/test/parse/test_document_type_declaration.rb +++ b/test/parse/test_document_type_declaration.rb @@ -24,6 +24,13 @@ def test_valid assert_equal("r", doctype.name) end + def test_non_ascii + doctype = parse(<<-DOCTYPE) + + DOCTYPE + assert_equal("été", doctype.name) + end + def test_garbage_plus_before_name_at_line_start exception = assert_raise(REXML::ParseException) do parse(<<-DOCTYPE) diff --git a/test/parse/test_entity_declaration.rb b/test/parse/test_entity_declaration.rb index 0cbd4550..c65b0026 100644 --- a/test/parse/test_entity_declaration.rb +++ b/test/parse/test_entity_declaration.rb @@ -40,6 +40,14 @@ def test_prohibited_character invalid&name "valid-entity-value">]> DETAIL end + + def test_non_ascii + document = REXML::Document.new(<<-XML) +]> +&été; + XML + assert_equal("summer", document.root.text) + end end # https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityDef diff --git a/test/parse/test_notation_declaration.rb b/test/parse/test_notation_declaration.rb index 11914d37..65015270 100644 --- a/test/parse/test_notation_declaration.rb +++ b/test/parse/test_notation_declaration.rb @@ -24,6 +24,11 @@ def test_name assert_equal("name", doctype.notation("name").name) end + def test_non_ascii_name + doctype = parse("") + assert_equal("été", doctype.notation("été").name) + end + def test_no_name exception = assert_raise(REXML::ParseException) do parse(<<-INTERNAL_SUBSET) diff --git a/test/parse/test_processing_instruction.rb b/test/parse/test_processing_instruction.rb index 723a8cd0..f7c7de64 100644 --- a/test/parse/test_processing_instruction.rb +++ b/test/parse/test_processing_instruction.rb @@ -234,6 +234,11 @@ def test_content_question assert_equal("con?tent", document.root.children.first.content) end + def test_non_ascii_target + document = parse("") + assert_equal("été", document.children.first.target) + end + def test_linear_performance_gt seq = [10000, 50000, 100000, 150000, 200000] assert_linear_performance(seq, rehearsal: 10) do |n|