2013-03-18 23:34:52 +00:00
|
|
|
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
|
2011-12-23 07:38:08 +00:00
|
|
|
/*
|
|
|
|
|
2013-03-27 22:17:37 +00:00
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2007-2013 Einar Lielmanis and contributors.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person
|
|
|
|
obtaining a copy of this software and associated documentation files
|
|
|
|
(the "Software"), to deal in the Software without restriction,
|
|
|
|
including without limitation the rights to use, copy, modify, merge,
|
|
|
|
publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
|
|
|
|
|
2011-12-23 07:38:08 +00:00
|
|
|
CSS Beautifier
|
|
|
|
---------------
|
|
|
|
|
|
|
|
Written by Harutyun Amirjanyan, (amirjanyan@gmail.com)
|
|
|
|
|
|
|
|
Based on code initially developed by: Einar Lielmanis, <elfz@laacz.lv>
|
|
|
|
http://jsbeautifier.org/
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
css_beautify(source_text);
|
|
|
|
css_beautify(source_text, options);
|
|
|
|
|
|
|
|
The options are:
|
|
|
|
indent_size (default 4) — indentation size,
|
|
|
|
indent_char (default space) — character to indent with,
|
|
|
|
|
|
|
|
e.g
|
|
|
|
|
|
|
|
css_beautify(css_source_text, {
|
|
|
|
'indent_size': 1,
|
|
|
|
'indent_char': '\t'
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
// http://www.w3.org/TR/CSS21/syndata.html#tokenization
|
2011-12-23 07:52:41 +00:00
|
|
|
// http://www.w3.org/TR/css3-syntax/
|
2011-12-23 07:38:08 +00:00
|
|
|
function css_beautify(source_text, options) {
|
|
|
|
options = options || {};
|
|
|
|
var indentSize = options.indent_size || 4;
|
|
|
|
var indentCharacter = options.indent_char || ' ';
|
|
|
|
|
|
|
|
// compatibility
|
2013-03-18 23:34:52 +00:00
|
|
|
if (typeof indentSize === "string") {
|
2013-03-18 23:29:44 +00:00
|
|
|
indentSize = parseInt(indentSize, 10);
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
2011-12-23 07:38:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
// tokenizer
|
|
|
|
var whiteRe = /^\s+$/;
|
|
|
|
var wordRe = /[\w$\-_]/;
|
|
|
|
|
|
|
|
var pos = -1, ch;
|
|
|
|
function next() {
|
2013-03-18 23:29:44 +00:00
|
|
|
ch = source_text.charAt(++pos);
|
|
|
|
return ch;
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
|
|
|
function peek() {
|
2013-03-18 23:29:44 +00:00
|
|
|
return source_text.charAt(pos+1);
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
|
|
|
function eatString(comma) {
|
2011-12-23 07:52:41 +00:00
|
|
|
var start = pos;
|
2011-12-23 07:38:08 +00:00
|
|
|
while(next()){
|
2013-03-18 23:34:52 +00:00
|
|
|
if (ch === "\\"){
|
2011-12-23 07:38:08 +00:00
|
|
|
next();
|
2011-12-23 07:52:41 +00:00
|
|
|
next();
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === comma) {
|
2011-12-23 07:52:41 +00:00
|
|
|
break;
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === "\n") {
|
2011-12-23 07:52:41 +00:00
|
|
|
break;
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-23 07:52:41 +00:00
|
|
|
return source_text.substring(start, pos + 1);
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function eatWhitespace() {
|
2011-12-23 07:52:41 +00:00
|
|
|
var start = pos;
|
2013-03-18 23:34:52 +00:00
|
|
|
while (whiteRe.test(peek())) {
|
2011-12-23 07:52:41 +00:00
|
|
|
pos++;
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
|
|
|
return pos !== start;
|
2011-12-23 07:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function skipWhitespace() {
|
|
|
|
var start = pos;
|
|
|
|
do{
|
2013-03-18 23:29:44 +00:00
|
|
|
}while (whiteRe.test(next()));
|
2013-03-18 23:34:52 +00:00
|
|
|
return pos !== start + 1;
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function eatComment() {
|
|
|
|
var start = pos;
|
2011-12-23 07:52:41 +00:00
|
|
|
next();
|
2011-12-23 07:38:08 +00:00
|
|
|
while (next()) {
|
2013-03-18 23:34:52 +00:00
|
|
|
if (ch === "*" && peek() === "/") {
|
2011-12-23 07:52:41 +00:00
|
|
|
pos ++;
|
2011-12-23 07:38:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-23 07:52:41 +00:00
|
|
|
return source_text.substring(start, pos + 1);
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-15 07:26:57 +00:00
|
|
|
function lookBack(str) {
|
2013-03-18 23:34:52 +00:00
|
|
|
return source_text.substring(pos-str.length, pos).toLowerCase() === str;
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// printer
|
|
|
|
var indentString = source_text.match(/^[\r\n]*[\t ]*/)[0];
|
|
|
|
var singleIndent = Array(indentSize + 1).join(indentCharacter);
|
|
|
|
var indentLevel = 0;
|
|
|
|
function indent() {
|
2011-12-23 07:52:41 +00:00
|
|
|
indentLevel++;
|
|
|
|
indentString += singleIndent;
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
|
|
|
function outdent() {
|
2011-12-23 07:52:41 +00:00
|
|
|
indentLevel--;
|
|
|
|
indentString = indentString.slice(0, -indentSize);
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 23:29:44 +00:00
|
|
|
var print = {};
|
2011-12-23 07:38:08 +00:00
|
|
|
print["{"] = function(ch) {
|
2011-12-23 07:52:41 +00:00
|
|
|
print.singleSpace();
|
|
|
|
output.push(ch);
|
|
|
|
print.newLine();
|
2013-03-18 23:29:44 +00:00
|
|
|
};
|
2011-12-23 07:38:08 +00:00
|
|
|
print["}"] = function(ch) {
|
2011-12-23 07:52:41 +00:00
|
|
|
print.newLine();
|
|
|
|
output.push(ch);
|
|
|
|
print.newLine();
|
2013-03-18 23:29:44 +00:00
|
|
|
};
|
2011-12-23 07:38:08 +00:00
|
|
|
|
|
|
|
print.newLine = function(keepWhitespace) {
|
2013-03-18 23:34:52 +00:00
|
|
|
if (!keepWhitespace) {
|
|
|
|
while (whiteRe.test(output[output.length - 1])) {
|
2011-12-23 07:52:41 +00:00
|
|
|
output.pop();
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-23 07:38:08 +00:00
|
|
|
|
2013-03-18 23:34:52 +00:00
|
|
|
if (output.length) {
|
2011-12-26 08:48:56 +00:00
|
|
|
output.push('\n');
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
|
|
|
if (indentString) {
|
2011-12-23 07:52:41 +00:00
|
|
|
output.push(indentString);
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
2013-03-18 23:29:44 +00:00
|
|
|
};
|
2011-12-23 07:38:08 +00:00
|
|
|
print.singleSpace = function() {
|
2013-03-18 23:34:52 +00:00
|
|
|
if (output.length && !whiteRe.test(output[output.length - 1])) {
|
2011-12-23 07:52:41 +00:00
|
|
|
output.push(' ');
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
2013-03-18 23:29:44 +00:00
|
|
|
};
|
2011-12-23 07:38:08 +00:00
|
|
|
var output = [];
|
2013-03-18 23:34:52 +00:00
|
|
|
if (indentString) {
|
2011-12-23 07:52:41 +00:00
|
|
|
output.push(indentString);
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
2011-12-23 07:38:08 +00:00
|
|
|
/*_____________________--------------------_____________________*/
|
|
|
|
|
|
|
|
while(true) {
|
2011-12-23 07:52:41 +00:00
|
|
|
var isAfterSpace = skipWhitespace();
|
2011-12-23 07:38:08 +00:00
|
|
|
|
2013-03-18 23:34:52 +00:00
|
|
|
if (!ch) {
|
2011-12-23 07:38:08 +00:00
|
|
|
break;
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
|
|
|
|
2011-12-23 07:38:08 +00:00
|
|
|
|
2013-03-18 23:34:52 +00:00
|
|
|
if (ch === '{') {
|
2011-12-23 07:52:41 +00:00
|
|
|
indent();
|
|
|
|
print["{"](ch);
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === '}') {
|
2011-12-23 07:52:41 +00:00
|
|
|
outdent();
|
|
|
|
print["}"](ch);
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === '"' || ch === '\'') {
|
2013-03-18 23:29:44 +00:00
|
|
|
output.push(eatString(ch));
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === ';') {
|
2011-12-23 07:52:41 +00:00
|
|
|
output.push(ch, '\n', indentString);
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === '/' && peek() === '*') { // comment
|
2011-12-23 07:52:41 +00:00
|
|
|
print.newLine();
|
|
|
|
output.push(eatComment(), "\n", indentString);
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === '(') { // may be a url
|
2013-01-15 07:26:57 +00:00
|
|
|
if (lookBack("url")) {
|
2012-11-05 20:15:58 +00:00
|
|
|
output.push(ch);
|
|
|
|
eatWhitespace();
|
|
|
|
if (next()) {
|
2013-03-18 23:34:52 +00:00
|
|
|
if (ch !== ')' && ch !== '"' && ch !== '\'') {
|
2011-12-23 07:52:41 +00:00
|
|
|
output.push(eatString(')'));
|
2013-03-18 23:34:52 +00:00
|
|
|
} else {
|
2011-12-23 07:52:41 +00:00
|
|
|
pos--;
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
2012-11-05 20:15:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-03-18 23:34:52 +00:00
|
|
|
if (isAfterSpace) {
|
2012-11-05 20:15:58 +00:00
|
|
|
print.singleSpace();
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
2012-11-05 20:15:58 +00:00
|
|
|
output.push(ch);
|
|
|
|
eatWhitespace();
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === ')') {
|
2011-12-23 07:52:41 +00:00
|
|
|
output.push(ch);
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === ',') {
|
2011-12-23 07:52:41 +00:00
|
|
|
eatWhitespace();
|
|
|
|
output.push(ch);
|
|
|
|
print.singleSpace();
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === ']') {
|
2011-12-23 07:52:41 +00:00
|
|
|
output.push(ch);
|
2013-03-18 23:34:52 +00:00
|
|
|
} else if (ch === '[' || ch === '=') { // no whitespace before or after
|
2011-12-23 07:52:41 +00:00
|
|
|
eatWhitespace();
|
|
|
|
output.push(ch);
|
2011-12-23 07:38:08 +00:00
|
|
|
} else {
|
2013-03-18 23:34:52 +00:00
|
|
|
if (isAfterSpace) {
|
2011-12-23 07:52:41 +00:00
|
|
|
print.singleSpace();
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|
2011-12-23 07:38:08 +00:00
|
|
|
|
2011-12-23 07:52:41 +00:00
|
|
|
output.push(ch);
|
2011-12-23 07:38:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var sweetCode = output.join('').replace(/[\n ]+$/, '');
|
|
|
|
return sweetCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-18 23:38:44 +00:00
|
|
|
if (typeof exports !== 'undefined') {
|
2011-12-23 07:38:08 +00:00
|
|
|
exports.css_beautify = css_beautify;
|
2013-03-18 23:34:52 +00:00
|
|
|
}
|