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

Handle unprefixed attributes in Element#attribute correctly #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 3 additions & 16 deletions lib/rexml/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1270,27 +1270,14 @@ def [](name_or_index)
# With arguments +name+ and +namespace+ given,
# returns the value of the named attribute if it exists, otherwise +nil+:
#
# xml_string = "<root xmlns:a='a' a:x='a:x' x='x'/>"
# xml_string = "<root xmlns:a='http://example.com/a' a:x='a:x' x='x'/>"
# document = REXML::Document.new(xml_string)
# document.root.attribute("x") # => x='x'
# document.root.attribute("x", "a") # => a:x='a:x'
# document.root.attribute("x", "http://example.com/a") # => a:x='a:x'
Comment on lines +1273 to +1276
Copy link
Contributor Author

@makenowjust makenowjust Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also updated here to make it clear that the namespace argument takes a namespace URI.

#
def attribute( name, namespace=nil )
prefix = namespaces.key(namespace) if namespace
prefix = nil if prefix == 'xmlns'

ret_val =
attributes.get_attribute( prefix ? "#{prefix}:#{name}" : name )

return ret_val unless ret_val.nil?
return nil if prefix.nil?

# now check that prefix'es namespace is not the same as the
# default namespace
return nil unless ( namespaces[ prefix ] == namespaces[ 'xmlns' ] )

attributes.get_attribute( name )

attributes.get_attribute( prefix ? "#{prefix}:#{name}" : name )
end

# :call-seq:
Expand Down
32 changes: 14 additions & 18 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1451,12 +1451,6 @@ def test_ticket_95
assert_equal(out1,out2)
end

def test_ticket_102
doc = REXML::Document.new '<doc xmlns="ns"><item name="foo"/></doc>'
assert_equal( "foo", doc.root.elements["*:item"].attribute("name","ns").to_s )
assert_equal( "item", doc.root.elements["*:item[@name='foo']"].name )
end

def test_ticket_14
# Per .2.5 Node Tests of XPath spec
assert_raise( REXML::UndefinedNamespaceException,
Expand All @@ -1478,18 +1472,6 @@ def test_ticket_105
assert_equal( 1, d.root.children.size )
end

# phantom namespace same as default namespace
def test_ticket_121
doc = REXML::Document.new(
'<doc xmlns="ns" xmlns:phantom="ns"><item name="foo">text</item></doc>'
)
assert_equal 'text', doc.text( "/*:doc/*:item[@name='foo']" )
assert_equal "name='foo'",
doc.root.elements["*:item"].attribute("name", "ns").inspect
assert_equal "<item name='foo'>text</item>",
doc.root.elements["*:item[@name='foo']"].to_s
end

def test_ticket_135
bean_element = REXML::Element.new("bean")
textToAdd = "(&#38;(|(memberof=CN=somegroupabcdefgh,OU=OUsucks,DC=hookemhorns,DC=com)(mail=*someco.com))(acct=%u)(!(extraparameter:2.2.222.222222.2.2.222:=2)))"
Expand Down Expand Up @@ -1521,6 +1503,20 @@ def test_ticket_138
REXML::Document.new(doc.root.to_s).root.attributes.to_h)
end

def test_unprefixed_attribute
doc = REXML::Document.new(<<~XML)
<root xmlns="http://example.org/test">
<foo bar="baz" />
</root>
XML

assert_equal("baz", doc.elements["//foo"].attribute("bar")&.value)

# Unprefixed attributes have no prefix and the default namespace is not applied.
# See https://www.w3.org/TR/xml-names/#defaulting.
assert_equal(nil, doc.elements["//foo"].attribute("bar", "http://example.org/test")&.value)
end

def test_empty_doc
assert(REXML::Document.new('').children.empty?)
end
Expand Down