diff --git a/Gruntfile.js b/Gruntfile.js index 6203b80..68d5bf8 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -9,6 +9,9 @@ 'use strict'; module.exports = function(grunt) { + var path = require('path'), + url = require('url'); + grunt.initConfig({ jshint: { @@ -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' + } } }, diff --git a/README.md b/README.md index 96a81f9..e770de9 100644 --- a/README.md +++ b/README.md @@ -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 { diff --git a/tasks/cdnify.js b/tasks/cdnify.js index 2f84ccf..14e4a5c 100644 --- a/tasks/cdnify.js +++ b/tasks/cdnify.js @@ -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) { @@ -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); @@ -113,7 +119,7 @@ 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); } } } @@ -121,7 +127,7 @@ module.exports = function (grunt) { // Update the URLs in any embedded stylesheets if (options.css) { soup.setInnerHTML('style', function (css) { - return rewriteCSSURLs(css, rewriteURL); + return rewriteCSSURLs(css, rewriteURLWithDirname); }); } diff --git a/test/cdnify_test.js b/test/cdnify_test.js index f586128..97e0d65 100644 --- a/test/cdnify_test.js +++ b/test/cdnify_test.js @@ -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(); } }; diff --git a/test/expected/css/custom-rewriter.css b/test/expected/css/custom-rewriter.css new file mode 100644 index 0000000..bb48907 --- /dev/null +++ b/test/expected/css/custom-rewriter.css @@ -0,0 +1,3 @@ +.image { + background: url('//cdn.example.com/stuff/images/custom-rewriter.jpg'); +} diff --git a/test/expected/html/custom-rewriter.html b/test/expected/html/custom-rewriter.html new file mode 100644 index 0000000..bb25625 --- /dev/null +++ b/test/expected/html/custom-rewriter.html @@ -0,0 +1,11 @@ + +
+ + + + + + + + + diff --git a/test/fixtures/css/custom-rewriter.css b/test/fixtures/css/custom-rewriter.css new file mode 100644 index 0000000..382fa90 --- /dev/null +++ b/test/fixtures/css/custom-rewriter.css @@ -0,0 +1,3 @@ +.image { + background: url('../images/custom-rewriter.jpg'); +} diff --git a/test/fixtures/html/custom-rewriter.html b/test/fixtures/html/custom-rewriter.html new file mode 100644 index 0000000..9a4e0c6 --- /dev/null +++ b/test/fixtures/html/custom-rewriter.html @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/test/fixtures/images/custom-rewriter.jpg b/test/fixtures/images/custom-rewriter.jpg new file mode 100644 index 0000000..e69de29