Skip to content
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

Remove loading=lazy attribute from iframes #3941

Merged
merged 3 commits into from
Dec 17, 2019
Merged
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
12 changes: 11 additions & 1 deletion includes/sanitizers/class-amp-iframe-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private function normalize_attributes( $attributes ) {

case 'allowfullscreen':
case 'allowtransparency':
if ( 'false' !== $value ) {
if ( 'false' !== strtolower( $value ) ) {
$out[ $name ] = '';
}
break;
Expand All @@ -229,6 +229,16 @@ private function normalize_attributes( $attributes ) {
// Omit these since amp-iframe will add them if needed if the `allowfullscreen` attribute is present.
break;

case 'loading':
/*
* The `amp-iframe` component already does lazy-loading by default; trigger a validation error only
* if the value is not `lazy`.
*/
if ( 'lazy' !== strtolower( $value ) ) {
$out[ $name ] = $value;
}
break;

default:
$out[ $name ] = $value;
break;
Expand Down
27 changes: 22 additions & 5 deletions tests/php/test-amp-iframe-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function get_data() {
],

'simple_iframe_without_noscript_or_placeholder' => [
'<iframe src="https://example.com/embed/132886713" width="500" height="281" frameborder="0" class="iframe-class" allowtransparency="false" allowfullscreen></iframe>',
'<iframe src="https://example.com/embed/132886713" width="500" height="281" frameborder="0" class="iframe-class" allowtransparency="FALSE" allowfullscreen></iframe>',
'<amp-iframe src="https://example.com/embed/132886713" width="500" height="281" frameborder="0" class="iframe-class amp-wp-enforced-sizes" allowfullscreen="" sandbox="allow-scripts allow-same-origin" layout="intrinsic"></amp-iframe>',
[
'add_noscript_fallback' => false,
Expand Down Expand Up @@ -288,7 +288,7 @@ public function get_data() {
'attributes_removed_from_noscript_iframe' => [
'<iframe src="https://example.com/embed/132886713" width="500" height="281" onclick="foo()" data-foo="bar"></iframe>',
'
<amp-iframe src="https://example.com/embed/132886713" width="500" height="281" data-foo="bar" sandbox="allow-scripts allow-same-origin" layout="intrinsic" class="amp-wp-enforced-sizes">
<amp-iframe src="https://example.com/embed/132886713" width="500" height="281" onclick="foo()" data-foo="bar" sandbox="allow-scripts allow-same-origin" layout="intrinsic" class="amp-wp-enforced-sizes">
<span placeholder="" class="amp-wp-iframe-placeholder"></span>
<noscript>
<iframe src="https://example.com/embed/132886713" width="500" height="281"></iframe>
Expand Down Expand Up @@ -410,6 +410,26 @@ public function get_data() {
</noscript>
</amp-iframe>',
],

'iframe_with_loading_lazy_attr' => [
'<iframe loading="lazy" src="https://example.com" width="320" height="640"></iframe>',
'
<amp-iframe src="https://example.com" width="320" height="640" sandbox="allow-scripts allow-same-origin" layout="intrinsic" class="amp-wp-enforced-sizes">
<noscript>
<iframe src="https://example.com" width="320" height="640"></iframe>
</noscript>
</amp-iframe>',
],

'iframe_with_loading_eager_attr' => [
'<iframe loading="eager" src="https://example.com" width="320" height="640"></iframe>',
'
<amp-iframe loading="eager" src="https://example.com" width="320" height="640" sandbox="allow-scripts allow-same-origin" layout="intrinsic" class="amp-wp-enforced-sizes">
<noscript>
<iframe src="https://example.com" width="320" height="640"></iframe>
</noscript>
</amp-iframe>',
],
];
}

Expand All @@ -434,9 +454,6 @@ public function test_converter( $source, $expected = null, $args = [] ) {
$sanitizer = new AMP_Iframe_Sanitizer( $dom, $args );
$sanitizer->sanitize();

$whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( $dom );
$whitelist_sanitizer->sanitize();

$content = AMP_DOM_Utils::get_content_from_dom( $dom );
$this->assertEqualMarkup( $expected, $content );
}
Expand Down