Skip to content

Commit 8d5ed5f

Browse files
authored
Fix inline CSS via drupal_add_css (#166)
1 parent c117716 commit 8d5ed5f

File tree

3 files changed

+111
-15
lines changed

3 files changed

+111
-15
lines changed

src/functions/common.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ function drupal_add_css(string|null $data = null, array|string|null $options = n
272272
if ($data === null) {
273273
return [];
274274
}
275+
275276
$attachment_subscriber = \Drupal::getContainer()->get(AttachmentResponseSubscriber::class);
276277
assert($attachment_subscriber instanceof AttachmentResponseSubscriber);
277278

@@ -280,10 +281,26 @@ function drupal_add_css(string|null $data = null, array|string|null $options = n
280281
} elseif ($options === null) {
281282
$options = [];
282283
}
284+
$options += [
285+
'type' => 'file',
286+
'group' => CSS_DEFAULT,
287+
'weight' => 0,
288+
'every_page' => false,
289+
'media' => 'all',
290+
'preprocess' => true,
291+
'data' => $data,
292+
'browsers' => [],
293+
];
283294
$type = $options['type'] ?? 'file';
295+
296+
// Files with a query string cannot be preprocessed.
297+
if ($options['type'] === 'file' && $options['preprocess'] && str_contains($data, '?')) {
298+
$options['preprocess'] = false;
299+
}
300+
284301
if ($type === 'inline') {
285302
$attachment_subscriber->addAttachments([
286-
'css' => $options,
303+
'css' => [$options],
287304
]);
288305
} elseif ($data) {
289306
$attachment_subscriber->addAttachments([

tests/src/Unit/Render/AttachmentResponseSubscriberTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
use Symfony\Component\HttpKernel\Event\ResponseEvent;
1414
use Symfony\Component\HttpKernel\HttpKernelInterface;
1515

16+
/**
17+
* @coversDefaultClass \Retrofit\Drupal\Render\AttachmentResponseSubscriber
18+
*/
1619
final class AttachmentResponseSubscriberTest extends TestCase
1720
{
21+
/**
22+
* @covers ::addAttachments
23+
* @covers ::onResponse
24+
*/
1825
public function testAddAttachments(): void
1926
{
2027
$sut = new AttachmentResponseSubscriber();
@@ -25,6 +32,7 @@ public function testAddAttachments(): void
2532
drupal_add_library('system', 'jquery');
2633
drupal_add_js(['hello' => 'World'], ['type' => 'setting']);
2734
drupal_add_css('https://example.com/cdn.js');
35+
drupal_add_css('.foo { color: pink }', ['type' => 'inline']);
2836

2937
$response = new HtmlResponse();
3038
$event = new ResponseEvent(
@@ -39,7 +47,26 @@ public function testAddAttachments(): void
3947
'library' => ['core/jquery'],
4048
'drupalSettings' => ['hello' => 'World'],
4149
'css' => [
42-
'https://example.com/cdn.js' => [],
50+
'https://example.com/cdn.js' => [
51+
'type' => 'file',
52+
'group' => 0,
53+
'weight' => 0,
54+
'every_page' => false,
55+
'media' => 'all',
56+
'preprocess' => true,
57+
'data' => 'https://example.com/cdn.js',
58+
'browsers' => [],
59+
],
60+
[
61+
'type' => 'inline',
62+
'group' => 0,
63+
'weight' => 0,
64+
'every_page' => false,
65+
'media' => 'all',
66+
'preprocess' => true,
67+
'data' => '.foo { color: pink }',
68+
'browsers' => [],
69+
]
4370
],
4471
], $response->getAttachments());
4572
}

tests/src/Unit/Render/RetrofitHtmlResponseAttachmentsProcessorTest.php

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717
final class RetrofitHtmlResponseAttachmentsProcessorTest extends TestCase
1818
{
1919
/**
20+
*
21+
* @param array<string, mixed> $attachments
22+
* @param array<string, mixed> $expected
23+
*
24+
* @covers ::__construct
2025
* @covers ::processAttachments
26+
*
27+
* @dataProvider dataAttachments
2128
*/
22-
public function testProcessAttachments(): void
29+
public function testProcessAttachments(array $attachments, array $expected): void
2330
{
2431
$response = new HtmlResponse('');
25-
$response->setAttachments([
26-
'library' => [
27-
['foo', 'bar']
28-
],
29-
]);
32+
$response->setAttachments($attachments);
3033
$inner = $this->createMock(AttachmentsResponseProcessorInterface::class);
3134
$inner->expects(self::once())
3235
->method('processAttachments')
@@ -35,14 +38,63 @@ public function testProcessAttachments(): void
3538
$libraryDiscovery = $this->createMock(RetrofitLibraryDiscovery::class);
3639
$sut = new RetrofitHtmlResponseAttachmentsProcessor($inner, $jsCollectionRenderer, $libraryDiscovery);
3740
$sut->processAttachments($response);
38-
self::assertEquals(
39-
[
40-
'library' => [
41-
'foo/bar',
42-
'retrofit/NXhscRe0440PFpI5dSznEVgmauL25KojD7u4e9aZwOM',
41+
self::assertEquals($expected, $response->getAttachments());
42+
}
43+
44+
/**
45+
* @return array<string, array<int, array<string, array<int, array<int|string, array<string, array<string,
46+
* string>|int|string>|bool|int|string>|string>>>>
47+
*/
48+
public static function dataAttachments(): array
49+
{
50+
return [
51+
'legacy libraries' => [
52+
[
53+
'library' => [
54+
['foo', 'bar']
55+
],
56+
],
57+
[
58+
'library' => [
59+
'foo/bar',
60+
'retrofit/NXhscRe0440PFpI5dSznEVgmauL25KojD7u4e9aZwOM',
61+
],
62+
],
63+
],
64+
'inline css' => [
65+
[
66+
'css' => [
67+
[
68+
'type' => 'inline',
69+
'group' => 0,
70+
'weight' => 0,
71+
'every_page' => false,
72+
'media' => 'all',
73+
'preprocess' => true,
74+
'data' => '.foo { color: pink }',
75+
'browsers' => [],
76+
],
77+
],
4378
],
79+
[
80+
'html_head' => [
81+
[
82+
[
83+
'#tag' => 'style',
84+
'#value' => '.foo { color: pink }',
85+
'#weight' => 0,
86+
'#attributes' => [
87+
'media' => 'all',
88+
],
89+
],
90+
'retrofit:0',
91+
],
92+
],
93+
'library' => [
94+
'retrofit/p0pYMgU_NanStScEFSFfzy8t6FiwrwJCbJdc0EbwWk0',
95+
],
96+
]
4497
],
45-
$response->getAttachments()
46-
);
98+
];
4799
}
48100
}

0 commit comments

Comments
 (0)