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

core(meta): refactor all meta audits to single artifact #7025

Merged
merged 4 commits into from
Jan 17, 2019
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: 1 addition & 1 deletion lighthouse-cli/test/fixtures/seo/seo-failure-cases.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<!-- FAIL(font-size): missing viewport -->
<meta name="viewport" content="invalid-content=should_have_looked_it_up">
<!-- no <meta name="description" content=""> -->
<meta name="robots" content="nofollow, NOINDEX, all">
<meta name="RoBots" content="nofollow, NOINDEX, all">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document the reason for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

<!-- FAIL(hreflang): invalid language code -->
<link rel="alternate" hreflang="xx" href="https://xx.example.com" />
<!-- FAIL(hreflang): spece before a valid code -->
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/audits/seo/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class FontSize extends Audit {
title: str_(UIStrings.title),
failureTitle: str_(UIStrings.failureTitle),
description: str_(UIStrings.description),
requiredArtifacts: ['FontSize', 'URL', 'Viewport'],
requiredArtifacts: ['FontSize', 'URL', 'MetaElements'],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no changes to functionality needed here because it just calls out to the viewport audit

};
}

Expand Down
10 changes: 6 additions & 4 deletions lighthouse-core/audits/seo/is-crawlable.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class IsCrawlable extends Audit {
description: 'Search engines are unable to include your pages in search results ' +
'if they don\'t have permission to crawl them. [Learn ' +
'more](https://developers.google.com/web/tools/lighthouse/audits/indexing).',
requiredArtifacts: ['MetaRobots', 'RobotsTxt', 'URL'],
requiredArtifacts: ['MetaElements', 'RobotsTxt', 'URL'],
};
}

