mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
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 <?php $parsedown = new Parsedown(); $parsedown->setNoMarkup(true); $parsedown->text('<div><strong>*Some text*</strong></div>'); // Outputs: // <p><div><strong><em>Some text</em><s;/strong></div></p> ```
This commit is contained in:
parent
93f7b26427
commit
0a3fde3774
@ -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(
|
||||
|
@ -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><div><em>content</em></div></p>
|
||||
<p>sparse:</p>
|
||||
<p><div>
|
||||
<div class="inner">
|
||||
<em>content</em>
|
||||
</div>
|
||||
</div></p>
|
||||
<p>paragraph</p>
|
||||
<p><style type="text/css"></p>
|
||||
<pre><code>p {
|
||||
color: red;
|
||||
}</code></pre>
|
||||
<p></style></p>
|
||||
EXPECTED_HTML;
|
||||
$parsedownWithNoMarkup = new Parsedown();
|
||||
$parsedownWithNoMarkup->setNoMarkup(true);
|
||||
$this->assertEquals($expectedHtml, $parsedownWithNoMarkup->text($markdownWithHtml));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user