Skip to content

Commit 5aff92b

Browse files
authored
Merge pull request #16497 from craftcms/feature/prevent-autoplay-new
Add AnimationBlocker to prevent autoplay of GIFs and other potentially animated filetypes
2 parents 6550875 + 67b4c00 commit 5aff92b

17 files changed

+444
-4
lines changed

CHANGELOG-WIP.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
### Accessibility
1111
- Improved the accessibility of Tags fields for screen readers. ([#16754](https://github.com/craftcms/cms/pull/16754))
1212
- Removed redundant ARIA roles and labels from reorder buttons. ([#16826](https://github.com/craftcms/cms/pull/16826))
13+
- Animated assets no longer have animated thumbnails. ([#16497](https://github.com/craftcms/cms/pull/16497))
1314

1415
### Content Management
1516
- Window scrolling is now blocked when a modal window is open. ([#16768](https://github.com/craftcms/cms/pull/16768))
@@ -47,6 +48,7 @@
4748
### Extensibility
4849
- Global nav items and breadcrumbs can now have `aria-label` attributes via an `ariaLabel` property.
4950
- Editable tables now support `icon` columns.
51+
- Added `craft\base\Element::couldHaveAnimatedThumb()`.
5052
- Added `craft\base\ElementInterface::baseGqlType()`.
5153
- Added `craft\base\ElementInterface::getSerializedFieldValuesForDb()`.
5254
- Added `craft\base\Field::EVENT_DEFINE_ACTION_MENU_ITEMS`. ([#16779](https://github.com/craftcms/cms/discussions/16779))

src/base/Element.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4184,6 +4184,7 @@ public function getThumbHtml(int $size): ?string
41844184
'sizes' => sprintf('calc(%srem/16)', $size),
41854185
'srcset' => sprintf('%s %sw, %s %sw', $thumbUrl, $size, $this->thumbUrl($size * 2), $size * 2),
41864186
'alt' => $this->thumbAlt(),
4187+
'animated' => $this->couldHaveAnimatedThumb(),
41874188
],
41884189
]);
41894190
}
@@ -4264,6 +4265,17 @@ protected function hasRoundedThumb(): bool
42644265
return false;
42654266
}
42664267

4268+
/**
4269+
* Returns whether the element’s thumbnail is potentially animated.
4270+
*
4271+
* @return boolean
4272+
* @since 5.7.0
4273+
*/
4274+
protected function couldHaveAnimatedThumb(): bool
4275+
{
4276+
return false;
4277+
}
4278+
42674279
/**
42684280
* @inheritdoc
42694281
*/

src/elements/Asset.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,6 +2353,9 @@ public function getPreviewThumbImg(int $desiredWidth, int $desiredHeight): strin
23532353
'sizes' => "{$thumbSizes[0][0]}px",
23542354
'srcset' => implode(', ', $srcsets),
23552355
'alt' => $this->thumbAlt(),
2356+
'data' => [
2357+
'animated' => $this->couldHaveAnimatedThumb(),
2358+
],
23562359
]);
23572360
}
23582361

@@ -2409,6 +2412,14 @@ public function getExtension(): string
24092412
return pathinfo($this->_filename, PATHINFO_EXTENSION);
24102413
}
24112414

2415+
/**
2416+
* @inheritdoc
2417+
*/
2418+
protected function couldHaveAnimatedThumb(): bool
2419+
{
2420+
return $this->getExtension() === 'gif' || $this->getExtension() === 'webp';
2421+
}
2422+
24122423
/**
24132424
* Returns the file’s MIME type, if it can be determined.
24142425
*
@@ -3392,6 +3403,7 @@ protected function htmlAttributes(string $context): array
33923403
'kind' => $this->kind,
33933404
'alt' => $this->alt,
33943405
'filename' => $this->filename,
3406+
'animated' => $this->couldHaveAnimatedThumb(),
33953407
],
33963408
];
33973409

src/translations/en/app.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,7 @@
11871187
'Password' => 'Password',
11881188
'Past year' => 'Past year',
11891189
'Past {num} days' => 'Past {num} days',
1190+
'Pause' => 'Pause',
11901191
'Pay {price}' => 'Pay {price}',
11911192
'Pending' => 'Pending',
11921193
'Perform Craft CMS and plugin updates' => 'Perform Craft CMS and plugin updates',
@@ -1201,6 +1202,7 @@
12011202
'Placeholder Text' => 'Placeholder Text',
12021203
'Plain Text' => 'Plain Text',
12031204
'Plane' => 'Plane',
1205+
'Play' => 'Play',
12041206
'Please fix the following in your {file} file before proceeding:' => 'Please fix the following in your {file} file before proceeding:',
12051207
'Please set a valid volume for storing the user photos in user settings page first.' => 'Please set a valid volume for storing the user photos in user settings page first.',
12061208
'Please talk to your host/IT department about upgrading your server.' => 'Please talk to your host/IT department about upgrading your server.',
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* @link https://craftcms.com/
4+
* @copyright Copyright (c) Pixel & Tonic, Inc.
5+
* @license https://craftcms.github.io/license/
6+
*/
7+
8+
namespace craft\web\assets\animationblocker;
9+
10+
use craft\web\AssetBundle;
11+
12+
/**
13+
* Asset bundle for the Animation Blocker class.
14+
*/
15+
class AnimationBlockerAsset extends AssetBundle
16+
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
public $sourcePath = __DIR__ . '/dist';
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public $js = [
26+
'AnimationBlocker.js',
27+
];
28+
}

src/web/assets/animationblocker/dist/AnimationBlocker.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/web/assets/animationblocker/dist/AnimationBlocker.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)