Skip to content

Commit 7d95b0f

Browse files
committed
fix(jruby): Node#attribute in HTML documents
and anything that depends on that, like NodeSet#attr. Fixes #3487
1 parent 58823ff commit 7d95b0f

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

ext/java/nokogiri/XmlNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ public class XmlNode extends RubyObject
652652
Node attribute = attributes.item(j);
653653
String localName = attribute.getLocalName();
654654
if (localName == null) {
655-
continue;
655+
localName = attribute.getNodeName();
656656
}
657657
if (localName.equals(name)) {
658658
return getCachedNodeOrCreate(context.runtime, attribute);

test/html4/test_node.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@ def test_to_a
2323

2424
def test_attr
2525
node = @html.at("div.baz")
26-
assert_equal(node["class"], node.attr("class"))
26+
assert_equal("baz", node["class"])
27+
assert_equal("baz", node.attr("class"))
28+
end
29+
30+
def test_attribute
31+
# https://github.com/sparklemotion/nokogiri/issues/3487
32+
node = @html.at("div.baz")
33+
refute_nil(node.attribute("class"))
34+
assert_equal("baz", node.attribute("class").value)
2735
end
2836

2937
def test_get_attribute

test/xml/test_node_set.rb

+17
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ class TestNodeSet < Nokogiri::TestCase
129129
end
130130
end
131131

132+
it "#attr on XML gets attribute from first node" do
133+
doc = Nokogiri::XML("<root><child name='ruby' /><child name='python' /></root>")
134+
children = doc.css("child")
135+
136+
refute_nil(children.attr("name"))
137+
assert_equal(children.first.attribute("name"), children.attr("name"))
138+
end
139+
140+
it "#attr on HTML gets attribute from first node" do
141+
# https://github.com/sparklemotion/nokogiri/issues/3487
142+
doc = Nokogiri::HTML("<root><child name='ruby' /><child name='python' /></root>")
143+
children = doc.css("child")
144+
145+
refute_nil(children.attr("name"))
146+
assert_equal(children.first.attribute("name"), children.attr("name"))
147+
end
148+
132149
it "#attribute with no args gets attribute from first node" do
133150
list.first["foo"] = "bar"
134151
assert_equal(list.first.attribute("foo"), list.attribute("foo"))

0 commit comments

Comments
 (0)