From 96c093d7b5a18a000f3c1a1bfb6caf9f63878d73 Mon Sep 17 00:00:00 2001 From: summersab Date: Tue, 23 Feb 2021 10:25:32 -0600 Subject: [PATCH 1/4] Add `remove` and (print) `html` methods Added methods to remove selected elements as well as return the HTML of the object. --- selector.inc | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/selector.inc b/selector.inc index affef19..0851144 100644 --- a/selector.inc +++ b/selector.inc @@ -16,20 +16,34 @@ define('SELECTOR_VERSION', '1.1.6'); */ class SelectorDOM { + private $dom = null; + private $xpath = null; + public function __construct($data) { if ($data instanceof DOMDocument) { - $this->xpath = new DOMXpath($data); + $this->dom = $data; } else { - $dom = new DOMDocument(); - @$dom->loadHTML($data); - $this->xpath = new DOMXpath($dom); + $this->dom = new DOMDocument(); + @$this->dom->loadHTML($data); } + $this->xpath = new DOMXpath($this->dom); } public function select($selector, $as_array = true) { $elements = $this->xpath->evaluate(selector_to_xpath($selector)); return $as_array ? elements_to_array($elements) : $elements; } + + public function remove($selector, $as_array = true) { + $nodes = $this->xpath->query(selector_to_xpath($selector)); + foreach ($nodes as $node) { + $node->parentNode->removeChild($node); + } + } + + public function html() { + return $this->dom->saveHTML(); + } } /** From 2652c7d2b91666474c2d481a2f478f0cd294dded Mon Sep 17 00:00:00 2001 From: summersab Date: Tue, 23 Feb 2021 10:26:54 -0600 Subject: [PATCH 2/4] Increment version . . . because why not? --- selector.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/selector.inc b/selector.inc index 0851144..1b2ec1a 100644 --- a/selector.inc +++ b/selector.inc @@ -2,7 +2,7 @@ // --- Selector.inc - (c) Copyright TJ Holowaychuk MIT Licensed -define('SELECTOR_VERSION', '1.1.6'); +define('SELECTOR_VERSION', '1.1.7'); /** * SelectorDOM. From 9d5b783c2ba0b870e06344b818b4d1977e127ba0 Mon Sep 17 00:00:00 2001 From: summersab Date: Tue, 23 Feb 2021 10:28:25 -0600 Subject: [PATCH 3/4] Notes for v1.1.7 What I changed --- History.rdoc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/History.rdoc b/History.rdoc index c40eedb..f4210ee 100644 --- a/History.rdoc +++ b/History.rdoc @@ -1,3 +1,7 @@ +=== 1.1.7 / 2021-02-23 +* Added `remove` method to delete selector from the object +* Added `html` method to return the object as raw html + === 1.1.5 / 2014-10-20 * Added nth-child-last support * Fixed attribute selectors From 8553953c82656ea8087586de6c560ec0aeb6a243 Mon Sep 17 00:00:00 2001 From: summersab Date: Tue, 23 Feb 2021 10:30:05 -0600 Subject: [PATCH 4/4] Update selector.inc Remove unnecessary param --- selector.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/selector.inc b/selector.inc index 1b2ec1a..f313aa9 100644 --- a/selector.inc +++ b/selector.inc @@ -34,7 +34,7 @@ class SelectorDOM { return $as_array ? elements_to_array($elements) : $elements; } - public function remove($selector, $as_array = true) { + public function remove($selector) { $nodes = $this->xpath->query(selector_to_xpath($selector)); foreach ($nodes as $node) { $node->parentNode->removeChild($node);