Socket
Socket
Sign inDemoInstall

xml-formatter

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml-formatter - npm Package Compare versions

Comparing version 2.6.1 to 3.0.0

dist/esnext/index.d.ts

465

dist/browser/xml-formatter-singleton.js
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.xmlFormatter = f()}})(function(){var define,module,exports;return (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){
"use strict";
/**
* @typedef {Object} ParsingOptions
* @property {function(node)} filter Returns false to exclude a node. Default is true.
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
var parsingState;
/**
* Parse the given XML string into an object.
*
* @param {String} xml
* @param {ParsingOptions} [options]
* @return {Object}
* @api public
*/
function parse(xml) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
function nextChild() {
return element(false) || text() || comment() || cdata();
}
options.filter = options.filter || function () {
return true;
};
function nextRootChild() {
match(/\s*/);
return element(true) || comment() || doctype() || processingInstruction(false);
}
function nextChild() {
return tag() || content() || comment() || cdata();
function parseDocument() {
var declaration = processingInstruction(true);
var children = [];
var documentRootNode;
var child = nextRootChild();
while (child) {
if (child.node.type === 'Element') {
if (documentRootNode) {
throw new Error('Found multiple root nodes');
}
documentRootNode = child.node;
}
if (!child.excluded) {
children.push(child.node);
}
child = nextRootChild();
}
function nextRootChild() {
match(/\s*/);
return tag(true) || comment() || doctype() || processingInstruction(false);
if (!documentRootNode) {
throw new Error('Failed to parse XML');
}
function document() {
var decl = declaration();
var children = [];
var documentRootNode;
var child = nextRootChild();
return {
declaration: declaration ? declaration.node : null,
root: documentRootNode,
children: children
};
}
while (child) {
if (child.node.type === 'Element') {
if (documentRootNode) {
throw new Error('Found multiple root nodes');
}
function processingInstruction(matchDeclaration) {
var m = matchDeclaration ? match(/^<\?(xml)\s*/) : match(/^<\?([\w-:.]+)\s*/);
if (!m) return; // tag
documentRootNode = child.node;
}
var node = {
name: m[1],
type: 'ProcessingInstruction',
attributes: {}
}; // attributes
if (!child.excluded) {
children.push(child.node);
}
while (!(eos() || is('?>'))) {
var attr = attribute();
child = nextRootChild();
if (attr) {
node.attributes[attr.name] = attr.value;
} else {
return;
}
}
if (!documentRootNode) {
throw new Error('Failed to parse XML');
}
match(/\?>/);
return {
excluded: matchDeclaration ? false : parsingState.options.filter(node) === false,
node: node
};
}
return {
declaration: decl ? decl.node : null,
root: documentRootNode,
children: children
};
}
function element(matchRoot) {
var m = match(/^<([\w-:.\u00C0-\u00FF]+)\s*/);
if (!m) return; // name
function declaration() {
return processingInstruction(true);
}
var node = {
type: 'Element',
name: m[1],
attributes: {},
children: []
};
var excluded = matchRoot ? false : parsingState.options.filter(node) === false; // attributes
function processingInstruction(matchDeclaration) {
var m = matchDeclaration ? match(/^<\?(xml)\s*/) : match(/^<\?([\w-:.]+)\s*/);
if (!m) return; // tag
while (!(eos() || is('>') || is('?>') || is('/>'))) {
var attr = attribute();
var node = {
name: m[1],
type: 'ProcessingInstruction',
attributes: {}
}; // attributes
while (!(eos() || is('?>'))) {
var attr = attribute();
if (!attr) return node;
if (attr) {
node.attributes[attr.name] = attr.value;
} else {
return;
}
} // self closing tag
match(/\?>/);
if (match(/^\s*\/>/)) {
node.children = null;
return {
excluded: matchDeclaration ? false : options.filter(node) === false,
excluded: excluded,
node: node

@@ -93,62 +109,53 @@ };

function tag(matchRoot) {
var m = match(/^<([\w-:.]+)\s*/);
if (!m) return; // name
match(/\??>/);
var node = {
type: 'Element',
name: m[1],
attributes: {},
children: []
}; // attributes
if (!excluded) {
// children
var child = nextChild();
while (!(eos() || is('>') || is('?>') || is('/>'))) {
var attr = attribute();
if (!attr) return node;
node.attributes[attr.name] = attr.value;
}
while (child) {
if (!child.excluded) {
node.children.push(child.node);
}
var excluded = matchRoot ? false : options.filter(node) === false; // self closing tag
if (match(/^\s*\/>/)) {
node.children = null;
return {
excluded: excluded,
node: node
};
child = nextChild();
}
} // closing
match(/\??>/);
if (!excluded) {
// children
var child = nextChild();
match(/^<\/[\w-:.]+>/);
return {
excluded: excluded,
node: node
};
}
while (child) {
if (!child.excluded) {
node.children.push(child.node);
}
function doctype() {
var m = match(/^<!DOCTYPE\s+[^>]*>/);
child = nextChild();
}
} // closing
match(/^<\/[\w-:.]+>/);
if (m) {
var node = {
type: 'DocumentType',
content: m[0]
};
return {
excluded: excluded,
excluded: parsingState.options.filter(node) === false,
node: node
};
}
}
function doctype() {
var m = match(/^<!DOCTYPE\s+[^>]*>/);
function cdata() {
if (parsingState.xml.startsWith('<![CDATA[')) {
var endPositionStart = parsingState.xml.indexOf(']]>');
if (m) {
if (endPositionStart > -1) {
var endPositionFinish = endPositionStart + 3;
var node = {
type: 'DocumentType',
content: m[0]
type: 'CDATA',
content: parsingState.xml.substring(0, endPositionFinish)
};
parsingState.xml = parsingState.xml.slice(endPositionFinish);
return {
excluded: options.filter(node) === false,
excluded: parsingState.options.filter(node) === false,
node: node

@@ -158,101 +165,100 @@ };

}
}
function cdata() {
if (xml.startsWith('<![CDATA[')) {
var endPositionStart = xml.indexOf(']]>');
function comment() {
var m = match(/^<!--[\s\S]*?-->/);
if (endPositionStart > -1) {
var endPositionFinish = endPositionStart + 3;
var node = {
type: 'CDATA',
content: xml.substring(0, endPositionFinish)
};
xml = xml.slice(endPositionFinish);
return {
excluded: options.filter(node) === false,
node: node
};
}
}
if (m) {
var node = {
type: 'Comment',
content: m[0]
};
return {
excluded: parsingState.options.filter(node) === false,
node: node
};
}
}
function comment() {
var m = match(/^<!--[\s\S]*?-->/);
function text() {
var m = match(/^([^<]+)/);
if (m) {
var node = {
type: 'Comment',
content: m[0]
};
return {
excluded: options.filter(node) === false,
node: node
};
}
if (m) {
var node = {
type: 'Text',
content: m[1]
};
return {
excluded: parsingState.options.filter(node) === false,
node: node
};
}
}
function content() {
var m = match(/^([^<]+)/);
function attribute() {
var m = match(/([\w-:.\u00C0-\u00FF]+)\s*=\s*("[^"]*"|'[^']*'|[\w\u00C0-\u00FF]+)\s*/);
if (m) {
var node = {
type: 'Text',
content: m[1]
};
return {
excluded: options.filter(node) === false,
node: node
};
}
}
function attribute() {
var m = match(/([\w-:.]+)\s*=\s*("[^"]*"|'[^']*'|\w+)\s*/);
if (!m) return;
if (m) {
return {
name: m[1],
value: strip(m[2])
value: stripQuotes(m[2])
};
}
/**
* Strip quotes from `val`.
*/
}
function stripQuotes(val) {
return val.replace(/^['"]|['"]$/g, '');
}
/**
* Match `re` and advance the string.
*/
function strip(val) {
return val.replace(/^['"]|['"]$/g, '');
}
/**
* Match `re` and advance the string.
*/
function match(re) {
var m = parsingState.xml.match(re);
function match(re) {
var m = xml.match(re);
if (!m) return;
xml = xml.slice(m[0].length);
if (m) {
parsingState.xml = parsingState.xml.slice(m[0].length);
return m;
}
/**
* End-of-source.
*/
}
/**
* End-of-source.
*/
function eos() {
return 0 === xml.length;
}
/**
* Check for `prefix`.
*/
function eos() {
return 0 === parsingState.xml.length;
}
/**
* Check for `prefix`.
*/
function is(prefix) {
return 0 === xml.indexOf(prefix);
}
function is(prefix) {
return 0 === parsingState.xml.indexOf(prefix);
}
/**
* Parse the given XML string into an object.
*/
function parseXml(xml) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
xml = xml.trim();
return document();
var filter = options.filter || function () {
return true;
};
parsingState = {
xml: xml,
options: Object.assign(Object.assign({}, options), {
filter: filter
})
};
return parseDocument();
}
module.exports = parse;
exports["default"] = parseXml;

@@ -262,22 +268,14 @@ },{}],"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 />`
*/
var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
return mod && mod.__esModule ? mod : {
"default": mod
};
};
/**
* @typedef {Object} XMLFormatterState
* @param {String} content
* @param {Number} level
* @param {XMLFormatterOptions} options
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* @param {XMLFormatterState} state
* @return {void}
*/
var xml_parser_xo_1 = __importDefault(require("xml-parser-xo"));
function newLine(state) {

@@ -292,27 +290,14 @@ if (!state.options.indentation && !state.options.lineSeparator) return;

}
/**
* @param {XMLFormatterState} state
* @param {String} content
* @return {void}
*/
function appendContent(state, content) {
state.content += content;
}
/**
* @param {Object} node
* @param {XMLFormatterState} state
* @param {Boolean} preserveSpace
* @return {void}
*/
function processNode(node, state, preserveSpace) {
if (typeof node.content === 'string') {
processContentNode(node, state, preserveSpace);
processContent(node.content, state, preserveSpace);
} else if (node.type === 'Element') {
processElementNode(node, state, preserveSpace);
} else if (node.type === 'ProcessingInstruction') {
processProcessingIntruction(node, state, preserveSpace);
processProcessingIntruction(node, state);
} else {

@@ -322,16 +307,9 @@ throw new Error('Unknown node type: ' + node.type);

}
/**
* @param {Object} node
* @param {XMLFormatterState} state
* @param {Boolean} preserveSpace
* @return {void}
*/
function processContentNode(node, state, preserveSpace) {
function processContent(content, state, preserveSpace) {
if (!preserveSpace) {
node.content = node.content.trim();
content = content.trim();
}
if (node.content.length > 0) {
if (content.length > 0) {
if (!preserveSpace && state.content.length > 0) {

@@ -341,13 +319,6 @@ newLine(state);

appendContent(state, node.content);
appendContent(state, content);
}
}
/**
* @param {Object} node
* @param {XMLFormatterState} state
* @param {Boolean} preserveSpace
* @return {void}
*/
function processElementNode(node, state, preserveSpace) {

@@ -369,2 +340,3 @@ if (!preserveSpace && state.content.length > 0) {

} else {
var nodeChildren = node.children;
appendContent(state, '>');

@@ -378,3 +350,3 @@ state.level++;

var containsNonTextNodes = false;
node.children.forEach(function (child, index) {
nodeChildren.forEach(function (child, index) {
if (child.type === 'Text') {

@@ -384,3 +356,3 @@ if (child.content.includes('\n')) {

child.content = child.content.trim();
} else if (index === 0 || index === node.children.length - 1) {
} else if (index === 0 || index === nodeChildren.length - 1) {
if (child.content.trim().length === 0) {

@@ -407,4 +379,4 @@ // If the text node is at the start or end and is empty, it should be ignored when formatting

node.children.forEach(function (child) {
processNode(child, state, preserveSpace || nodePreserveSpace, state.options);
nodeChildren.forEach(function (child) {
processNode(child, state, preserveSpace || nodePreserveSpace);
});

@@ -420,9 +392,3 @@ state.level--;

}
/**
* @param {XMLFormatterState} state
* @param {Record<String, String>} attributes
* @return {void}
*/
function processAttributes(state, attributes) {

@@ -434,9 +400,3 @@ Object.keys(attributes).forEach(function (attr) {

}
/**
* @param {Object} node
* @param {XMLFormatterState} state
* @return {void}
*/
function processProcessingIntruction(node, state) {

@@ -453,6 +413,2 @@ if (state.content.length > 0) {

* Converts the given XML into human readable format.
*
* @param {String} xml
* @param {XMLFormatterOptions} options
* @returns {string}
*/

@@ -467,6 +423,3 @@

options.whiteSpaceAtEndOfSelfclosingTag = !!options.whiteSpaceAtEndOfSelfclosingTag;
var parser = require('xml-parser-xo');
var parsedXml = parser(xml, {
var parsedXml = (0, xml_parser_xo_1["default"])(xml, {
filter: options.filter

@@ -490,5 +443,5 @@ });

module.exports = format;
exports["default"] = format;
},{"xml-parser-xo":1}]},{},[])("xml-formatter")
});
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){
"use strict";
/**
* @typedef {Object} ParsingOptions
* @property {function(node)} filter Returns false to exclude a node. Default is true.
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
var parsingState;
/**
* Parse the given XML string into an object.
*
* @param {String} xml
* @param {ParsingOptions} [options]
* @return {Object}
* @api public
*/
function parse(xml) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
function nextChild() {
return element(false) || text() || comment() || cdata();
}
options.filter = options.filter || function () {
return true;
};
function nextRootChild() {
match(/\s*/);
return element(true) || comment() || doctype() || processingInstruction(false);
}
function nextChild() {
return tag() || content() || comment() || cdata();
function parseDocument() {
var declaration = processingInstruction(true);
var children = [];
var documentRootNode;
var child = nextRootChild();
while (child) {
if (child.node.type === 'Element') {
if (documentRootNode) {
throw new Error('Found multiple root nodes');
}
documentRootNode = child.node;
}
if (!child.excluded) {
children.push(child.node);
}
child = nextRootChild();
}
function nextRootChild() {
match(/\s*/);
return tag(true) || comment() || doctype() || processingInstruction(false);
if (!documentRootNode) {
throw new Error('Failed to parse XML');
}
function document() {
var decl = declaration();
var children = [];
var documentRootNode;
var child = nextRootChild();
return {
declaration: declaration ? declaration.node : null,
root: documentRootNode,
children: children
};
}
while (child) {
if (child.node.type === 'Element') {
if (documentRootNode) {
throw new Error('Found multiple root nodes');
}
function processingInstruction(matchDeclaration) {
var m = matchDeclaration ? match(/^<\?(xml)\s*/) : match(/^<\?([\w-:.]+)\s*/);
if (!m) return; // tag
documentRootNode = child.node;
}
var node = {
name: m[1],
type: 'ProcessingInstruction',
attributes: {}
}; // attributes
if (!child.excluded) {
children.push(child.node);
}
while (!(eos() || is('?>'))) {
var attr = attribute();
child = nextRootChild();
if (attr) {
node.attributes[attr.name] = attr.value;
} else {
return;
}
}
if (!documentRootNode) {
throw new Error('Failed to parse XML');
}
match(/\?>/);
return {
excluded: matchDeclaration ? false : parsingState.options.filter(node) === false,
node: node
};
}
return {
declaration: decl ? decl.node : null,
root: documentRootNode,
children: children
};
}
function element(matchRoot) {
var m = match(/^<([\w-:.\u00C0-\u00FF]+)\s*/);
if (!m) return; // name
function declaration() {
return processingInstruction(true);
}
var node = {
type: 'Element',
name: m[1],
attributes: {},
children: []
};
var excluded = matchRoot ? false : parsingState.options.filter(node) === false; // attributes
function processingInstruction(matchDeclaration) {
var m = matchDeclaration ? match(/^<\?(xml)\s*/) : match(/^<\?([\w-:.]+)\s*/);
if (!m) return; // tag
while (!(eos() || is('>') || is('?>') || is('/>'))) {
var attr = attribute();
var node = {
name: m[1],
type: 'ProcessingInstruction',
attributes: {}
}; // attributes
while (!(eos() || is('?>'))) {
var attr = attribute();
if (!attr) return node;
if (attr) {
node.attributes[attr.name] = attr.value;
} else {
return;
}
} // self closing tag
match(/\?>/);
if (match(/^\s*\/>/)) {
node.children = null;
return {
excluded: matchDeclaration ? false : options.filter(node) === false,
excluded: excluded,
node: node

@@ -93,62 +109,53 @@ };

function tag(matchRoot) {
var m = match(/^<([\w-:.]+)\s*/);
if (!m) return; // name
match(/\??>/);
var node = {
type: 'Element',
name: m[1],
attributes: {},
children: []
}; // attributes
if (!excluded) {
// children
var child = nextChild();
while (!(eos() || is('>') || is('?>') || is('/>'))) {
var attr = attribute();
if (!attr) return node;
node.attributes[attr.name] = attr.value;
}
while (child) {
if (!child.excluded) {
node.children.push(child.node);
}
var excluded = matchRoot ? false : options.filter(node) === false; // self closing tag
if (match(/^\s*\/>/)) {
node.children = null;
return {
excluded: excluded,
node: node
};
child = nextChild();
}
} // closing
match(/\??>/);
if (!excluded) {
// children
var child = nextChild();
match(/^<\/[\w-:.]+>/);
return {
excluded: excluded,
node: node
};
}
while (child) {
if (!child.excluded) {
node.children.push(child.node);
}
function doctype() {
var m = match(/^<!DOCTYPE\s+[^>]*>/);
child = nextChild();
}
} // closing
match(/^<\/[\w-:.]+>/);
if (m) {
var node = {
type: 'DocumentType',
content: m[0]
};
return {
excluded: excluded,
excluded: parsingState.options.filter(node) === false,
node: node
};
}
}
function doctype() {
var m = match(/^<!DOCTYPE\s+[^>]*>/);
function cdata() {
if (parsingState.xml.startsWith('<![CDATA[')) {
var endPositionStart = parsingState.xml.indexOf(']]>');
if (m) {
if (endPositionStart > -1) {
var endPositionFinish = endPositionStart + 3;
var node = {
type: 'DocumentType',
content: m[0]
type: 'CDATA',
content: parsingState.xml.substring(0, endPositionFinish)
};
parsingState.xml = parsingState.xml.slice(endPositionFinish);
return {
excluded: options.filter(node) === false,
excluded: parsingState.options.filter(node) === false,
node: node

@@ -158,101 +165,100 @@ };

}
}
function cdata() {
if (xml.startsWith('<![CDATA[')) {
var endPositionStart = xml.indexOf(']]>');
function comment() {
var m = match(/^<!--[\s\S]*?-->/);
if (endPositionStart > -1) {
var endPositionFinish = endPositionStart + 3;
var node = {
type: 'CDATA',
content: xml.substring(0, endPositionFinish)
};
xml = xml.slice(endPositionFinish);
return {
excluded: options.filter(node) === false,
node: node
};
}
}
if (m) {
var node = {
type: 'Comment',
content: m[0]
};
return {
excluded: parsingState.options.filter(node) === false,
node: node
};
}
}
function comment() {
var m = match(/^<!--[\s\S]*?-->/);
function text() {
var m = match(/^([^<]+)/);
if (m) {
var node = {
type: 'Comment',
content: m[0]
};
return {
excluded: options.filter(node) === false,
node: node
};
}
if (m) {
var node = {
type: 'Text',
content: m[1]
};
return {
excluded: parsingState.options.filter(node) === false,
node: node
};
}
}
function content() {
var m = match(/^([^<]+)/);
function attribute() {
var m = match(/([\w-:.\u00C0-\u00FF]+)\s*=\s*("[^"]*"|'[^']*'|[\w\u00C0-\u00FF]+)\s*/);
if (m) {
var node = {
type: 'Text',
content: m[1]
};
return {
excluded: options.filter(node) === false,
node: node
};
}
}
function attribute() {
var m = match(/([\w-:.]+)\s*=\s*("[^"]*"|'[^']*'|\w+)\s*/);
if (!m) return;
if (m) {
return {
name: m[1],
value: strip(m[2])
value: stripQuotes(m[2])
};
}
/**
* Strip quotes from `val`.
*/
}
function stripQuotes(val) {
return val.replace(/^['"]|['"]$/g, '');
}
/**
* Match `re` and advance the string.
*/
function strip(val) {
return val.replace(/^['"]|['"]$/g, '');
}
/**
* Match `re` and advance the string.
*/
function match(re) {
var m = parsingState.xml.match(re);
function match(re) {
var m = xml.match(re);
if (!m) return;
xml = xml.slice(m[0].length);
if (m) {
parsingState.xml = parsingState.xml.slice(m[0].length);
return m;
}
/**
* End-of-source.
*/
}
/**
* End-of-source.
*/
function eos() {
return 0 === xml.length;
}
/**
* Check for `prefix`.
*/
function eos() {
return 0 === parsingState.xml.length;
}
/**
* Check for `prefix`.
*/
function is(prefix) {
return 0 === xml.indexOf(prefix);
}
function is(prefix) {
return 0 === parsingState.xml.indexOf(prefix);
}
/**
* Parse the given XML string into an object.
*/
function parseXml(xml) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
xml = xml.trim();
return document();
var filter = options.filter || function () {
return true;
};
parsingState = {
xml: xml,
options: Object.assign(Object.assign({}, options), {
filter: filter
})
};
return parseDocument();
}
module.exports = parse;
exports["default"] = parseXml;

@@ -262,22 +268,14 @@ },{}],"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 />`
*/
var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
return mod && mod.__esModule ? mod : {
"default": mod
};
};
/**
* @typedef {Object} XMLFormatterState
* @param {String} content
* @param {Number} level
* @param {XMLFormatterOptions} options
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* @param {XMLFormatterState} state
* @return {void}
*/
var xml_parser_xo_1 = __importDefault(require("xml-parser-xo"));
function newLine(state) {

@@ -292,27 +290,14 @@ if (!state.options.indentation && !state.options.lineSeparator) return;

}
/**
* @param {XMLFormatterState} state
* @param {String} content
* @return {void}
*/
function appendContent(state, content) {
state.content += content;
}
/**
* @param {Object} node
* @param {XMLFormatterState} state
* @param {Boolean} preserveSpace
* @return {void}
*/
function processNode(node, state, preserveSpace) {
if (typeof node.content === 'string') {
processContentNode(node, state, preserveSpace);
processContent(node.content, state, preserveSpace);
} else if (node.type === 'Element') {
processElementNode(node, state, preserveSpace);
} else if (node.type === 'ProcessingInstruction') {
processProcessingIntruction(node, state, preserveSpace);
processProcessingIntruction(node, state);
} else {

@@ -322,16 +307,9 @@ throw new Error('Unknown node type: ' + node.type);

}
/**
* @param {Object} node
* @param {XMLFormatterState} state
* @param {Boolean} preserveSpace
* @return {void}
*/
function processContentNode(node, state, preserveSpace) {
function processContent(content, state, preserveSpace) {
if (!preserveSpace) {
node.content = node.content.trim();
content = content.trim();
}
if (node.content.length > 0) {
if (content.length > 0) {
if (!preserveSpace && state.content.length > 0) {

@@ -341,13 +319,6 @@ newLine(state);

appendContent(state, node.content);
appendContent(state, content);
}
}
/**
* @param {Object} node
* @param {XMLFormatterState} state
* @param {Boolean} preserveSpace
* @return {void}
*/
function processElementNode(node, state, preserveSpace) {

@@ -369,2 +340,3 @@ if (!preserveSpace && state.content.length > 0) {

} else {
var nodeChildren = node.children;
appendContent(state, '>');

@@ -378,3 +350,3 @@ state.level++;

var containsNonTextNodes = false;
node.children.forEach(function (child, index) {
nodeChildren.forEach(function (child, index) {
if (child.type === 'Text') {

@@ -384,3 +356,3 @@ if (child.content.includes('\n')) {

child.content = child.content.trim();
} else if (index === 0 || index === node.children.length - 1) {
} else if (index === 0 || index === nodeChildren.length - 1) {
if (child.content.trim().length === 0) {

@@ -407,4 +379,4 @@ // If the text node is at the start or end and is empty, it should be ignored when formatting

node.children.forEach(function (child) {
processNode(child, state, preserveSpace || nodePreserveSpace, state.options);
nodeChildren.forEach(function (child) {
processNode(child, state, preserveSpace || nodePreserveSpace);
});

@@ -420,9 +392,3 @@ state.level--;

}
/**
* @param {XMLFormatterState} state
* @param {Record<String, String>} attributes
* @return {void}
*/
function processAttributes(state, attributes) {

@@ -434,9 +400,3 @@ Object.keys(attributes).forEach(function (attr) {

}
/**
* @param {Object} node
* @param {XMLFormatterState} state
* @return {void}
*/
function processProcessingIntruction(node, state) {

@@ -453,6 +413,2 @@ if (state.content.length > 0) {

* Converts the given XML into human readable format.
*
* @param {String} xml
* @param {XMLFormatterOptions} options
* @returns {string}
*/

@@ -467,6 +423,3 @@

options.whiteSpaceAtEndOfSelfclosingTag = !!options.whiteSpaceAtEndOfSelfclosingTag;
var parser = require('xml-parser-xo');
var parsedXml = parser(xml, {
var parsedXml = (0, xml_parser_xo_1["default"])(xml, {
filter: options.filter

@@ -490,4 +443,4 @@ });

module.exports = format;
exports["default"] = format;
},{"xml-parser-xo":1}]},{},[]);
{
"name": "xml-formatter",
"version": "2.6.1",
"version": "3.0.0",
"repository": "github:chrisbottin/xml-formatter",

@@ -12,13 +12,17 @@ "bugs": {

"license": "MIT",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "mocha",
"prepublishOnly": "eslint . && npm test && npm run dist:prepare && npm run dist:build",
"dist:prepare": "rm -rf ./dist && mkdir -p ./dist/browser",
"dist:build": "npm run dist:build:require && npm run dist:build:singleton",
"dist:build:require": "browserify -g [ babelify --presets [ @babel/preset-env ] ] -r ./index.js:xml-formatter -o ./dist/browser/xml-formatter.js",
"dist:build:singleton": "browserify -g [ babelify --presets [ @babel/preset-env ] ] -r ./index.js:xml-formatter -o ./dist/browser/xml-formatter-singleton.js -s xmlFormatter"
"lint": "eslint . --ext=js,ts",
"clean": "rm -rf ./dist",
"compile": "npm run clean && npm run lint && npm run compile:commonjs && npm run compile:esnext && npm run compile:browser",
"compile:commonjs": "tsc --module commonjs --outDir ./dist --noEmit false",
"compile:esnext": "tsc --module esnext --outDir ./dist/esnext --noEmit false",
"compile:browser": "mkdir -p ./dist/browser && npm run compile:browser:require && npm run compile:browser:singleton",
"compile:browser:require": "browserify -g [ babelify --presets [ @babel/preset-env ] ] -r ./dist/index.js:xml-formatter -o ./dist/browser/xml-formatter.js",
"compile:browser:singleton": "browserify -g [ babelify --presets [ @babel/preset-env ] ] -r ./dist/index.js:xml-formatter -o ./dist/browser/xml-formatter-singleton.js -s xmlFormatter",
"test": "mocha --require=ts-node/register --extension=js,ts --spec=test",
"prepublishOnly": "npm run test && npm run compile"
},
"engines": {
"node": ">= 10"
"node": ">= 14"
},

@@ -37,3 +41,3 @@ "keywords": [

"dependencies": {
"xml-parser-xo": "^3.2.0"
"xml-parser-xo": "^4.0.0"
},

@@ -43,2 +47,8 @@ "devDependencies": {

"@babel/preset-env": "^7.15.8",
"@types/chai": "^4.3.3",
"@types/glob": "^7.2.0",
"@types/mocha": "^7.0.2",
"@types/node": "^14.18.29",
"@typescript-eslint/eslint-plugin": "^5.37.0",
"@typescript-eslint/parser": "^5.37.0",
"babelify": "^10.0.0",

@@ -49,4 +59,6 @@ "browserify": "^13.1.1",

"glob": "^7.1.7",
"mocha": "^6.2.1"
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc