diff --git a/src/css.js b/src/css.js index 9ec6e7f..4d805c5 100644 --- a/src/css.js +++ b/src/css.js @@ -29,8 +29,8 @@ module.exports = function( options, callback ) return callback( null ); // Skip } - var css = "url(\"" + datauriContent + "\");"; - var re = new RegExp( "url\\(\\s?[\"']?(" + inline.escapeSpecialChars( args.src ) + ")[\"']?\\s?\\);", "g" ); + var css = "url(\"" + datauriContent + "\")"; + var re = new RegExp( "url\\(\\s?[\"']?(" + inline.escapeSpecialChars( args.src ) + ")[\"']?\\s?\\)", "g" ); result = result.replace( re, () => css ); return callback( null ); @@ -39,8 +39,8 @@ module.exports = function( options, callback ) var rebase = function( src ) { - var css = "url(\"" + path.join( settings.rebaseRelativeTo, src ).replace( /\\/g, "/" ) + "\");"; - var re = new RegExp( "url\\(\\s?[\"']?(" + inline.escapeSpecialChars( src ) + ")[\"']?\\s?\\);", "g" ); + var css = "url(\"" + path.join( settings.rebaseRelativeTo, src ).replace( /\\/g, "/" ) + "\")"; + var re = new RegExp( "url\\(\\s?[\"']?(" + inline.escapeSpecialChars( src ) + ")[\"']?\\s?\\)", "g" ); result = result.replace( re, () => css ); }; @@ -48,17 +48,19 @@ module.exports = function( options, callback ) var tasks = []; var found = null; - var urlRegex = /url\(\s?["']?([^)'"]+)["']?\s?\);.*/gi; + var urlRegex = /url\(\s?["']?([^)'"]+)["']?\s?\).*/i; + var index = 0; if( settings.rebaseRelativeTo ) { var matches = {}; var src; - while( ( found = urlRegex.exec( result ) ) !== null ) + while( ( found = urlRegex.exec( result.substring(index) ) ) !== null ) { src = found[ 1 ]; matches[ src ] = true; + index = found.index + index + 1; } for( src in matches ) @@ -73,7 +75,8 @@ module.exports = function( options, callback ) var inlineAttributeCommentRegex = new RegExp( "\\/\\*\\s?" + settings.inlineAttribute + "\\s?\\*\\/", "i" ); var inlineAttributeIgnoreCommentRegex = new RegExp( "\\/\\*\\s?" + settings.inlineAttribute + "-ignore\\s?\\*\\/", "i" ); - while( ( found = urlRegex.exec( result ) ) !== null ) + index = 0; + while( ( found = urlRegex.exec( result.substring(index) ) ) !== null ) { if( !inlineAttributeIgnoreCommentRegex.test( found[ 0 ] ) && ( settings.images || inlineAttributeCommentRegex.test( found[ 0 ] ) ) ) @@ -84,6 +87,7 @@ module.exports = function( options, callback ) limit: settings.images } ) ); } + index = found.index + index + 1; } parallel( tasks, function( err ) diff --git a/src/html.js b/src/html.js index 0e0f1a6..e09adeb 100644 --- a/src/html.js +++ b/src/html.js @@ -86,7 +86,7 @@ module.exports = function( options, callback ) var cssOptions = xtend( {}, settings, { fileContent: content.toString(), - rebaseRelativeTo: path.relative( settings.relativeTo, path.join( settings.relativeTo, args.src, ".." + path.sep ) ) + rebaseRelativeTo: path.relative( settings.relativeTo, settings.rebaseRelativeTo || path.join( settings.relativeTo, args.src, ".." + path.sep ) ) } ); css( cssOptions, function( err, content ) diff --git a/src/util.js b/src/util.js index 56f19b6..ef0945e 100644 --- a/src/util.js +++ b/src/util.js @@ -133,7 +133,7 @@ function getRemote( uri, settings, callback, toDataUri ) util.getInlineFilePath = function( src, relativeTo ) { src = src.replace( /^\//, "" ); - return path.resolve( relativeTo, src ).replace( /\?.*$/, "" ); + return path.resolve( relativeTo, src ).replace( /[\?#].*$/, "" ); }; util.getInlineFileContents = function( src, relativeTo ) diff --git a/test/cases/assets/fonts.css b/test/cases/assets/fonts.css new file mode 100644 index 0000000..ea45d08 --- /dev/null +++ b/test/cases/assets/fonts.css @@ -0,0 +1,17 @@ +@font-face { + font-family: 'Open Sans'; + font-style: normal; + font-weight: 400; + src: url('open-sans-v13-latin-regular.eot'); + src: url('open-sans-v13-latin-regular.eot?#iefix') format('embedded-opentype'), + url(open-sans-v13-latin-regular.woff2) format('woff2'), + url("open-sans-v13-latin-regular.woff") format('woff'), + url('open-sans-v13-latin-regular.svg#OpenSans') format('svg'); +} +@font-face { + font-family: 'Open Sans'; + font-style: normal; + font-weight: 400; + src: url('open-sans-v13-latin-regular.eot'); + src: url('open-sans-v13-latin-regular.eot?#iefix') format('embedded-opentype'), url(open-sans-v13-latin-regular.woff2) format('woff2'), url("open-sans-v13-latin-regular.woff") format('woff'), url('open-sans-v13-latin-regular.svg#OpenSans') format('svg'); +} \ No newline at end of file diff --git a/test/cases/assets/fonts/open-sans-v13-latin-regular.eot b/test/cases/assets/fonts/open-sans-v13-latin-regular.eot new file mode 100644 index 0000000..1d98e6e Binary files /dev/null and b/test/cases/assets/fonts/open-sans-v13-latin-regular.eot differ diff --git a/test/cases/assets/fonts/open-sans-v13-latin-regular.svg b/test/cases/assets/fonts/open-sans-v13-latin-regular.svg new file mode 100644 index 0000000..052c59f --- /dev/null +++ b/test/cases/assets/fonts/open-sans-v13-latin-regular.svg @@ -0,0 +1,1637 @@ + + + diff --git a/test/cases/assets/fonts/open-sans-v13-latin-regular.woff b/test/cases/assets/fonts/open-sans-v13-latin-regular.woff new file mode 100644 index 0000000..e096d04 Binary files /dev/null and b/test/cases/assets/fonts/open-sans-v13-latin-regular.woff differ diff --git a/test/cases/assets/fonts/open-sans-v13-latin-regular.woff2 b/test/cases/assets/fonts/open-sans-v13-latin-regular.woff2 new file mode 100644 index 0000000..5287058 Binary files /dev/null and b/test/cases/assets/fonts/open-sans-v13-latin-regular.woff2 differ diff --git a/test/cases/css-rebase.css b/test/cases/css-rebase.css new file mode 100644 index 0000000..aa0ea7b --- /dev/null +++ b/test/cases/css-rebase.css @@ -0,0 +1,3 @@ +body { + background: url(icon.png); +} \ No newline at end of file diff --git a/test/cases/css-rebase.html b/test/cases/css-rebase.html new file mode 100644 index 0000000..32bf38a --- /dev/null +++ b/test/cases/css-rebase.html @@ -0,0 +1,9 @@ + + +
+