Skip to content

Commit

Permalink
handle anchor tags with no href in noopener audit
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Dec 22, 2016
1 parent c5402c9 commit 9cef271
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ <h2>Do better web tester page</h2>
<template id="noopener-links-tmpl">
<!-- FAIL - does not use rel="noopener" to open external link -->
<a href="https://www.google.com/" target="_blank">external link</a>
<!-- FAIL - does not use rel="noopener" but no href attribute -->
<a target="_blank">external link</a>
<!-- PASS -->
<a href="https://www.google.com/" target="_blank" rel="noopener nofollow">external link that uses rel noopener and another unrelated rel attribute</a>
<!-- PASS -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ class ExternalAnchorsUseRelNoopenerAudit extends Audit {

const pageHost = new URL(artifacts.URL.finalUrl).host;
// Filter usages to exclude anchors that are same origin
// TODO: better extendedInfo for anchors with no href attribute:
// https://github.com/GoogleChrome/lighthouse/issues/1233
const failingAnchors = artifacts.AnchorsWithNoRelNoopener.usages
.filter(anchor => new URL(anchor.href).host !== pageHost)
.filter(anchor => anchor.href === '' || new URL(anchor.href).host !== pageHost)
.map(anchor => {
return {
url: `<a
href="${anchor.href}"
${anchor.target ? ` target="${anchor.target}"` : ''}
${anchor.rel ? ` rel="${anchor.rel}"` : ''}>...
</a>`
url: '<a' +
(anchor.href !== '' ? ` href="${anchor.href}"` : '') +
(anchor.target ? ` target="${anchor.target}"` : '') +
(anchor.rel ? ` rel="${anchor.rel}"` : '') + '>...' +
'</a>'
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,17 @@ describe('External anchors use rel="noopener"', () => {
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.extendedInfo.value.length, 2);
});

it('handles links with no href attribute', () => {
const auditResult = ExternalAnchorsAudit.audit({
AnchorsWithNoRelNoopener: {
usages: [
{href: ''}
]
},
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.extendedInfo.value.length, 1);
});
});

0 comments on commit 9cef271

Please sign in to comment.