Skip to content

Commit

Permalink
new_audit(anchor-text-audit): descriptive anchor text audit (#3490)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdzwinel authored and paulirish committed Oct 19, 2017
1 parent da35caa commit f911e84
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lighthouse-cli/test/fixtures/seo/seo-failure-cases.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<!-- no <meta name="description" content=""> -->
</head>
<body>
bad crawlz
<h1>SEO</h1>

<h2>Anchor text</h2>
<a href='https://example.com'>click this</a>
<a href='/test.html'> click this </a>
<a href='/test.html'>CLICK THIS</a>
</body>
</html>
8 changes: 7 additions & 1 deletion lighthouse-cli/test/fixtures/seo/seo-tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
<meta name="description" content="The premiere destination for testing your SEO audit gathering">
</head>
<body>
crawlz me
<h1>SEO</h1>

<h2>Anchor text</h2>
<a href='https://example.com'>descriptive link</a>
<a href='#non-descriptive-local'>click this</a>
<a href='https://example.com/non-descriptive-no-follow.html' rel="nofollow">click this</a>
<a href='javascript:void(0);'>click this</a>
</body>
</html>
12 changes: 12 additions & 0 deletions lighthouse-cli/test/smokehouse/seo/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ module.exports = [
'http-status-code': {
score: true,
},
'link-text': {
score: true,
},
},
},
{
Expand All @@ -49,6 +52,15 @@ module.exports = [
score: false,
displayValue: '403',
},
'link-text': {
score: false,
displayValue: '3 links found',
details: {
items: {
length: 3,
},
},
},
},
},
];
76 changes: 76 additions & 0 deletions lighthouse-core/audits/seo/link-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @license Copyright 2017 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 Audit = require('../audit');
const URL = require('../../lib/url-shim');
const BLOCKLIST = new Set([
'click here',
'click this',
'go',
'here',
'this',
'start',
'right here',
'more',
'learn more',
]);

class LinkText extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
category: 'Content Best Practices',
name: 'link-text',
description: 'Links have descriptive text.',
failureDescription: 'Links do not have descriptive text',
helpText: 'Descriptive link text helps search engines understand your content. ' +
'[Learn more](https://webmasters.googleblog.com/2008/10/importance-of-link-architecture.html)',
requiredArtifacts: ['URL', 'CrawlableLinks'],
};
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
const failingLinks = artifacts.CrawlableLinks
.filter(link => {
if (
link.href.toLowerCase().startsWith('javascript:') ||
URL.equalWithExcludedFragments(link.href, artifacts.URL.finalUrl)
) {
return false;
}

return BLOCKLIST.has(link.text.trim().toLowerCase());
});

const headings = [
{key: 'href', itemType: 'url', text: 'Link destination'},
{key: 'text', itemType: 'text', text: 'Link Text'},
];

const details = Audit.makeTableDetails(headings, failingLinks);
let displayValue;

if (failingLinks.length) {
displayValue = failingLinks.length > 1 ?
`${failingLinks.length} links found` : '1 link found';
}

return {
rawValue: failingLinks.length === 0,
details,
displayValue,
};
}
}

module.exports = LinkText;
3 changes: 3 additions & 0 deletions lighthouse-core/config/seo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ module.exports = {
passName: 'defaultPass',
gatherers: [
'seo/meta-description',
'seo/crawlable-links',
],
}],
audits: [
'seo/meta-description',
'seo/http-status-code',
'seo/link-text',
],
groups: {
'seo-mobile': {
Expand All @@ -41,6 +43,7 @@ module.exports = {
{id: 'document-title', weight: 1, group: 'seo-content'},
{id: 'meta-description', weight: 1, group: 'seo-content'},
{id: 'http-status-code', weight: 1, group: 'seo-crawl'},
{id: 'link-text', weight: 1, group: 'seo-content'},
],
},
},
Expand Down
33 changes: 33 additions & 0 deletions lighthouse-core/gather/gatherers/seo/crawlable-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license Copyright 2017 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');
const DOMHelpers = require('../../../lib/dom-helpers.js');

class CrawlableLinks extends Gatherer {
/**
* @param {{driver: !Object}} options Run options
* @return {!Promise<!Array<{href: string, text: string}>>}
*/
afterPass(options) {
const expression = `(function() {
${DOMHelpers.getElementsInDocumentFnString}; // define function on page
const selector = 'a[href]:not([rel~="nofollow"])';
const elements = getElementsInDocument(selector);
return elements
.map(node => ({
href: node.href,
text: node.innerText
}));
})()`;

return options.driver.evaluateAsync(expression);
}
}

module.exports = CrawlableLinks;

82 changes: 82 additions & 0 deletions lighthouse-core/test/audits/seo/anchor-text-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @license Copyright 2017 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 LinkTextAudit = require('../../../audits/seo/link-text.js');
const assert = require('assert');

/* eslint-env mocha */

describe('SEO: link text audit', () => {
it('fails when link with non descriptive text is found', () => {
const invalidLink = {href: 'https://example.com/otherpage.html', text: 'click here'};
const artifacts = {
URL: {
finalUrl: 'https://example.com/page.html',
},
CrawlableLinks: [
{href: 'https://example.com/otherpage.html', text: 'legit link text'},
invalidLink,
{href: 'https://example.com/otherpage.html', text: 'legit link text'},
],
};

const auditResult = LinkTextAudit.audit(artifacts);
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.details.items.length, 1);
assert.equal(auditResult.details.items[0][0].text, invalidLink.href);
assert.equal(auditResult.details.items[0][1].text, invalidLink.text);
});

it('ignores links pointing to the main document', () => {
const artifacts = {
URL: {
finalUrl: 'https://example.com/page.html',
},
CrawlableLinks: [
{href: 'https://example.com/otherpage.html', text: 'legit link text'},
{href: 'https://example.com/page.html', text: 'click here'},
{href: 'https://example.com/page.html#test', text: 'click here'},
{href: 'https://example.com/otherpage.html', text: 'legit link text'},
],
};

const auditResult = LinkTextAudit.audit(artifacts);
assert.equal(auditResult.rawValue, true);
});

it('ignores javascript: links', () => {
const artifacts = {
URL: {
finalUrl: 'https://example.com/page.html',
},
CrawlableLinks: [
{href: 'javascript:alert(1)', text: 'click here'},
{href: 'JavaScript:window.location="/otherpage.html"', text: 'click here'},
{href: 'JAVASCRIPT:void(0)', text: 'click here'},
],
};

const auditResult = LinkTextAudit.audit(artifacts);
assert.equal(auditResult.rawValue, true);
});

it('passes when all links have descriptive texts', () => {
const artifacts = {
URL: {
finalUrl: 'https://example.com/page.html',
},
CrawlableLinks: [
{href: 'https://example.com/otherpage.html', text: 'legit link text'},
{href: 'http://example.com/page.html?test=test', text: 'legit link text'},
{href: 'file://Users/user/Desktop/file.png', text: 'legit link text'},
],
};

const auditResult = LinkTextAudit.audit(artifacts);
assert.equal(auditResult.rawValue, true);
});
});

0 comments on commit f911e84

Please sign in to comment.