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

Add dirname of current src file to rewriter function arguments. #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
'use strict';

module.exports = function(grunt) {
var path = require('path'),
url = require('url');

grunt.initConfig({

jshint: {
Expand Down Expand Up @@ -54,6 +57,29 @@ module.exports = function(grunt) {
files: {
'test/output/sample-custom-options.html': 'test/fixtures/sample.html'
}
},
customRewriter: {
options: {
base: false,
rewriter: function (originalURL, dirname) {
// If it looks like an absolute URL (based on a simplistic regexp match), don't do any rewrites.
if (!originalURL.match(/^(\w+:)?\/\//)) {
// If it's a relative path (no leading slash), assume it's relative to the src file and
// rebase it to be relative to the test/fixtures/ directory instead.
if (originalURL && originalURL[0] !== '/') {
originalURL = path.relative('test/fixtures/', path.resolve(dirname, originalURL));
}

return url.resolve('//cdn.example.com/', path.join('stuff/', originalURL));
}

return originalURL;
}
},
files: {
'test/output/css/custom-rewriter.css': 'test/fixtures/css/custom-rewriter.css',
'test/output/html/custom-rewriter.html': 'test/fixtures/html/custom-rewriter.html'
}
}
},

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Example:
cdnify: {
someTarget: {
options: {
rewriter: function (url) {
rewriter: function (url, dirnameOfSrc) {
if (url.indexOf('data:') === 0) {
return url; // leave data URIs untouched
} else {
Expand Down
14 changes: 10 additions & 4 deletions tasks/cdnify.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ module.exports = function (grunt) {

this.files.forEach(function (file) {
var srcFile = file.src,
destFile = file.dest;
destFile = file.dest,
dirname = path.dirname(srcFile),
rewriteURLWithDirname;

rewriteURLWithDirname = function(url) {
return rewriteURL(url, dirname);
};

if (typeof srcFile !== 'string') {
if (srcFile.length > 1) {
Expand All @@ -98,7 +104,7 @@ module.exports = function (grunt) {
// It's a CSS file
var oldCSS = grunt.file.read(srcFile),
newCSS = options.css ?
rewriteCSSURLs(oldCSS, rewriteURL) :
rewriteCSSURLs(oldCSS, rewriteURLWithDirname) :
oldCSS;

grunt.file.write(destFile, newCSS);
Expand All @@ -113,15 +119,15 @@ module.exports = function (grunt) {
if (options.html.hasOwnProperty(search)) {
var attr = options.html[search];
if (attr) {
soup.setAttribute(search, attr, rewriteURL);
soup.setAttribute(search, attr, rewriteURLWithDirname);
}
}
}

// Update the URLs in any embedded stylesheets
if (options.css) {
soup.setInnerHTML('style', function (css) {
return rewriteCSSURLs(css, rewriteURL);
return rewriteCSSURLs(css, rewriteURLWithDirname);
});
}

Expand Down
20 changes: 20 additions & 0 deletions test/cdnify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ exports.cdnify = {
var expected = grunt.file.read('test/expected/sample-custom-options.html');
test.equal(actual, expected);

test.done();
},

'HTML file including external CSS with custom rewriter function': function (test) {
test.expect(1);

var actual = grunt.file.read('test/output/html/custom-rewriter.html');
var expected = grunt.file.read('test/expected/html/custom-rewriter.html');
test.equal(actual, expected);

test.done();
},

'CSS file with custom rewriter function': function (test) {
test.expect(1);

var actual = grunt.file.read('test/output/css/custom-rewriter.css');
var expected = grunt.file.read('test/expected/css/custom-rewriter.css');
test.equal(actual, expected);

test.done();
}
};
3 changes: 3 additions & 0 deletions test/expected/css/custom-rewriter.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.image {
background: url('//cdn.example.com/stuff/images/custom-rewriter.jpg');
}
11 changes: 11 additions & 0 deletions test/expected/html/custom-rewriter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,400italic,700">
<link rel="icon" href="//cdn.example.com/stuff/favicon.ico">
<link rel="apple-touch-icon" href="//cdn.example.com/stuff/apple-touch-icon.png">
<link rel="stylesheet" href="//cdn.example.com/stuff/css/custom-rewriter.css">
</head>

<body>
</body>
</html>
3 changes: 3 additions & 0 deletions test/fixtures/css/custom-rewriter.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.image {
background: url('../images/custom-rewriter.jpg');
}
11 changes: 11 additions & 0 deletions test/fixtures/html/custom-rewriter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,400italic,700">
<link rel="icon" href="/favicon.ico">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="stylesheet" href="../css/custom-rewriter.css">
</head>

<body>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.