Expand All @@ -80,20 +80,22 @@ class IsCrawlable extends Audit {
*/
static audit(artifacts, context) {
const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
const metaRobots = artifacts.MetaElements.find(meta => meta.name === 'robots');

return MainResource.request({devtoolsLog, URL: artifacts.URL}, context)
.then(mainResource => {
/** @type {Array<Object<string, LH.Audit.DetailsItem>>} */
const blockingDirectives = [];

if (artifacts.MetaRobots) {
const isBlocking = hasBlockingDirective(artifacts.MetaRobots);
if (metaRobots) {
const metaRobotsContent = metaRobots.content || '';
const isBlocking = hasBlockingDirective(metaRobotsContent);

if (isBlocking) {
blockingDirectives.push({
source: {
type: /** @type {'node'} */ ('node'),
snippet: `<meta name="robots" content="${artifacts.MetaRobots}" />`,
snippet: `<meta name="robots" content="${metaRobotsContent}" />`,
},
});
}
Expand Down
8 changes: 5 additions & 3 deletions lighthouse-core/audits/seo/meta-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Description extends Audit {
description: 'Meta descriptions may be included in search results to concisely summarize ' +
'page content. ' +
'[Learn more](https://developers.google.com/web/tools/lighthouse/audits/description).',
requiredArtifacts: ['MetaDescription'],
requiredArtifacts: ['MetaElements'],
};
}

Expand All @@ -28,13 +28,15 @@ class Description extends Audit {
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
if (artifacts.MetaDescription === null) {
const metaDescription = artifacts.MetaElements.find(meta => meta.name === 'description');
if (!metaDescription) {
return {
rawValue: false,
};
}

if (artifacts.MetaDescription.trim().length === 0) {
const description = metaDescription.content || '';
if (description.trim().length === 0) {
return {
rawValue: false,
explanation: 'Description text is empty.',
Expand Down
13 changes: 7 additions & 6 deletions lighthouse-core/audits/themed-omnibox.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ThemedOmnibox extends MultiCheckAudit {
failureTitle: 'Does not set an address-bar theme color',
description: 'The browser address bar can be themed to match your site. ' +
'[Learn more](https://developers.google.com/web/tools/lighthouse/audits/address-bar).',
requiredArtifacts: ['Manifest', 'ThemeColor'],
requiredArtifacts: ['Manifest', 'MetaElements'],
};
}

Expand All @@ -43,13 +43,13 @@ class ThemedOmnibox extends MultiCheckAudit {
}

/**
* @param {LH.Artifacts['ThemeColor']} themeColorMeta
* @param {LH.Artifacts['MetaElements'][0]|undefined} themeColorMeta
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we make an alias of LH.Artifacts.MetaElement since we use this so often?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only usage of it, but sure :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only usage of it, but sure

ha, whoops

* @param {Array<string>} failures
*/
static assessMetaThemecolor(themeColorMeta, failures) {
if (themeColorMeta === null) {
if (!themeColorMeta) {
failures.push('No `<meta name="theme-color">` tag found');
} else if (!ThemedOmnibox.isValidColor(themeColorMeta)) {
} else if (!ThemedOmnibox.isValidColor(themeColorMeta.content || '')) {
failures.push('The theme-color meta tag did not contain a valid CSS color');
}
}
Expand Down Expand Up @@ -79,14 +79,15 @@ class ThemedOmnibox extends MultiCheckAudit {
/** @type {Array<string>} */
const failures = [];

const themeColorMeta = artifacts.MetaElements.find(meta => meta.name === 'theme-color');
const manifestValues = await ManifestValues.request(artifacts.Manifest, context);
ThemedOmnibox.assessManifest(manifestValues, failures);
ThemedOmnibox.assessMetaThemecolor(artifacts.ThemeColor, failures);
ThemedOmnibox.assessMetaThemecolor(themeColorMeta, failures);

return {
failures,
manifestValues,
themeColor: artifacts.ThemeColor,
themeColor: (themeColorMeta && themeColorMeta.content) || null,
};
}
}
Expand Down
7 changes: 4 additions & 3 deletions lighthouse-core/audits/viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Viewport extends Audit {
'or `initial-scale`',
description: 'Add a viewport meta tag to optimize your app for mobile screens. ' +
'[Learn more](https://developers.google.com/web/tools/lighthouse/audits/has-viewport-meta-tag).',
requiredArtifacts: ['Viewport'],
requiredArtifacts: ['MetaElements'],
};
}

Expand All @@ -29,15 +29,16 @@ class Viewport extends Audit {
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
if (artifacts.Viewport === null) {
const viewportMeta = artifacts.MetaElements.find(meta => meta.name === 'viewport');
if (!viewportMeta) {
return {
explanation: 'No viewport meta tag found',
rawValue: false,
};
}

const warnings = [];
const parsedProps = Parser.parseMetaViewPortContent(artifacts.Viewport);
const parsedProps = Parser.parseMetaViewPortContent(viewportMeta.content || '');

if (Object.keys(parsedProps.unknownProperties).length) {
warnings.push(`Invalid properties found: ${JSON.stringify(parsedProps.unknownProperties)}`);
Expand Down
5 changes: 1 addition & 4 deletions lighthouse-core/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,14 @@ const defaultConfig = {
gatherers: [
'scripts',
'css-usage',
'viewport',
'viewport-dimensions',
'theme-color',
'manifest',
'runtime-exceptions',
'chrome-console-messages',
'image-usage',
'accessibility',
'link-elements',
'meta-elements',
'dobetterweb/anchors-with-no-rel-noopener',
'dobetterweb/appcache',
'dobetterweb/doctype',
Expand All @@ -104,10 +103,8 @@ const defaultConfig = {
'dobetterweb/password-inputs-with-prevented-paste',
'dobetterweb/response-compression',
'dobetterweb/tags-blocking-first-paint',
'seo/meta-description',
'seo/font-size',
'seo/crawlable-links',
'seo/meta-robots',
'seo/hreflang',
'seo/embedded-content',
'seo/canonical',
Expand Down
34 changes: 34 additions & 0 deletions lighthouse-core/gather/gatherers/meta-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const Gatherer = require('./gatherer.js');
const getElementsInDocumentString = require('../../lib/page-functions.js').getElementsInDocumentString; // eslint-disable-line max-len

class MetaElements extends Gatherer {
/**
* @param {LH.Gatherer.PassContext} passContext
* @return {Promise<LH.Artifacts['MetaElements']>}
*/
async afterPass(passContext) {
const driver = passContext.driver;

// We'll use evaluateAsync because the `node.getAttribute` method doesn't actually normalize
// the values like access from JavaScript does.
return driver.evaluateAsync(`(() => {
${getElementsInDocumentString};

return getElementsInDocument('head meta').map(meta => {
return {
name: meta.name.toLowerCase(),
content: meta.content,
};
});
})()`, {useIsolation: true});
}
}

module.exports = MetaElements;
24 changes: 0 additions & 24 deletions lighthouse-core/gather/gatherers/seo/meta-description.js

This file was deleted.

23 changes: 0 additions & 23 deletions lighthouse-core/gather/gatherers/seo/meta-robots.js

This file was deleted.

23 changes: 0 additions & 23 deletions lighthouse-core/gather/gatherers/theme-color.js

This file was deleted.

23 changes: 0 additions & 23 deletions lighthouse-core/gather/gatherers/viewport.js

This file was deleted.

20 changes: 11 additions & 9 deletions lighthouse-core/test/audits/seo/font-size-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ const validViewport = 'width=device-width';
/* eslint-env jest */

describe('SEO: Font size audit', () => {
const makeMetaElements = viewport => viewport ? [{name: 'viewport', content: viewport}] : [];

it('fails when viewport is not set', () => {
const artifacts = {
URL,
Viewport: null,
MetaElements: makeMetaElements(null),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the conditional in makeMetaElements just for this case? null could just be undefined or this one could just be [] an no conditional needed :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, done

FontSize: [],
};

Expand All @@ -29,7 +31,7 @@ describe('SEO: Font size audit', () => {
it('fails when less than 60% of text is legible', () => {
const artifacts = {
URL,
Viewport: validViewport,
MetaElements: makeMetaElements(validViewport),
FontSize: {
totalTextLength: 100,
visitedTextLength: 100,
Expand All @@ -51,7 +53,7 @@ describe('SEO: Font size audit', () => {
it('passes when there is no text', () => {
const artifacts = {
URL,
Viewport: validViewport,
MetaElements: makeMetaElements(validViewport),
FontSize: {
totalTextLength: 0,
visitedTextLength: 0,
Expand All @@ -70,7 +72,7 @@ describe('SEO: Font size audit', () => {
it('passes when more than 60% of text is legible', () => {
const artifacts = {
URL,
Viewport: validViewport,
MetaElements: makeMetaElements(validViewport),
FontSize: {
totalTextLength: 330,
visitedTextLength: 330,
Expand Down Expand Up @@ -106,7 +108,7 @@ describe('SEO: Font size audit', () => {
};
const artifacts = {
URL,
Viewport: validViewport,
MetaElements: makeMetaElements(validViewport),
FontSize: {
totalTextLength: 7,
visitedTextLength: 7,
Expand All @@ -130,7 +132,7 @@ describe('SEO: Font size audit', () => {
it('adds a category for failing text that wasn\'t analyzed', () => {
const artifacts = {
URL,
Viewport: validViewport,
MetaElements: makeMetaElements(validViewport),
FontSize: {
totalTextLength: 100,
visitedTextLength: 100,
Expand All @@ -152,7 +154,7 @@ describe('SEO: Font size audit', () => {
it('informs user if audit haven\'t covered all text on the page', () => {
const artifacts = {
URL,
Viewport: validViewport,
MetaElements: makeMetaElements(validViewport),
FontSize: {
totalTextLength: 100,
visitedTextLength: 50,
Expand All @@ -172,7 +174,7 @@ describe('SEO: Font size audit', () => {
it('maintains 2 trailing decimal places', () => {
const artifacts = {
URL,
Viewport: validViewport,
MetaElements: makeMetaElements(validViewport),
FontSize: {
totalTextLength: 323,
visitedTextLength: 323,
Expand All @@ -191,7 +193,7 @@ describe('SEO: Font size audit', () => {
it('maintains 2 trailing decimal places with only 1 leading digit', () => {
const artifacts = {
URL,
Viewport: validViewport,
MetaElements: makeMetaElements(validViewport),
FontSize: {
totalTextLength: 323,
visitedTextLength: 323,
Expand Down
Loading