2 * vkBeautify - javascript plugin to pretty-print or minify text in XML, JSON, CSS and SQL formats.
4 * Version - 0.99.00.beta
5 * Copyright (c) 2012 Vadim Kiryukhin
6 * vkiryukhin @ gmail.com
7 * http://www.eslinstructor.net/vkbeautify/
9 * Dual licensed under the MIT and GPL licenses:
10 * http://www.opensource.org/licenses/mit-license.php
11 * http://www.gnu.org/licenses/gpl.html
15 * vkbeautify.xml(text [,indent_pattern]);
16 * vkbeautify.json(text [,indent_pattern]);
17 * vkbeautify.css(text [,indent_pattern]);
18 * vkbeautify.sql(text [,indent_pattern]);
20 * @text - String; text to beatufy;
21 * @indent_pattern - Integer | String;
22 * Integer: number of white spaces;
23 * String: character string to visualize indentation ( can also be a set of white spaces )
26 * vkbeautify.xmlmin(text [,preserve_comments]);
27 * vkbeautify.jsonmin(text);
28 * vkbeautify.cssmin(text [,preserve_comments]);
29 * vkbeautify.sqlmin(text);
31 * @text - String; text to minify;
32 * @preserve_comments - Bool; [optional];
33 * Set this flag to true to prevent removing comments from @text ( minxml and mincss functions only. )
36 * vkbeautify.xml(text); // pretty print XML
37 * vkbeautify.json(text, 4 ); // pretty print JSON
38 * vkbeautify.css(text, '. . . .'); // pretty print CSS
39 * vkbeautify.sql(text, '----'); // pretty print SQL
41 * vkbeautify.xmlmin(text, true);// minify XML, preserve comments
42 * vkbeautify.jsonmin(text);// minify JSON
43 * vkbeautify.cssmin(text);// minify CSS, remove comments ( default )
44 * vkbeautify.sqlmin(text);// minify SQL
50 function createShiftArr(step) {
54 if ( isNaN(parseInt(step)) ) { // argument is string
56 } else { // argument is integer
58 case 1: space = ' '; break;
59 case 2: space = ' '; break;
60 case 3: space = ' '; break;
61 case 4: space = ' '; break;
62 case 5: space = ' '; break;
63 case 6: space = ' '; break;
64 case 7: space = ' '; break;
65 case 8: space = ' '; break;
66 case 9: space = ' '; break;
67 case 10: space = ' '; break;
68 case 11: space = ' '; break;
69 case 12: space = ' '; break;
73 var shift = ['\n']; // array of shifts
74 for(ix=0;ix<100;ix++){
75 shift.push(shift[ix]+space);
80 function vkbeautify(){
81 this.step = ' '; // 4 spaces
82 this.shift = createShiftArr(this.step);
85 vkbeautify.prototype.xml = function(text,step) {
87 var ar = text.replace(/>\s{0,}</g,"><")
88 .replace(/</g,"~::~<")
89 .replace(/\s*xmlns\:/g,"~::~xmlns:")
90 .replace(/\s*xmlns\=/g,"~::~xmlns=")
97 shift = step ? createShiftArr(step) : this.shift;
99 for(ix=0;ix<len;ix++) {
100 // start comment or <![CDATA[...]]> or <!DOCTYPE //
101 if(ar[ix].search(/<!/) > -1) {
102 str += shift[deep]+ar[ix];
104 // end comment or <![CDATA[...]]> //
105 if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) {
109 // end comment or <![CDATA[...]]> //
110 if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1) {
115 if( /^<\w/.exec(ar[ix-1]) && /^<\/\w/.exec(ar[ix]) &&
116 /^<[\w:\-\.\,]+/.exec(ar[ix-1]) == /^<\/[\w:\-\.\,]+/.exec(ar[ix])[0].replace('/','')) {
118 if(!inComment) deep--;
121 if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) == -1 && ar[ix].search(/\/>/) == -1 ) {
122 str = !inComment ? str += shift[deep++]+ar[ix] : str += ar[ix];
125 if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) > -1) {
126 str = !inComment ? str += shift[deep]+ar[ix] : str += ar[ix];
129 if(ar[ix].search(/<\//) > -1) {
130 str = !inComment ? str += shift[--deep]+ar[ix] : str += ar[ix];
133 if(ar[ix].search(/\/>/) > -1 ) {
134 str = !inComment ? str += shift[deep]+ar[ix] : str += ar[ix];
137 if(ar[ix].search(/<\?/) > -1) {
138 str += shift[deep]+ar[ix];
141 if( ar[ix].search(/xmlns\:/) > -1 || ar[ix].search(/xmlns\=/) > -1) {
142 str += shift[deep]+ar[ix];
150 return (str[0] == '\n') ? str.slice(1) : str;
153 vkbeautify.prototype.json = function(text,step) {
155 var step = step ? step : this.step;
157 if (typeof JSON === 'undefined' ) return text;
159 if ( typeof text === "string" ) return JSON.stringify(JSON.parse(text), null, step);
160 if ( typeof text === "object" ) return JSON.stringify(text, null, step);
162 return text; // text is not string nor object
165 vkbeautify.prototype.css = function(text, step) {
167 var ar = text.replace(/\s{1,}/g,' ')
168 .replace(/\{/g,"{~::~")
169 .replace(/\}/g,"~::~}~::~")
170 .replace(/\;/g,";~::~")
171 .replace(/\/\*/g,"~::~/*")
172 .replace(/\*\//g,"*/~::~")
173 .replace(/~::~\s{0,}~::~/g,"~::~")
179 shift = step ? createShiftArr(step) : this.shift;
181 for(ix=0;ix<len;ix++) {
183 if( /\{/.exec(ar[ix])) {
184 str += shift[deep++]+ar[ix];
186 if( /\}/.exec(ar[ix])) {
187 str += shift[--deep]+ar[ix];
189 if( /\*\\/.exec(ar[ix])) {
190 str += shift[deep]+ar[ix];
193 str += shift[deep]+ar[ix];
196 return str.replace(/^\n{1,}/,'');
199 //----------------------------------------------------------------------------
201 function isSubquery(str, parenthesisLevel) {
202 return parenthesisLevel - (str.replace(/\(/g,'').length - str.replace(/\)/g,'').length )
205 function split_sql(str, tab) {
207 return str.replace(/\s{1,}/g," ")
209 .replace(/ AND /ig,"~::~"+tab+tab+"AND ")
210 .replace(/ BETWEEN /ig,"~::~"+tab+"BETWEEN ")
211 .replace(/ CASE /ig,"~::~"+tab+"CASE ")
212 .replace(/ ELSE /ig,"~::~"+tab+"ELSE ")
213 .replace(/ END /ig,"~::~"+tab+"END ")
214 .replace(/ FROM /ig,"~::~FROM ")
215 .replace(/ GROUP\s{1,}BY/ig,"~::~GROUP BY ")
216 .replace(/ HAVING /ig,"~::~HAVING ")
217 //.replace(/ SET /ig," SET~::~")
218 .replace(/ IN /ig," IN ")
220 .replace(/ JOIN /ig,"~::~JOIN ")
221 .replace(/ CROSS~::~{1,}JOIN /ig,"~::~CROSS JOIN ")
222 .replace(/ INNER~::~{1,}JOIN /ig,"~::~INNER JOIN ")
223 .replace(/ LEFT~::~{1,}JOIN /ig,"~::~LEFT JOIN ")
224 .replace(/ RIGHT~::~{1,}JOIN /ig,"~::~RIGHT JOIN ")
226 .replace(/ ON /ig,"~::~"+tab+"ON ")
227 .replace(/ OR /ig,"~::~"+tab+tab+"OR ")
228 .replace(/ ORDER\s{1,}BY/ig,"~::~ORDER BY ")
229 .replace(/ OVER /ig,"~::~"+tab+"OVER ")
231 .replace(/\(\s{0,}SELECT /ig,"~::~(SELECT ")
232 .replace(/\)\s{0,}SELECT /ig,")~::~SELECT ")
234 .replace(/ THEN /ig," THEN~::~"+tab+"")
235 .replace(/ UNION /ig,"~::~UNION~::~")
236 .replace(/ USING /ig,"~::~USING ")
237 .replace(/ WHEN /ig,"~::~"+tab+"WHEN ")
238 .replace(/ WHERE /ig,"~::~WHERE ")
239 .replace(/ WITH /ig,"~::~WITH ")
241 //.replace(/\,\s{0,}\(/ig,",~::~( ")
242 //.replace(/\,/ig,",~::~"+tab+tab+"")
244 .replace(/ ALL /ig," ALL ")
245 .replace(/ AS /ig," AS ")
246 .replace(/ ASC /ig," ASC ")
247 .replace(/ DESC /ig," DESC ")
248 .replace(/ DISTINCT /ig," DISTINCT ")
249 .replace(/ EXISTS /ig," EXISTS ")
250 .replace(/ NOT /ig," NOT ")
251 .replace(/ NULL /ig," NULL ")
252 .replace(/ LIKE /ig," LIKE ")
253 .replace(/\s{0,}SELECT /ig,"SELECT ")
254 .replace(/\s{0,}UPDATE /ig,"UPDATE ")
255 .replace(/ SET /ig," SET ")
257 .replace(/~::~{1,}/g,"~::~")
261 vkbeautify.prototype.sql = function(text,step) {
263 var ar_by_quote = text.replace(/\s{1,}/g," ")
264 .replace(/\'/ig,"~::~\'")
266 len = ar_by_quote.length,
269 tab = this.step,//+this.step,
272 parenthesisLevel = 0,
275 shift = step ? createShiftArr(step) : this.shift;;
277 for(ix=0;ix<len;ix++) {
279 ar = ar.concat(ar_by_quote[ix]);
281 ar = ar.concat(split_sql(ar_by_quote[ix], tab) );
286 for(ix=0;ix<len;ix++) {
288 parenthesisLevel = isSubquery(ar[ix], parenthesisLevel);
290 if( /\s{0,}\s{0,}SELECT\s{0,}/.exec(ar[ix])) {
291 ar[ix] = ar[ix].replace(/\,/g,",\n"+tab+tab+"")
294 if( /\s{0,}\s{0,}SET\s{0,}/.exec(ar[ix])) {
295 ar[ix] = ar[ix].replace(/\,/g,",\n"+tab+tab+"")
298 if( /\s{0,}\(\s{0,}SELECT\s{0,}/.exec(ar[ix])) {
300 str += shift[deep]+ar[ix];
302 if( /\'/.exec(ar[ix]) ) {
303 if(parenthesisLevel<1 && deep) {
309 str += shift[deep]+ar[ix];
310 if(parenthesisLevel<1 && deep) {
317 str = str.replace(/^\n{1,}/,'').replace(/\n{1,}/g,"\n");
322 vkbeautify.prototype.xmlmin = function(text, preserveComments) {
324 var str = preserveComments ? text
325 : text.replace(/\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>/g,"")
326 .replace(/[ \r\n\t]{1,}xmlns/g, ' xmlns');
327 return str.replace(/>\s{0,}</g,"><");
330 vkbeautify.prototype.jsonmin = function(text) {
332 if (typeof JSON === 'undefined' ) return text;
334 return JSON.stringify(JSON.parse(text), null, 0);
338 vkbeautify.prototype.cssmin = function(text, preserveComments) {
340 var str = preserveComments ? text
341 : text.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\//g,"") ;
343 return str.replace(/\s{1,}/g,' ')
344 .replace(/\{\s{1,}/g,"{")
345 .replace(/\}\s{1,}/g,"}")
346 .replace(/\;\s{1,}/g,";")
347 .replace(/\/\*\s{1,}/g,"/*")
348 .replace(/\*\/\s{1,}/g,"*/");
351 vkbeautify.prototype.sqlmin = function(text) {
352 return text.replace(/\s{1,}/g," ").replace(/\s{1,}\(/,"(").replace(/\s{1,}\)/,")");
355 window.vkbeautify = new vkbeautify();