fuchsia: '#f0f',
yellow: '#ff0'
},
-
+
process: function(data) {
var specialComments = [],
contentBlocks = [];
var start = new Date().getTime();
data = data.replace(pattern, replacement);
var end = new Date().getTime();
-
+
if (end > start)
util.print(pattern + ' -> ' + (end - start) + ' milliseconds\n');
}
};
-
+
// strip comments one by one
for (var end = 0; end < data.length; ) {
var start = data.indexOf('/*', end);
end = data.indexOf('*/', start);
if (start == -1 || end == -1) break;
-
+
if (data[start + 2] == '!') {
// in case of special comments, replace them with a placeholder
specialComments.push(data.substring(start, end + 2));
}
end = start;
}
-
+
// replace content: with a placeholder
for (var end = 0; end < data.length; ) {
var start = data.indexOf('content', end);
if (start == -1) break;
-
+
var wrapper = /[^ :]/.exec(data.substring(start + 7))[0];
if (/['"]/.test(wrapper) == false) {
end = start + 7;
var firstIndex = data.indexOf(wrapper, start);
var lastIndex = data.indexOf(wrapper, firstIndex + 1);
-
+
contentBlocks.push(data.substring(firstIndex, lastIndex + 1));
data = data.substring(0, firstIndex) + '__CSSCONTENT__' + data.substring(lastIndex + 1);
end = lastIndex + 1;
}
-
+
replace(/;\s*;+/g, ';') // whitespace between semicolons & multiple semicolons
replace(/\n/g, '') // line breaks
replace(/\s+/g, ' ') // multiple whitespace
replace(/\*([\.#:\[])/g, '$1') // remove universal selector when not needed (*#id, *.class etc)
replace(/ {/g, '{') // whitespace before definition
replace(/\} /g, '}') // whitespace after definition
-
+
// Get the special comments, content content, and spaces inside calc back
replace(/calc\([^\}]+\}/g, function(match) {
return match.replace(/\+/g, ' + ');
});
replace(/__CSSCOMMENT__/g, function() { return specialComments.shift(); });
replace(/__CSSCONTENT__/g, function() { return contentBlocks.shift(); });
-
+
return data.trim() // trim spaces at beginning and end
}
};