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

core(rel=noopener-audit): Only test http/https links #4036

Merged
merged 4 commits into from
Dec 14, 2017

Conversation

sanjsanj
Copy link
Contributor

Closes #3714

Only filter anchor links that start with an http protocol for the rel=noopener audit.

@sanjsanj
Copy link
Contributor Author

Fixing the tests now...

Copy link
Collaborator

@patrickhulce patrickhulce left a 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

<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

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'))
Copy link
Collaborator

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

Copy link
Contributor Author

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.

it('handles links with no href attribute', () => {
const auditResult = ExternalAnchorsAudit.audit({
AnchorsWithNoRelNoopener: [
{href: ''},
{href: 'http://'},
{href: 'http:'},
],

Copy link
Contributor Author

@sanjsanj sanjsanj Dec 12, 2017

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.

Copy link
Collaborator

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

@sanjsanj
Copy link
Contributor Author

@patrickhulce Thanks for the fast feedback! Yup sure, leave it with me I'll get that done asap.

@sanjsanj
Copy link
Contributor Author

sanjsanj commented Dec 12, 2017

Ok I've added a handful more tests. While I was at it I couldn't resist tidying them up a bit;

  • Group passes and fails together to make it a bit more organised
  • Add comments where there weren't any
  • Separate the href=null and href="http*" unit test out into two separate tests since they're testing different functionality (imo)
  • Unify the grammar a bit

@sanjsanj
Copy link
Contributor Author

Sorry again, fixing the tests... :)

@sanjsanj
Copy link
Contributor Author

Sorry, forgot to update after fixing the failing tests but the PR is ready for more feedback when you have time, TY!

Copy link
Collaborator

@patrickhulce patrickhulce left a 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!! 🎉 🥇

@patrickhulce patrickhulce merged commit be9dcbf into GoogleChrome:master Dec 14, 2017
@sanjsanj
Copy link
Contributor Author

Sweeeet, cheers @patrickhulce !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants