Skip to content

Commit 8420c68

Browse files
committed
bug #145 fix: escape special chars in code text (nikophil)
This PR was merged into the main branch. Discussion ---------- fix: escape special chars in code text Hello there! here is a fix for symfony/symfony-docs#17587 I had hard time understanding the problem was only occurring for code of type "text"! cheers! Commits ------- 3407885 fix: escape special chars in code text
2 parents 1bc91f9 + 3407885 commit 8420c68

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/Renderers/CodeNodeRenderer.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public function render(): string
6262
$languages = array_unique([$language, $languageMapping]);
6363

6464
if ('text' === $language) {
65-
$highlightedCode = $code;
65+
// Highlighter escapes correctly the code, we need to manually escape only for "text" code
66+
$highlightedCode = $this->escapeForbiddenCharactersInsideCodeBlock($code);
6667
} else {
6768
$this->configureHighlighter();
6869

@@ -117,23 +118,6 @@ public static function isLanguageSupported(string $lang): bool
117118
return \in_array($lang, $supportedLanguages, true);
118119
}
119120

120-
private function getLines(string $code): array
121-
{
122-
$lines = preg_split('/\r\n|\r|\n/', $code);
123-
$reversedLines = array_reverse($lines);
124-
125-
// trim empty lines at the end of the code
126-
foreach ($reversedLines as $key => $line) {
127-
if ('' !== trim($line)) {
128-
break;
129-
}
130-
131-
unset($reversedLines[$key]);
132-
}
133-
134-
return array_reverse($reversedLines);
135-
}
136-
137121
private function configureHighlighter()
138122
{
139123
if (false === self::$isHighlighterConfigured) {
@@ -143,4 +127,15 @@ private function configureHighlighter()
143127

144128
self::$isHighlighterConfigured = true;
145129
}
130+
131+
/**
132+
* Code blocks are displayed in "<pre>" tags, which has some reserved characters:
133+
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
134+
*/
135+
private function escapeForbiddenCharactersInsideCodeBlock(string $code): string
136+
{
137+
$codeEscaped = preg_replace('/&(?!amp;|lt;|gt;|quot;)/', '&amp;', $code);
138+
139+
return strtr($codeEscaped, ['<' => '&lt;', '>' => '&gt;', '"' => '&quot;']);
140+
}
146141
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div translate="no" data-loc="1" class="notranslate codeblock codeblock-length-sm codeblock-text">
22
<div class="codeblock-scroll">
33
<pre class="codeblock-lines">1</pre>
4-
<pre class="codeblock-code"><code>some text</code></pre>
4+
<pre class="codeblock-code"><code>some text with special chars &lt; &gt; " &amp; and some text with special chars already escaped &lt; &gt; " &amp;</code></pre>
55
</div>
66
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

22
.. code-block:: text
3-
some text
3+
some text with special chars < > " & and some text with special chars already escaped &lt; &gt; &quot; &amp;

0 commit comments

Comments
 (0)