Skip to content

[feature] document.parent should exist #3466

Open
@doriantaylor

Description

@doriantaylor

IMO this should work for parity when the node type is unknown: document.parent == document.

Consider the following:

$ pry
[1] pry(main)> require 'nokogiri'
=> true                          
[2] pry(main)> document = Nokogiri.XML '<foo/>' 
=> #(Document:0x140 {                   
  name = "document",
  children = [ #(Element:0x154 { name = "foo" })]
  })
[3] pry(main)> document.parent
NoMethodError: undefined method `parent' for #<Nokogiri::XML::Document:0x140 name="document" children=[#<Nokogiri::XML::Element:0x154 name="foo">]>
from (pry):4:in `__pry__'

Expected behavior

It shouldn't crash (indeed, the documentation says the method is there). document.parent should just be a no-op that returns self.

Environment

# Nokogiri (1.18.3)
    ---
    warnings: []
    nokogiri:
      version: 1.18.3
      cppflags:
      - "-I/var/lib/gems/3.2.0/gems/nokogiri-1.18.3-x86_64-linux-gnu/ext/nokogiri"
      - "-I/var/lib/gems/3.2.0/gems/nokogiri-1.18.3-x86_64-linux-gnu/ext/nokogiri/include"
      - "-I/var/lib/gems/3.2.0/gems/nokogiri-1.18.3-x86_64-linux-gnu/ext/nokogiri/include/libxml2"
      ldflags: []
    ruby:
      version: 3.2.3
      platform: x86_64-linux-gnu
      gem_platform: x86_64-linux-gnu
      description: ruby 3.2.3 (2024-01-18 revision 52bb2ac0a6) [x86_64-linux-gnu]
      engine: ruby
    libxml:
      source: packaged
      precompiled: true
      patches:
      - 0001-Remove-script-macro-support.patch
      - 0002-Update-entities-to-remove-handling-of-ssi.patch
      - '0009-allow-wildcard-namespaces.patch'
      - 0010-update-config.guess-and-config.sub-for-libxml2.patch
      - 0011-rip-out-libxml2-s-libc_single_threaded-support.patch
      - '0019-xpath-Use-separate-static-hash-table-for-standard-fu.patch'
      memory_management: ruby
      iconv_enabled: true
      compiled: 2.13.6
      loaded: 2.13.6
    libxslt:
      source: packaged
      precompiled: true
      patches:
      - 0001-update-config.guess-and-config.sub-for-libxslt.patch
      datetime_enabled: true
      compiled: 1.1.42
      loaded: 1.1.42
    other_libraries:
      zlib: 1.3.1
      libgumbo: 1.0.0-nokogiri

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions