xml-formatter
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -243,2 +243,16 @@ require=(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
},{}],"xml-formatter":[function(require,module,exports){ | ||
/** | ||
* @typedef {Object} XMLFormatterOptions | ||
* @property {string} [indentation=' '] The value used for indentation | ||
* @property {function(node): boolean} [filter] Return false to exclude the node. | ||
* @property {boolean} [collapseContent=false] True to keep content in the same line as the element. Only works if element contains at least one text node | ||
* @property {string} [lineSeparator='\r\n'] The line separator to use | ||
* @property {string} [whiteSpaceAtEndOfSelfclosingTag=false] to either end ad self closing tag with `<tag/>` or `<tag />` | ||
*/ | ||
/** | ||
* | ||
* @param {*} output | ||
*/ | ||
function newLine(output) { | ||
@@ -256,7 +270,10 @@ output.content += output.options.lineSeparator; | ||
function processNode(node, output, preserveSpace) { | ||
/** | ||
* @param {XMLFormatterOptions} options | ||
*/ | ||
function processNode(node, output, preserveSpace, options) { | ||
if (typeof node.content === 'string') { | ||
processContentNode(node, output, preserveSpace); | ||
} else if (node.type === 'Element') { | ||
processElement(node, output, preserveSpace); | ||
processElement(node, output, preserveSpace, options); | ||
} else if (node.type === 'ProcessingInstruction') { | ||
@@ -278,3 +295,6 @@ processProcessingIntruction(node, output, preserveSpace); | ||
function processElement(node, output, preserveSpace) { | ||
/** | ||
* @param {XMLFormatterOptions} options | ||
*/ | ||
function processElement(node, output, preserveSpace, options) { | ||
if (!preserveSpace && output.content.length > 0) { | ||
@@ -288,4 +308,5 @@ newLine(output); | ||
if (node.children === null) { | ||
const selfClosingNodeClosingTag = options.whiteSpaceAtEndOfSelfclosingTag ? ' />' : '/>' | ||
// self-closing node | ||
appendContent(output, '/>'); | ||
appendContent(output, selfClosingNodeClosingTag); | ||
} else if (node.children.length === 0) { | ||
@@ -314,3 +335,3 @@ // empty node | ||
node.children.forEach(function(child) { | ||
processNode(child, output, preserveSpace || nodePreserveSpace); | ||
processNode(child, output, preserveSpace || nodePreserveSpace, options); | ||
}); | ||
@@ -347,7 +368,8 @@ | ||
* @param {String} xml | ||
* @param {Object} options | ||
* @param {XMLFormatterOptions} options | ||
* @config {String} [indentation=' '] The value used for indentation | ||
* @config {function(node)} [filter] Return false to exclude the node. | ||
* @config {function(node): boolean} [filter] Return false to exclude the node. | ||
* @config {Boolean} [collapseContent=false] True to keep content in the same line as the element. Only works if element contains at least one text node | ||
* @config {String} [lineSeparator='\r\n'] The line separator to use | ||
* @config {string} [whiteSpaceAtEndOfSelfclosingTag=false] to either end with `<tag/>` or `<tag />` | ||
* @returns {string} | ||
@@ -361,2 +383,3 @@ */ | ||
options.lineSeparator = options.lineSeparator || '\r\n'; | ||
options.whiteSpaceAtEndOfSelfclosingTag = !!options.whiteSpaceAtEndOfSelfclosingTag; | ||
@@ -372,3 +395,3 @@ const parse = require('xml-parser-xo'); | ||
parsedXml.children.forEach(function(child) { | ||
processNode(child, output, false); | ||
processNode(child, output, false, options); | ||
}); | ||
@@ -375,0 +398,0 @@ |
39
index.js
@@ -0,1 +1,15 @@ | ||
/** | ||
* @typedef {Object} XMLFormatterOptions | ||
* @property {string} [indentation=' '] The value used for indentation | ||
* @property {function(node): boolean} [filter] Return false to exclude the node. | ||
* @property {boolean} [collapseContent=false] True to keep content in the same line as the element. Only works if element contains at least one text node | ||
* @property {string} [lineSeparator='\r\n'] The line separator to use | ||
* @property {string} [whiteSpaceAtEndOfSelfclosingTag=false] to either end ad self closing tag with `<tag/>` or `<tag />` | ||
*/ | ||
/** | ||
* | ||
* @param {*} output | ||
*/ | ||
function newLine(output) { | ||
@@ -13,7 +27,10 @@ output.content += output.options.lineSeparator; | ||
function processNode(node, output, preserveSpace) { | ||
/** | ||
* @param {XMLFormatterOptions} options | ||
*/ | ||
function processNode(node, output, preserveSpace, options) { | ||
if (typeof node.content === 'string') { | ||
processContentNode(node, output, preserveSpace); | ||
} else if (node.type === 'Element') { | ||
processElement(node, output, preserveSpace); | ||
processElement(node, output, preserveSpace, options); | ||
} else if (node.type === 'ProcessingInstruction') { | ||
@@ -35,3 +52,6 @@ processProcessingIntruction(node, output, preserveSpace); | ||
function processElement(node, output, preserveSpace) { | ||
/** | ||
* @param {XMLFormatterOptions} options | ||
*/ | ||
function processElement(node, output, preserveSpace, options) { | ||
if (!preserveSpace && output.content.length > 0) { | ||
@@ -45,4 +65,5 @@ newLine(output); | ||
if (node.children === null) { | ||
const selfClosingNodeClosingTag = options.whiteSpaceAtEndOfSelfclosingTag ? ' />' : '/>' | ||
// self-closing node | ||
appendContent(output, '/>'); | ||
appendContent(output, selfClosingNodeClosingTag); | ||
} else if (node.children.length === 0) { | ||
@@ -71,3 +92,3 @@ // empty node | ||
node.children.forEach(function(child) { | ||
processNode(child, output, preserveSpace || nodePreserveSpace); | ||
processNode(child, output, preserveSpace || nodePreserveSpace, options); | ||
}); | ||
@@ -104,7 +125,8 @@ | ||
* @param {String} xml | ||
* @param {Object} options | ||
* @param {XMLFormatterOptions} options | ||
* @config {String} [indentation=' '] The value used for indentation | ||
* @config {function(node)} [filter] Return false to exclude the node. | ||
* @config {function(node): boolean} [filter] Return false to exclude the node. | ||
* @config {Boolean} [collapseContent=false] True to keep content in the same line as the element. Only works if element contains at least one text node | ||
* @config {String} [lineSeparator='\r\n'] The line separator to use | ||
* @config {string} [whiteSpaceAtEndOfSelfclosingTag=false] to either end with `<tag/>` or `<tag />` | ||
* @returns {string} | ||
@@ -118,2 +140,3 @@ */ | ||
options.lineSeparator = options.lineSeparator || '\r\n'; | ||
options.whiteSpaceAtEndOfSelfclosingTag = !!options.whiteSpaceAtEndOfSelfclosingTag; | ||
@@ -129,3 +152,3 @@ const parse = require('xml-parser-xo'); | ||
parsedXml.children.forEach(function(child) { | ||
processNode(child, output, false); | ||
processNode(child, output, false, options); | ||
}); | ||
@@ -132,0 +155,0 @@ |
{ | ||
"name": "xml-formatter", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"repository": "github:chrisbottin/xml-formatter", | ||
@@ -5,0 +5,0 @@ "bugs": { |
@@ -72,2 +72,3 @@ | ||
- `lineSeparator` (String, default=`\r\n`) Specify the line separator to use | ||
- `whiteSpaceAtEndOfSelfclosingTag` (Boolean, default=false) to either end ad self closing tag with `<tag/>` or `<tag />` | ||
@@ -74,0 +75,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
172681
472
106