diff --git a/packages/guides-code/src/Code/Twig/CodeExtension.php b/packages/guides-code/src/Code/Twig/CodeExtension.php index 45ae0f461..84ef4dcba 100644 --- a/packages/guides-code/src/Code/Twig/CodeExtension.php +++ b/packages/guides-code/src/Code/Twig/CodeExtension.php @@ -36,14 +36,19 @@ public function getFilters(): array ]; } - /** @param array $context */ - public function highlight(array $context, string $code, string $language = 'text'): string + /** + * `CodeNode::getLanguage()` is nullable, and templates pass it straight into this filter, so null has to be + * accepted here and mean the same as an omitted language. + * + * @param array $context + */ + public function highlight(array $context, string $code, string|null $language = null): string { $debugInformation = $context['debugInformation'] ?? []; if (!is_array($debugInformation)) { $debugInformation = []; } - return ($this->highlighter)($language, $code, $debugInformation)->code; + return ($this->highlighter)($language ?? 'text', $code, $debugInformation)->code; } } diff --git a/packages/guides-code/tests/unit/Twig/CodeExtensionTest.php b/packages/guides-code/tests/unit/Twig/CodeExtensionTest.php new file mode 100644 index 000000000..e7584b6c4 --- /dev/null +++ b/packages/guides-code/tests/unit/Twig/CodeExtensionTest.php @@ -0,0 +1,57 @@ +extension = new CodeExtension(new HighlightPhpHighlighter(new HighlightPHP(), new NullLogger())); + } + + public function testItHighlightsCodeWithoutALanguageAsPlainText(): void + { + // A highlighter treats this differently per language: as "text" the quoted part stays plain, in a + // programming language it becomes a string token. Plain input would be returned unchanged by every + // language and could therefore not tell the fallback apart from any other one. + $code = ' & "b"'; + + self::assertSame( + $this->extension->highlight([], $code, 'text'), + $this->extension->highlight([], $code, null), + 'A CodeNode without a language must be rendered like an explicit "text" language', + ); + + self::assertNotSame( + $this->extension->highlight([], $code, 'php'), + $this->extension->highlight([], $code, null), + 'The fixture must be able to tell the "text" fallback apart from another language', + ); + } + + public function testItHighlightsCodeWithALanguage(): void + { + self::assertStringContainsString( + 'hljs-keyword', + $this->extension->highlight([], '{{ renderNode(node.caption) }} {%- endif -%} - {%- include "body/code/highlighted-code.html.twig" -%} diff --git a/tests/Integration/tests/code/literalinclude-without-language/expected/index.html b/tests/Integration/tests/code/literalinclude-without-language/expected/index.html new file mode 100644 index 000000000..e3207a7c5 --- /dev/null +++ b/tests/Integration/tests/code/literalinclude-without-language/expected/index.html @@ -0,0 +1,18 @@ + +
+

index

+
<?php
+
+declare(strict_types=1);
+
+class Example
+{
+    public function test(): string
+    {
+        return 'this is a test';
+    }
+}
+
+ +
+ diff --git a/tests/Integration/tests/code/literalinclude-without-language/input/_code/_Example.php b/tests/Integration/tests/code/literalinclude-without-language/input/_code/_Example.php new file mode 100644 index 000000000..ee23d326b --- /dev/null +++ b/tests/Integration/tests/code/literalinclude-without-language/input/_code/_Example.php @@ -0,0 +1,11 @@ + + + + + diff --git a/tests/Integration/tests/code/literalinclude-without-language/input/index.rst b/tests/Integration/tests/code/literalinclude-without-language/input/index.rst new file mode 100644 index 000000000..f881c44d9 --- /dev/null +++ b/tests/Integration/tests/code/literalinclude-without-language/input/index.rst @@ -0,0 +1,4 @@ +index +===== + +.. literalinclude:: _code/_Example.php diff --git a/tests/Integration/tests/markdown/code-md/expected/index.html b/tests/Integration/tests/markdown/code-md/expected/index.html index 14e6691d1..93182a726 100644 --- a/tests/Integration/tests/markdown/code-md/expected/index.html +++ b/tests/Integration/tests/markdown/code-md/expected/index.html @@ -2,7 +2,7 @@

Markdown with Code Blocks

-
<html>
+            
<html>
   <head>
   </head>
 </html>
@@ -10,7 +10,7 @@ 

Markdown with Code Blocks

Fenced Code Blocks

-
{
+            
{
   "firstName": "John",
   "lastName": "Smith",
   "age": 25