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