From cdaf86b0392c88af87c9d946b732498529e07b3b Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 8 Apr 2018 17:39:24 +0100 Subject: [PATCH 1/2] Add seperate depth-first function instead of replacing recursive method --- Parsedown.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Parsedown.php b/Parsedown.php index 0ac3e3d..eebd2f9 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -1631,6 +1631,22 @@ class Parsedown } protected function elementApplyRecursive($closure, array $Element) + { + $Element = call_user_func($closure, $Element); + + if (isset($Element['elements'])) + { + $Element['elements'] = $this->elementsApplyRecursive($closure, $Element['elements']); + } + elseif (isset($Element['element'])) + { + $Element['element'] = $this->elementApplyRecursive($closure, $Element['element']); + } + + return $Element; + } + + protected function elementApplyRecursiveDepthFirst($closure, array $Element) { if (isset($Element['elements'])) { @@ -1658,6 +1674,18 @@ class Parsedown return $newElements; } + protected function elementsApplyRecursiveDepthFirst($closure, array $Elements) + { + $newElements = array(); + + foreach ($Elements as $Element) + { + $newElements[] = $this->elementApplyRecursiveDepthFirst($closure, $Element); + } + + return $newElements; + } + protected function element(array $Element) { if ($this->safeMode) From 86940be22421f709107d7be8a890d67ea65c25e5 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 8 Apr 2018 17:49:36 +0100 Subject: [PATCH 2/2] Use mutating loop instead of creating new array --- Parsedown.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index eebd2f9..9b7a3e9 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -1664,26 +1664,22 @@ class Parsedown protected function elementsApplyRecursive($closure, array $Elements) { - $newElements = array(); - - foreach ($Elements as $Element) + foreach ($Elements as &$Element) { - $newElements[] = $this->elementApplyRecursive($closure, $Element); + $Element = $this->elementApplyRecursive($closure, $Element); } - return $newElements; + return $Elements; } protected function elementsApplyRecursiveDepthFirst($closure, array $Elements) { - $newElements = array(); - - foreach ($Elements as $Element) + foreach ($Elements as &$Element) { - $newElements[] = $this->elementApplyRecursiveDepthFirst($closure, $Element); + $Element = $this->elementApplyRecursiveDepthFirst($closure, $Element); } - return $newElements; + return $Elements; } protected function element(array $Element)