-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
core(rel=noopener-audit): Only test http/https links #4036
Conversation
Fixing the tests now... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the quick patch @sanjsanj!! 👏
would you mind adding a few sample test cases to catch this failure?
the smoke tests file is over here
lighthouse/lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Lines 103 to 113 in f49ef06
<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> | |
<!-- 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 --> | |
<a href="./doesnotexist" target="_blank">internal link is ok</a> | |
</template> |
and unit tests over here
lighthouse/lighthouse-core/test/audits/dobetterweb/external-anchors-use-rel-noopener-test.js
Lines 43 to 68 in f49ef06
it('handles links with no href attribute', () => { | |
const auditResult = ExternalAnchorsAudit.audit({ | |
AnchorsWithNoRelNoopener: [ | |
{href: ''}, | |
{href: 'http://'}, | |
{href: 'http:'}, | |
], | |
URL: {finalUrl: URL}, | |
}); | |
assert.equal(auditResult.rawValue, false); | |
assert.equal(auditResult.details.items.length, 3); | |
assert.equal(auditResult.details.items.length, 3); | |
assert.ok(auditResult.debugString, 'includes debugString'); | |
}); | |
it('does not fail for links with javascript in href attribute', () => { | |
const auditResult = ExternalAnchorsAudit.audit({ | |
AnchorsWithNoRelNoopener: [ | |
{href: 'javascript:void(0)'}, | |
{href: 'JAVASCRIPT:void(0)'}, | |
], | |
URL: {finalUrl: URL}, | |
}); | |
assert.equal(auditResult.rawValue, true); | |
assert.equal(auditResult.details.items.length, 0); | |
}); |
can throw in a check for handling href === null
in unit tests and the mail links in both cases
// Ignore href's that are not real links | ||
return !anchor.href || !anchor.href.toLowerCase().startsWith('javascript:'); | ||
}) | ||
.filter(anchor => anchor.href.toLowerCase().startsWith('http')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably need a anchor.href &&
here to be safe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@patrickhulce I meant to ask about that, it was previously checking for !anchor.href
, and the unit tests seem to suggest that's what it's wanting to do, so I've put that back in.
lighthouse/lighthouse-core/test/audits/dobetterweb/external-anchors-use-rel-noopener-test.js
Lines 43 to 49 in f49ef06
it('handles links with no href attribute', () => { | |
const auditResult = ExternalAnchorsAudit.audit({ | |
AnchorsWithNoRelNoopener: [ | |
{href: ''}, | |
{href: 'http://'}, | |
{href: 'http:'}, | |
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless this unit test is wrong and this audit should ignore anchors without external links, and I presume you need an href to be an external link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh whoops, yeah that's right you can ignore my comment then :)
when glancing I assumed it was there as a null
guard for the next case, but I remember now it was intentional
…or the bug that raised this issue
@patrickhulce Thanks for the fast feedback! Yup sure, leave it with me I'll get that done asap. |
Ok I've added a handful more tests. While I was at it I couldn't resist tidying them up a bit;
|
Sorry again, fixing the tests... :) |
Sorry, forgot to update after fixing the failing tests but the PR is ready for more feedback when you have time, TY! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for jumping in here on this @sanjsanj! congrats on your first contribution!! 🎉 🥇
Sweeeet, cheers @patrickhulce ! |
Closes #3714
Only filter anchor links that start with an http protocol for the
rel=noopener
audit.