Skip to content

Commit

Permalink
Use assetgraph to load file-urls when traversing local files non-recu…
Browse files Browse the repository at this point in the history
…rsively, but stop processing them once loaded. Closes #140
  • Loading branch information
Munter committed Mar 31, 2018
1 parent c79f013 commit d1be716
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/index.js
Expand Up @@ -441,6 +441,13 @@ async function hyperlink({
});
return;
}

// In non-recursive mode local assets might be marked as end-of-line.
// This is specifically relevant to local file-URLs
if (asset.stopProcessing) {
return;
}

for (const relation of asset.externalRelations) {
// Only do work for supported protocols
if (!['http:', 'https:', 'file:'].includes(relation.to.protocol)) {
Expand Down Expand Up @@ -521,7 +528,13 @@ async function hyperlink({
if (!relation.crossorigin && recursive) {
follow = true;
} else if (relation.from !== relation.to) {
relation.to.check = true;
// If we are handling local file-urls, follow but mark as end-of-line in processing
if (!recursive && relation.from.protocol === 'file:' && relation.to.protocol === 'file:') {
follow = true;
relation.to.stopProcessing = true;
} else {
relation.to.check = true;
}
}
} else if (/^(?:JavaScript|Css)Source(?:Mapping)Url$/.test(relation.type)) {
if (followSourceMaps) {
Expand All @@ -539,7 +552,13 @@ async function hyperlink({

if (follow) {
if (assetTypesWithoutRelations.includes(relation.to.type)) {
relation.to.check = true;
// If we are handling local file-urls, follow but mark as end-of-line in processing
if (!recursive && relation.from.protocol === 'file:' && relation.to.protocol === 'file:') {
relation.to.stopProcessing = true;
assetQueue.push(relation.to);
} else {
relation.to.check = true;
}
} else {
assetQueue.push(relation.to);
}
Expand Down
26 changes: 26 additions & 0 deletions test/index.js
Expand Up @@ -349,6 +349,32 @@ describe('hyperlink', function () {
});
});

describe('on a local file system', function () {
it('should not execute tests on outgoing relations of other pages when recursion is disabled', async function () {
const t = new TapRender();
sinon.spy(t, 'push');
await hyperlink({
recursive: false,
root: pathModule.resolve(__dirname, '..', 'testdata', 'recursive'),
inputUrls: [ 'index.html' ]
}, t);

expect(t.close(), 'to satisfy', { count: 3, pass: 3, fail: 0, skip: 0, todo: 0 });
expect(t.push, 'to have no calls satisfying', () => {
t.push(null, {
operator: 'fragment-check',
name: 'fragment-check testdata/recursive/page.html --> index.html#brokenfragment'
});
});
expect(t.push, 'to have no calls satisfying', () => {
t.push(null, {
operator: 'external-check',
name: 'external-check testdata/recursive/index.html --> hyperlink.gif'
});
});
});
});

describe('with document fragments', function () {
it('should not complain when a referenced fragment exists in the target HTML', async function () {
httpception([
Expand Down
Binary file added testdata/recursive/hyperlink.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions testdata/recursive/index.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<a href="page.html">page</a>
<<img src="hyperlink.gif" alt="">
</body>
</html>
10 changes: 10 additions & 0 deletions testdata/recursive/page.html
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>page</title>
</head>
<body>
<a href="index.html#brokenfragment">index link to missing fragment</a>
</body>
</html>
3 changes: 3 additions & 0 deletions testdata/recursive/style.css
@@ -0,0 +1,3 @@
body {
background: red;
}

0 comments on commit d1be716

Please sign in to comment.