Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling with "xml:" prefixed namespace #208

Merged
merged 5 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ module Private
default_entities.each do |term|
DEFAULT_ENTITIES_PATTERNS[term] = /&#{term};/
end
XML_PREFIXED_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
end
private_constant :Private

Expand Down Expand Up @@ -185,7 +186,7 @@ def stream=( source )
@tags = []
@stack = []
@entities = []
@namespaces = {}
@namespaces = {"xml" => Private::XML_PREFIXED_NAMESPACE}
@namespaces_restore_stack = []
end

Expand Down Expand Up @@ -790,7 +791,7 @@ def parse_attributes(prefixes)
@source.match(/\s*/um, true)
if prefix == "xmlns"
if local_part == "xml"
if value != "http://www.w3.org/XML/1998/namespace"
if value != Private::XML_PREFIXED_NAMESPACE
msg = "The 'xml' prefix must not be bound to any other namespace "+
"(http://www.w3.org/TR/REC-xml-names/#ns-decl)"
raise REXML::ParseException.new( msg, @source, self )
Expand Down
35 changes: 35 additions & 0 deletions test/parser/test_base_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,40 @@ def test_large_xml
parser.position < xml.bytesize
end
end

def test_attribute_prefixed_by_xml
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>XHTML Document</title>
</head>
<body>
<h1>XHTML Document</h1>
<p xml:lang="ja" lang="ja">この段落は日本語です。</p>
</body>
</html>
XML

parser = REXML::Parsers::BaseParser.new(xml)
5.times {parser.pull}

html = parser.pull
assert_equal([:start_element,
"html",
{"xmlns" => "http://www.w3.org/1999/xhtml",
"xml:lang" => "en",
"lang" => "en"}],
html)

15.times {parser.pull}

p = parser.pull
assert_equal([:start_element,
"p",
{"xml:lang" => "ja", "lang" => "ja"}],
p)
end
end
end
Loading