From 654dd74074dd2cef16b679fbb993bab6d38283eb Mon Sep 17 00:00:00 2001
From: Emanuil Rusev
Date: Sat, 18 Jan 2014 14:47:46 +0200
Subject: [PATCH] lines that start with inline html should not get parsed as
block-level markup, should resolve #54 and #57
---
Parsedown.php | 70 ++++++++++++++-----
tests/data/block-level_html.html | 10 +--
tests/data/block-level_html.md | 11 +--
tests/data/nested_block-level_html.html | 10 +++
tests/data/nested_block-level_html.md | 11 +++
tests/data/self-closing_block-level_html.html | 4 ++
tests/data/self-closing_block-level_html.md | 7 ++
tests/data/span-level_html.html | 3 +-
tests/data/span-level_html.md | 4 +-
9 files changed, 96 insertions(+), 34 deletions(-)
create mode 100644 tests/data/nested_block-level_html.html
create mode 100644 tests/data/nested_block-level_html.md
create mode 100644 tests/data/self-closing_block-level_html.html
create mode 100644 tests/data/self-closing_block-level_html.md
diff --git a/Parsedown.php b/Parsedown.php
index c9bf419..c998bc8 100755
--- a/Parsedown.php
+++ b/Parsedown.php
@@ -149,12 +149,12 @@ class Parsedown
if ( ! isset($element['closed']))
{
- if (preg_match('{<'.$element['root '].'>$}', $line)) # opening tag
+ if (strpos($line, $element['start']) !== false) # opening tag
{
$element['depth']++;
}
- if (preg_match('{'.$element['root '].'>$}', $line)) # closing tag
+ if (strpos($line, $element['end']) !== false) # closing tag
{
$element['depth'] > 0
? $element['depth']--
@@ -358,34 +358,70 @@ class Parsedown
{
case '<':
- # self-closing tag
+ $position = strpos($deindented_line, '>');
- if (preg_match('{^<.+?/>$}', $deindented_line))
+ if ($position > 1) # tag
{
- $elements []= $element;
+ $name = substr($deindented_line, 1, $position - 1);
+ $name = rtrim($name);
- $element = array(
- 'type' => 'self-closing tag',
- 'text' => $deindented_line,
+ if (substr($name, -1) === '/')
+ {
+ $self_closing = true;
+
+ $name = substr($name, 0, -1);
+ }
+
+ $position = strpos($name, ' ');
+
+ if ($position)
+ {
+ $name = substr($name, 0, $position);
+ }
+
+ if ( ! ctype_alpha($name))
+ {
+ break;
+ }
+
+ $inline_tags = array(
+ 'a', 'abbr', 'acronym', 'b', 'bdo', 'big', 'br', 'button',
+ 'cite', 'code', 'dfn', 'em', 'i', 'img', 'input', 'kbd',
+ 'label', 'map', 'object', 'q', 'samp', 'script', 'select', 'small',
+ 'span', 'strong', 'sub', 'sup', 'textarea', 'tt', 'var',
);
- continue 2;
- }
+ if (in_array($name, $inline_tags))
+ {
+ break;
+ }
- # opening tag
-
- if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $deindented_line, $matches))
- {
$elements []= $element;
+ if (isset($self_closing))
+ {
+ $element = array(
+ 'type' => 'self-closing tag',
+ 'text' => $deindented_line,
+ );
+
+ unset($self_closing);
+
+ continue 2;
+ }
+
$element = array(
'type' => 'block-level markup',
'text' => $deindented_line,
- 'root ' => strtolower($matches[1]),
+ 'start' => '<'.$name.'>',
+ 'end' => ''.$name.'>',
'depth' => 0,
);
- preg_match('{'.$matches[1].'>\s*$}', $deindented_line) and $element['closed'] = true;
+ if (strpos($deindented_line, $element['end']))
+ {
+ $element['closed'] = true;
+ }
continue 2;
}
@@ -638,7 +674,7 @@ class Parsedown
case 'block-level markup':
- $markup .= $this->parse_span_elements($element['text'])."\n";
+ $markup .= $element['text']."\n";
break;
diff --git a/tests/data/block-level_html.html b/tests/data/block-level_html.html
index b3d23c5..c4ccf54 100644
--- a/tests/data/block-level_html.html
+++ b/tests/data/block-level_html.html
@@ -1,9 +1,5 @@
-content
-
-nested elements:
+_content_
+sparse:
-parent
-
-child
-
+_content_
\ No newline at end of file
diff --git a/tests/data/block-level_html.md b/tests/data/block-level_html.md
index ee5b19d..40ba893 100644
--- a/tests/data/block-level_html.md
+++ b/tests/data/block-level_html.md
@@ -1,12 +1,7 @@
-content
+_content_
-
-
-nested elements:
+sparse:
-parent
-
-child
-
+_content_
\ No newline at end of file
diff --git a/tests/data/nested_block-level_html.html b/tests/data/nested_block-level_html.html
new file mode 100644
index 0000000..bfbef54
--- /dev/null
+++ b/tests/data/nested_block-level_html.html
@@ -0,0 +1,10 @@
+
+_parent_
+
+_child_
+
+
+_adopted child_
+
+
+outside
\ No newline at end of file
diff --git a/tests/data/nested_block-level_html.md b/tests/data/nested_block-level_html.md
new file mode 100644
index 0000000..5e01e10
--- /dev/null
+++ b/tests/data/nested_block-level_html.md
@@ -0,0 +1,11 @@
+
+_parent_
+
+_child_
+
+
+_adopted child_
+
+
+
+_outside_
\ No newline at end of file
diff --git a/tests/data/self-closing_block-level_html.html b/tests/data/self-closing_block-level_html.html
new file mode 100644
index 0000000..c3cb1f4
--- /dev/null
+++ b/tests/data/self-closing_block-level_html.html
@@ -0,0 +1,4 @@
+
+attributes:
+
+...
\ No newline at end of file
diff --git a/tests/data/self-closing_block-level_html.md b/tests/data/self-closing_block-level_html.md
new file mode 100644
index 0000000..95f00ec
--- /dev/null
+++ b/tests/data/self-closing_block-level_html.md
@@ -0,0 +1,7 @@
+
+
+attributes:
+
+
+
+...
\ No newline at end of file
diff --git a/tests/data/span-level_html.html b/tests/data/span-level_html.html
index 9ef717c..590b634 100644
--- a/tests/data/span-level_html.html
+++ b/tests/data/span-level_html.html
@@ -1,3 +1,4 @@
an important link
broken
-line
\ No newline at end of file
+line
+inline tag at the beginning
\ No newline at end of file
diff --git a/tests/data/span-level_html.md b/tests/data/span-level_html.md
index 934ee16..aadf6fc 100644
--- a/tests/data/span-level_html.md
+++ b/tests/data/span-level_html.md
@@ -1,4 +1,6 @@
an important link
broken
-line
\ No newline at end of file
+line
+
+inline tag at the beginning
\ No newline at end of file