1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Merge pull request #224 from hkdobrev/no-markup-option

Add `noMarkup` option to escape user HTML
This commit is contained in:
Emanuil Rusev 2014-09-21 23:19:23 +03:00
commit e0965ce09b
2 changed files with 70 additions and 0 deletions

View File

@ -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(

View File

@ -62,4 +62,46 @@ class Test extends PHPUnit_Framework_TestCase
return $data;
}
public function test_no_markup()
{
$markdownWithHtml = <<<MARKDOWN_WITH_MARKUP
<div>_content_</div>
sparse:
<div>
<div class="inner">
_content_
</div>
</div>
paragraph
<style type="text/css">
p {
color: red;
}
</style>
MARKDOWN_WITH_MARKUP;
$expectedHtml = <<<EXPECTED_HTML
<p>&lt;div><em>content</em>&lt;/div></p>
<p>sparse:</p>
<p>&lt;div>
&lt;div class="inner">
<em>content</em>
&lt;/div>
&lt;/div></p>
<p>paragraph</p>
<p>&lt;style type="text/css"></p>
<pre><code>p {
color: red;
}</code></pre>
<p>&lt;/style></p>
EXPECTED_HTML;
$parsedownWithNoMarkup = new Parsedown();
$parsedownWithNoMarkup->setNoMarkup(true);
$this->assertEquals($expectedHtml, $parsedownWithNoMarkup->text($markdownWithHtml));
}
}