diff --git a/core/lib/Drupal/Core/Asset/CssCollectionRenderer.php b/core/lib/Drupal/Core/Asset/CssCollectionRenderer.php index 24ea64ea7acc..13c53e24b4cf 100644 --- a/core/lib/Drupal/Core/Asset/CssCollectionRenderer.php +++ b/core/lib/Drupal/Core/Asset/CssCollectionRenderer.php @@ -72,6 +72,11 @@ class CssCollectionRenderer implements AssetCollectionRendererInterface { throw new \Exception('Invalid CSS asset type.'); } + // Merge any additional attributes. + if (!empty($css_asset['attributes'])) { + $element['#attributes'] += $css_asset['attributes']; + } + $elements[] = $element; } diff --git a/core/tests/Drupal/Tests/Core/Asset/CssCollectionRendererUnitTest.php b/core/tests/Drupal/Tests/Core/Asset/CssCollectionRendererUnitTest.php index b96f5eaa8647..39d65bfa4e9f 100644 --- a/core/tests/Drupal/Tests/Core/Asset/CssCollectionRendererUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/CssCollectionRendererUnitTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\Core\Asset; use Drupal\Core\Asset\CssCollectionRenderer; use Drupal\Tests\UnitTestCase; +use Drupal\Core\State\StateInterface; /** * Tests the CSS asset collection renderer. @@ -27,18 +28,13 @@ class CssCollectionRendererUnitTest extends UnitTestCase { protected $fileCssGroup; /** - * The state mock class. - * - * @var \Drupal\Core\State\StateInterface|\PHPUnit\Framework\MockObject\MockObject + * {@inheritdoc} */ - protected $state; - protected function setUp(): void { parent::setUp(); - - $this->state = $this->createMock('Drupal\Core\State\StateInterface'); - - $this->renderer = new CssCollectionRenderer($this->state); + $state = $this->prophesize(StateInterface::class); + $state->get('system.css_js_query_string', '0')->shouldBeCalledOnce()->willReturn(NULL); + $this->renderer = new CssCollectionRenderer($state->reveal()); $this->fileCssGroup = [ 'group' => -100, 'type' => 'file', @@ -76,15 +72,16 @@ class CssCollectionRendererUnitTest extends UnitTestCase { * @see testRender */ public function providerTestRender() { - $create_link_element = function ($href, $media = 'all', $browsers = []) { + $create_link_element = function ($href, $media = 'all', $browsers = [], $custom_attributes = []) { + $attributes = [ + 'rel' => 'stylesheet', + 'media' => $media, + 'href' => $href, + ]; return [ '#type' => 'html_tag', '#tag' => 'link', - '#attributes' => [ - 'rel' => 'stylesheet', - 'media' => $media, - 'href' => $href, - ], + '#attributes' => array_replace($attributes, $custom_attributes), '#browsers' => $browsers, ]; }; @@ -93,6 +90,8 @@ class CssCollectionRendererUnitTest extends UnitTestCase { return ['group' => 0, 'type' => 'file', 'media' => $media, 'preprocess' => $preprocess, 'data' => $data, 'browsers' => []]; }; + $custom_attributes = ['integrity' => 'sha384-psK1OYPAYjYUhtDYW+Pj2yc', 'crossorigin' => 'anonymous', 'random-attribute' => 'test']; + return [ // Single external CSS asset. 0 => [ @@ -106,7 +105,7 @@ class CssCollectionRendererUnitTest extends UnitTestCase { ], ], // Single file CSS asset. - 2 => [ + 1 => [ [ 0 => ['group' => 0, 'type' => 'file', 'media' => 'all', 'preprocess' => TRUE, 'data' => 'public://css/file-all', 'browsers' => []], ], @@ -114,6 +113,15 @@ class CssCollectionRendererUnitTest extends UnitTestCase { 0 => $create_link_element(file_url_transform_relative(file_create_url('public://css/file-all')) . '?', 'all'), ], ], + // Single file CSS asset with custom attributes. + 2 => [ + [ + 0 => ['group' => 0, 'type' => 'file', 'media' => 'all', 'preprocess' => TRUE, 'data' => 'public://css/file-all', 'browsers' => [], 'attributes' => $custom_attributes], + ], + [ + 0 => $create_link_element(file_url_transform_relative(file_create_url('public://css/file-all')) . '?', 'all', [], $custom_attributes), + ], + ], // 31 file CSS assets: expect 31 link elements. 3 => [ [ @@ -185,7 +193,7 @@ class CssCollectionRendererUnitTest extends UnitTestCase { ], // 32 file CSS assets with the same properties, except for the 10th and // 20th files, they have different 'media' properties. - 5 => [ + 4 => [ [ 0 => $create_file_css_asset('public://css/1.css'), 1 => $create_file_css_asset('public://css/2.css'), @@ -264,10 +272,6 @@ class CssCollectionRendererUnitTest extends UnitTestCase { * @dataProvider providerTestRender */ public function testRender(array $css_assets, array $render_elements) { - $this->state->expects($this->once()) - ->method('get') - ->with('system.css_js_query_string') - ->will($this->returnValue(NULL)); $this->assertSame($render_elements, $this->renderer->render($css_assets)); } @@ -275,10 +279,6 @@ class CssCollectionRendererUnitTest extends UnitTestCase { * Tests a CSS asset group with the invalid 'type' => 'internal'. */ public function testRenderInvalidType() { - $this->state->expects($this->once()) - ->method('get') - ->with('system.css_js_query_string') - ->will($this->returnValue(NULL)); $this->expectException('Exception'); $this->expectExceptionMessage('Invalid CSS asset type.');