Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Rules/HtmlRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

final class HtmlRule implements Rule, ProvidesFirstChar
{
/**
* Elements whose content is raw text rather than Markdown.
*
* @see https://spec.commonmark.org/0.31.2/#html-blocks
*/
private const array RAW_TEXT_TAGS = ['pre', 'script', 'style', 'textarea'];

public string $firstChar = '<';

public function shouldParse(Parser $parser): bool
Expand Down Expand Up @@ -84,6 +91,13 @@ public function parse(Parser $parser): Token

$content .= $parser->consumeWhile(Parser::NEW_LINE);

return new HtmlToken($content);
return new HtmlToken(
$content,
raw: in_array(
strtolower($tagName),
self::RAW_TEXT_TAGS,
strict: true,
),
);
}
}
7 changes: 7 additions & 0 deletions src/Tokens/HtmlToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ final class HtmlToken implements Token
{
public function __construct(
public string $html,

/** Whether the content is raw text that must never be parsed as Markdown. */
public bool $raw = false,
) {}

public function parse(Parser $parser): string
{
if ($this->raw) {
return $this->html;
}

return $parser
->forToken($this, [
new BoldAndItalicRule(),
Expand Down
60 changes: 60 additions & 0 deletions tests/Rules/HtmlRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,64 @@ public function test_void_tags_are_case_insensitive(): void

$this->assertSame("<BR>\n<p>Hello</p>", $html);
}

#[Test]
public function lex_keeps_script_content_raw(): void
{
$html =
(string) new Parser(highlighter: null, rules: [new HtmlRule()])->parse(
'<script>const x = "**a**";</script>',
);

$this->assertSame('<script>const x = "**a**";</script>', $html);
}

#[Test]
public function lex_keeps_style_content_raw(): void
{
$html =
(string) new Parser(highlighter: null, rules: [new HtmlRule()])->parse(
'<style>a::after{content:"*x*"}</style>',
);

$this->assertSame('<style>a::after{content:"*x*"}</style>', $html);
}

#[Test]
public function lex_keeps_pre_and_textarea_content_raw(): void
{
$parser = new Parser(highlighter: null, rules: [new HtmlRule()]);

$this->assertSame(
'<pre>**a** _b_</pre>',
(string) $parser->parse('<pre>**a** _b_</pre>'),
);

$this->assertSame(
'<textarea>**a**</textarea>',
(string) $parser->parse('<textarea>**a**</textarea>'),
);
}

#[Test]
public function lex_detects_raw_text_tags_case_insensitively(): void
{
$html =
(string) new Parser(highlighter: null, rules: [new HtmlRule()])->parse(
'<SCRIPT>**a**</SCRIPT>',
);

$this->assertSame('<SCRIPT>**a**</SCRIPT>', $html);
}

#[Test]
public function lex_still_parses_markdown_in_other_elements(): void
{
$html =
(string) new Parser(highlighter: null, rules: [new HtmlRule()])->parse(
'<div>**a**</div>',
);

$this->assertSame('<div><strong>a</strong></div>', $html);
}
}
Loading