Skip to content

Commit

Permalink
Add a new "stylesheets" target
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Apr 24, 2024
1 parent 4b352cf commit 181cffc
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 21 deletions.
6 changes: 3 additions & 3 deletions core-bundle/contao/library/Contao/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,10 +851,10 @@ public static function replaceDynamicScriptTags($strBuffer)
}
}

// Add the Twig style sheets
if (!empty($GLOBALS['TL_TWIG_CSS']) && \is_array($GLOBALS['TL_TWIG_CSS']))
// Add the component style sheets
if (!empty($GLOBALS['TL_STYLE_SHEETS']) && \is_array($GLOBALS['TL_STYLE_SHEETS']))
{
foreach (array_unique($GLOBALS['TL_TWIG_CSS']) as $head)
foreach (array_unique($GLOBALS['TL_STYLE_SHEETS']) as $head)
{
$strScripts .= $head;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
{% if sorting %}
{% set tablesort_css_file = asset('css/tablesort.min.css', 'contao-components/tristen-tablesort') %}

{% add "tablesort_css" to head %}
{% add "tablesort_css" to stylesheets %}
{% with {file: tablesort_css_file} %}{{ block('stylesheet_component') }}{% endwith %}
{% endadd %}
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
{% block style %}
{% set handorgel_css_file = asset('css/handorgel.min.css', 'contao-components/handorgel') %}

{% add "handorgel_css" to head %}
{% add "handorgel_css" to stylesheets %}
{% with {file: handorgel_css_file} %}{{ block('stylesheet_component') }}{% endwith %}
{% endadd %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{% endblock %}

{% block style %}
{% add "highlighter_css" to head %}
{% add "highlighter_css" to stylesheets %}
{% with {file: highlighter_css_file} %}{{ block('stylesheet_component') }}{% endwith %}
{% endadd %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
{% block style %}
{% set swiper_css_file = asset('css/swiper-bundle.min.css', 'contao-components/swiper') %}

{% add "swiper_css" to head %}
{% add "swiper_css" to stylesheets %}
{% with {file: swiper_css_file} %}{{ block('stylesheet_component') }}{% endwith %}
{% endadd %}
{% endblock %}
10 changes: 8 additions & 2 deletions core-bundle/src/Twig/Extension/ContaoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,16 +394,22 @@ public function addDocumentContent(string|null $identifier, string $content, Doc
// TODO: This should make use of the response context in the future.
if (DocumentLocation::head === $location) {
if (null !== $identifier) {
$GLOBALS['TL_TWIG_CSS'][$identifier] = $content;
$GLOBALS['TL_HEAD'][$identifier] = $content;
} else {
$GLOBALS['TL_TWIG_CSS'][] = $content;
$GLOBALS['TL_HEAD'][] = $content;
}
} elseif (DocumentLocation::endOfBody === $location) {
if (null !== $identifier) {
$GLOBALS['TL_BODY'][$identifier] = $content;
} else {
$GLOBALS['TL_BODY'][] = $content;
}
} elseif (DocumentLocation::stylesheets === $location) {
if (null !== $identifier) {
$GLOBALS['TL_STYLE_SHEETS'][$identifier] = $content;
} else {
$GLOBALS['TL_STYLE_SHEETS'][] = $content;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions core-bundle/src/Twig/ResponseContext/DocumentLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
enum DocumentLocation: string
{
case head = 'head';
case stylesheets = 'stylesheets';
case endOfBody = 'body';
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testOutputsAccordion(): void
HTML;

$this->assertSameHtml($expectedOutput, $response->getContent());
$this->assertArrayHasKey('handorgel_css', $responseContextData['head']);
$this->assertArrayHasKey('handorgel_css', $responseContextData['stylesheets']);
$this->assertArrayHasKey('handorgel_js', $responseContextData['body']);
}

Expand Down Expand Up @@ -122,7 +122,7 @@ public function testDoesNotAddTheDataOpenAttribute(): void
HTML;

$this->assertSameHtml($expectedOutput, $response->getContent());
$this->assertArrayHasKey('handorgel_css', $responseContextData['head']);
$this->assertArrayHasKey('handorgel_css', $responseContextData['stylesheets']);
$this->assertArrayHasKey('handorgel_js', $responseContextData['body']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testOutputsHighlightedCode(): void
<link rel="stylesheet" href="/foundation.css">
HTML;

$additionalHeadCode = $responseContextData[DocumentLocation::head->value];
$additionalHeadCode = $responseContextData[DocumentLocation::stylesheets->value];

$this->assertCount(1, $additionalHeadCode);
$this->assertSameHtml($expectedHeadCode, $additionalHeadCode['highlighter_css']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ static function () use ($modelData): Metadata|null {
// Record response context data
$responseContextData = array_filter([
DocumentLocation::head->value => $GLOBALS['TL_HEAD'] ?? [],
DocumentLocation::stylesheets->value => $GLOBALS['TL_STYLE_SHEETS'] ?? [],
DocumentLocation::endOfBody->value => $GLOBALS['TL_BODY'] ?? [],
]);

// Reset state
unset($GLOBALS['TL_HEAD'], $GLOBALS['TL_BODY']);
unset($GLOBALS['TL_HEAD'], $GLOBALS['TL_STYLE_SHEETS'], $GLOBALS['TL_BODY']);

$this->resetStaticProperties([Highlighter::class]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testOutputsMarkup(): void
HTML;

$this->assertSameHtml($expectedOutput, $response->getContent());
$this->assertArrayHasKey('swiper_css', $responseContextData['head']);
$this->assertArrayHasKey('swiper_css', $responseContextData['stylesheets']);
$this->assertArrayHasKey('swiper_js', $responseContextData['body']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ public function testOutputsTableWithHeaderAndFooter(): void

$this->assertSameHtml($expectedOutput, $response->getContent());

$additionalHeadCode = $responseContextData[DocumentLocation::head->value];
$additionalBodyCode = $responseContextData[DocumentLocation::endOfBody->value];

$this->assertCount(2, $additionalHeadCode);
$this->assertArrayHasKey('tablesort_css', $responseContextData['head']);
$this->assertCount(1, $additionalBodyCode);
$this->assertArrayHasKey('tablesort_script', $additionalBodyCode);

$this->assertMatchesRegularExpression(
'/<script>[^<]+tablesort.min.js[^<]+<\/script>/',
$additionalHeadCode['tablesort_script'],
$additionalBodyCode['tablesort_script'],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function testOutputsToplinkAndScript(string $linkText, string $expectedLi
$additionalBodyCode = $responseContextData[DocumentLocation::endOfBody->value];

$this->assertCount(1, $additionalBodyCode);
$this->assertArrayHasKey('toplink_script', $additionalBodyCode);

$this->assertMatchesRegularExpression(
'/<script>[^<]+link\.href = location\.href[^<]+<\/script>/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public function testOutputsVimeoVideoWithSplashScreen(): void
$additionalBodyCode = $responseContextData[DocumentLocation::endOfBody->value];

$this->assertCount(1, $additionalBodyCode);
$this->assertArrayHasKey('splash_screen_script', $additionalBodyCode);

$this->assertMatchesRegularExpression(
'/<script>[^<]+button\.insertAdjacentHTML[^<]+<\/script>/',
$additionalBodyCode['splash_screen_script'],
Expand Down
33 changes: 30 additions & 3 deletions core-bundle/tests/Twig/ResponseContext/AddTokenParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testGetTag(): void
* @param list<string>|array<string, string> $expectedHeadContent
* @param list<string>|array<string, string> $expectedBodyContent
*/
public function testAddsContent(string $code, array $expectedHeadContent, array $expectedBodyContent): void
public function testAddsContent(string $code, array $expectedHeadContent, array $expectedStyleSheetContent, array $expectedBodyContent): void
{
$environment = new Environment($this->createMock(LoaderInterface::class));

Expand All @@ -59,9 +59,10 @@ public function testAddsContent(string $code, array $expectedHeadContent, array
$this->assertSame('', $environment->render('template.html.twig'));

$this->assertSame($expectedHeadContent, $GLOBALS['TL_HEAD'] ?? []);
$this->assertSame($expectedStyleSheetContent, $GLOBALS['TL_STYLE_SHEETS'] ?? []);
$this->assertSame($expectedBodyContent, $GLOBALS['TL_BODY'] ?? []);

unset($GLOBALS['TL_HEAD'], $GLOBALS['TL_BODY']);
unset($GLOBALS['TL_HEAD'], $GLOBALS['TL_STYLE_SHEETS'], $GLOBALS['TL_BODY']);
}

public static function provideSources(): iterable
Expand All @@ -70,20 +71,32 @@ public static function provideSources(): iterable
'{% add to head %}head content{% endadd %}',
['head content'],
[],
[],
];

yield 'add to stylesheets' => [
'{% add to stylesheets %}stylesheets content{% endadd %}',
[],
['stylesheets content'],
[],
];

yield 'add to body' => [
'{% add to body %}body content{% endadd %}',
[],
[],
['body content'],
];

yield 'add multiple' => [
"{% add to head %}head content{% endadd %}\n".
"{% add to stylesheets %}stylesheets content{% endadd %}\n".
"{% add to body %}body content{% endadd %}\n".
"{% add to head %}head content{% endadd %}\n".
"{% add to stylesheets %}stylesheets content{% endadd %}\n".
'{% add to body %}body content{% endadd %}',
['head content', 'head content'],
['stylesheets content', 'stylesheets content'],
['body content', 'body content'],
];

Expand All @@ -92,28 +105,42 @@ public static function provideSources(): iterable
"{% add 'foo' to head %}overwritten head content{% endadd %}",
['foo' => 'overwritten head content'],
[],
[],
];

yield 'add named to stylesheets' => [
"{% add 'foo' to stylesheets %}stylesheets content{% endadd %}\n".
"{% add 'foo' to stylesheets %}overwritten stylesheets content{% endadd %}",
[],
['foo' => 'overwritten stylesheets content'],
[],
];

yield 'add named to body' => [
"{% add 'foo' to body %}body content{% endadd %}\n".
"{% add 'foo' to body %}overwritten body content{% endadd %}",
[],
[],
['foo' => 'overwritten body content'],
];

yield 'add multiple named' => [
"{% add 'foo' to head %}head content{% endadd %}\n".
"{% add 'foo' to stylesheets %}stylesheets content{% endadd %}\n".
"{% add 'foo' to body %}body content{% endadd %}\n".
"{% add 'foo' to head %}head content{% endadd %}\n".
"{% add 'foo' to stylesheets %}stylesheets content{% endadd %}\n".
"{% add 'foo' to body %}body content{% endadd %}",
['foo' => 'head content'],
['foo' => 'stylesheets content'],
['foo' => 'body content'],
];

yield 'add with complex content' => [
"{% set var = 'bar' %}\n".
'{% add to body %}foo {{ var }}{% endadd %}',
[],
[],
['foo bar'],
];
}
Expand All @@ -140,7 +167,7 @@ public static function provideInvalidSources(): iterable
{
yield 'invalid target' => [
'{% add to stomach %}apple{% endadd %}',
'The parameter "stomach" is not a valid location for the "add" tag, use "head" or "body" instead in "template.html.twig"',
'The parameter "stomach" is not a valid location for the "add" tag, use "head" or "stylesheets" or "body" instead in "template.html.twig"',
];

yield 'malformed target' => [
Expand Down

0 comments on commit 181cffc

Please sign in to comment.