Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Asset;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Html;
|
Chris@0
|
6 use Drupal\Core\State\StateInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Renders CSS assets.
|
Chris@0
|
10 *
|
Chris@0
|
11 * For production websites, LINK tags are preferable to STYLE tags with @import
|
Chris@0
|
12 * statements, because:
|
Chris@0
|
13 * - They are the standard tag intended for linking to a resource.
|
Chris@0
|
14 * - On Firefox 2 and perhaps other browsers, CSS files included with @import
|
Chris@0
|
15 * statements don't get saved when saving the complete web page for offline
|
Chris@0
|
16 * use: https://www.drupal.org/node/145218.
|
Chris@0
|
17 * - On IE, if only LINK tags and no @import statements are used, all the CSS
|
Chris@0
|
18 * files are downloaded in parallel, resulting in faster page load, but if
|
Chris@0
|
19 * @import statements are used and span across multiple STYLE tags, all the
|
Chris@0
|
20 * ones from one STYLE tag must be downloaded before downloading begins for
|
Chris@0
|
21 * the next STYLE tag. Furthermore, IE7 does not support media declaration on
|
Chris@0
|
22 * the @import statement, so multiple STYLE tags must be used when different
|
Chris@0
|
23 * files are for different media types. Non-IE browsers always download in
|
Chris@0
|
24 * parallel, so this is an IE-specific performance quirk:
|
Chris@0
|
25 * http://www.stevesouders.com/blog/2009/04/09/dont-use-import/.
|
Chris@0
|
26 *
|
Chris@0
|
27 * However, IE has an annoying limit of 31 total CSS inclusion tags
|
Chris@0
|
28 * (https://www.drupal.org/node/228818) and LINK tags are limited to one file
|
Chris@0
|
29 * per tag, whereas STYLE tags can contain multiple @import statements allowing
|
Chris@0
|
30 * multiple files to be loaded per tag. When CSS aggregation is disabled, a
|
Chris@0
|
31 * Drupal site can easily have more than 31 CSS files that need to be loaded, so
|
Chris@0
|
32 * using LINK tags exclusively would result in a site that would display
|
Chris@0
|
33 * incorrectly in IE. Depending on different needs, different strategies can be
|
Chris@0
|
34 * employed to decide when to use LINK tags and when to use STYLE tags.
|
Chris@0
|
35 *
|
Chris@0
|
36 * The strategy employed by this class is to use LINK tags for all aggregate
|
Chris@0
|
37 * files and for all files that cannot be aggregated (e.g., if 'preprocess' is
|
Chris@0
|
38 * set to FALSE or the type is 'external'), and to use STYLE tags for groups
|
Chris@0
|
39 * of files that could be aggregated together but aren't (e.g., if the site-wide
|
Chris@0
|
40 * aggregation setting is disabled). This results in all LINK tags when
|
Chris@0
|
41 * aggregation is enabled, a guarantee that as many or only slightly more tags
|
Chris@0
|
42 * are used with aggregation disabled than enabled (so that if the limit were to
|
Chris@0
|
43 * be crossed with aggregation enabled, the site developer would also notice the
|
Chris@0
|
44 * problem while aggregation is disabled), and an easy way for a developer to
|
Chris@0
|
45 * view HTML source while aggregation is disabled and know what files will be
|
Chris@0
|
46 * aggregated together when aggregation becomes enabled.
|
Chris@0
|
47 *
|
Chris@0
|
48 * This class evaluates the aggregation enabled/disabled condition on a group
|
Chris@0
|
49 * by group basis by testing whether an aggregate file has been made for the
|
Chris@0
|
50 * group rather than by testing the site-wide aggregation setting. This allows
|
Chris@0
|
51 * this class to work correctly even if modules have implemented custom
|
Chris@0
|
52 * logic for grouping and aggregating files.
|
Chris@0
|
53 */
|
Chris@0
|
54 class CssCollectionRenderer implements AssetCollectionRendererInterface {
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * The state key/value store.
|
Chris@0
|
58 *
|
Chris@0
|
59 * @var \Drupal\Core\State\StateInterface
|
Chris@0
|
60 */
|
Chris@0
|
61 protected $state;
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * Constructs a CssCollectionRenderer.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @param \Drupal\Core\State\StateInterface $state
|
Chris@0
|
67 * The state key/value store.
|
Chris@0
|
68 */
|
Chris@0
|
69 public function __construct(StateInterface $state) {
|
Chris@0
|
70 $this->state = $state;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * {@inheritdoc}
|
Chris@0
|
75 */
|
Chris@0
|
76 public function render(array $css_assets) {
|
Chris@0
|
77 $elements = [];
|
Chris@0
|
78
|
Chris@0
|
79 // A dummy query-string is added to filenames, to gain control over
|
Chris@0
|
80 // browser-caching. The string changes on every update or full cache
|
Chris@0
|
81 // flush, forcing browsers to load a new copy of the files, as the
|
Chris@0
|
82 // URL changed.
|
Chris@0
|
83 $query_string = $this->state->get('system.css_js_query_string') ?: '0';
|
Chris@0
|
84
|
Chris@0
|
85 // Defaults for LINK and STYLE elements.
|
Chris@0
|
86 $link_element_defaults = [
|
Chris@0
|
87 '#type' => 'html_tag',
|
Chris@0
|
88 '#tag' => 'link',
|
Chris@0
|
89 '#attributes' => [
|
Chris@0
|
90 'rel' => 'stylesheet',
|
Chris@0
|
91 ],
|
Chris@0
|
92 ];
|
Chris@0
|
93 $style_element_defaults = [
|
Chris@0
|
94 '#type' => 'html_tag',
|
Chris@0
|
95 '#tag' => 'style',
|
Chris@0
|
96 ];
|
Chris@0
|
97
|
Chris@0
|
98 // For filthy IE hack.
|
Chris@0
|
99 $current_ie_group_keys = NULL;
|
Chris@0
|
100 $get_ie_group_key = function ($css_asset) {
|
Chris@0
|
101 return [$css_asset['type'], $css_asset['preprocess'], $css_asset['group'], $css_asset['media'], $css_asset['browsers']];
|
Chris@0
|
102 };
|
Chris@0
|
103
|
Chris@0
|
104 // Loop through all CSS assets, by key, to allow for the special IE
|
Chris@0
|
105 // workaround.
|
Chris@0
|
106 $css_assets_keys = array_keys($css_assets);
|
Chris@0
|
107 for ($i = 0; $i < count($css_assets_keys); $i++) {
|
Chris@0
|
108 $css_asset = $css_assets[$css_assets_keys[$i]];
|
Chris@0
|
109 switch ($css_asset['type']) {
|
Chris@0
|
110 // For file items, there are three possibilities.
|
Chris@0
|
111 // - There are up to 31 CSS assets on the page (some of which may be
|
Chris@0
|
112 // aggregated). In this case, output a LINK tag for file CSS assets.
|
Chris@0
|
113 // - There are more than 31 CSS assets on the page, yet we must stay
|
Chris@0
|
114 // below IE<10's limit of 31 total CSS inclusion tags, we handle this
|
Chris@0
|
115 // in two ways:
|
Chris@0
|
116 // - file CSS assets that are not eligible for aggregation (their
|
Chris@0
|
117 // 'preprocess' flag has been set to FALSE): in this case, output a
|
Chris@0
|
118 // LINK tag.
|
Chris@0
|
119 // - file CSS assets that can be aggregated (and possibly have been):
|
Chris@0
|
120 // in this case, figure out which subsequent file CSS assets share
|
Chris@0
|
121 // the same key properties ('group', 'media' and 'browsers') and
|
Chris@0
|
122 // output this group into as few STYLE tags as possible (a STYLE
|
Chris@0
|
123 // tag may contain only 31 @import statements).
|
Chris@0
|
124 case 'file':
|
Chris@0
|
125 // The dummy query string needs to be added to the URL to control
|
Chris@0
|
126 // browser-caching.
|
Chris@0
|
127 $query_string_separator = (strpos($css_asset['data'], '?') !== FALSE) ? '&' : '?';
|
Chris@0
|
128
|
Chris@0
|
129 // As long as the current page will not run into IE's limit for CSS
|
Chris@0
|
130 // assets: output a LINK tag for a file CSS asset.
|
Chris@0
|
131 if (count($css_assets) <= 31) {
|
Chris@0
|
132 $element = $link_element_defaults;
|
Chris@0
|
133 $element['#attributes']['href'] = file_url_transform_relative(file_create_url($css_asset['data'])) . $query_string_separator . $query_string;
|
Chris@0
|
134 $element['#attributes']['media'] = $css_asset['media'];
|
Chris@0
|
135 $element['#browsers'] = $css_asset['browsers'];
|
Chris@0
|
136 $elements[] = $element;
|
Chris@0
|
137 }
|
Chris@0
|
138 // The current page will run into IE's limits for CSS assets: work
|
Chris@0
|
139 // around these limits by performing a light form of grouping.
|
Chris@0
|
140 // Once Drupal only needs to support IE10 and later, we can drop this.
|
Chris@0
|
141 else {
|
Chris@0
|
142 // The file CSS asset is ineligible for aggregation: output it in a
|
Chris@0
|
143 // LINK tag.
|
Chris@0
|
144 if (!$css_asset['preprocess']) {
|
Chris@0
|
145 $element = $link_element_defaults;
|
Chris@0
|
146 $element['#attributes']['href'] = file_url_transform_relative(file_create_url($css_asset['data'])) . $query_string_separator . $query_string;
|
Chris@0
|
147 $element['#attributes']['media'] = $css_asset['media'];
|
Chris@0
|
148 $element['#browsers'] = $css_asset['browsers'];
|
Chris@0
|
149 $elements[] = $element;
|
Chris@0
|
150 }
|
Chris@0
|
151 // The file CSS asset can be aggregated, but hasn't been: combine
|
Chris@0
|
152 // multiple items into as few STYLE tags as possible.
|
Chris@0
|
153 else {
|
Chris@0
|
154 $import = [];
|
Chris@0
|
155 // Start with the current CSS asset, iterate over subsequent CSS
|
Chris@0
|
156 // assets and find which ones have the same 'type', 'group',
|
Chris@0
|
157 // 'preprocess', 'media' and 'browsers' properties.
|
Chris@0
|
158 $j = $i;
|
Chris@0
|
159 $next_css_asset = $css_asset;
|
Chris@0
|
160 $current_ie_group_key = $get_ie_group_key($css_asset);
|
Chris@0
|
161 do {
|
Chris@0
|
162 // The dummy query string needs to be added to the URL to
|
Chris@0
|
163 // control browser-caching. IE7 does not support a media type on
|
Chris@0
|
164 // the @import statement, so we instead specify the media for
|
Chris@0
|
165 // the group on the STYLE tag.
|
Chris@0
|
166 $import[] = '@import url("' . Html::escape(file_url_transform_relative(file_create_url($next_css_asset['data'])) . '?' . $query_string) . '");';
|
Chris@0
|
167 // Move the outer for loop skip the next item, since we
|
Chris@0
|
168 // processed it here.
|
Chris@0
|
169 $i = $j;
|
Chris@0
|
170 // Retrieve next CSS asset, unless there is none: then break.
|
Chris@0
|
171 if ($j + 1 < count($css_assets_keys)) {
|
Chris@0
|
172 $j++;
|
Chris@0
|
173 $next_css_asset = $css_assets[$css_assets_keys[$j]];
|
Chris@0
|
174 }
|
Chris@0
|
175 else {
|
Chris@0
|
176 break;
|
Chris@0
|
177 }
|
Chris@0
|
178 } while ($get_ie_group_key($next_css_asset) == $current_ie_group_key);
|
Chris@0
|
179
|
Chris@0
|
180 // In addition to IE's limit of 31 total CSS inclusion tags, it
|
Chris@0
|
181 // also has a limit of 31 @import statements per STYLE tag.
|
Chris@0
|
182 while (!empty($import)) {
|
Chris@0
|
183 $import_batch = array_slice($import, 0, 31);
|
Chris@0
|
184 $import = array_slice($import, 31);
|
Chris@0
|
185 $element = $style_element_defaults;
|
Chris@0
|
186 // This simplifies the JavaScript regex, allowing each line
|
Chris@0
|
187 // (separated by \n) to be treated as a completely different
|
Chris@0
|
188 // string. This means that we can use ^ and $ on one line at a
|
Chris@0
|
189 // time, and not worry about style tags since they'll never
|
Chris@0
|
190 // match the regex.
|
Chris@0
|
191 $element['#value'] = "\n" . implode("\n", $import_batch) . "\n";
|
Chris@0
|
192 $element['#attributes']['media'] = $css_asset['media'];
|
Chris@0
|
193 $element['#browsers'] = $css_asset['browsers'];
|
Chris@0
|
194 $elements[] = $element;
|
Chris@0
|
195 }
|
Chris@0
|
196 }
|
Chris@0
|
197 }
|
Chris@0
|
198 break;
|
Chris@0
|
199
|
Chris@0
|
200 // Output a LINK tag for an external CSS asset. The asset's 'data'
|
Chris@0
|
201 // property contains the full URL.
|
Chris@0
|
202 case 'external':
|
Chris@0
|
203 $element = $link_element_defaults;
|
Chris@0
|
204 $element['#attributes']['href'] = $css_asset['data'];
|
Chris@0
|
205 $element['#attributes']['media'] = $css_asset['media'];
|
Chris@0
|
206 $element['#browsers'] = $css_asset['browsers'];
|
Chris@0
|
207 $elements[] = $element;
|
Chris@0
|
208 break;
|
Chris@0
|
209
|
Chris@0
|
210 default:
|
Chris@0
|
211 throw new \Exception('Invalid CSS asset type.');
|
Chris@0
|
212 }
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 return $elements;
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 }
|