fix: detection of absolute URLs

If a URL is resolved before checking for absolute URLs, even relative
URLs will be detected as absolute.

Includes updates to the skip-absolute-urls test.

Closes #126
This commit is contained in:
Joey Parrish 2016-11-17 11:06:54 -08:00
parent 68c345ae8e
commit d53c651921
6 changed files with 40 additions and 17 deletions

View File

@ -5,7 +5,8 @@ var basename = require('path').basename;
function resolve(inliner, todo, $) {
debug('start %s favicon', todo.length);
return todo.map(function links(link) {
var url = inliner.resolve(inliner.url, $(link).attr('href'));
var url = $(link).attr('href');
url = inliner.resolve(inliner.url, url);
inliner.emit('progress', 'processing favicon ' + basename(url));
return inliner.image(url).then(function then(dataURL) {
$(link).attr('href', dataURL);

View File

@ -4,7 +4,7 @@ var debug = require('debug')('inliner');
function resolve(inliner, todo, $) {
debug('start %s links', todo.length);
return todo.map(function images(image) {
var url = inliner.resolve(inliner.url, $(image).attr('src'));
var url = $(image).attr('src');
if (inliner.options.skipAbsoluteUrls &&
(url.indexOf('//') === 0 || url.indexOf('http') === 0)) {
@ -13,6 +13,7 @@ function resolve(inliner, todo, $) {
return false;
}
url = inliner.resolve(inliner.url, url);
return inliner.image(url).then(function then(dataURL) {
$(image).attr('src', dataURL);
}).then(inliner.jobs.done.images);

View File

@ -6,15 +6,15 @@ function resolve(inliner, todo, $) {
debug('start %s links', todo.length);
return todo.map(function links(link) {
var url = $(link).attr('href');
if (url.indexOf('http') !== 0) {
url = inliner.resolve(inliner.url, url);
}
if (inliner.options.skipAbsoluteUrls &&
(url.indexOf('//') === 0 || url.indexOf('http') === 0)) {
debug('skipping remote links');
inliner.emit('progress', 'skipping remote links');
return false;
}
if (url.indexOf('http') !== 0) {
url = inliner.resolve(inliner.url, url);
}
inliner.emit('progress', 'processing external css ' + basename(url));
return inliner.get(url).then(function then(res) {
var css = res.body;

View File

@ -5,24 +5,38 @@ var Promise = require('es6-promise').Promise; // jshint ignore:line
function resolve(inliner, todo, $) {
debug('start %s videos', todo.length);
return todo.map(function videos(video) {
var url = inliner.resolve(inliner.url, $(video).attr('src'));
var posterUrl = inliner.resolve(inliner.url, $(video).attr('poster'));
var url = $(video).attr('src');
var posterUrl = $(video).attr('poster');
var promises = [];
if (inliner.options.skipAbsoluteUrls &&
(url.indexOf('//') === 0 || url.indexOf('http') === 0)) {
debug('skipping remote video');
inliner.emit('progress', 'skipping remote video');
return false;
} else {
debug('resolving local video');
url = inliner.resolve(inliner.url, url);
promises.push(inliner.image(url).then(function then(dataURL) {
$(video).attr('src', dataURL);
}));
}
return Promise.all([
inliner.image(url).then(function then(dataURL) {
$(video).attr('src', dataURL);
}),
inliner.image(posterUrl).then(function then(dataURL) {
if (inliner.options.skipAbsoluteUrls &&
(posterUrl.indexOf('//') === 0 || posterUrl.indexOf('http') === 0)) {
debug('skipping remote video');
inliner.emit('progress', 'skipping remote video');
} else {
debug('resolving local poster');
posterUrl = inliner.resolve(inliner.url, posterUrl);
promises.push(inliner.image(posterUrl).then(function then(dataURL) {
$(video).attr('poster', dataURL);
}),
]).then(inliner.jobs.done.videos);
}));
}
if (!promises.length) {
return false;
}
return Promise.all(promises).then(inliner.jobs.done.videos);
});
}

File diff suppressed because one or more lines are too long

View File

@ -7,11 +7,17 @@
<body>
<script src="script-local.js"></script>
<script src="https://cdn.rawgit.com/zloirock/core-js/master/client/shim.min.js"></script>
<link rel="icon" type="image/png" href="colour.png">
<link rel="icon" type="image/png" href="http://localhost:54321/colour.png">
<link rel="shortcut icon" type="image/png" href="colour.png">
<link rel="shortcut icon" type="image/png" href="http://localhost:54321/colour.png">
<link href="import.css" rel="stylesheet" type="text/css" />
<link href="http://localhost:54321/css-ext-import.css" rel="stylesheet" />
<img src="1x1.gif">
<img src="http://localhost:54321/image.jpg">
<video src="video.webm" poster="1x1.gif"></video>
<video src="http://localhost:54321/video.webm?name=value"></video>
<video src="video.webm" poster="http://localhost:54321/1x1.gif?name=value"></video>
<video src="http://localhost:54321/video.webm?name=value" poster="1x1.gif"></video>
<video src="http://localhost:54321/video.webm?name=value" poster="http://localhost:54321/1x1.gif?name=value"></video>
</body>
</html>