Skip to content

Commit

Permalink
resolve pull request bug;
Browse files Browse the repository at this point in the history
resolve relative path with "path" package;
add font-face test case;
  • Loading branch information
lackhurt committed May 26, 2016
1 parent d4e1db4 commit cecd685
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
tmp
.idea
tmp
51 changes: 8 additions & 43 deletions tasks/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ var IMPORT_REG = /@import\s+[^;\n]+;/g;
var PATH_REG = /@import\s+['"](.*)['"]/;
var PATH_REG_WITH_URL = /@import\s+url\s*\(\s*['"](.*)['"]\s*\)/;
// var BACKGROUND_REG = /(background[^;\}]+)url\s*\(['"]?([^\)'"]*)['"]?\)([^;\n]*)[;\}]/g;
var ASSETS_URL_REG = /url\s*\(\s*['"]*(.+?)\s*['"]*\)\s*([;,])*/ig;
var ASSETS_URL_REG = /url\s*\(\s*['"]?([^\)'"]*)['"]?\)([^;\n]*\s*[;,]?)/ig;

var path = require('path');

/**
* 分析出文件内的@import指令引入的css路径(这里直处理的相对路径的)
Expand Down Expand Up @@ -45,57 +47,20 @@ function isRelativeUrl(url) {
return !/^(http|ftp|https):\/\//.test(url) && /^[^\/]/.test(url);
}

/**
* 解决相对路径问题
* @param srcFilePath
* @param relativePath
* @returns {*}
*/
function fetchImportPath(srcFilePath, relativePath) {
if (/\/[^\/]*$/.test(srcFilePath)) {
return srcFilePath.replace(/\/[^\/]*$/, '/') + relativePath;
} else {
return './' + relativePath;
}
}

/**
* Resolve paths and urls of assets (fonts, background ..etc)
*
* @method resolveRelativeUrls
* @author Sidati <contact@sidati.com>
* @param string url
* @param string path
* @param string srcFilePath
* @param string relativePath
* @return string Resolved Path/Url
*/
function resolveRelativeUrls(path, url) {

var x = url.split('\/').filter(function(val) {
return val != '';
});

var image = x.pop();
var newPath = path.split('\/').filter(function(val) {
return val != '';
})

newPath.pop();

if (x.length > 0) {
var i, j = newPath.length;

while (i = x.pop()) {
if (i != newPath[j] && i != '..' && i != '.') {
newPath.push(i);
}
j--;
}
function resolveRelativeUrls(srcFilePath, relativePath) {
return path.join(path.dirname(srcFilePath), relativePath);
}

newPath.push(image);
return newPath.join('/');
}

/**
* 是否是base64
* @param url
Expand Down Expand Up @@ -124,6 +89,6 @@ module.exports = {
replaceExtraResourcesPath: replaceExtraResourcesPath,
parseExtraCss: parseExtraCss,
isRelativeUrl: isRelativeUrl,
fetchImportPath: fetchImportPath,
fetchImportPath: resolveRelativeUrls,
splitCssFileByImport: splitCssFileByImport
};
10 changes: 10 additions & 0 deletions test/expected/simple_concat/all.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ h3 {
}



.test {
background-image: url('data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7');
}



@font-face
{
font-family: myFirstFont;
src: url('module/module_a/Sansation_Light.ttf'),
url('module/module_a/Sansation_Light.eot'); /* IE9+ */
}
3 changes: 2 additions & 1 deletion test/fixtures/style/all.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ h3 {
}
@import "lib/fancybox/fancybox.css";
@import "module/module_a/module_a.css";
@import "module/module_a/with_base64_data.css";
@import "module/module_a/with_base64_data.css";
@import "module/module_a/font_face.css";
6 changes: 6 additions & 0 deletions test/fixtures/style/module/module_a/font_face.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@font-face
{
font-family: myFirstFont;
src: url('Sansation_Light.ttf'),
url('Sansation_Light.eot'); /* IE9+ */
}
2 changes: 1 addition & 1 deletion test/fixtures/style/module/module_a/module_a.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
.test-absolute-path {
background: url('http://test.com/test.jpg');

}
}

0 comments on commit cecd685

Please sign in to comment.