1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/rexml] Fix attribute's default namespace behavior

NOTE: It's a backward incompatible change. If we have any serious
problems with this change, we may revert this change.

The XML namespace specification says the default namespace doesn't
apply to attribute names but it does in REXML without this change:

https://www.w3.org/TR/xml-names/#uniqAttrs

> the default namespace does not apply to attribute names

REXML reports a parse error for the following XML that is described as
a valid XML in the XML nsmaspace specification without this change:

    <!-- http://www.w3.org is bound to n1 and is the default -->
    <x xmlns:n1="http://www.w3.org"
       xmlns="http://www.w3.org" >
      <good a="1"     b="2" />
      <good a="1"     n1:a="2" />
    </x>

If attribute doesn't have prefix, the attribute should return "" for
both #prefix and #namespace.

9e4fd552bc
This commit is contained in:
Kouhei Sutou 2019-01-01 06:32:18 +09:00 committed by Hiroshi SHIBATA
parent 9b36f0a787
commit 0f18bc7fca
4 changed files with 63 additions and 22 deletions

View file

@ -67,15 +67,11 @@ module REXML
# e.add_attribute( "nsa:a", "aval" )
# e.add_attribute( "b", "bval" )
# e.attributes.get_attribute( "a" ).prefix # -> "nsa"
# e.attributes.get_attribute( "b" ).prefix # -> "elns"
# e.attributes.get_attribute( "b" ).prefix # -> ""
# a = Attribute.new( "x", "y" )
# a.prefix # -> ""
def prefix
pf = super
if pf == ""
pf = @element.prefix if @element
end
pf
super
end
# Returns the namespace URL, if defined, or nil otherwise
@ -87,9 +83,8 @@ module REXML
# e.attribute("ns:a").namespace # => "http://url"
# e.attribute("nsx:a").namespace # => nil
#
# TODO: This method should always return nil for no namespace
# attribute. Because the default namespace doesn't apply to
# attribute name.
# This method always returns "" for no namespace attribute. Because
# the default namespace doesn't apply to attribute names.
#
# From https://www.w3.org/TR/xml-names/#uniqAttrs
#
@ -99,10 +94,14 @@ module REXML
# e.add_namespace("", "http://example.com/")
# e.namespace # => "http://example.com/"
# e.add_attribute("a", "b")
# e.attribute("a").namespace # => nil
# e.attribute("a").namespace # => ""
def namespace arg=nil
arg = prefix if arg.nil?
@element.namespace arg
if arg == ""
""
else
@element.namespace(arg)
end
end
# Returns true if other is an Attribute and has the same name and value,