diff --git a/Parsedown.php b/Parsedown.php index 1dde17d..186896c 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -1600,26 +1600,42 @@ class Parsedown protected function handleElementRecursive(array $Element) { - $Element = $this->handle($Element); + return $this->elementApplyRecursive(array($this, 'handle'), $Element); + } + + protected function handleElementsRecursive(array $Elements) + { + return $this->elementsApplyRecursive(array($this, 'handle'), $Elements); + } + + protected function elementApplyRecursive($closure, array $Element) + { + $Element = call_user_func($closure, $Element); if (isset($Element['elements'])) { - $Element['elements'] = $this->handleElementsRecursive($Element['elements']); + $Element['elements'] = $this->elementsApplyRecursive($closure, $Element['elements']); } elseif (isset($Element['element'])) { - $Element['element'] = $this->handleElementRecursive($Element['element']); + $Element['element'] = $this->elementApplyRecursive($closure, $Element['element']); } return $Element; } - protected function handleElementsRecursive(array $Elements) + protected function elementsApplyRecursive($closure, array $Elements) { - return array_map(array($this, 'handleElementRecursive'), $Elements); + return array_reduce( + $Elements, + function (array $Elements, array $Element) use ($closure) { + $Elements[] = $this->elementApplyRecursive($closure, $Element); + return $Elements; + }, + array() + ); } - protected function element(array $Element) { if ($this->safeMode)