* Adds EscapeStore helper class to aid escaping and restoring.
* Necessary for selector- and property-level optimizations.
+2.0.0 / 2013-xx-xx (UNRELEASED)
+==================
+
+* Adds simplified and more advanced text escaping / restoring via EscapeStore class.
+
1.1.1 / 2013-09-07
==================
+var EscapeStore = require('./escape-store');
+
module.exports = function Comments(keepSpecialComments, keepBreaks, lineBreak) {
- var comments = [];
+ var comments = new EscapeStore('CSSCOMMENT');
return {
// Strip special comments (/*! ... */) by replacing them by __CSSCOMMENT__ marker
tempData.push(data.substring(cursor, nextStart));
if (data[nextStart + 2] == '!') {
// in case of special comments, replace them with a placeholder
- comments.push(data.substring(nextStart, nextEnd + 2));
- tempData.push('__CSSCOMMENT__');
+ var comment = data.substring(nextStart, nextEnd + 2);
+ var placeholder = comments.store(comment);
+ tempData.push(placeholder);
}
cursor = nextEnd + 2;
}
},
restore: function(data) {
- var commentsCount = comments.length;
+ var restored = 0;
var breakSuffix = keepBreaks ? lineBreak : '';
- return data.replace(new RegExp('__CSSCOMMENT__(' + lineBreak + '| )?', 'g'), function() {
+ return data.replace(new RegExp(comments.placeholderPattern + '(' + lineBreak + '| )?', 'g'), function(match, placeholder) {
+ restored++;
+
switch (keepSpecialComments) {
case '*':
- return comments.shift() + breakSuffix;
+ return comments.restore(placeholder) + breakSuffix;
case 1:
- return comments.length == commentsCount ?
- comments.shift() + breakSuffix :
+ return restored == 1 ?
+ comments.restore(placeholder) + breakSuffix :
'';
case 0:
return '';
--- /dev/null
+module.exports = function EscapeStore(placeholderRoot) {
+ var placeholderToData = {};
+ var dataToPlaceholder = {};
+ var count = 0;
+ var nextPlaceholder = function() {
+ return '__' + placeholderRoot + (count++) + '__';
+ };
+ var pattern = '(__' + placeholderRoot + '\\d{1,}__)';
+
+ return {
+ placeholderPattern: pattern,
+
+ placeholderRegExp: new RegExp(pattern, 'g'),
+
+ store: function(data) {
+ var placeholder = dataToPlaceholder[data];
+ if (!placeholder) {
+ placeholder = nextPlaceholder();
+ placeholderToData[placeholder] = data;
+ dataToPlaceholder[data] = placeholder;
+ }
+
+ return placeholder;
+ },
+
+ restore: function(placeholder) {
+ return placeholderToData[placeholder];
+ }
+ };
+};
+var EscapeStore = require('./escape-store');
+
module.exports = function Expressions() {
- var expressions = [];
+ var expressions = new EscapeStore('EXPRESSION');
var findEnd = function(data, start) {
var end = start + 'expression'.length;
nextEnd = findEnd(data, nextStart);
+ var expression = data.substring(nextStart, nextEnd);
+ var placeholder = expressions.store(expression);
tempData.push(data.substring(cursor, nextStart));
- tempData.push('__EXPRESSION__');
- expressions.push(data.substring(nextStart, nextEnd));
+ tempData.push(placeholder);
cursor = nextEnd;
}
},
restore: function(data) {
- return data.replace(/__EXPRESSION__/g, function() {
- return expressions.shift();
- });
+ return data.replace(expressions.placeholderRegExp, expressions.restore);
}
};
};
+var EscapeStore = require('./escape-store');
+
module.exports = function Free() {
- var texts = [];
+ var texts = new EscapeStore('CSSFREETEXT');
return {
// Strip content tags by replacing them by the __CSSFREETEXT__
if (nextStart == -1 || nextEnd == -1)
break;
+ var text = data.substring(nextStart, nextEnd + 1);
+ var placeholder = texts.store(text);
tempData.push(data.substring(cursor, nextStart));
- tempData.push('__CSSFREETEXT__');
- texts.push(data.substring(nextStart, nextEnd + 1));
+ tempData.push(placeholder);
cursor = nextEnd + 1;
}
},
restore: function(data) {
- return data.replace(/__CSSFREETEXT__/g, function() {
- return texts.shift();
- });
+ return data.replace(texts.placeholderRegExp, texts.restore);
}
};
};
+var EscapeStore = require('./escape-store');
+
module.exports = function Urls() {
- var urls = [];
+ var urls = new EscapeStore('URL');
return {
// Strip urls by replacing them by the __URL__
nextEnd = data.indexOf(')', nextStart);
+ var url = data.substring(nextStart, nextEnd + 1);
+ var placeholder = urls.store(url);
tempData.push(data.substring(cursor, nextStart));
- tempData.push('__URL__');
- urls.push(data.substring(nextStart, nextEnd + 1));
+ tempData.push(placeholder);
cursor = nextEnd + 1;
}
},
restore: function(data) {
- return data.replace(/__URL__/g, function() {
- return urls.shift();
- });
+ return data.replace(urls.placeholderRegExp, urls.restore);
}
};
};