From d6d5f53ff4dfd221596e15536ee9a5956133a29f Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sat, 1 Oct 2016 15:56:14 +0100 Subject: [PATCH 1/6] =?UTF-8?q?Fix=20Issue=20#358=20=E2=80=93=20preventing?= =?UTF-8?q?=20double=20nested=20links?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Add the ability for a parsed element to enforce that the line handler not parse any (immediate) child elements of a specified type. 2. Use 1. to allow parsed Url elements to tell the line handler not to parse any child Links or Urls where they are immediate children. --- Parsedown.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 646af86..e34efe8 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -987,7 +987,7 @@ class Parsedown # ~ # - public function line($text) + public function line($text, $non_nestables=array()) { $markup = ''; @@ -1003,6 +1003,12 @@ class Parsedown foreach ($this->InlineTypes[$marker] as $inlineType) { + + if(in_array($inlineType, $non_nestables)) + { + continue; + } + $Inline = $this->{'inline'.$inlineType}($Excerpt); if ( ! isset($Inline)) @@ -1183,6 +1189,7 @@ class Parsedown $Element = array( 'name' => 'a', 'handler' => 'line', + 'non_nestables' => array('Url', 'Link'), 'text' => null, 'attributes' => array( 'href' => null, @@ -1410,9 +1417,11 @@ class Parsedown { $markup .= '>'; + if(!isset($Element['non_nestables'])) $Element['non_nestables'] = array(); + if (isset($Element['handler'])) { - $markup .= $this->{$Element['handler']}($Element['text']); + $markup .= $this->{$Element['handler']}($Element['text'], $Element['non_nestables']); } else { From 4d3600f273cc3613cf420408c9016635216c15ea Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 2 Oct 2016 17:37:08 +0100 Subject: [PATCH 2/6] Extend disallowed assertion depth capabilities I've built on the functionality of feature 1. in the previous commit to allow non nestables to be asserted indefinitely, or to a specified depth. --- Parsedown.php | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index e34efe8..b708236 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -1003,10 +1003,23 @@ class Parsedown foreach ($this->InlineTypes[$marker] as $inlineType) { - - if(in_array($inlineType, $non_nestables)) + foreach ($non_nestables as $key => $non_nestable) { - continue; + if (is_array($non_nestable)) + { + + if ($non_nestable[0] === $inlineType) + { + continue 2; + } + } + else + { + if ($non_nestable === $inlineType) + { + continue 2; + } + } } $Inline = $this->{'inline'.$inlineType}($Excerpt); @@ -1030,6 +1043,26 @@ class Parsedown $Inline['position'] = $markerPosition; } + foreach ($non_nestables as $key => $non_nestable) + { + if (is_array($non_nestable) && isset($non_nestable[1]) && is_int($non_nestable[1])){ + if($non_nestable[1] > 1) + { + $Inline['element']['non_nestables'][] = array($non_nestable[0], $non_nestable[1] -1); + } + + } + elseif (is_array($non_nestable) && ! isset($non_nestable[1])) + { + $Inline['element']['non_nestables'][] = array($non_nestable[0]); + } + elseif ( ! is_array($non_nestable)) + { + $Inline['element']['non_nestables'][] = $non_nestable; + } + } + + # the text that comes before the inline $unmarkedText = substr($text, 0, $Inline['position']); From 50952b3243cbc4585b2e3f6f609445d34bc4d794 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 2 Oct 2016 18:26:13 +0100 Subject: [PATCH 3/6] Line handler may prevent specified element nesting This commit serves to add comments detailing parts of the new functionality, and to adjust syntax preferences to match that of the surrounding document. The commit title also now reflects the most significant change made. --- Parsedown.php | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index b708236..6b6df9c 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -1003,22 +1003,19 @@ class Parsedown foreach ($this->InlineTypes[$marker] as $inlineType) { + # check to see if the current inline type is nestable in the current context + foreach ($non_nestables as $key => $non_nestable) { - if (is_array($non_nestable)) + # case that we used array syntax + if (is_array($non_nestable) and $non_nestable[0] === $inlineType) { - - if ($non_nestable[0] === $inlineType) - { - continue 2; - } + continue 2; } - else + # case that we used plain string syntax + elseif ( ! is_array($non_nestable) and $non_nestable === $inlineType) { - if ($non_nestable === $inlineType) - { - continue 2; - } + continue 2; } } @@ -1043,25 +1040,27 @@ class Parsedown $Inline['position'] = $markerPosition; } + # cause the new element to 'inherit' our non nestables, if appropriate + foreach ($non_nestables as $key => $non_nestable) { - if (is_array($non_nestable) && isset($non_nestable[1]) && is_int($non_nestable[1])){ - if($non_nestable[1] > 1) - { - $Inline['element']['non_nestables'][] = array($non_nestable[0], $non_nestable[1] -1); - } - + # array syntax, and depth is sufficient to pass on + if (is_array($non_nestable) and isset($non_nestable[1]) and + is_int($non_nestable[1]) and $non_nestable[1] > 1) + { + $Inline['element']['non_nestables'][] = array($non_nestable[0], $non_nestable[1] -1); } - elseif (is_array($non_nestable) && ! isset($non_nestable[1])) + # array syntax, and depth is indefinite + elseif (is_array($non_nestable) and ! isset($non_nestable[1])) { $Inline['element']['non_nestables'][] = array($non_nestable[0]); } + # string syntax, so depth is indefinite elseif ( ! is_array($non_nestable)) { $Inline['element']['non_nestables'][] = $non_nestable; } } - # the text that comes before the inline $unmarkedText = substr($text, 0, $Inline['position']); @@ -1450,7 +1449,10 @@ class Parsedown { $markup .= '>'; - if(!isset($Element['non_nestables'])) $Element['non_nestables'] = array(); + if (!isset($Element['non_nestables'])) + { + $Element['non_nestables'] = array(); + } if (isset($Element['handler'])) { From a81aedeb109ed16270bbf6aaead7b41e5593caeb Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Tue, 4 Oct 2016 15:27:11 +0100 Subject: [PATCH 4/6] Line handler may prevent specified element nesting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removed granularity controls – elements are assumed to be non nestable indefinitely once declared. --- Parsedown.php | 34 +++++----------------------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 6b6df9c..f1e09d9 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -1005,18 +1005,9 @@ class Parsedown { # check to see if the current inline type is nestable in the current context - foreach ($non_nestables as $key => $non_nestable) + if (in_array($inlineType, $non_nestables)) { - # case that we used array syntax - if (is_array($non_nestable) and $non_nestable[0] === $inlineType) - { - continue 2; - } - # case that we used plain string syntax - elseif ( ! is_array($non_nestable) and $non_nestable === $inlineType) - { - continue 2; - } + continue; } $Inline = $this->{'inline'.$inlineType}($Excerpt); @@ -1040,26 +1031,11 @@ class Parsedown $Inline['position'] = $markerPosition; } - # cause the new element to 'inherit' our non nestables, if appropriate + # cause the new element to 'inherit' our non nestables - foreach ($non_nestables as $key => $non_nestable) + foreach ($non_nestables as $non_nestable) { - # array syntax, and depth is sufficient to pass on - if (is_array($non_nestable) and isset($non_nestable[1]) and - is_int($non_nestable[1]) and $non_nestable[1] > 1) - { - $Inline['element']['non_nestables'][] = array($non_nestable[0], $non_nestable[1] -1); - } - # array syntax, and depth is indefinite - elseif (is_array($non_nestable) and ! isset($non_nestable[1])) - { - $Inline['element']['non_nestables'][] = array($non_nestable[0]); - } - # string syntax, so depth is indefinite - elseif ( ! is_array($non_nestable)) - { - $Inline['element']['non_nestables'][] = $non_nestable; - } + $Inline['element']['non_nestables'][] = $non_nestable; } # the text that comes before the inline From 543a6c4175a1debd148e933fb271f5b8f088b199 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Tue, 4 Oct 2016 18:59:36 +0100 Subject: [PATCH 5/6] Line handler may prevent specified element nesting Check if array is empty to shave some performance hits in the case than no non nestables are present. --- Parsedown.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parsedown.php b/Parsedown.php index f1e09d9..fc2158f 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -1005,7 +1005,7 @@ class Parsedown { # check to see if the current inline type is nestable in the current context - if (in_array($inlineType, $non_nestables)) + if ( ! empty($non_nestables) and in_array($inlineType, $non_nestables)) { continue; } From 3aef89b3994d942d4eb85d6df0c8c43a157ac5d7 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sat, 8 Oct 2016 17:54:04 +0100 Subject: [PATCH 6/6] Line handler may prevent specified element nesting Swap `under_scores` for `camelCasing` --- Parsedown.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index fc2158f..e2b9d60 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -987,7 +987,7 @@ class Parsedown # ~ # - public function line($text, $non_nestables=array()) + public function line($text, $nonNestables=array()) { $markup = ''; @@ -1005,7 +1005,7 @@ class Parsedown { # check to see if the current inline type is nestable in the current context - if ( ! empty($non_nestables) and in_array($inlineType, $non_nestables)) + if ( ! empty($nonNestables) and in_array($inlineType, $nonNestables)) { continue; } @@ -1033,9 +1033,9 @@ class Parsedown # cause the new element to 'inherit' our non nestables - foreach ($non_nestables as $non_nestable) + foreach ($nonNestables as $non_nestable) { - $Inline['element']['non_nestables'][] = $non_nestable; + $Inline['element']['nonNestables'][] = $non_nestable; } # the text that comes before the inline @@ -1197,7 +1197,7 @@ class Parsedown $Element = array( 'name' => 'a', 'handler' => 'line', - 'non_nestables' => array('Url', 'Link'), + 'nonNestables' => array('Url', 'Link'), 'text' => null, 'attributes' => array( 'href' => null, @@ -1425,14 +1425,14 @@ class Parsedown { $markup .= '>'; - if (!isset($Element['non_nestables'])) + if (!isset($Element['nonNestables'])) { - $Element['non_nestables'] = array(); + $Element['nonNestables'] = array(); } if (isset($Element['handler'])) { - $markup .= $this->{$Element['handler']}($Element['text'], $Element['non_nestables']); + $markup .= $this->{$Element['handler']}($Element['text'], $Element['nonNestables']); } else {