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

handle anchor tags with no href in noopener audit #1238

Merged
merged 1 commit into from
Dec 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 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,9 @@ <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" and has no href attribute, giving an
href value of '' when read, which will throw in a `new URL('')` constructor -->
<a target="_blank">external link</a>
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe add a comment here or in the audit that when no href is given, the browser has a '' by default. Thats why you do the '' check I suppose.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

<!-- 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,16 @@ 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}"` : '') + '>'
};
});

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);
});
});