Skip to content

Commit

Permalink
Merge pull request #27 from seyfert/master
Browse files Browse the repository at this point in the history
rebaseRelativeTo fixes
  • Loading branch information
jrit authored Mar 6, 2017
2 parents cc83c2f + a6ed777 commit e6b4dd7
Show file tree
Hide file tree
Showing 13 changed files with 1,741 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -39,26 +39,28 @@ 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 );
};

var result = settings.fileContent;
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 )
Expand All @@ -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 ] ) ) )
Expand All @@ -84,6 +87,7 @@ module.exports = function( options, callback )
limit: settings.images
} ) );
}
index = found.index + index + 1;
}

parallel( tasks, function( err )
Expand Down
2 changes: 1 addition & 1 deletion src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
17 changes: 17 additions & 0 deletions test/cases/assets/fonts.css
Original file line number Diff line number Diff line change
@@ -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');
}
Binary file not shown.
1,637 changes: 1,637 additions & 0 deletions test/cases/assets/fonts/open-sans-v13-latin-regular.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions test/cases/css-rebase.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url(icon.png);
}
9 changes: 9 additions & 0 deletions test/cases/css-rebase.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<link href="assets/fonts.css" rel="stylesheet">
</head>
<body>
</body>
</html>
3 changes: 3 additions & 0 deletions test/cases/css-rebase_out.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url("assets/icon.png");
}
27 changes: 27 additions & 0 deletions test/cases/css-rebase_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<style>
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: url("assets/fonts/open-sans-v13-latin-regular.eot");
src: url("assets/fonts/open-sans-v13-latin-regular.eot?#iefix") format('embedded-opentype'),
url("assets/fonts/open-sans-v13-latin-regular.woff2") format('woff2'),
url("assets/fonts/open-sans-v13-latin-regular.woff") format('woff'),
url("assets/fonts/open-sans-v13-latin-regular.svg#OpenSans") format('svg');
}
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: url("assets/fonts/open-sans-v13-latin-regular.eot");
src: url("assets/fonts/open-sans-v13-latin-regular.eot?#iefix") format('embedded-opentype'), url("assets/fonts/open-sans-v13-latin-regular.woff2") format('woff2'), url("assets/fonts/open-sans-v13-latin-regular.woff") format('woff'), url("assets/fonts/open-sans-v13-latin-regular.svg#OpenSans") format('svg');
}
</style>
</head>
<body>
</body>
</html>
32 changes: 32 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ describe( "html", function()
}
);
} );

it( "should rebase inline local links relative to", function( done )
{
var expected = readFile( "test/cases/css-rebase_out.html" );

inline.html( {
fileContent: readFile( "test/cases/css-rebase.html" ),
relativeTo: "test/cases/",
rebaseRelativeTo: "test/cases/assets/fonts"
},
function( err, result )
{
testEquality( err, result, expected, done );
}
);
} );
} );

describe( "scripts", function()
Expand Down Expand Up @@ -579,6 +595,22 @@ describe( "css", function()
}
);
} );

it( "should rebase local urls", function( done )
{
var expected = readFile( "test/cases/css-rebase_out.css" );

inline.css( {
fileContent: readFile( "test/cases/css-rebase.css" ),
rebaseRelativeTo: "assets",
images: false
},
function( err, result )
{
testEquality( err, result, expected, done );
}
);
} );
} );

describe( "util", function()
Expand Down

0 comments on commit e6b4dd7

Please sign in to comment.