Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdzwinel committed Oct 11, 2017
1 parent bce97b9 commit b16c181
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
3 changes: 2 additions & 1 deletion lighthouse-cli/test/fixtures/seo/seo-failure-cases.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ <h1>SEO</h1>

<h2>Anchor text</h2>
<a href='https://example.com'>click this</a>
<a href='/main.html'> click this </a>
<a href='/test.html'> click this </a>
<a href='/test.html'>CLICK THIS</a>
</body>
</html>
2 changes: 1 addition & 1 deletion lighthouse-cli/test/smokehouse/seo/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = [
},
'anchor-text': {
score: false,
displayValue: '2 anchors found',
displayValue: '3 anchors found',
},
},
},
Expand Down
31 changes: 14 additions & 17 deletions lighthouse-core/audits/seo/anchor-text.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* @license Copyright 2016 Google Inc. All Rights Reserved.
* @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 BLACKLIST = [
const BLOCKLIST = new Set([
'click here',
'click this',
'go',
Expand All @@ -17,7 +17,7 @@ const BLACKLIST = [
'right here',
'more',
'learn more',
];
]);

class AnchorText extends Audit {
/**
Expand All @@ -40,41 +40,38 @@ class AnchorText extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
const pageHost = new URL(artifacts.URL.finalUrl);
const failingAnchors = artifacts.CrawlableAnchors
.filter(anchor => {
const url = new URL(anchor.href);

if (
url.protocol.toLowerCase() === 'javascript:' ||
(url.origin == pageHost.origin &&
url.pathname == pageHost.pathname &&
url.search == pageHost.search)
anchor.href.toLowerCase().startsWith('javascript:') ||
URL.equalWithExcludedFragments(anchor.href, artifacts.URL.finalUrl)
) {
return false;
}

return BLACKLIST.includes(anchor.text.trim());
})
.map(anchor => ({
href: anchor.href,
text: anchor.text,
}));
return BLOCKLIST.has(anchor.text.trim().toLowerCase());
});

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

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

if (failingAnchors.length) {
displayValue = failingAnchors.length > 1 ?
`${failingAnchors.length} anchors found` : '1 anchor found';
}

return {
rawValue: failingAnchors.length === 0,
extendedInfo: {
value: failingAnchors,
},
details,
displayValue: failingAnchors.length ? `${failingAnchors.length} anchors found` : undefined,
displayValue,
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/audits/seo/anchor-text-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('SEO: anchor text audit', () => {
const auditResult = AnchorTextAudit.audit(artifacts);
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.extendedInfo.value.length, 1);
assert.equal(auditResult.extendedInfo.value.includes(invalidAnchor), false);
assert.equal(auditResult.extendedInfo.value.includes(invalidAnchor), true);
});

it('ignores links pointing to the main document', () => {
Expand Down

0 comments on commit b16c181

Please sign in to comment.