Skip to content

[Forwardport] CSS load order incorrect using default_head_blocks.xml #1821 #14815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/internal/Magento/Framework/View/Layout/etc/head.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<xs:attribute name="sizes" type="xs:string"/>
<xs:attribute name="target" type="xs:string"/>
<xs:attribute name="type" type="xs:string"/>
<xs:attribute name="order" type="xs:integer"/>
<xs:attribute name="src_type" type="xs:string"/>
</xs:complexType>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Head implements Layout\GeneratorInterface
*/
protected $assetProperties = [
'ie_condition',
'order'
];

/**
Expand Down
13 changes: 11 additions & 2 deletions lib/internal/Magento/Framework/View/Page/Config/Reader/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,16 @@ public function interpret(
Layout\Element $headElement
) {
$pageConfigStructure = $readerContext->getPageConfigStructure();
/** @var \Magento\Framework\View\Layout\Element $node */
foreach ($headElement as $node) {
$nodes = iterator_to_array($headElement, false);

usort(
$nodes,
function (Layout\Element $current, Layout\Element $next) {
return $current->getAttribute('order') <=> $next->getAttribute('order');
}
);

foreach ($nodes as $node) {
switch ($node->getName()) {
case self::HEAD_CSS:
case self::HEAD_SCRIPT:
Expand Down Expand Up @@ -105,6 +113,7 @@ public function interpret(
break;
}
}

return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ protected function setUp()
);
}

public function testProcess()
/**
* @param array $assets
*
* @dataProvider testProcessAssetDataProvider
*/
public function testProcess($assets)
{
$generatorContextMock = $this->createMock(Context::class);
$this->title->expects($this->any())->method('set')->with()->will($this->returnSelf());
Expand All @@ -75,39 +80,19 @@ public function testProcess()
->with('customcss/render/css')
->willReturn('http://magento.dev/customcss/render/css');

$assets = [
'remoteCss' => [
'src' => 'file-url-css',
'src_type' => 'url',
'content_type' => 'css',
'media' => 'all',
],
'remoteLink' => [
'src' => 'file-url-link',
'src_type' => 'url',
'media' => 'all',
],
'controllerCss' => [
'src' => 'customcss/render/css',
'src_type' => 'controller',
'content_type' => 'css',
'media' => 'all',
],
'name' => [
'src' => 'file-path',
'ie_condition' => 'lt IE 7',
'content_type' => 'css',
'media' => 'print',
],
];

$this->pageConfigMock->expects($this->at(0))
->method('addRemotePageAsset')
->with('file-url-css', 'css', ['attributes' => ['media' => 'all']]);
$this->pageConfigMock->expects($this->at(1))
->method('addRemotePageAsset')
->with('file-url-link', Head::VIRTUAL_CONTENT_TYPE_LINK, ['attributes' => ['media' => 'all']]);
->with('file-url-css-last', 'css', ['attributes' => ['media' => 'all' ] , 'order' => 30]);
$this->pageConfigMock->expects($this->at(2))
->method('addRemotePageAsset')
->with('file-url-css-first', 'css', ['attributes' => ['media' => 'all'] , 'order' => 10]);
$this->pageConfigMock->expects($this->at(3))
->method('addRemotePageAsset')
->with('file-url-link', Head::VIRTUAL_CONTENT_TYPE_LINK, ['attributes' => ['media' => 'all']]);
$this->pageConfigMock->expects($this->at(4))
->method('addRemotePageAsset')
->with('http://magento.dev/customcss/render/css', 'css', ['attributes' => ['media' => 'all']]);
$this->pageConfigMock->expects($this->once())
Expand Down Expand Up @@ -154,4 +139,51 @@ public function testProcess()
$result = $this->headGenerator->process($readerContextMock, $generatorContextMock);
$this->assertEquals($this->headGenerator, $result);
}

public function testProcessAssetDataProvider()
{
return [
[
'assets' => [
'remoteCss' => [
'src' => 'file-url-css',
'src_type' => 'url',
'content_type' => 'css',
'media' => 'all',
],
'remoteCssOrderedLast' => [
'src' => 'file-url-css-last',
'src_type' => 'url',
'content_type' => 'css',
'media' => 'all',
'order' => 30,
],
'remoteCssOrderedFirst' => [
'src' => 'file-url-css-first',
'src_type' => 'url',
'content_type' => 'css',
'media' => 'all',
'order' => 10,
],
'remoteLink' => [
'src' => 'file-url-link',
'src_type' => 'url',
'media' => 'all',
],
'controllerCss' => [
'src' => 'customcss/render/css',
'src_type' => 'controller',
'content_type' => 'css',
'media' => 'all',
],
'name' => [
'src' => 'file-path',
'ie_condition' => 'lt IE 7',
'content_type' => 'css',
'media' => 'print',
],
],
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testInterpret()

$structureMock->expects($this->at(4))
->method('addAssets')
->with('path/file.css', ['src' => 'path/file.css', 'media' => 'all', 'content_type' => 'css'])
->with('path/file-3.css', ['src' => 'path/file-3.css', 'media' => 'all', 'content_type' => 'css'])
->willReturnSelf();

$structureMock->expects($this->at(5))
Expand All @@ -82,6 +82,26 @@ public function testInterpret()
->with(Config::ELEMENT_TYPE_HEAD, 'head_attribute_name', 'head_attribute_value')
->willReturnSelf();

$structureMock->expects($this->at(9))
->method('addAssets')
->with('path/file-1.css', [
'src' => 'path/file-1.css',
'media' => 'all',
'content_type' => 'css',
'order' => 10
])
->willReturnSelf();

$structureMock->expects($this->at(10))
->method('addAssets')
->with('path/file-2.css', [
'src' => 'path/file-2.css',
'media' => 'all',
'content_type' => 'css',
'order' => 30
])
->willReturnSelf();

$this->assertEquals($this->model, $this->model->interpret($readerContextMock, $element->children()[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<meta name="meta_name" content="meta_content"/>
<meta property="og:video:secure_url" content="https://secure.example.com/movie.swf" />
<meta property="og:locale:alternate" content="uk_UA" />
<css src="path/file.css" media="all" />
<css src="path/file-1.css" order="10" media="all" />
<css src="path/file-2.css" order="30" media="all" />
<css src="path/file-3.css" media="all" />
<script src="path/file.js" defer="defer"/>
<link src="http://url.com" src_type="url"/>
<remove src="path/remove/file.css"/>
Expand Down