From daf9b4bdd19b4d27354e75c24d9930178a237cd5 Mon Sep 17 00:00:00 2001 From: Andreas Lubbe Date: Thu, 8 Jan 2015 19:15:52 +0100 Subject: [PATCH 1/4] test now checks ' ' case --- test/test_xml.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_xml.js b/test/test_xml.js index 7ac1322..6c86e3e 100644 --- a/test/test_xml.js +++ b/test/test_xml.js @@ -1,5 +1,5 @@ - -var xml = ' bbb ]]>', + +var xml = ' bbb ]]> ', pp_xml = require('../pretty-data').pd.xml(xml), pp_xmlmin_com = require('../pretty-data').pd.xmlmin(xml,true), pp_xmlmin = require('../pretty-data').pd.xmlmin(xml); From 2421ea53a62783562f56d1cc8b08a89129fbd8ee Mon Sep 17 00:00:00 2001 From: Andreas Lubbe Date: Thu, 8 Jan 2015 19:16:19 +0100 Subject: [PATCH 2/4] removed whitespace from file --- pretty-data.js | 108 ++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/pretty-data.js b/pretty-data.js index 6e53132..412d4d2 100644 --- a/pretty-data.js +++ b/pretty-data.js @@ -1,11 +1,11 @@ /** * pretty-data - nodejs plugin to pretty-print or minify data in XML, JSON and CSS formats. -* +* * Version - 0.40.0 * Copyright (c) 2012 Vadim Kiryukhin * vkiryukhin @ gmail.com * http://www.eslinstructor.net/pretty-data/ -* +* * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html @@ -15,20 +15,20 @@ * pd.css(data ) - pretty print CSS; * pd.sql(data) - pretty print SQL; * -* pd.xmlmin(data [, preserveComments] ) - minify XML; -* pd.jsonmin(data) - minify JSON; -* pd.cssmin(data [, preserveComments] ) - minify CSS; -* pd.sqlmin(data) - minify SQL; +* pd.xmlmin(data [, preserveComments] ) - minify XML; +* pd.jsonmin(data) - minify JSON; +* pd.cssmin(data [, preserveComments] ) - minify CSS; +* pd.sqlmin(data) - minify SQL; * * PARAMETERS: * * @data - String; XML, JSON, CSS or SQL text to beautify; -* @preserveComments - Bool (optional, used in minxml and mincss only); -* Set this flag to true to prevent removing comments from @text; +* @preserveComments - Bool (optional, used in minxml and mincss only); +* Set this flag to true to prevent removing comments from @text; * @Return - String; -* +* * USAGE: -* +* * var pd = require('pretty-data').pd; * * var xml_pp = pd.xml(xml_text); @@ -56,11 +56,11 @@ function pp() { // initialize array with shifts // for(ix=0;ix or -1) { + if(ar[ix].search(/ -1) { str += this.shift[deep]+ar[ix]; - inComment = true; + inComment = true; // end comment or // - if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) { - inComment = false; + if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) { + inComment = false; } - } else + } else // end comment or // - if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1) { + if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1) { str += ar[ix]; - inComment = false; - } else + inComment = false; + } else // // if( /^<\w/.exec(ar[ix-1]) && /^<\/\w/.exec(ar[ix]) && - /^<[\w:\-\.\,]+/.exec(ar[ix-1]) == /^<\/[\w:\-\.\,]+/.exec(ar[ix])[0].replace('/','')) { + /^<[\w:\-\.\,]+/.exec(ar[ix-1]) == /^<\/[\w:\-\.\,]+/.exec(ar[ix])[0].replace('/','')) { str += ar[ix]; if(!inComment) deep--; } else // // if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) == -1 && ar[ix].search(/\/>/) == -1 ) { str = !inComment ? str += this.shift[deep++]+ar[ix] : str += ar[ix]; - } else + } else // ... // if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) > -1) { str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; - } else + } else // // - if(ar[ix].search(/<\//) > -1) { + if(ar[ix].search(/<\//) > -1) { str = !inComment ? str += this.shift[--deep]+ar[ix] : str += ar[ix]; - } else + } else // // - if(ar[ix].search(/\/>/) > -1 ) { + if(ar[ix].search(/\/>/) > -1 ) { str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; - } else + } else // // - if(ar[ix].search(/<\?/) > -1) { + if(ar[ix].search(/<\?/) > -1) { str += this.shift[deep]+ar[ix]; - } else + } else // xmlns // - if( ar[ix].search(/xmlns\:/) > -1 || ar[ix].search(/xmlns\=/) > -1) { + if( ar[ix].search(/xmlns\:/) > -1 || ar[ix].search(/xmlns\=/) > -1) { str += this.shift[deep]+ar[ix]; - } - + } + else { str += ar[ix]; } } - + return (str[0] == '\n') ? str.slice(1) : str; } @@ -159,16 +159,16 @@ pp.prototype.css = function(text) { deep = 0, str = '', ix = 0; - + for(ix=0;ix/g,""); - return str.replace(/>\s{0,}<"); + return str.replace(/>\s{0,}<"); } pp.prototype.jsonmin = function(text) { - + return text.replace(/\s{0,}\{\s{0,}/g,"{") .replace(/\s{0,}\[$/g,"[") .replace(/\[\s{0,}/g,"[") @@ -310,11 +310,11 @@ pp.prototype.jsonmin = function(text) { .replace(/:\s{0,}\[/g,':[') .replace(/\,\s{0,}\[/g,',[') .replace(/\,\s{2,}/g,', ') - .replace(/\]\s{0,},\s{0,}\[/g,'],['); + .replace(/\]\s{0,},\s{0,}\[/g,'],['); } pp.prototype.cssmin = function(text, preserveComments) { - + var str = preserveComments ? text : text.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\//g,"") ; return str.replace(/\s{1,}/g,' ') @@ -323,7 +323,7 @@ pp.prototype.cssmin = function(text, preserveComments) { .replace(/\;\s{1,}/g,";") .replace(/\/\*\s{1,}/g,"/*") .replace(/\*\/\s{1,}/g,"*/"); -} +} pp.prototype.sqlmin = function(text) { return text.replace(/\s{1,}/g," ").replace(/\s{1,}\(/,"(").replace(/\s{1,}\)/,")"); @@ -331,7 +331,7 @@ pp.prototype.sqlmin = function(text) { // -------------------------------------------------------------------------------------------- -exports.pd= new pp; +exports.pd= new pp; From 9df27bc18f396d3a26603e7aefdd298825a9b200 Mon Sep 17 00:00:00 2001 From: Andreas Lubbe Date: Thu, 8 Jan 2015 19:16:55 +0100 Subject: [PATCH 3/4] changed xml regex to fix ' ' case --- pretty-data.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pretty-data.js b/pretty-data.js index 412d4d2..b4ac7b6 100644 --- a/pretty-data.js +++ b/pretty-data.js @@ -65,7 +65,7 @@ function pp() { pp.prototype.xml = function(text) { - var ar = text.replace(/>\s{0,}<") + var ar = text.replace(/>\s*<(?!\/)/g,"><") .replace(/ Date: Thu, 8 Jan 2015 20:48:05 +0100 Subject: [PATCH 4/4] beautifing XML more than once no longer changes the output --- pretty-data.js | 1 + test/test_xml.js | 2 ++ 2 files changed, 3 insertions(+) diff --git a/pretty-data.js b/pretty-data.js index b4ac7b6..44a47dc 100644 --- a/pretty-data.js +++ b/pretty-data.js @@ -66,6 +66,7 @@ function pp() { pp.prototype.xml = function(text) { var ar = text.replace(/>\s*<(?!\/)/g,"><") + .replace(/(\r\n|\n|\r)\s*/g, "") .replace(/