-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new_audit(anchor-text-audit): descriptive anchor text audit (#3490)
- Loading branch information
Showing
7 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,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; |
This file contains 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 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,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; | ||
|
This file contains 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,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); | ||
}); | ||
}); |