-
Notifications
You must be signed in to change notification settings - Fork 28
Ec36 avoid autoplay #40
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
Merged
utarwyn
merged 5 commits into
green-code-initiative:main
from
chihebtalan:ec36-avoid-autoplay
Jan 27, 2025
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5a3b699
Add rule "Avoid autoplay"
chihebtalan 229c8e2
Add rule "Avoid autoplay"
chihebtalan 752d60c
Add rule "Avoid autoplay"
chihebtalan 7f91907
Merge branch 'main' into ec36-avoid-autoplay
utarwyn 593f1f1
Apply some PR feedbacks
utarwyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Disallow autoplay and enforce preload='none' for video and audio elements (`@ecocode/avoid-autoplay`) | ||
|
|
||
| ⚠️ This rule _warns_ in the ✅ `recommended` config. | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| ⚠️ This rule _warns_ in the ✅ `recommended` config. | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
utarwyn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Why is this an issue? | ||
|
|
||
| Automatic videos and audio files activation (autoplay) during web pages loading uses resources on each tier (device, | ||
| network, data center). In many cases, automatic playback is not necessary. Moreover, it can draw users' attention and | ||
| distract them from the initially requested service. Therefore, whenever possible, these playbacks should be initiated by | ||
| the users and by not using the autoplay attributes in the `<audio>` and `<video>` elements. | ||
|
|
||
| Nevertheless, some parts of the video or audio files may be downloaded even if autoplay is not activated. Moreover, data | ||
| will be unnecessarily downloaded even if users do not start the video playback. It is therefore necessary to force | ||
| browsers not to preload anything by setting the `preload` attribute to `none`. | ||
|
|
||
| ```jsx | ||
| return ( | ||
| <> | ||
| <video src="video.mp4" autoplay/> // Non-compliant | ||
| <video src="video.mp4" preload="auto"/> // Non-compliant | ||
| <video src="video.mp4" autoplay preload="auto"/> // Non-compliant | ||
| <video src="video.mp4" preload="none"/> // Compliant | ||
| </> | ||
| ) | ||
| ``` | ||
|
|
||
| This rule is build for [React](https://react.dev/) and JSX. | ||
|
|
||
| ## Resources | ||
|
|
||
| ### Documentation | ||
|
|
||
| - [Mozilla Web Technology for Developers](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/autoplay) - | ||
| Autoplay in HTML | ||
| - [Mozilla Web Technology for Developers](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video) - Video and | ||
| audio content | ||
| - [Mozilla Web Technology for Developers](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#attr-preload) - | ||
| Preload in HTML | ||
| - [W3C](https://w3c.github.io/sustyweb/star.html#UX16-1) - Autoplay attribute | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * ecoCode JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs | ||
| * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| "use strict"; | ||
|
|
||
| /** @type {import("eslint").Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: "suggestion", | ||
| docs: { | ||
| description: | ||
| "Disallow autoplay and enforce preload='none' for video and audio elements", | ||
| category: "eco-design", | ||
| recommended: "warn", | ||
| }, | ||
| messages: { | ||
| NoAutoplay: | ||
| "Avoid using autoplay attribute for <video> and <audio> elements. Reference to Rule RGESN 4.1 : https://www.arcep.fr/mes-demarches-et-services/entreprises/fiches-pratiques/referentiel-general-ecoconception-services-numeriques.html", | ||
| EnforcePreloadNone: | ||
| "Set preload='none' for <video> and <audio> elements. Reference to Rule RGESN 4.1 : https://www.arcep.fr/mes-demarches-et-services/entreprises/fiches-pratiques/referentiel-general-ecoconception-services-numeriques.html", | ||
| NoAutoplay_EnforcePreloadNone: | ||
| "Avoid using autoplay attribute and set preload='none' for <video> and <audio> elements. Reference to Rule RGESN 4.1 : https://www.arcep.fr/mes-demarches-et-services/entreprises/fiches-pratiques/referentiel-general-ecoconception-services-numeriques.html ", | ||
utarwyn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| schema: [], | ||
| }, | ||
| create(context) { | ||
| return { | ||
| JSXOpeningElement(node) { | ||
| if (node.name.name === "video" || node.name.name === "audio") { | ||
| const autoplayAttr = node.attributes.find( | ||
| (attr) => attr.name.name === "autoplay", | ||
| ); | ||
| const preloadAttr = node.attributes.find( | ||
| (attr) => attr.name.name === "preload", | ||
| ); | ||
| if ( | ||
| autoplayAttr && | ||
| (!preloadAttr || preloadAttr.value.value !== "none") | ||
| ) { | ||
| context.report({ | ||
| node: autoplayAttr || preloadAttr, | ||
| messageId: "NoAutoplay_EnforcePreloadNone", | ||
| }); | ||
| } else { | ||
| if (autoplayAttr) { | ||
| context.report({ | ||
| node: autoplayAttr, | ||
| messageId: "NoAutoplay", | ||
| }); | ||
| } | ||
|
|
||
| if (!preloadAttr || preloadAttr.value.value !== "none") { | ||
| context.report({ | ||
| node: preloadAttr || node, | ||
| messageId: "EnforcePreloadNone", | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * ecoCode JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs | ||
| * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| "use strict"; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Requirements | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const rule = require("../../../lib/rules/avoid-autoplay"); | ||
| const RuleTester = require("eslint").RuleTester; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Tests | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parserOptions: { | ||
| ecmaVersion: 2021, | ||
| sourceType: "module", | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| }, | ||
| }); | ||
| const expectedError1 = { | ||
| messageId: "NoAutoplay", | ||
| type: "JSXAttribute", | ||
| }; | ||
| const expectedError2 = { | ||
| messageId: "EnforcePreloadNone", | ||
| type: "JSXAttribute", | ||
| }; | ||
| const expectedError3 = { | ||
utarwyn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| messageId: "NoAutoplay_EnforcePreloadNone", | ||
| type: "JSXAttribute", | ||
| }; | ||
|
|
||
| ruleTester.run("autoplay-audio-video-attribute-not-present", rule, { | ||
| valid: [ | ||
| ` | ||
| <audio preload="none"></audio> | ||
| `, | ||
| '<video preload="none"></video>', | ||
| ], | ||
|
|
||
| invalid: [ | ||
| { | ||
| code: ` | ||
| <audio autoplay></audio> | ||
| `, | ||
| errors: [expectedError3], | ||
| }, | ||
| { | ||
| code: ` | ||
| <video autoplay preload="auto"></video> | ||
| `, | ||
| errors: [expectedError3], | ||
| }, | ||
| { | ||
| code: '<video autoplay preload="none"></video>', | ||
| errors: [expectedError1], | ||
| }, | ||
| { | ||
| code: '<audio preload="auto"></audio>', | ||
| errors: [expectedError2], | ||
| }, | ||
| ], | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
sonar-plugin/src/main/java/io/ecocode/javascript/checks/AvoidAutoPlay.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * ecoCode JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs | ||
| * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package io.ecocode.javascript.checks; | ||
|
|
||
| import org.sonar.check.Rule; | ||
| import org.sonar.plugins.javascript.api.EslintBasedCheck; | ||
| import org.sonar.plugins.javascript.api.JavaScriptRule; | ||
| import org.sonar.plugins.javascript.api.TypeScriptRule; | ||
| @JavaScriptRule | ||
| @TypeScriptRule | ||
| @Rule(key = AvoidAutoPlay.RULE_KEY) | ||
| public class AvoidAutoPlay implements EslintBasedCheck { | ||
| public static final String RULE_KEY = "EC36"; | ||
|
|
||
| @Override | ||
| public String eslintKey() { | ||
| return "@ecocode/avoid-autoplay"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| "EC25", | ||
| "EC26", | ||
| "EC29", | ||
| "EC30" | ||
| "EC30", | ||
| "EC36" | ||
| ] | ||
| } | ||
3 changes: 2 additions & 1 deletion
3
sonar-plugin/src/main/resources/io/ecocode/profiles/ecocode_typescript_profile.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| { | ||
| "name": "ecoCode", | ||
| "ruleKeys": [ | ||
| "EC13" | ||
| "EC13", | ||
| "EC36" | ||
utarwyn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ] | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
sonar-plugin/src/main/resources/io/ecocode/rules/javascript/EC36.html
utarwyn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Title</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| </body> | ||
| </html> |
22 changes: 22 additions & 0 deletions
22
sonar-plugin/src/main/resources/io/ecocode/rules/javascript/EC36.json
utarwyn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "title": "Avoid autoplay for videos and audio content", | ||
| "type": "CODE_SMELL", | ||
| "code": { | ||
| "impacts": { | ||
| "MAINTAINABILITY": "MEDIUM" | ||
| }, | ||
| "attribute": "EFFICIENT" | ||
| }, | ||
| "status": "ready", | ||
| "remediation": { | ||
| "func": "Constant\/Issue", | ||
| "constantCost": "5min" | ||
| }, | ||
| "tags": [ | ||
| "ecocode", | ||
| "eco-design", | ||
| "video", | ||
| "audio" | ||
| ], | ||
| "defaultSeverity": "Major" | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.