-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathReorderHead.php
366 lines (329 loc) · 11.4 KB
/
ReorderHead.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
namespace AmpProject\Optimizer\Transformer;
use AmpProject\Amp;
use AmpProject\Html\Attribute;
use AmpProject\Dom\Document;
use AmpProject\Dom\Element;
use AmpProject\Optimizer\ErrorCollection;
use AmpProject\Optimizer\Transformer;
use AmpProject\Html\Tag;
use DOMNode;
use DOMNodeList;
/**
* Transformer applying the head reordering transformations to the HTML input.
*
* ReorderHead reorders the children of <head>. Specifically, it
* orders the <head> like so:
* (0) <meta charset> tag
* (1) <style amp-runtime> (inserted by AmpRuntimeCss transformer)
* (2) remaining <meta> tags (those other than <meta charset>)
* (3) AMP runtime .js <script> tag
* (4) AMP viewer runtime .js <script>
* (5) <script> tags that are render delaying
* (6) <script> tags for remaining extensions
* (7) <link> tag for favicons
* (8) <link> tag for resource hints
* (9) <link rel=stylesheet> tags before <style amp-custom>
* (10) <style amp-custom>
* (11) any other tags allowed in <head>
* (12) AMP boilerplate (first style amp-boilerplate, then noscript)
*
* This is ported from the NodeJS optimizer while verifying against the Go version.
*
* NodeJS:
* @version c92d6023ea4c9edadff593742a992da2b400a75d
* @link https://github.com/ampproject/amp-toolbox/blob/c92d6023ea4c9edadff593742a992da2b400a75d/packages/optimizer/lib/transformers/ReorderHeadTransformer.js
*
* Go:
* @version ea0959046c179953de43077eafaeb720f9b20bdf
* @link https://github.com/ampproject/amppackager/blob/ea0959046c179953de43077eafaeb720f9b20bdf/transformer/transformers/reorderhead.go
*
* @package ampproject/amp-toolbox
*/
final class ReorderHead implements Transformer
{
/**
* Regular expression pattern to match resource hints pointing to an AMP resource.
*/
const AMP_RESOURCE_HINT_SRC_PATTERN = '#(^|[\b/])cdn\.ampproject\.org($|[\b/])#i';
/*
* Different categories of <head> tags to track and reorder.
*/
// phpcs:disable Squiz.Commenting.VariableComment.Missing
private $ampResourceHints = [];
private $linkIcons = [];
private $linkStyleAmpRuntime = null;
private $linkStylesheetsBeforeAmpCustom = [];
private $metaCharset = null;
private $metaViewport = null;
private $metaOther = [];
private $noscript = null;
private $others = [];
private $resourceHintLinks = [];
private $scriptAmpRuntime = [];
private $scriptAmpViewer = [];
private $scriptNonRenderDelayingExtensions = [];
private $scriptRenderDelayingExtensions = [];
private $styleAmpBoilerplate = null;
private $styleAmpCustom = null;
private $styleAmpRuntime = null;
/**
* Apply transformations to the provided DOM document.
*
* @param Document $document DOM document to apply the transformations to.
* @param ErrorCollection $errors Collection of errors that are collected during transformation.
* @return void
*/
public function transform(Document $document, ErrorCollection $errors)
{
$nodes = $document->head->childNodes;
if (! $nodes instanceof DOMNodeList || $nodes->length === 0) {
return;
}
while ($document->head->hasChildNodes()) {
$node = $document->head->removeChild($document->head->firstChild);
$this->registerNode($node);
}
$this->appendToHead($document);
}
/**
* Register a given node in the appropriate category.
*
* @param DOMNode $node Node to register.
*/
private function registerNode(DOMNode $node)
{
if (! $node instanceof Element) {
if ($node->nodeType === XML_TEXT_NODE) {
$nodeContent = trim($node->textContent);
if (empty($nodeContent)) {
return;
}
}
$this->others[] = $node;
return;
}
switch ($node->tagName) {
case Tag::META:
$this->registerMeta($node);
break;
case Tag::SCRIPT:
$this->registerScript($node);
break;
case Tag::STYLE:
$this->registerStyle($node);
break;
case Tag::LINK:
$this->registerLink($node);
break;
case Tag::NOSCRIPT:
$this->noscript = $node; // @todo Make this an array.
break;
default:
$this->others[] = $node;
}
}
/**
* Register a <meta> node.
*
* @param Element $node Node to register.
*/
private function registerMeta(Element $node)
{
if ($node->hasAttribute(Attribute::CHARSET)) {
$this->metaCharset = $node;
return;
}
if ($node->getAttribute(Attribute::NAME) === Attribute::VIEWPORT) {
$this->metaViewport = $node;
return;
}
$this->metaOther[] = $node;
}
/**
* Register a <script> node.
*
* @param Element $node Node to register.
*/
private function registerScript(Element $node)
{
$nomodule = (int)$node->hasAttribute('nomodule');
if (Amp::isRuntimeScript($node)) {
$this->scriptAmpRuntime[$nomodule] = $node;
return;
}
if (Amp::isViewerScript($node)) {
$this->scriptAmpViewer[$nomodule] = $node;
return;
}
if ($node->hasAttribute(Attribute::CUSTOM_ELEMENT)) {
$name = $node->getAttribute(Attribute::CUSTOM_ELEMENT);
if (Amp::isRenderDelayingExtension($node)) {
$this->scriptRenderDelayingExtensions[$name][$nomodule] = $node;
return;
}
$this->scriptNonRenderDelayingExtensions[$name][$nomodule] = $node;
return;
}
if ($node->hasAttribute(Attribute::CUSTOM_TEMPLATE)) {
$name = $node->getAttribute(Attribute::CUSTOM_TEMPLATE);
$this->scriptNonRenderDelayingExtensions[$name][$nomodule] = $node;
return;
}
if ($node->hasAttribute(Attribute::HOST_SERVICE)) {
$name = $node->getAttribute(Attribute::HOST_SERVICE);
$this->scriptNonRenderDelayingExtensions[$name][$nomodule] = $node;
return;
}
$this->others[] = $node;
}
/**
* Register a <style> node.
*
* @param Element $node Node to register.
*/
private function registerStyle(Element $node)
{
if ($node->hasAttribute(Attribute::AMP_RUNTIME)) {
$this->styleAmpRuntime = $node;
return;
}
if ($node->hasAttribute(Attribute::AMP_CUSTOM)) {
$this->styleAmpCustom = $node;
return;
}
if (
$node->hasAttribute(Attribute::AMP_BOILERPLATE)
|| $node->hasAttribute(Attribute::AMP4ADS_BOILERPLATE)
) {
$this->styleAmpBoilerplate = $node;
return;
}
$this->others[] = $node;
}
/**
* Register a <link> node.
*
* @param Element $node Node to register.
*/
private function registerLink(Element $node)
{
$rel = $node->getAttribute(Attribute::REL);
if ($this->containsWord($rel, Attribute::REL_STYLESHEET)) {
$href = $node->getAttribute(Attribute::HREF);
if ($href && substr($href, -7) === '/v0.css') {
$this->linkStyleAmpRuntime = $node;
return;
}
if (! $this->styleAmpCustom) {
// We haven't seen amp-custom yet.
$this->linkStylesheetsBeforeAmpCustom[] = $node;
return;
}
}
if ($this->containsWord($rel, Attribute::REL_ICON)) {
$this->linkIcons[] = $node;
return;
}
if (
$this->containsWord($rel, Attribute::REL_PRELOAD)
|| $this->containsWord($rel, Attribute::REL_PREFETCH)
|| $this->containsWord($rel, Attribute::REL_DNS_PREFETCH)
|| $this->containsWord($rel, Attribute::REL_PRECONNECT)
|| $this->containsWord($rel, Attribute::REL_MODULEPRELOAD)
) {
if ($this->isHintForAmp($node)) {
$this->ampResourceHints[] = $node;
} else {
$this->resourceHintLinks[] = $node;
}
return;
}
$this->others[] = $node;
}
/**
* Append all registered nodes to the <head> node.
*
* @param Document $document Document to append the nodes to.
*/
private function appendToHead(Document $document)
{
$categories = [
'metaCharset',
'metaViewport',
'ampResourceHints',
'linkStyleAmpRuntime',
'styleAmpRuntime',
'metaOther',
'resourceHintLinks',
'scriptAmpRuntime',
'scriptAmpViewer',
'scriptRenderDelayingExtensions',
'scriptNonRenderDelayingExtensions',
'linkIcons',
'linkStylesheetsBeforeAmpCustom',
'styleAmpCustom',
'others',
'styleAmpBoilerplate',
'noscript',
];
foreach ($categories as $category) {
if ($this->$category === null) {
continue;
}
if ($this->$category instanceof DOMNode) {
$node = $document->importNode($this->$category);
$document->head->appendChild($node);
} elseif (is_array($this->$category)) {
$this->recursiveKeySort($this->$category);
array_walk_recursive(
$this->$category,
static function (DOMNode $node) use ($document) {
$node = $document->importNode($node);
$document->head->appendChild($node);
}
);
}
}
}
/**
* Sort array keys recursively.
*
* @param array|mixed $item Item.
*/
private function recursiveKeySort(&$item)
{
if (is_array($item)) {
ksort($item);
array_walk($item, [$this, 'recursiveKeySort']);
}
}
/**
* Check if a given string contains another string, respecting word boundaries..
*
* @param string $haystack Haystack string to look in.
* @param string $needle Needle string to search for.
* @return bool Whether the needle was found in the haystack.
*/
private function containsWord($haystack, $needle)
{
if (empty($haystack) || empty($needle)) {
return false;
}
return preg_match('/(^|\s)' . preg_quote($needle, '/') . '(\s|$)/i', $haystack);
}
/**
* Check whether a given resource hint link element is pointing to an AMP resource.
*
* @param Element $node Link element to check.
* @return bool Whether the link element is pointing to an AMP resource.
*/
private function isHintForAmp(Element $node)
{
$href = $node->getAttribute(Attribute::HREF);
if (empty($href)) {
return false;
}
return (bool)preg_match(self::AMP_RESOURCE_HINT_SRC_PATTERN, $href);
}
}