From 04398734c3e2f164e73ea9988028a906bb2d274a Mon Sep 17 00:00:00 2001 From: gtbu Date: Mon, 17 Feb 2025 19:20:53 +0100 Subject: [PATCH] dom wrapper update 3.01 --- include/common.php | 2 +- include/thirdparty/dom/NodeList.php | 2 +- include/thirdparty/dom/README.md | 42 ++++++++++++++++++- .../dom/Traits/ManipulationTrait.php | 4 +- .../thirdparty/dom/Traits/TraversalTrait.php | 2 +- 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/include/common.php b/include/common.php index bb5e28e..f4dfe20 100644 --- a/include/common.php +++ b/include/common.php @@ -45,7 +45,7 @@ gp_defined('notify_deprecated', true); // Since 5.2 //gp_defined('CMS_DOMAIN', 'http://gpeasy.loc'); gp_defined('CMS_DOMAIN', 'https://github.com/gtbu'); gp_defined('CMS_READABLE_DOMAIN', 'github.com/gtbu'); -gp_defined('CMS_NAME', 'Typesetter'); +gp_defined('CMS_NAME', 'Typesetter5'); gp_defined('CMS_NAME_FULL', 'Typesetter5 CMS'); gp_defined('addon_browse_path', CMS_DOMAIN . '/index.php'); gp_defined('debug_path', CMS_DOMAIN . '/index.php/Debug'); diff --git a/include/thirdparty/dom/NodeList.php b/include/thirdparty/dom/NodeList.php index 4dc5b70..bdaa946 100644 --- a/include/thirdparty/dom/NodeList.php +++ b/include/thirdparty/dom/NodeList.php @@ -30,7 +30,7 @@ class NodeList extends NodeCollection * @param Document $document * @param iterable $nodes */ - public function __construct(Document $document = null, ?iterable $nodes = null) { + public function __construct(?Document $document = null, ?iterable $nodes = null) { parent::__construct($nodes); $this->document = $document; diff --git a/include/thirdparty/dom/README.md b/include/thirdparty/dom/README.md index ffda30f..a11f118 100644 --- a/include/thirdparty/dom/README.md +++ b/include/thirdparty/dom/README.md @@ -500,9 +500,21 @@ $doc->find('div').removeClass('first'); self substituteWith(string|NodeList|\DOMNode|callable $input) ``` +Replace each node in the current set with the contents provided. + ##### Example ``` php +$doc = (new Document())->html('

Hello World!

'); +$doc->find('b')->substituteWith(function($node) { + return '' . $node->text() . ''; +}); +echo $doc->html(); +``` + +*Result:* +``` html +

Hello World!

``` --- @@ -513,9 +525,33 @@ self substituteWith(string|NodeList|\DOMNode|callable $input) string|self text([string|NodeList|\DOMNode|callable $input = null]) ``` -##### Example +Get the text contents of the current set. + +##### Example (get) ``` php +$doc = (new Document())->html('
Hello World!
'); +echo $doc->find('.text')->text(); +``` + +*Result:* +``` html +Hello World! +``` + +Set the text contents for current set. + +##### Example (set) + +``` php +$doc = (new Document())->html('
The quick brown fox jumps over the lazy dog
'); +$doc->find('.text')->text('Hello World!'); +echo $doc->html(); +``` + +*Result:* +``` html +
Hello World!
``` --- @@ -1092,6 +1128,8 @@ $slicedNodes = $nodes->slice(1, 3); int count() ``` +Return number of nodes in the current set + ##### Example ``` php @@ -1108,6 +1146,8 @@ echo $nodes->count(); self each(callable $function) ``` +Iterate through for each item in the existing set via callback + ##### Example ``` php diff --git a/include/thirdparty/dom/Traits/ManipulationTrait.php b/include/thirdparty/dom/Traits/ManipulationTrait.php index 183203b..c749ccf 100644 --- a/include/thirdparty/dom/Traits/ManipulationTrait.php +++ b/include/thirdparty/dom/Traits/ManipulationTrait.php @@ -184,7 +184,9 @@ trait ManipulationTrait public function substituteWith(string|NodeList|\DOMNode|callable $input): self { $this->manipulateNodesWithInput($input, function($node, $newNodes) { foreach ($newNodes as $newNode) { - $node->parent()->replaceChild($newNode, $node); + if ($node->parent()) { + $node->parent()->replaceChild($newNode, $node); + } } }); diff --git a/include/thirdparty/dom/Traits/TraversalTrait.php b/include/thirdparty/dom/Traits/TraversalTrait.php index 8b05f4a..c0abe9b 100644 --- a/include/thirdparty/dom/Traits/TraversalTrait.php +++ b/include/thirdparty/dom/Traits/TraversalTrait.php @@ -297,7 +297,7 @@ trait TraversalTrait * * @return NodeList */ - public function parents(string $selector = null): NodeList { + public function parents(?string $selector = null): NodeList { return $this->parentsUntil(null, $selector); }