Skip to content
Snippets Groups Projects
Unverified Commit 758adf0e authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2716115 by LittleCoding, gnuget, jungle, gapple, tatarbj,...

Issue #2716115 by LittleCoding, gnuget, jungle, gapple, tatarbj, sandeep_jangra, erlendoos, markcarver, NickDickinsonWilde, cayriawill, catch, geek-merlin: Allow attributes passed with CSS in libraries (SRI)
parent 704745bc
Branches
Tags
8 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!1012Issue #3226887: Hreflang on non-canonical content pages,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10,!596Issue #3046532: deleting an entity reference field, used in a contextual view, makes the whole site unrecoverable,!496Issue #2463967: Use .user.ini file for PHP settings,!144Issue #2666286: Clean up menu_ui to conform to Drupal coding standards,!16Draft: Resolve #2081585 "History storage",!13Resolve #2903456
......@@ -72,6 +72,11 @@ public function render(array $css_assets) {
throw new \Exception('Invalid CSS asset type.');
}
// Merge any additional attributes.
if (!empty($css_asset['attributes'])) {
$element['#attributes'] += $css_asset['attributes'];
}
$elements[] = $element;
}
......
......@@ -4,6 +4,7 @@
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 @@ protected function setUp(): void {
* @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 @@ public function providerTestRender() {
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 @@ public function providerTestRender() {
],
],
// 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 @@ public function providerTestRender() {
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 @@ public function providerTestRender() {
],
// 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 @@ public function providerTestRender() {
* @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 @@ public function testRender(array $css_assets, array $render_elements) {
* 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.');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment