From cecd6851a4eec103e422c8c223728a85234c4e9a Mon Sep 17 00:00:00 2001 From: lackhurt Date: Thu, 26 May 2016 13:09:02 +0800 Subject: [PATCH] resolve pull request bug; resolve relative path with "path" package; add font-face test case; --- .gitignore | 3 +- tasks/util.js | 51 +++---------------- test/expected/simple_concat/all.css | 10 ++++ test/fixtures/style/all.css | 3 +- .../style/module/module_a/font_face.css | 6 +++ .../style/module/module_a/module_a.css | 2 +- 6 files changed, 29 insertions(+), 46 deletions(-) create mode 100644 test/fixtures/style/module/module_a/font_face.css diff --git a/.gitignore b/.gitignore index b785247..699791f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules npm-debug.log -tmp +.idea +tmp \ No newline at end of file diff --git a/tasks/util.js b/tasks/util.js index 1d3e27f..adbe9ad 100644 --- a/tasks/util.js +++ b/tasks/util.js @@ -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路径(这里直处理的相对路径的) @@ -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 - * @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 @@ -124,6 +89,6 @@ module.exports = { replaceExtraResourcesPath: replaceExtraResourcesPath, parseExtraCss: parseExtraCss, isRelativeUrl: isRelativeUrl, - fetchImportPath: fetchImportPath, + fetchImportPath: resolveRelativeUrls, splitCssFileByImport: splitCssFileByImport }; diff --git a/test/expected/simple_concat/all.css b/test/expected/simple_concat/all.css index 3249ea3..eb0f12d 100644 --- a/test/expected/simple_concat/all.css +++ b/test/expected/simple_concat/all.css @@ -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+ */ +} \ No newline at end of file diff --git a/test/fixtures/style/all.css b/test/fixtures/style/all.css index 2c9c44b..9aaa14f 100644 --- a/test/fixtures/style/all.css +++ b/test/fixtures/style/all.css @@ -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"; \ No newline at end of file +@import "module/module_a/with_base64_data.css"; +@import "module/module_a/font_face.css"; \ No newline at end of file diff --git a/test/fixtures/style/module/module_a/font_face.css b/test/fixtures/style/module/module_a/font_face.css new file mode 100644 index 0000000..ee6bdc9 --- /dev/null +++ b/test/fixtures/style/module/module_a/font_face.css @@ -0,0 +1,6 @@ +@font-face +{ + font-family: myFirstFont; + src: url('Sansation_Light.ttf'), + url('Sansation_Light.eot'); /* IE9+ */ +} \ No newline at end of file diff --git a/test/fixtures/style/module/module_a/module_a.css b/test/fixtures/style/module/module_a/module_a.css index 1c6bf2e..0f1e41d 100644 --- a/test/fixtures/style/module/module_a/module_a.css +++ b/test/fixtures/style/module/module_a/module_a.css @@ -11,4 +11,4 @@ .test-absolute-path { background: url('http://test.com/test.jpg'); -} \ No newline at end of file +}