Skip to content

Add ancestors, descendants, path, and line_number methods to Node#47

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/add-node-methods
Draft

Add ancestors, descendants, path, and line_number methods to Node#47
Copilot wants to merge 6 commits intomainfrom
copilot/add-node-methods

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 26, 2025

Implements four node navigation methods referenced in documentation but previously unimplemented.

Changes

Node class (lib/moxml/node.rb)

  • ancestors - Returns NodeSet of all ancestor nodes from parent to document
  • descendants - Returns NodeSet of all descendant nodes (recursive children)
  • path - Returns XPath expression locating the node
  • line_number - Returns source line number (adapter-dependent)

Adapter implementations (lib/moxml/adapter/*.rb)

  • Nokogiri/LibXML: Delegate to native path and line/line_num methods
  • Oga/REXML/Ox/HeadedOx: Custom XPath generation with sibling position handling; line_number returns nil

Usage

doc = context.parse(xml)
title = doc.at_xpath('//book/title')

title.ancestors          #=> NodeSet[<book>, <library>, document]
title.path               #=> "/library/book/title"
title.line_number        #=> 5 (or nil if unsupported)

section = doc.at_xpath('//section')
section.descendants      #=> NodeSet[<book>, <title>, <author>, text nodes...]

Tests

Added comprehensive test coverage in spec/moxml/node_spec.rb for all four methods.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add more node methods</issue_title>
<issue_description>Several methods were mentioned in the original readme file, but weren't implemented, and it seems like nobody missed them:

  • ancestors
  • descendants
  • path
  • line_number

I'll remove them from readme for now and will work on them in a separate PR.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: ronaldtse <11865+ronaldtse@users.noreply.github.com>
Copy link
Copy Markdown

@github-advanced-security github-advanced-security AI left a comment

Choose a reason for hiding this comment

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

CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Copilot AI and others added 4 commits November 26, 2025 04:58
Co-authored-by: ronaldtse <11865+ronaldtse@users.noreply.github.com>
Co-authored-by: ronaldtse <11865+ronaldtse@users.noreply.github.com>
…ing comments

Co-authored-by: ronaldtse <11865+ronaldtse@users.noreply.github.com>
Co-authored-by: ronaldtse <11865+ronaldtse@users.noreply.github.com>
Copilot AI changed the title [WIP] Add more node methods for ancestors, descendants, path, and line number Add ancestors, descendants, path, and line_number methods to Node Nov 26, 2025
Copilot AI requested a review from ronaldtse November 26, 2025 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add more node methods

3 participants