Skip to content

Commit b4660de

Browse files
committed
Improve normalization of crossorigin attribute value
1 parent d7d24b4 commit b4660de

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ final class Image_Prioritizer_Img_Tag_Visitor extends Image_Prioritizer_Tag_Visi
2828
* @since n.e.x.t Separate the processing of IMG and PICTURE elements.
2929
*
3030
* @param OD_Tag_Visitor_Context $context Tag visitor context.
31-
*
3231
* @return bool Whether the tag should be tracked in URL Metrics.
3332
*/
3433
public function __invoke( OD_Tag_Visitor_Context $context ): bool {
@@ -50,8 +49,7 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
5049
* @since n.e.x.t
5150
*
5251
* @param OD_HTML_Tag_Processor $processor HTML tag processor.
53-
* @param OD_Tag_Visitor_Context $context Tag visitor context.
54-
*
52+
* @param OD_Tag_Visitor_Context $context Tag visitor context.
5553
* @return bool Whether the tag should be tracked in URL Metrics.
5654
*/
5755
private function process_img( OD_HTML_Tag_Processor $processor, OD_Tag_Visitor_Context $context ): bool {
@@ -189,8 +187,7 @@ private function process_img( OD_HTML_Tag_Processor $processor, OD_Tag_Visitor_C
189187
* @since n.e.x.t
190188
*
191189
* @param OD_HTML_Tag_Processor $processor HTML tag processor.
192-
* @param OD_Tag_Visitor_Context $context Tag visitor context.
193-
*
190+
* @param OD_Tag_Visitor_Context $context Tag visitor context.
194191
* @return bool Whether the tag should be tracked in URL Metrics.
195192
*/
196193
private function process_picture( OD_HTML_Tag_Processor $processor, OD_Tag_Visitor_Context $context ): bool {
@@ -200,21 +197,21 @@ private function process_picture( OD_HTML_Tag_Processor $processor, OD_Tag_Visit
200197
$referrerpolicy = null;
201198
$crossorigin = null;
202199

203-
// Loop through child tags until we reach the closing </picture> tag.
200+
// Loop through child tags until we reach the closing PICTURE tag.
204201
while ( $processor->next_tag() ) {
205202
$tag = $processor->get_tag();
206203

207-
// If we reached the closing </picture> tag, break.
204+
// If we reached the closing PICTURE tag, break.
208205
if ( 'PICTURE' === $tag && $processor->is_tag_closer() ) {
209206
break;
210207
}
211208

212-
// Collect <source> elements.
209+
// Collect SOURCE elements.
213210
if ( 'SOURCE' === $tag && ! $processor->is_tag_closer() ) {
214211
$media = $processor->get_attribute( 'media' );
215212
$type = $processor->get_attribute( 'type' );
216213

217-
// Ensure that all <source> elements have a type attribute and no media attribute.
214+
// Ensure that all SOURCE elements have a type attribute and no media attribute.
218215
if ( null !== $media || null === $type ) {
219216
return false;
220217
}

plugins/image-prioritizer/class-image-prioritizer-tag-visitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function get_attribute_value( OD_HTML_Tag_Processor $processor, string
6161
if ( is_string( $value ) ) {
6262
$value = strtolower( trim( $value, " \t\f\r\n" ) );
6363
}
64-
if ( 'crossorigin' === $attribute_name && ( true === $value || '' === trim( $value ) ) ) {
64+
if ( 'crossorigin' === $attribute_name && 'use-credentials' !== $value ) {
6565
$value = 'anonymous';
6666
}
6767
return $value;

plugins/image-prioritizer/tests/test-cases/different-lcp-elements-for-all-breakpoints.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
return array(
33
'set_up' => static function ( Test_Image_Prioritizer_Helper $test_case ): void {
4-
$breakpoint_max_widths = array( 480, 600, 782 );
4+
$breakpoint_max_widths = array( 480, 600, 782, 1000 );
55

66
add_filter(
77
'od_breakpoint_max_widths',
@@ -28,6 +28,10 @@ static function () use ( $breakpoint_max_widths ) {
2828
'isLCP' => false,
2929
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[4][self::IMG]',
3030
),
31+
array(
32+
'isLCP' => false,
33+
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::IMG]',
34+
),
3135
);
3236
$elements[ $i ]['isLCP'] = true;
3337
OD_URL_Metrics_Post_Type::store_url_metric(
@@ -52,6 +56,7 @@ static function () use ( $breakpoint_max_widths ) {
5256
<img src="https://example.com/phablet-logo.png" alt="Phablet Logo" width="600" height="600" crossorigin="" referrerpolicy="no-referrer">
5357
<img src="https://example.com/tablet-logo.png" alt="Tablet Logo" width="600" height="600" crossorigin="anonymous" referrerpolicy="no-referrer-when-downgrade">
5458
<img src="https://example.net/desktop-logo.png" alt="Desktop Logo" width="600" height="600" crossorigin="use-credentials" referrerpolicy="origin-when-cross-origin">
59+
<img src="https://example.net/ultra-desktop-logo.png" alt="Desktop Logo" width="600" height="600" crossorigin=" something-custom " referrerpolicy="same-origin">
5560
</body>
5661
</html>
5762
',
@@ -63,13 +68,15 @@ static function () use ( $breakpoint_max_widths ) {
6368
<link data-od-added-tag rel="preload" fetchpriority="high" as="image" href="https://example.com/mobile-logo.png" crossorigin="anonymous" media="screen and (max-width: 480px)">
6469
<link data-od-added-tag rel="preload" fetchpriority="high" as="image" href="https://example.com/phablet-logo.png" crossorigin="anonymous" referrerpolicy="no-referrer" media="screen and (min-width: 481px) and (max-width: 600px)">
6570
<link data-od-added-tag rel="preload" fetchpriority="high" as="image" href="https://example.com/tablet-logo.png" crossorigin="anonymous" referrerpolicy="no-referrer-when-downgrade" media="screen and (min-width: 601px) and (max-width: 782px)">
66-
<link data-od-added-tag rel="preload" fetchpriority="high" as="image" href="https://example.net/desktop-logo.png" crossorigin="use-credentials" referrerpolicy="origin-when-cross-origin" media="screen and (min-width: 783px)">
71+
<link data-od-added-tag rel="preload" fetchpriority="high" as="image" href="https://example.net/desktop-logo.png" crossorigin="use-credentials" referrerpolicy="origin-when-cross-origin" media="screen and (min-width: 783px) and (max-width: 1000px)">
72+
<link data-od-added-tag rel="preload" fetchpriority="high" as="image" href="https://example.net/ultra-desktop-logo.png" crossorigin="anonymous" referrerpolicy="same-origin" media="screen and (min-width: 1001px)">
6773
</head>
6874
<body>
6975
<img data-od-xpath="/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]" src="https://example.com/mobile-logo.png" alt="Mobile Logo" width="600" height="600" crossorigin>
7076
<img data-od-xpath="/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]" src="https://example.com/phablet-logo.png" alt="Phablet Logo" width="600" height="600" crossorigin="" referrerpolicy="no-referrer">
7177
<img data-od-xpath="/*[1][self::HTML]/*[2][self::BODY]/*[3][self::IMG]" src="https://example.com/tablet-logo.png" alt="Tablet Logo" width="600" height="600" crossorigin="anonymous" referrerpolicy="no-referrer-when-downgrade">
7278
<img data-od-xpath="/*[1][self::HTML]/*[2][self::BODY]/*[4][self::IMG]" src="https://example.net/desktop-logo.png" alt="Desktop Logo" width="600" height="600" crossorigin="use-credentials" referrerpolicy="origin-when-cross-origin">
79+
<img data-od-xpath="/*[1][self::HTML]/*[2][self::BODY]/*[5][self::IMG]" src="https://example.net/ultra-desktop-logo.png" alt="Desktop Logo" width="600" height="600" crossorigin=" something-custom " referrerpolicy="same-origin">
7380
<script type="module">/* import detect ... */</script>
7481
</body>
7582
</html>

0 commit comments

Comments
 (0)