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

Add amp-img srcset linter check #926

Merged
merged 4 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/linter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { HeroImageIsDefined } from "./rules/HeroImageIsDefined";
import { FastGoogleFontsDisplay } from "./rules/FastGoogleFontsDisplay";
import { GoogleFontPreconnect } from "./rules/GoogleFontPreconnect";
import { BoilerplateIsRemoved } from "./rules/BoilerplateIsRemoved";
import { AmpImgUsesSrcSet } from './rules/AmpImgUsesSrcSet';
import { RuleConstructor } from "./rule";
import { isArray } from "util";

Expand Down Expand Up @@ -142,6 +143,7 @@ function testsForMode(type: LintMode) {
BoilerplateIsRemoved,
ModuleRuntimeUsed,
HeroImageIsDefined,
AmpImgUsesSrcSet,
])
);
return tests.get(type) || [];
Expand Down
35 changes: 35 additions & 0 deletions packages/linter/src/rules/AmpImgUsesSrcSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Context } from "../index";
import { Rule } from "../rule";

const SVG_URL_PATTERN = /^[^?]+\.svg(\?.*)?$/i

export class AmpImgUsesSrcSet extends Rule {
async run(context: Context) {
const $ = context.$;

const incorrectImages = $("amp-img")
.filter((_, e) => {
const src = $(e).attr("src");
const layout = $(e).attr("layout");
const srcset = $(e).attr("srcset");
return (
!SVG_URL_PATTERN.exec(src)
&& layout && layout !== 'fixed' && layout != 'fixed-height'
&& !srcset
)
});
if (incorrectImages.length > 0) {
return this.warn(
"Not all responsive <amp-img> define a srcset. Using AMP Optimizer might help."
);
}
return this.pass();
}
meta() {
return {
url: "https://amp.dev/documentation/guides-and-tutorials/optimize-and-measure/amp-optimizer-guide/explainer/?format=websites#image-optimization",
title: "Responsive <amp-img> uses srcset",
info: "",
};
}
}
48 changes: 48 additions & 0 deletions packages/linter/tests/local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { HeroImageIsDefined } from "../src/rules/HeroImageIsDefined";
import { FastGoogleFontsDisplay } from "../src/rules/FastGoogleFontsDisplay";
import { GoogleFontPreconnect } from "../src/rules/GoogleFontPreconnect";
import { BoilerplateIsRemoved } from "../src/rules/BoilerplateIsRemoved";
import { AmpImgUsesSrcSet } from '../src/rules/AmpImgUsesSrcSet';

describe(AmpImgAmpPixelPreferred.name, () => {
it(`${AmpImgAmpPixelPreferred.name} - <amp-img height="1" width="1">`, async () => {
Expand Down Expand Up @@ -72,6 +73,53 @@ describe(AmpImgAmpPixelPreferred.name, () => {
});
});

describe(AmpImgUsesSrcSet.name, () => {
it(`${AmpImgUsesSrcSet.name} - responsive - no srcset`, async () => {
return assertWarn(
runLocalTest(
AmpImgUsesSrcSet,
`${__dirname}/local/AmpImgUsesSrcSet-1/source.html`
)
);
});

it(`${AmpImgUsesSrcSet.name} - svg`, async () => {
return assertPass(
runLocalTest(
AmpImgUsesSrcSet,
`${__dirname}/local/AmpImgUsesSrcSet-2/source.html`
)
);
});

it(`${AmpImgUsesSrcSet.name} - fixed`, async () => {
return assertPass(
runLocalTest(
AmpImgUsesSrcSet,
`${__dirname}/local/AmpImgUsesSrcSet-3/source.html`
)
);
});

it(`${AmpImgUsesSrcSet.name} - fixed-height`, async () => {
return assertPass(
runLocalTest(
AmpImgUsesSrcSet,
`${__dirname}/local/AmpImgUsesSrcSet-4/source.html`
)
);
});

it(`${AmpImgUsesSrcSet.name} - srcset`, async () => {
return assertPass(
runLocalTest(
AmpImgUsesSrcSet,
`${__dirname}/local/AmpImgUsesSrcSet-5/source.html`
)
);
});
});

describe(BoilerplateIsRemoved.name, () => {
it(`${BoilerplateIsRemoved.name} - boilerplate removed`, async () => {
return assertPass(
Expand Down
20 changes: 20 additions & 0 deletions packages/linter/tests/local/AmpImgUsesSrcSet-1/source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
</style>
</head>
<body>
<amp-img src="/img/amp_by_example_logo.png"
width="100"
height="100"
layout="responsive"
></amp-img>
</body>
</html>
20 changes: 20 additions & 0 deletions packages/linter/tests/local/AmpImgUsesSrcSet-2/source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
</style>
</head>
<body>
<amp-img src="/img/amp_by_example_logo.svg"
width="100"
height="100"
layout="responsive"
></amp-img>
</body>
</html>
20 changes: 20 additions & 0 deletions packages/linter/tests/local/AmpImgUsesSrcSet-3/source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
</style>
</head>
<body>
<amp-img src="/img/amp_by_example_logo.png"
width="100"
height="100"
layout="fixed"
></amp-img>
</body>
</html>
19 changes: 19 additions & 0 deletions packages/linter/tests/local/AmpImgUsesSrcSet-4/source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
</style>
</head>
<body>
<amp-img src="/img/amp_by_example_logo.png"
height="100"
layout="fixed-height"
></amp-img>
</body>
</html>
22 changes: 22 additions & 0 deletions packages/linter/tests/local/AmpImgUsesSrcSet-5/source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
</style>
</head>
<body>
<amp-img src="/img/test.png"
srcset="/img/test.png 1800w,/img/test_1200.png 1200w,/img/test_600.png 600w,/img/test_300.png 300w"
width="1800"
height="1000"
layout="responsive"
>
</amp-img>
</body>
</html>