From 0a3fde3774ee7d53c5a2476688cef49f0faccff3 Mon Sep 17 00:00:00 2001
From: Haralan Dobrev
Date: Sat, 20 Sep 2014 14:52:05 +0300
Subject: [PATCH] Add `noMarkup` option to escape user HTML
Resolves #106.
This change introduces a new option - `noMarkup`. You could set it the
`setNoMarkup()` method similar to the `setBreaksEnabled()` one.
Example usage:
``` php
setNoMarkup(true);
$parsedown->text('*Some text*
');
// Outputs:
// <div><strong>Some text<s;/strong></div>
```
---
Parsedown.php | 28 ++++++++++++++++++++++++++++
test/Test.php | 42 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+)
diff --git a/Parsedown.php b/Parsedown.php
index e9a8cbd..7be569c 100755
--- a/Parsedown.php
+++ b/Parsedown.php
@@ -59,6 +59,11 @@ class Parsedown
private $breaksEnabled;
+ /**
+ * @var boolean If true HTML is escaped
+ */
+ private $noMarkup = false;
+
function setBreaksEnabled($breaksEnabled)
{
$this->breaksEnabled = $breaksEnabled;
@@ -66,6 +71,19 @@ class Parsedown
return $this;
}
+ /**
+ * Set the `noMarkup` option
+ *
+ * @param boolean $noMarkup If true HTML is escaped
+ * @return $this
+ */
+ function setNoMarkup($noMarkup)
+ {
+ $this->noMarkup = (bool) $noMarkup;
+
+ return $this;
+ }
+
#
# Lines
#
@@ -619,6 +637,11 @@ class Parsedown
protected function identifyMarkup($Line)
{
+ if ($this->noMarkup)
+ {
+ return null;
+ }
+
if (preg_match('/^<(\w[\w\d]*)(?:[ ][^>]*)?(\/?)[ ]*>/', $Line['text'], $matches))
{
if (in_array($matches[1], $this->textLevelElements))
@@ -1144,6 +1167,11 @@ class Parsedown
protected function identifyTag($Excerpt)
{
+ if ($this->noMarkup)
+ {
+ return null;
+ }
+
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<\/?\w.*?>/', $Excerpt['text'], $matches))
{
return array(
diff --git a/test/Test.php b/test/Test.php
index 5171d84..7b60def 100644
--- a/test/Test.php
+++ b/test/Test.php
@@ -62,4 +62,46 @@ class Test extends PHPUnit_Framework_TestCase
return $data;
}
+
+ public function test_no_markup()
+ {
+ $markdownWithHtml = <<_content_
+
+sparse:
+
+
+
+paragraph
+
+
+MARKDOWN_WITH_MARKUP;
+
+ $expectedHtml = <<<div>content</div>
+sparse:
+<div>
+<div class="inner">
+content
+</div>
+</div>
+paragraph
+<style type="text/css">
+p {
+ color: red;
+}
+</style>
+EXPECTED_HTML;
+ $parsedownWithNoMarkup = new Parsedown();
+ $parsedownWithNoMarkup->setNoMarkup(true);
+ $this->assertEquals($expectedHtml, $parsedownWithNoMarkup->text($markdownWithHtml));
+ }
}