From 228a11cb4452e6087b8e968342ba8f5f9fe42e23 Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Thu, 6 Jan 2022 16:14:54 -0600 Subject: [PATCH] Added Node::getNodePath --- README.md | 7 +++++++ lib/Node.php | 4 ++++ tests/cases/TestNode.php | 7 +++++++ 3 files changed, 18 insertions(+) diff --git a/README.md b/README.md index ab8591e..d010eff 100644 --- a/README.md +++ b/README.md @@ -229,9 +229,16 @@ partial abstract class Node implements \Stringable { public const WALK_REJECT = 0x02; public const WALK_SKIP_CHILDREN = 0x04; public const WALK_STOP = 0x08; + + + public function getNodePath(): ?string; } ``` +#### MensBeam\HTML\DOM\Node::getNodePath #### + +Carryover from PHP's DOM. It's a useful method that returns an XPath location path for the node. Returns a string if successful or null on failure. + ### MensBeam\HTML\DOM\ParentNode ### ```php diff --git a/lib/Node.php b/lib/Node.php index cb7e112..a1fc82f 100644 --- a/lib/Node.php +++ b/lib/Node.php @@ -436,6 +436,10 @@ abstract class Node implements \Stringable { return $this->containsInner($this->innerNode, $this->getInnerNode($other)); } + public function getNodePath(): ?string { + return $this->innerNode->getNodePath(); + } + public function getRootNode(): ?Node { # The getRootNode(options) method steps are to return this’s shadow-including # root if options["composed"] is true; otherwise this’s root. diff --git a/tests/cases/TestNode.php b/tests/cases/TestNode.php index 52eaac5..e835519 100644 --- a/tests/cases/TestNode.php +++ b/tests/cases/TestNode.php @@ -265,6 +265,13 @@ class TestNode extends \PHPUnit\Framework\TestCase { } + public function testMethod_getNodePath(): void { + $d = new Document('

ook

'); + $ook = $d->getElementById('ook'); + $this->assertSame('/html/body/div/span/div[2]/p[2]', $ook->getNodePath()); + } + + /** * @covers \MensBeam\HTML\DOM\Node::insertBefore *