diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b6e7e95c5..5e1f31959f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA ### Changed * The constant `Struct::HTMLElementDescription` is no longer defined. (#3432, #3433) @viralpraxis +* `Document` objects now respond to `#parent`, which will always return `nil`. Previously, the method was `undef`ed from its parent class `Node`. #3466 ### Fixed diff --git a/lib/nokogiri/xml/document.rb b/lib/nokogiri/xml/document.rb index 445b815672..fa54eef08d 100644 --- a/lib/nokogiri/xml/document.rb +++ b/lib/nokogiri/xml/document.rb @@ -322,6 +322,13 @@ def document self end + # call-seq: parent => nil + # + # For a Document, parent always returns +nil+ + def parent + nil + end + # :call-seq: # collect_namespaces() → Hash # @@ -430,7 +437,7 @@ def fragment(tags = nil) DocumentFragment.new(self, tags, root) end - undef_method :swap, :parent, :namespace, :default_namespace= + undef_method :swap, :namespace, :default_namespace= undef_method :add_namespace_definition, :attributes undef_method :namespace_definitions, :line, :add_namespace diff --git a/test/xml/test_document.rb b/test/xml/test_document.rb index f56f344390..7b7403d656 100644 --- a/test/xml/test_document.rb +++ b/test/xml/test_document.rb @@ -711,9 +711,8 @@ def test_url_kwarg def test_document_parent xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE) - assert_raises(NoMethodError) do - xml.parent - end + + assert_nil(xml.parent) end def test_document_name