From fb6efb83d4a7cc30095f0be76e511e8ff1dbd89f Mon Sep 17 00:00:00 2001 From: Kristian Kraljic Date: Thu, 6 Aug 2020 15:16:23 +0200 Subject: [PATCH] Switch to minify-xml for xmlmin I would like to get rid of the phun on my README.md [1]. pretty-data is currently not minifying very much in regards to XML optimization. Thus I would like to suggest, you could switch to my library minify-xml. It is also based on simple regular expression replacements, thus it is fits very well to your library. I also took your "between tags" and "comment" regular expressions and adapted them. This change is compatible to existing usages of your API. If preserveComments is set, it will be forwarded to my library with the same behaviour. This is just a suggestion, but my library is optimizing many more things in regards to XML such as removing unnecessary whitespace in attributes, removing unused namespaces, collapsing empty elements. It also features a better CDATA handling, as the way you did it could easily fail when to be optimized things are part of CDATA. [1] https://github.com/kristian/minify-xml#readme --- .gitignore | 1 + package.json | 19 +++++++++++-------- pretty-data.js | 27 ++++++++++++++------------- 3 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/package.json b/package.json index 590a721..d61e4ae 100644 --- a/package.json +++ b/package.json @@ -13,18 +13,21 @@ "type": "git", "url": "http://github.com/vkiryukhin/pretty-data.git" }, - "main":"./pretty-data", + "main": "./pretty-data", "keywords": [ - "pretty print", - "beautify", - "minify", - "XML", - "JSON", - "CSS", - "SQL" + "pretty print", + "beautify", + "minify", + "XML", + "JSON", + "CSS", + "SQL" ], "license": "MIT", "engine": { "node": ">=0.4.9" + }, + "dependencies": { + "minify-xml": "^2.1.0" } } diff --git a/pretty-data.js b/pretty-data.js index 740a4c2..3cb2858 100644 --- a/pretty-data.js +++ b/pretty-data.js @@ -15,24 +15,26 @@ * 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, options] ) - 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; -* @Return - String; +* @data - String; XML, JSON, CSS or SQL text to beautify; +* @preserveComments - Bool (optional, used in xmlmin and cssmin only); +* Set this flag to true to prevent removing comments from @data; +* @options - Object (optional, used in xmlmin only); +* Use this object to set additional flags flags; +* @Return - String; * * USAGE: * * var pd = require('pretty-data').pd; * * var xml_pp = pd.xml(xml_text); -* var xml_min = pd.xmlmin(xml_text [,true]); +* var xml_min = pd.xmlmin(xml_text [, true, { removeWhitespaceBetweenTags: false }]); * var json_pp = pd.json(json_text); * var json_min = pd.jsonmin(json_text); * var css_pp = pd.css(css_text); @@ -285,11 +287,10 @@ pp.prototype.sql = function(text) { // ----------------------- min section ---------------------------------------------------- +const minifyXml = require("minify-xml").minify; pp.prototype.xmlmin = function(text, preserveComments) { - - var str = preserveComments ? text - : text.replace(/\/g,""); - return str.replace(/>\s{0,}<"); + return minifyXml(text, typeof preserveComments === "boolean" ? + { removeComments: !preserveComments } : preserveComments); }; pp.prototype.jsonmin = function(text) {