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
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
12 changes: 10 additions & 2 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,18 @@ <h2>Do better web tester page</h2>
<!-- 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 -->
<!-- FAIL - external link that does have a rel attribute but it is not "noopener" -->
<a href="https://www.google.com/" target="_blank" rel="nofollow">external link</a>
<!-- PASS - external link correctly uses rel="noopener" and an additional rel value -->
<a href="https://www.google.com/" target="_blank" rel="noopener nofollow">external link that uses rel noopener and another unrelated rel attribute</a>
<!-- PASS -->
<!-- PASS - external link correctly uses rel="noopener" -->
<a href="https://www.google.com/" target="_blank" rel="noopener">external link that uses rel noopener</a>
<!-- PASS - internal link without rel="noopener" -->
<a href="./doesnotexist" target="_blank">internal link is ok</a>
<!-- PASS - href uses javascript: -->
<a href="javascript:void(0)" target="_blank"></a>
<!-- PASS - href uses mailto: -->
<a href="mailto:inbox@email.com" target="_blank"></a>
</template>

<template id="password-inputs-can-be-pasted-into">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ module.exports = [
'If they are not used as hyperlinks, consider removing the _blank target.',
extendedInfo: {
value: {
length: 2,
length: 3,
},
},
details: {
items: {
length: 2,
length: 3,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class ExternalAnchorsUseRelNoopenerAudit extends Audit {
}
})
.filter(anchor => {
// Ignore href's that are not real links
return !anchor.href || !anchor.href.toLowerCase().startsWith('javascript:');
return !anchor.href || anchor.href.toLowerCase().startsWith('http');
})
.map(anchor => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ describe('External anchors use rel="noopener"', () => {
assert.equal(auditResult.details.items.length, 0);
});

it('passes when links have 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);
});

it('passes when links have mailto in href attribute', () => {
const auditResult = ExternalAnchorsAudit.audit({
AnchorsWithNoRelNoopener: [
{href: 'mailto:inbox@email.com'},
{href: 'MAILTO:INBOX@EMAIL.COM'},
],
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, true);
assert.equal(auditResult.details.items.length, 0);
});

it('fails when links are from different hosts than the page host', () => {
const auditResult = ExternalAnchorsAudit.audit({
AnchorsWithNoRelNoopener: [
Expand All @@ -40,30 +64,32 @@ describe('External anchors use rel="noopener"', () => {
assert.equal(auditResult.details.items.length, 2);
});

it('handles links with no href attribute', () => {
it('fails when links have 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.equal(auditResult.details.items.length, 1);
assert.equal(auditResult.details.items.length, 1);
assert.ok(auditResult.debugString, 'includes debugString');
});

it('does not fail for links with javascript in href attribute', () => {
it('fails when links have href attribute starting with a protocol', () => {
const auditResult = ExternalAnchorsAudit.audit({
AnchorsWithNoRelNoopener: [
{href: 'javascript:void(0)'},
{href: 'JAVASCRIPT:void(0)'},
{href: 'http://'},
{href: 'http:'},
{href: 'https://'},
{href: 'https:'},
],
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, true);
assert.equal(auditResult.details.items.length, 0);
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.details.items.length, 4);
assert.equal(auditResult.details.items.length, 4);
assert.ok(auditResult.debugString, 'includes debugString');
});
});