var flatBlock = /(^@(font\-face|page|\-ms\-viewport|\-o\-viewport|viewport|counter\-style)|\\@.+?)/;
var WHITESPACE = /\s/g;
var MULTI_WHITESPACE = /\s{2,}/g;
-var WHITESPACE_COLON = / ?: ?/g;
-var WHITESPACE_SEMICOLON = / ?; ?/g;
var WHITESPACE_COMMA = / ?, ?/g;
-var WHITESPACE_PREFIX = /([\(,]) /g;
-var WHITESPACE_SUFFIX = / ([\),])/g;
-var MULTI_SEMICOLON = /;{2,}/g;
-var TRAILING_SEMICOLON = /;$/;
function Tokenizer(minifyContext) {
this.minifyContext = minifyContext;
}
Tokenizer.prototype.toTokens = function (data) {
+ data = data.replace(/\r\n/g, '\n');
+
var chunker = new Chunker(data, '}', 128);
if (chunker.isEmpty())
return [];
return tokenize(context);
};
+function valueMapper(property) { return { value: property }; }
+
function extractProperties(string) {
- var extracted = string
- .replace(WHITESPACE, ' ')
- .replace(MULTI_WHITESPACE, ' ')
- .replace(WHITESPACE_COLON, ':')
- .replace(WHITESPACE_PREFIX, '$1')
- .replace(WHITESPACE_SUFFIX, '$1')
- .replace(WHITESPACE_SEMICOLON, ';')
- .replace(MULTI_SEMICOLON, ';')
- .trim()
- .replace(TRAILING_SEMICOLON, '');
-
- return extracted.length > 0 ?
- extracted.split(';').map(function (property) { return { value: property }; }) :
- [];
+ var properties = [];
+ var buffer = [];
+ var isWhitespace;
+ var wasWhitespace;
+ var isSpecial;
+ var wasSpecial;
+ var current;
+ var wasCloseParenthesis;
+
+ for (var i = 0, l = string.length; i < l; i++) {
+ current = string[i];
+
+ if (current === ';') {
+ if (wasWhitespace && buffer[buffer.length - 1] === ' ')
+ buffer.pop();
+ if (buffer.length > 0)
+ properties.push({ value: buffer.join('') });
+ buffer = [];
+ } else {
+ isWhitespace = current === ' ' || current === '\t' || current === '\n';
+ isSpecial = current === ':' || current === '[' || current === ']' || current === ',' || current === '(' || current === ')';
+
+ if (wasWhitespace && isSpecial) {
+ buffer.pop();
+ buffer.push(current);
+ } else if (isWhitespace && wasSpecial && !wasCloseParenthesis) {
+ } else if (isWhitespace && !wasWhitespace && buffer.length > 0) {
+ buffer.push(' ');
+ } else if (isWhitespace && buffer.length === 0) {
+ } else if (isWhitespace && wasWhitespace) {
+ } else {
+ buffer.push(isWhitespace ? ' ' : current);
+ }
+ }
+
+ wasSpecial = isSpecial;
+ wasWhitespace = isWhitespace;
+ wasCloseParenthesis = current === ')';
+ }
+
+ if (wasWhitespace && buffer[buffer.length - 1] === ' ')
+ buffer.pop();
+ if (buffer.length > 0)
+ properties.push({ value: buffer.join('') });
+
+ return properties;
}
function extractSelectors(string) {
.replace(WHITESPACE_COMMA, ',')
.trim();
- return new Splitter(',').split(extracted).map(function (selector) {
- return { value: selector };
- });
+ return new Splitter(',').split(extracted).map(valueMapper);
}
function extractBlock(string) {