Skip to content
Browse files

Merge pull request #72 from veged/fixpack

Fixpack
  • Loading branch information...
2 parents 9c2c1be + 55bd902 commit 1a7c29a9d8d57a790c8de25972ca1ac861eb0b45 @afelix afelix committed Mar 7, 2013
View
6 lib/freeze.js
@@ -62,8 +62,8 @@ var freeze = exports.freeze = function(filePath, content) {
if (_freezeDir) {
if (content === undefined) {
- if (!FS.existsSync(filePath)) throw new Error();
- if (FS.statSync(filePath).isDirectory()) throw new Error();
+ if (!FS.existsSync(filePath)) throw new Error("No such file or directory: " + filePath);
+ if (FS.statSync(filePath).isDirectory()) throw new Error("Is a directory (file needed): " + filePath);
content = FS.readFileSync(filePath);
}
@@ -347,5 +347,5 @@ function arraySplice(a1, from, to, a2) {
* @returns {boolean} True if it is symbolic link, otherwise false.
*/
function isSymLink(path) {
- return FS.lstatSync(path).isSymbolicLink();
+ return FS.existsSync(path) && FS.lstatSync(path).isSymbolicLink();
}
View
5 lib/tech.js
@@ -79,8 +79,9 @@ exports.Tech = INHERIT({
},
child: function(type, path) {
- var children = this.children;
- return children[path] || (children[path] = this.tech.createFile(this.pathTo(path), type, this));
+ var children = this.children,
+ key = type + '|' + path;
+ return children[key] || (children[key] = this.tech.createFile(this.pathTo(path), type, this));
},
pathTo: function(path) {
View
116 lib/techs/css-fast.js
@@ -1,116 +0,0 @@
-var INHERIT = require('inherit'),
- base = require('../tech'),
- cssbase = require('./css-base'),
-
- stringRe = "(?:(?:'[^'\\r\\n]*')|(?:\"[^\"\\r\\n]*\"))",
- urlRe = "(?:(?:url\\(\\s*" + stringRe + "\\s*\\))|(?:url\\(\\s*[^\\s\\r\\n'\"]*\\s*\\)))",
- commentRe = '(?:/\\*[^*]*\\*+(?:[^/][^*]*\\*+)*/)',
- importRe = '(?:\\@import\\s+(' + urlRe + '|' + stringRe + '))',
- allRe = new RegExp(commentRe + '|' + importRe + '|' + urlRe, 'g'),
- urlStringRx = new RegExp('^' + urlRe + '$');
-
-exports.Tech = INHERIT(cssbase.Tech, {
- File: exports.File = INHERIT(cssbase.File, {
-
- parseInclude: function(content) {
- var m, found = [];
-
- if (Buffer.isBuffer(content)) content = content.toString('utf8');
-
- while (m = allRe.exec(content)) {
- if (m[0].lastIndexOf('/*', 0) === 0) {
- // skip comment
- } else if (m[0].charAt(0) === '@') {
- // @import
- var url = parseUrl(m[1]);
- if (isIncludeProcessable(url)) found.push({
- type: 'include',
- url: url,
- range: [m.index, allRe.lastIndex]
- });
- } else if (urlStringRx.test(m[0])) {
- // url(...)
- var url = parseUrl(m[0]);
- if (isLinkProcessable(url)) found.push({
- type: 'link',
- url: url,
- range: [m.index, allRe.lastIndex - 1]
- });
- } else {
- throw new Error('Failed to match: ' + m[0]);
- }
- }
-
- return makeParsed(found, content);
- },
-
- processInclude: function(path, content) {
- var parsed = content || this.content;
-
- for(var i = 0; i < parsed.length; i++) {
- var item = parsed[i];
-
- if (typeof item === 'string') continue;
-
- if (item.type === 'include') {
- parsed[i] = '/* ' + item.url + ' begin */\n' +
- this.child('include', item.url).process(path) +
- '\n/* ' + item.url + ' end */\n';
-
- continue;
- }
-
- parsed[i] = this.child('link', item.url).process(path);
- }
-
- return parsed.join('');
- },
-
- processLink: function(path) {
- return 'url(' + this.__base(path) + ')';
- },
-
- processPath: function(path) {
- return path.replace(/^(.*?)(\?|$)/, '$1');
- }
-
- })
-});
-
-function parseUrl(url) {
- if (url.lastIndexOf('url(', 0) === 0) url = url.replace(/^url\(\s*/, '').replace(/\s*\)$/, '');
-
- if (url.charAt(0) === '\'' || url.charAt(0) === '"') url = url.substr(1, url.length - 2);
-
- return url;
-}
-
-function isLinkProcessable(url) {
- return !(~['#', '?', '/'].indexOf(url.charAt(0)) || isAbsoluteUrl(url));
-}
-
-function isIncludeProcessable(url) {
- return !isAbsoluteUrl(url);
-}
-
-function isAbsoluteUrl(url) {
- return /^\w+:/.test(url);
-}
-
-function makeParsed(items, content) {
- var result = [],
- lastInd = 0;
-
- items.forEach(function(item) {
- if (lastInd > item.range[0]) throw 'index out of range';
-
- if (lastInd < item.range[0]) result.push(content.substring(lastInd, item.range[0]));
-
- result.push(item);
- lastInd = item.range[1] + 1;
- });
-
- if (lastInd < content.length) result.push(content.substring(lastInd));
-
- return result;
-}
View
57 lib/techs/css-ometajs.js
@@ -0,0 +1,57 @@
+var INHERIT = require('inherit'),
+ util = require('util'),
+ base = require('../tech'),
+ cssbase = require('./css-base'),
+ ometajs = require('./css.ometajs.js'),
+ parser = ometajs.BorschikCSSParser,
+ children = ometajs.BorschikCSSChildren,
+ translator = ometajs.BorschikCSSTranslator;
+
+function correctInherit(derived, base) {
+ util.inherits(derived, base);
+
+ // Copy all static methods and properties
+ Object.keys(base).forEach(function(key) {
+ if (!base.hasOwnProperty(key)) return;
+ derived[key] = base[key];
+ });
+
+ return derived;
+}
+
+exports.Tech = INHERIT(cssbase.Tech, {
+ File: INHERIT(cssbase.File, {
+
+ __constructor: function(tech, path, type, parent) {
+ var file = this;
+
+ this.__base(tech, path, type, parent);
+
+ function SpecificChildren(source) {
+ children.call(this, source);
+ this._file = file;
+ }
+ this._children = correctInherit(SpecificChildren, children);
+ },
+
+ parseInclude: function(content) {
+ return this._children.children(
+ this,
+ parser.parse(content))
+ },
+
+ processInclude: function(path) {
+ function SpecificTranslator(source) {
+ translator.call(this, source);
+ this._path = path;
+ }
+
+ return correctInherit(SpecificTranslator, translator).translate(path, this.content);
+ },
+
+ processPath: function(path) {
+ return path.replace(/^(.*?)(\?|$)/, '$1');
+ }
+
+ })
+});
View
135 lib/techs/css.js
@@ -1,57 +1,124 @@
var INHERIT = require('inherit'),
- util = require('util'),
base = require('../tech'),
cssbase = require('./css-base'),
- ometajs = require('./css.ometajs.js'),
- parser = ometajs.BorschikCSSParser,
- children = ometajs.BorschikCSSChildren,
- translator = ometajs.BorschikCSSTranslator;
-
-function correctInherit(derived, base) {
- util.inherits(derived, base);
-
- // Copy all static methods and properties
- Object.keys(base).forEach(function(key) {
- if (!base.hasOwnProperty(key)) return;
- derived[key] = base[key];
- });
+ U = require('../util'),
- return derived;
-}
+ stringRe = "(?:(?:'[^'\\r\\n]*')|(?:\"[^\"\\r\\n]*\"))",
+ urlRe = "(?:(?:url\\(\\s*" + stringRe + "\\s*\\))|(?:url\\(\\s*[^\\s\\r\\n'\"]*\\s*\\)))",
+ srcRe = "(?:src\\s*=\\s*[^,]+)",
+ commentRe = '(?:/\\*[^*]*\\*+(?:[^/][^*]*\\*+)*/)',
+ importRe = '(?:\\@import\\s+(' + urlRe + '|' + stringRe + '))',
+ allRe = new RegExp(commentRe + '|' + importRe + '|' + urlRe + '|' + srcRe, 'g'),
+ urlStringRx = new RegExp('^' + urlRe + '$'),
+ srcStringRx = new RegExp('^' + srcRe + '$');
exports.Tech = INHERIT(cssbase.Tech, {
- File: INHERIT(cssbase.File, {
+ File: exports.File = INHERIT(cssbase.File, {
- __constructor: function(tech, path, type, parent) {
- var file = this;
+ parseInclude: function(content) {
+ var m, found = [];
- this.__base(tech, path, type, parent);
+ if (Buffer.isBuffer(content)) content = content.toString('utf8');
- function SpecificChildren(source) {
- children.call(this, source);
- this._file = file;
+ while (m = allRe.exec(content)) {
+ if (m[0].lastIndexOf('/*', 0) === 0) {
+ // skip comment
+ } else if (m[0].charAt(0) === '@') {
+ // @import
+ var url = parseUrl(m[1]);
+ if (isIncludeProcessable(url)) found.push({
+ type: 'include',
+ url: url,
+ range: [m.index, allRe.lastIndex]
+ });
+ } else if (urlStringRx.test(m[0])) {
+ // url(...)
+ var url = parseUrl(m[0]);
+ if (U.isLinkProcessable(url)) found.push({
+ type: 'linkUrl',
+ url: url,
+ range: [m.index, allRe.lastIndex - 1]
+ });
+ } else if (srcStringRx.test(m[0])) {
+ // src=...
+ var src = parseSrc(m[0]);
+ if (U.isLinkProcessable(src)) found.push({
+ type: 'linkSrc',
+ url: src,
+ range: [m.index, allRe.lastIndex - 1]
+ });
+ } else {
+ throw new Error('Failed to match: ' + m[0]);
+ }
}
- this._children = correctInherit(SpecificChildren, children);
- },
- parseInclude: function(content) {
- return this._children.children(
- this,
- parser.parse(content))
+ return makeParsed(found, content);
},
- processInclude: function(path) {
- function SpecificTranslator(source) {
- translator.call(this, source);
- this._path = path;
+ processInclude: function(path, content) {
+ var parsed = content || this.content;
+
+ for(var i = 0; i < parsed.length; i++) {
+ var item = parsed[i];
+
+ if (typeof item === 'string') continue;
+
+ if (item.type === 'include') {
+ parsed[i] = '/* ' + item.url + ' begin */\n' +
+ this.child('include', item.url).process(path) +
+ '\n/* ' + item.url + ' end */\n';
+
+ continue;
+ }
+
+ parsed[i] = this.child(item.type, item.url).process(path);
}
- return correctInherit(SpecificTranslator, translator).translate(path, this.content);
+ return parsed.join('');
},
+ processLink: function(path) {
+ if (this.childType === 'linkUrl') return 'url(' + this.__base(path) + ')';
+ if (this.childType === 'linkSrc') return 'src=' + this.__base(path);
+ },
+
processPath: function(path) {
return path.replace(/^(.*?)(\?|$)/, '$1');
}
})
});
+
+function parseUrl(url) {
+ if (url.lastIndexOf('url(', 0) === 0) url = url.replace(/^url\(\s*/, '').replace(/\s*\)$/, '');
+
+ if (url.charAt(0) === '\'' || url.charAt(0) === '"') url = url.substr(1, url.length - 2);
+
+ return url;
+}
+
+function parseSrc(src) {
+ src = src.replace(/^src\s*=\s*/, '').replace(/\s*\)$/, '');
+
+ if (src.charAt(0) === '\'' || src.charAt(0) === '"') src = src.substr(1, src.length - 2);
+
+ return src;
+}
+
+function makeParsed(items, content) {
+ var result = [],
+ lastInd = 0;
+
+ items.forEach(function(item) {
+ if (lastInd > item.range[0]) throw 'index out of range';
+
+ if (lastInd < item.range[0]) result.push(content.substring(lastInd, item.range[0]));
+
+ result.push(item);
+ lastInd = item.range[1] + 1;
+ });
+
+ if (lastInd < content.length) result.push(content.substring(lastInd));
+
+ return result;
+}
View
2 lib/techs/css.ometajs
@@ -39,7 +39,7 @@ ometa BorschikCSSChildren <: CSSTransformer {
]] -> [#AlphaImageLoader, c] | ^raw,
AlphaImageLoaderAttrs = [listOf(#AlphaImageLoaderAttr, ','):c] -> [#AlphaImageLoaderAttrs, c],
- AlphaImageLoaderAttr = ``src'':n '=' <(~',' char)+>:v -> [#AlphaImageLoaderAttr, n,
+ AlphaImageLoaderAttr = ``src'':n '=' <(~',' char)+>:v ?(!v.replace(/["']/g, '').match(/^((\w+:)?\/\/|data\:|\/|#|\?)/)) -> [#AlphaImageLoaderAttr, n,
[#borschik,
this._file.child(
'link',
View
14 lib/techs/css.ometajs.js
@@ -10,8 +10,8 @@ var BSJSTranslator = ometajs_.grammars.BSJSTranslator;
var cssptt = require("cssp/src/cssptt"), CSSParser = cssptt.CSSParser, CSSTransformer = cssptt.CSSTransformer, CSSTranslator = cssptt.CSSTranslator;
-var BorschikCSSParser = function BorschikCSSParser(source) {
- CSSParser.call(this, source);
+var BorschikCSSParser = function BorschikCSSParser(source, opts) {
+ CSSParser.call(this, source, opts);
};
BorschikCSSParser.grammarName = "BorschikCSSParser";
@@ -28,8 +28,8 @@ BorschikCSSParser.parse = function(content) {
return this.matchAll(String(content), "stylesheet");
};
-var BorschikCSSChildren = function BorschikCSSChildren(source) {
- CSSTransformer.call(this, source);
+var BorschikCSSChildren = function BorschikCSSChildren(source, opts) {
+ CSSTransformer.call(this, source, opts);
};
BorschikCSSChildren.grammarName = "BorschikCSSChildren";
@@ -113,7 +113,7 @@ BorschikCSSChildren.prototype["AlphaImageLoaderAttr"] = function $AlphaImageLoad
}, true) && this._rule("char", false, [], null, this["char"]);
});
});
- }, true) && (v = this._getIntermediate(), true) && this._exec([ "AlphaImageLoaderAttr", n, [ "borschik", this._file.child("link", v.replace(/["']/g, "")) ] ]);
+ }, true) && (v = this._getIntermediate(), true) && !v.replace(/["']/g, "").match(/^((\w+:)?\/\/|data\:|\/|#|\?)/) && this._exec([ "AlphaImageLoaderAttr", n, [ "borschik", this._file.child("link", v.replace(/["']/g, "")) ] ]);
}) || this._atomic(function() {
var n, v;
return this._list(function() {
@@ -138,8 +138,8 @@ BorschikCSSChildren.children = function children(file, content) {
return this.match(content, "stylesheet");
};
-var BorschikCSSTranslator = function BorschikCSSTranslator(source) {
- CSSTranslator.call(this, source);
+var BorschikCSSTranslator = function BorschikCSSTranslator(source, opts) {
+ CSSTranslator.call(this, source, opts);
};
BorschikCSSTranslator.grammarName = "BorschikCSSTranslator";
View
8 lib/util.js
@@ -53,3 +53,11 @@ exports.stringToBoolean = function(s, def) {
if (s == 'no' || s == 'false') return false;
return !!def;
};
+
+exports.isLinkProcessable = function(url) {
+ return !(~['#', '?', '/'].indexOf(url.charAt(0)) || isAbsoluteUrl(url));
+};
+
+function isAbsoluteUrl(url) {
+ return /^\w+:/.test(url);
+}
View
20 test/freeze-test.js
@@ -137,16 +137,16 @@ describe('freeze from .css (-t css)', function() {
testFreeze('css', 'freeze_from_css', 'test.css', '_test.css', 'ok_css.css', true, false);
});
-describe('freeze from .css (-t css-fast)', function() {
- testFreeze('css-fast', 'freeze_from_css', 'test.css', '_test.css', 'ok_css.css', true, false);
+describe('freeze from .css (-t css-ometajs)', function() {
+ testFreeze('css-ometajs', 'freeze_from_css', 'test.css', '_test.css', 'ok_css.css', true, false);
});
describe('freeze excepts from .css (-t css)', function() {
testFreeze('css', 'freeze_excepts', 'test.css', '_test.css', 'ok_css.css', true, false);
});
-describe('freeze excepts from .css (-t css-fast)', function() {
- testFreeze('css-fast', 'freeze_excepts', 'test.css', '_test.css', 'ok_css.css', true, false);
+describe('freeze excepts from .css (-t css-ometajs)', function() {
+ testFreeze('css-ometajs', 'freeze_excepts', 'test.css', '_test.css', 'ok_css.css', true, false);
});
describe('followSymlinks', function() {
@@ -232,8 +232,8 @@ describe('CSSO yes, tech css', function() {
testFreeze('css', 'csso_test', 'a.css', '_a.css', 'ok_css.css', true, true);
});
-describe('CSSO yes, tech css-fast', function() {
- testFreeze('css-fast', 'csso_test', 'a.css', '_a.css', 'ok_css.css', true, true);
+describe('CSSO yes, tech css-ometajs', function() {
+ testFreeze('css-ometajs', 'csso_test', 'a.css', '_a.css', 'ok_css.css', true, true);
});
function testJS(tech, dir, inPath, outPath, okPath) {
@@ -261,3 +261,11 @@ describe('UglifyJS yes, tech js', function() {
describe('UglifyJS yes, tech js+coffee', function() {
testJS('js+coffee', 'uglifyjs_test', 'test.coffee', '_test.js', 'ok_jscoffee.js');
});
+
+describe('freeze AlphaImageLoader from .css (-t css)', function() {
+ testFreeze('css', 'freeze_alphaimageloader', 'test.css', '_test.css', 'ok_css.css', true, false);
+});
+
+describe('freeze AlphaImageLoader from .css (-t css-ometajs)', function() {
+ testFreeze('css-ometajs', 'freeze_alphaimageloader', 'test.css', '_test.css', 'ok_css.css', true, false);
+});
View
9 test/freeze_alphaimageloader/.borschik
@@ -0,0 +1,9 @@
+{
+ "paths" : {
+ "./": "//a/b/"
+ },
+
+ "freeze_paths" : {
+ "./": "test/test2/"
+ }
+}
View
4 test/freeze_alphaimageloader/ok_css.css
@@ -0,0 +1,4 @@
+a {
+ filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://test/test.png',sizingMethod=crop);
+ filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="//a/b/test/test2/wFPs-e1B3wMRud8TzGw7YHjS08I.png",sizingMethod=crop);
+}
View
4 test/freeze_alphaimageloader/test.css
@@ -0,0 +1,4 @@
+a {
+ filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://test/test.png',sizingMethod=crop);
+ filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='test.png',sizingMethod=crop);
+}
View
1 test/freeze_alphaimageloader/test.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1a7c29a

Please sign in to comment.
Something went wrong with that request. Please try again.