From af2722468e3e4fb13bd3ade01e7e87762e695740 Mon Sep 17 00:00:00 2001 From: Joshua Graham Date: Thu, 24 Feb 2022 23:50:32 +1100 Subject: [PATCH] Add function detect if any current context node, so that xpath() works, else a find() must be done first. --- src/Rct567/DomQuery/DomQueryNodes.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Rct567/DomQuery/DomQueryNodes.php b/src/Rct567/DomQuery/DomQueryNodes.php index 0189221..6079926 100644 --- a/src/Rct567/DomQuery/DomQueryNodes.php +++ b/src/Rct567/DomQuery/DomQueryNodes.php @@ -665,6 +665,24 @@ public function xpathQuery(string $expression, \DOMNode $context_node=null) return false; } + /** + * Shows if the context node can be used in xpath queries. + * + * If it's not set, it can be set then used in the following: + * @code + * if (!$dom->isContextNodeSet()) { + * $dom = $dom->find(':root'); + * } + * $dom->xpath("/child::div[contains(concat(' ', normalize-space(@class), ' '), ' myclass ')]"); + * + * @endcode + * + * @return bool + */ + public function isContextNodeSet() { + return (isset($this->root_instance) || isset($this->xpath_query)); + } + /** * Create dom xpath instance *