Skip to content

Commit 80e888c

Browse files
committed
deprecate: Reader#attribute_nodes
This method will be removed in a future version
1 parent 8d0c4ea commit 80e888c

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

ext/java/nokogiri/XmlReader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public class XmlReader extends RubyObject
141141
public IRubyObject
142142
attribute_nodes(ThreadContext context)
143143
{
144+
context.runtime.getWarnings().warn("Reader#attribute_nodes is deprecated and will be removed in a future version of Nokogiri. Please use Reader#attribute_hash instead.");
144145
return currentNode().getAttributesNodes();
145146
}
146147

ext/nokogiri/xml_reader.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ namespaces(VALUE self)
151151
/*
152152
:call-seq: attribute_nodes() → Array<Nokogiri::XML::Attr>
153153
154-
Get the attributes of the current node as an Array of Attr
154+
Get the attributes of the current node as an Array of XML:Attr
155+
156+
⚠ This method is deprecated and unsafe to use. It will be removed in a future version of Nokogiri.
157+
158+
See related: #attribute_hash, #attributes
155159
*/
156160
static VALUE
157161
rb_xml_reader_attribute_nodes(VALUE rb_reader)
@@ -161,6 +165,10 @@ rb_xml_reader_attribute_nodes(VALUE rb_reader)
161165
VALUE attr_nodes;
162166
int j;
163167

168+
// TODO: deprecated, remove in Nokogiri v1.15, see https://github.com/sparklemotion/nokogiri/issues/2598
169+
// After removal, we can also remove all the "node_has_a_document" special handling from xml_node.c
170+
NOKO_WARN_DEPRECATION("Reader#attribute_nodes is deprecated and will be removed in a future version of Nokogiri. Please use Reader#attribute_hash instead.");
171+
164172
Data_Get_Struct(rb_reader, xmlTextReader, c_reader);
165173

166174
if (! has_attributes(c_reader)) {

lib/nokogiri/xml/reader.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ def initialize(source, url = nil, encoding = nil) # :nodoc:
8383
end
8484
private :initialize
8585

86-
# Get the attributes of the current node as a Hash
86+
# Get the attributes and namespaces of the current node as a Hash.
8787
#
88-
# [Returns] (Hash<String, String>) Attribute names and values
88+
# This is the union of Reader#attribute_hash and Reader#namespaces
89+
#
90+
# [Returns]
91+
# (Hash<String, String>) Attribute names and values, and namespace prefixes and hrefs.
8992
def attributes
9093
attribute_hash.merge(namespaces)
9194
end

test/xml/test_reader.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,9 @@ def test_reader_node_attributes_keep_a_reference_to_the_reader
472472

473473
reader = Nokogiri::XML::Reader.from_memory(xml)
474474
reader.each do |element|
475-
attribute_nodes += element.attribute_nodes
475+
assert_output(nil, /Reader#attribute_nodes is deprecated/) do
476+
attribute_nodes += element.attribute_nodes
477+
end
476478
end
477479
end
478480

@@ -489,7 +491,9 @@ def test_namespaced_attributes
489491
eoxml
490492
attr_ns = []
491493
while reader.read
492-
if reader.node_type == Nokogiri::XML::Node::ELEMENT_NODE
494+
next unless reader.node_type == Nokogiri::XML::Node::ELEMENT_NODE
495+
496+
assert_output(nil, /Reader#attribute_nodes is deprecated/) do
493497
reader.attribute_nodes.each { |attr| attr_ns << (attr.namespace.nil? ? nil : attr.namespace.prefix) }
494498
end
495499
end
@@ -593,14 +597,17 @@ def test_read_from_memory
593597
end
594598

595599
def test_large_document_smoke_test
596-
# simply run on a large document to verify that there no GC issues
597-
xml = []
598-
xml << "<elements>"
599-
10000.times { |j| xml << "<element id=\"#{j}\"/>" }
600-
xml << "</elements>"
601-
xml = xml.join("\n")
600+
skip_unless_libxml2("valgrind tests should only run with libxml2")
602601

603-
Nokogiri::XML::Reader.from_memory(xml).each(&:attributes)
602+
refute_valgrind_errors do
603+
xml = []
604+
xml << "<elements>"
605+
10000.times { |j| xml << "<element id=\"#{j}\"/>" }
606+
xml << "</elements>"
607+
xml = xml.join("\n")
608+
609+
Nokogiri::XML::Reader.from_memory(xml).each(&:attributes)
610+
end
604611
end
605612

606613
def test_correct_outer_xml_inclusion

0 commit comments

Comments
 (0)