xml-beautify
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -1,204 +0,2 @@ | ||
(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.XmlBeautify = 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){ | ||
var XmlBeautify = require('./src/XmlBeautify.js'); | ||
module.exports = XmlBeautify; | ||
},{"./src/XmlBeautify.js":2}],2:[function(require,module,exports){ | ||
/* | ||
* xml-beautify - pretty-print text in XML formats. | ||
* | ||
* Copyright (c) 2018 Tom Misawa, riversun.org@gmail.com | ||
* | ||
* MIT license: | ||
* http://www.opensource.org/licenses/mit-license.php | ||
* | ||
* Usage: | ||
* | ||
* var resultXmlText = new XmlBeautify().beautify(textInput.value, | ||
* { | ||
* indent: " ", //indent pattern like white spaces | ||
* useSelfClosingElement: true //true:use self-closing element when empty element. | ||
* }); | ||
* | ||
* How "useSelfClosingElement" property works. | ||
* | ||
* useSelfClosingElement:true | ||
* <foo></foo> ==> <foo/> | ||
* | ||
* useSelfClosingElement:false | ||
* <foo></foo> ==> <foo></foo> | ||
* | ||
*/ | ||
var XmlBeautify = | ||
(function () { | ||
'use strict'; | ||
function XmlBeautify() { | ||
this.parser = new DOMParser(); | ||
} | ||
XmlBeautify.prototype.hasXmlDef = function (xmlText) { | ||
return xmlText.indexOf('<?xml') >= 0; | ||
} | ||
XmlBeautify.prototype.getEncoding = function (xmlText) { | ||
var me = this; | ||
if (!me.hasXmlDef(xmlText)) { | ||
return null; | ||
} | ||
var encodingStartPos = xmlText.toLowerCase().indexOf('encoding="') + 'encoding="'.length; | ||
var encodingEndPos = xmlText.indexOf('"?>'); | ||
var encoding = xmlText.substr(encodingStartPos, encodingEndPos - encodingStartPos); | ||
return encoding; | ||
} | ||
XmlBeautify.prototype.beautify = function (xmlText, data) { | ||
var me = this; | ||
var doc = me.parser.parseFromString(xmlText, "text/xml"); | ||
var indent = " "; | ||
var encoding = "UTF-8"; | ||
var useSelfClosingElement = false; | ||
if (data) { | ||
if (data.indent) { | ||
indent = data.indent; | ||
} | ||
if (data.useSelfClosingElement == true) { | ||
useSelfClosingElement = data.useSelfClosingElement; | ||
} | ||
} | ||
var xmlHeader = null; | ||
if (me.hasXmlDef(xmlText)) { | ||
var encoding = me.getEncoding(xmlText); | ||
xmlHeader = '<?xml version="1.0" encoding="' + encoding + '"?>'; | ||
} | ||
var buildInfo = { | ||
indentText: indent, | ||
xmlText: "", | ||
useSelfClosingElement: useSelfClosingElement, | ||
indentLevel: 0 | ||
} | ||
me._parseInternally(doc.children[0], buildInfo); | ||
var resultXml = ""; | ||
if (xmlHeader) { | ||
resultXml += xmlHeader + '\n'; | ||
} | ||
resultXml += buildInfo.xmlText; | ||
return resultXml; | ||
}; | ||
XmlBeautify.prototype._parseInternally = function (element, buildInfo) { | ||
var me = this; | ||
var elementTextContent = element.textContent; | ||
var blankReplacedElementContent = elementTextContent.replace(/ /g, '').replace(/\r?\n/g, '').replace(/\n/g, '').replace(/\t/g, ''); | ||
if (blankReplacedElementContent.length == 0) { | ||
elementTextContent = ""; | ||
} | ||
var elementHasNoChildren = !(element.children.length > 0); | ||
var elementHasValueOrChildren = (elementTextContent && elementTextContent.length > 0); | ||
var elementHasItsValue = elementHasNoChildren && elementHasValueOrChildren; | ||
var isEmptyElement = elementHasNoChildren && !elementHasValueOrChildren; | ||
var useSelfClosingElement = buildInfo.useSelfClosingElement; | ||
var startTagPrefix = '<'; | ||
var startTagSuffix = '>'; | ||
var startTagSuffixEmpty = ' />'; | ||
var endTagPrefix = '</'; | ||
var endTagSuffix = '>'; | ||
var valueOfElement = ''; | ||
if (elementHasItsValue) { | ||
valueOfElement = elementTextContent; | ||
} | ||
var indentText = ""; | ||
var idx; | ||
for (idx = 0; idx < buildInfo.indentLevel; idx++) { | ||
indentText += buildInfo.indentText; | ||
} | ||
buildInfo.xmlText += indentText; | ||
buildInfo.xmlText += startTagPrefix + element.tagName | ||
//add attributes | ||
for (var i = 0; i < element.attributes.length; i++) { | ||
var attr = element.attributes[i]; | ||
buildInfo.xmlText += ' ' + attr.name + '=' + '"' + attr.textContent + '"'; | ||
} | ||
if (isEmptyElement && useSelfClosingElement) { | ||
buildInfo.xmlText += startTagSuffixEmpty; | ||
} else { | ||
buildInfo.xmlText += startTagSuffix; | ||
} | ||
if (elementHasItsValue) { | ||
buildInfo.xmlText += valueOfElement; | ||
} else { | ||
if (isEmptyElement && !useSelfClosingElement) { | ||
} else { | ||
buildInfo.xmlText += '\n'; | ||
} | ||
} | ||
buildInfo.indentLevel++; | ||
for (var i = 0; i < element.children.length; i++) { | ||
var child = element.children[i]; | ||
me._parseInternally(child, buildInfo); | ||
} | ||
buildInfo.indentLevel--; | ||
if (isEmptyElement) { | ||
if (useSelfClosingElement) { | ||
} else { | ||
var endTag = endTagPrefix + element.tagName + endTagSuffix; | ||
buildInfo.xmlText += endTag; | ||
buildInfo.xmlText += '\n'; | ||
} | ||
} else { | ||
var endTag = endTagPrefix + element.tagName + endTagSuffix; | ||
if (!(elementHasNoChildren && elementHasValueOrChildren)) { | ||
buildInfo.xmlText += indentText; | ||
} | ||
buildInfo.xmlText += endTag; | ||
buildInfo.xmlText += '\n'; | ||
} | ||
}; | ||
return XmlBeautify; | ||
})(); | ||
module.exports = XmlBeautify; | ||
},{}]},{},[1])(1) | ||
}); | ||
/*! XmlBeautify v1.1.0 Copyright (c) 2019-2021 https://github.com/riversun(riversun.org@gmail.com) */ | ||
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.XmlBeautify=n():e.XmlBeautify=n()}(this,(function(){return function(e){var n={};function t(r){if(n[r])return n[r].exports;var l=n[r]={i:r,l:!1,exports:{}};return e[r].call(l.exports,l,l.exports,t),l.l=!0,l.exports}return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:r})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,n){if(1&n&&(e=t(e)),8&n)return e;if(4&n&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(t.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&n&&"string"!=typeof e)for(var l in e)t.d(r,l,function(n){return e[n]}.bind(null,l));return r},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},t.p="/",t(t.s=0)}([function(e,n,t){e.exports=t(1)},function(e,n,t){"use strict";function r(e,n){for(var t=0;t<n.length;t++){var r=n[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}t.r(n),t.d(n,"default",(function(){return l}));var l=function(){function e(n){!function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}(this,e);var t=n||{};this.userExternalParser=!1,t.parser?(this.userExternalParser=!0,this.parser=new t.parser):this.parser=new DOMParser}var n,t,l;return n=e,(t=[{key:"hasXmlDef",value:function(e){return e.indexOf("<?xml")>=0}},{key:"getEncoding",value:function(e){if(!this.hasXmlDef(e))return null;var n=e.toLowerCase().indexOf('encoding="')+'encoding="'.length,t=e.indexOf('"?>');return e.substr(n,t-n)}},{key:"_children",value:function(e){for(var n=[],t=e.childNodes.length,r=0;r<t;r++)1===e.childNodes[r].nodeType&&n.push(e.childNodes[r]);return n}},{key:"beautify",value:function(e,n){var t=this,r=t.parser.parseFromString(e,"text/xml"),l=" ",a=!1;n&&(n.indent&&(l=n.indent),1==n.useSelfClosingElement&&(a=n.useSelfClosingElement));var i=null;t.hasXmlDef(e)&&(i='<?xml version="1.0" encoding="'+t.getEncoding(e)+'"?>');var o={indentText:l,xmlText:"",useSelfClosingElement:a,indentLevel:0};t.userExternalParser?t._parseInternally(this._children(r)[0],o):t._parseInternally(r.children[0],o);var u="";return i&&(u+=i+"\n"),u+=o.xmlText}},{key:"_parseInternally",value:function(e,n){var t,r=this,l=e.textContent;0==l.replace(/ /g,"").replace(/\r?\n/g,"").replace(/\n/g,"").replace(/\t/g,"").length&&(l=""),t=r.userExternalParser?!(r._children(e).length>0):!(e.children.length>0);var a=l&&l.length>0,i=t&&a,o=t&&!a,u=n.useSelfClosingElement,s="";i&&(s=l);for(var f,c="",d=0;d<n.indentLevel;d++)c+=n.indentText;n.xmlText+=c,n.xmlText+="<"+e.tagName;for(var x=0;x<e.attributes.length;x++){var p=e.attributes[x];n.xmlText+=" "+p.name+'="'+p.textContent+'"'}n.xmlText+=o&&u?" />":">",i?n.xmlText+=s:o&&!u||(n.xmlText+="\n"),n.indentLevel++,f=r.userExternalParser?r._children(e).length:e.children.length;for(var m=0;m<f;m++){var v=void 0;v=r.userExternalParser?r._children(e)[m]:e.children[m],r._parseInternally(v,n)}if(n.indentLevel--,o)if(u);else{var h="</"+e.tagName+">";n.xmlText+=h,n.xmlText+="\n"}else{var g="</"+e.tagName+">";t&&a||(n.xmlText+=c),n.xmlText+=g,n.xmlText+="\n"}}}])&&r(n.prototype,t),l&&r(n,l),e}()}]).default})); |
var XmlBeautify = require('./src/XmlBeautify.js'); | ||
module.exports = XmlBeautify; |
{ | ||
"name": "xml-beautify", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "XML pretty formatter that corresponds to the self-closing empty elements.", | ||
"main": "index.js", | ||
"main": "dist/XmlBeautify.js", | ||
"scripts": { | ||
"start": "run-p watch server:nodejs", | ||
"server:nodejs": "http-server ./example -p 7000 -o -c-1", | ||
"watch": "run-p watch:*", | ||
"build": "browserify index.js --standalone XmlBeautify -o dist/XmlBeautify.js", | ||
"watch:example-js": "watchify index.js --standalone XmlBeautify -o example/XmlBeautify.js -v" | ||
"start": "webpack-dev-server", | ||
"build": "webpack --config webpack.config.js", | ||
"release": "webpack --config webpack.config.js --mode production" | ||
}, | ||
@@ -24,7 +22,16 @@ "repository": { | ||
"devDependencies": { | ||
"browserify": "^14.5.0", | ||
"http-server": "^0.10.0", | ||
"npm-run-all": "^4.1.2", | ||
"watchify": "^3.9.0" | ||
"@babel/core": "^7.12.10", | ||
"@babel/preset-env": "^7.12.11", | ||
"babel-jest": "^26.6.3", | ||
"babel-loader": "^8.2.2", | ||
"jest": "^26.6.3", | ||
"npm-run-all": "^4.1.5", | ||
"webpack": "^4.46.0", | ||
"webpack-cli": "^3.3.12", | ||
"webpack-dev-server": "^3.11.2", | ||
"xmldom": "^0.4.0" | ||
}, | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"keywords": [ | ||
@@ -31,0 +38,0 @@ "beautify", |
130
README.md
@@ -17,30 +17,5 @@ # Overview | ||
# Demo | ||
## demo on the web | ||
https://riversun.github.io/xml-beautify/index.html | ||
### download standalone *.js | ||
## Example of result | ||
https://github.com/riversun/xml-beautify/blob/master/dist/XmlBeautify.js | ||
<hr> | ||
## demo on node.js | ||
**clone this project and type** | ||
```shell | ||
git clone https://github.com/riversun/xml-beautify.git | ||
npm start | ||
``` | ||
### install via npm | ||
```shell | ||
npm install xml-beautify | ||
``` | ||
## Example | ||
@@ -86,2 +61,103 @@ [BEFORE] source XML | ||
``` | ||
``` | ||
# Install | ||
## install via npm | ||
```shell | ||
npm install xml-beautify | ||
``` | ||
## use from CDN | ||
``` | ||
<script src="https://cdn.jsdelivr.net/npm/xml-beautify/dist/XmlBeautify.js"></script> | ||
``` | ||
# Demo | ||
## demo on the web | ||
https://riversun.github.io/xml-beautify/index.html | ||
## demo on node.js | ||
**clone this project and type** | ||
```shell | ||
git clone https://github.com/riversun/xml-beautify.git | ||
npm start | ||
``` | ||
# Run on Browser | ||
```javascript | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body> | ||
<script src="https://cdn.jsdelivr.net/npm/xml-beautify/dist/XmlBeautify.js"></script> | ||
<script> | ||
const srcXmlText = `<?xml version="1.0" encoding="utf-8"?><example version="2.0"> | ||
<head> | ||
<title>Original Title</title> | ||
</head> | ||
<body> | ||
<element message="Greeting" title="Chapter1"> | ||
<element message="We say good morning in the morning."></element><element message="We say hello at noon."/> | ||
<element message="We say good evening at night."/> | ||
</element> | ||
<element message="Thank" title="Chapter2"> | ||
<element>value</element> | ||
<element></element> | ||
</element> | ||
</body> | ||
</example>`; | ||
const beautifiedXmlText = new XmlBeautify().beautify(srcXmlText); | ||
console.log(beautifiedXmlText); | ||
</script> | ||
</body> | ||
</html> | ||
``` | ||
# Run on Node.js | ||
To run XmlBeautify on node.js, need to install an external DOMParser like as follows. | ||
``` | ||
npm install xmldom | ||
``` | ||
And specify it as follows, | ||
```javascript | ||
new XmlBeautify({ parser: DOMParser }) | ||
``` | ||
- Example for Node.js | ||
```javascript | ||
const XmlBeautify = require('xml-beautify'); | ||
const { DOMParser } = require('xmldom');// When used in a node.js environment, DOMParser is needed. | ||
const srcXmlText = `<?xml version="1.0" encoding="utf-8"?><example version="2.0"> | ||
<head> | ||
<title>Original Title</title> | ||
</head> | ||
<body> | ||
<element message="Greeting" title="Chapter1"> | ||
<element message="We say good morning in the morning."></element><element message="We say hello at noon."/> | ||
<element message="We say good evening at night."/> | ||
</element> | ||
<element message="Thank" title="Chapter2"> | ||
<element>value</element> | ||
<element></element> | ||
</element> | ||
</body> | ||
</example>`; | ||
const beautifiedXmlText = new XmlBeautify({ parser: DOMParser }).beautify(srcXmlText); | ||
console.log(beautifiedXmlText); | ||
``` |
@@ -11,5 +11,5 @@ /* | ||
* | ||
* var resultXmlText = new XmlBeautify().beautify(textInput.value, | ||
* const resultXmlText = new XmlBeautify().beautify(textInput.value, | ||
* { | ||
* indent: " ", //indent pattern like white spaces | ||
* indent: ' ', //indent pattern like white spaces | ||
* useSelfClosingElement: true //true:use self-closing element when empty element. | ||
@@ -25,175 +25,215 @@ * }); | ||
* <foo></foo> ==> <foo></foo> | ||
* | ||
* | ||
*/ | ||
var XmlBeautify = | ||
(function () { | ||
'use strict'; | ||
const ELEMENT_NODE = 1; | ||
const ATTRIBUTE_NODE = 2; | ||
const TEXT_NODE = 3; | ||
const CDATA_SECTION_NODE = 4; | ||
const PROCESSING_INSTRUCTION_NODE = 7; | ||
const COMMENT_NODE = 8; | ||
const DOCUMENT_NODE = 9; | ||
const DOCUMENT_TYPE_NODE = 10; | ||
const DOCUMENT_FRAGMENT_NODE = 11; | ||
export default class XmlBeautify { | ||
constructor(option) { | ||
const opt = option || {}; | ||
this.userExternalParser = false; | ||
if (opt.parser) { | ||
this.userExternalParser = true; | ||
this.parser = new opt.parser(); | ||
} else { | ||
this.parser = new DOMParser(); | ||
} | ||
} | ||
function XmlBeautify() { | ||
this.parser = new DOMParser(); | ||
hasXmlDef(xmlText) { | ||
return xmlText.indexOf('<?xml') >= 0; | ||
} | ||
} | ||
getEncoding(xmlText) { | ||
const me = this; | ||
if (!me.hasXmlDef(xmlText)) { | ||
return null; | ||
} | ||
XmlBeautify.prototype.hasXmlDef = function (xmlText) { | ||
return xmlText.indexOf('<?xml') >= 0; | ||
} | ||
XmlBeautify.prototype.getEncoding = function (xmlText) { | ||
var me = this; | ||
if (!me.hasXmlDef(xmlText)) { | ||
return null; | ||
} | ||
const encodingStartPos = xmlText.toLowerCase().indexOf('encoding="') + 'encoding="'.length; | ||
const encodingEndPos = xmlText.indexOf('"?>'); | ||
const encoding = xmlText.substr(encodingStartPos, encodingEndPos - encodingStartPos); | ||
return encoding; | ||
} | ||
var encodingStartPos = xmlText.toLowerCase().indexOf('encoding="') + 'encoding="'.length; | ||
var encodingEndPos = xmlText.indexOf('"?>'); | ||
var encoding = xmlText.substr(encodingStartPos, encodingEndPos - encodingStartPos); | ||
return encoding; | ||
} | ||
XmlBeautify.prototype.beautify = function (xmlText, data) { | ||
var me = this; | ||
var doc = me.parser.parseFromString(xmlText, "text/xml"); | ||
_children(element) { | ||
const _ret = []; | ||
var indent = " "; | ||
var encoding = "UTF-8"; | ||
var useSelfClosingElement = false; | ||
const c = element.childNodes.length; | ||
for (let i = 0; i < c; i++) { | ||
if (element.childNodes[i].nodeType === ELEMENT_NODE) { | ||
_ret.push(element.childNodes[i]); | ||
} | ||
} | ||
return _ret; | ||
if (data) { | ||
if (data.indent) { | ||
indent = data.indent; | ||
} | ||
if (data.useSelfClosingElement == true) { | ||
useSelfClosingElement = data.useSelfClosingElement; | ||
} | ||
} | ||
} | ||
var xmlHeader = null; | ||
beautify(xmlText, data) { | ||
const me = this; | ||
if (me.hasXmlDef(xmlText)) { | ||
var encoding = me.getEncoding(xmlText); | ||
xmlHeader = '<?xml version="1.0" encoding="' + encoding + '"?>'; | ||
} | ||
var buildInfo = { | ||
indentText: indent, | ||
xmlText: "", | ||
useSelfClosingElement: useSelfClosingElement, | ||
indentLevel: 0 | ||
} | ||
const doc = me.parser.parseFromString(xmlText, 'text/xml'); | ||
let indent = ' '; | ||
const encoding = 'UTF-8'; | ||
let useSelfClosingElement = false; | ||
me._parseInternally(doc.children[0], buildInfo); | ||
if (data) { | ||
if (data.indent) { | ||
indent = data.indent; | ||
} | ||
var resultXml = ""; | ||
if (data.useSelfClosingElement == true) { | ||
useSelfClosingElement = data.useSelfClosingElement; | ||
} | ||
} | ||
if (xmlHeader) { | ||
resultXml += xmlHeader + '\n'; | ||
} | ||
resultXml += buildInfo.xmlText; | ||
let xmlHeader = null; | ||
return resultXml; | ||
if (me.hasXmlDef(xmlText)) { | ||
const encoding = me.getEncoding(xmlText); | ||
xmlHeader = '<?xml version="1.0" encoding="' + encoding + '"?>'; | ||
} | ||
const buildInfo = { | ||
indentText: indent, | ||
xmlText: '', | ||
useSelfClosingElement: useSelfClosingElement, | ||
indentLevel: 0 | ||
} | ||
if (me.userExternalParser) { | ||
me._parseInternally(this._children(doc)[0], buildInfo); | ||
} else { | ||
me._parseInternally(doc.children[0], buildInfo); | ||
} | ||
}; | ||
let resultXml = ''; | ||
XmlBeautify.prototype._parseInternally = function (element, buildInfo) { | ||
var me = this; | ||
if (xmlHeader) { | ||
resultXml += xmlHeader + '\n'; | ||
} | ||
resultXml += buildInfo.xmlText; | ||
var elementTextContent = element.textContent; | ||
return resultXml; | ||
var blankReplacedElementContent = elementTextContent.replace(/ /g, '').replace(/\r?\n/g, '').replace(/\n/g, '').replace(/\t/g, ''); | ||
if (blankReplacedElementContent.length == 0) { | ||
elementTextContent = ""; | ||
} | ||
}; | ||
var elementHasNoChildren = !(element.children.length > 0); | ||
var elementHasValueOrChildren = (elementTextContent && elementTextContent.length > 0); | ||
var elementHasItsValue = elementHasNoChildren && elementHasValueOrChildren; | ||
var isEmptyElement = elementHasNoChildren && !elementHasValueOrChildren; | ||
_parseInternally(element, buildInfo) { | ||
const me = this; | ||
var useSelfClosingElement = buildInfo.useSelfClosingElement; | ||
let elementTextContent = element.textContent; | ||
var startTagPrefix = '<'; | ||
var startTagSuffix = '>'; | ||
var startTagSuffixEmpty = ' />'; | ||
var endTagPrefix = '</'; | ||
var endTagSuffix = '>'; | ||
const blankReplacedElementContent = elementTextContent.replace(/ /g, '').replace(/\r?\n/g, '').replace(/\n/g, '').replace(/\t/g, ''); | ||
var valueOfElement = ''; | ||
if (blankReplacedElementContent.length == 0) { | ||
elementTextContent = ''; | ||
} | ||
if (elementHasItsValue) { | ||
let elementHasNoChildren; | ||
if (me.userExternalParser) { | ||
elementHasNoChildren = !(me._children(element).length > 0); | ||
} else { | ||
elementHasNoChildren = !(element.children.length > 0); | ||
} | ||
valueOfElement = elementTextContent; | ||
const elementHasValueOrChildren = (elementTextContent && elementTextContent.length > 0); | ||
const elementHasItsValue = elementHasNoChildren && elementHasValueOrChildren; | ||
const isEmptyElement = elementHasNoChildren && !elementHasValueOrChildren; | ||
const useSelfClosingElement = buildInfo.useSelfClosingElement; | ||
} | ||
const startTagPrefix = '<'; | ||
const startTagSuffix = '>'; | ||
const startTagSuffixEmpty = ' />'; | ||
const endTagPrefix = '</'; | ||
const endTagSuffix = '>'; | ||
var indentText = ""; | ||
let valueOfElement = ''; | ||
var idx; | ||
if (elementHasItsValue) { | ||
valueOfElement = elementTextContent; | ||
} | ||
for (idx = 0; idx < buildInfo.indentLevel; idx++) { | ||
indentText += buildInfo.indentText; | ||
} | ||
buildInfo.xmlText += indentText; | ||
buildInfo.xmlText += startTagPrefix + element.tagName | ||
let indentText = ''; | ||
//add attributes | ||
for (var i = 0; i < element.attributes.length; i++) { | ||
var attr = element.attributes[i]; | ||
buildInfo.xmlText += ' ' + attr.name + '=' + '"' + attr.textContent + '"'; | ||
} | ||
if (isEmptyElement && useSelfClosingElement) { | ||
buildInfo.xmlText += startTagSuffixEmpty; | ||
for (let idx = 0; idx < buildInfo.indentLevel; idx++) { | ||
indentText += buildInfo.indentText; | ||
} | ||
buildInfo.xmlText += indentText; | ||
buildInfo.xmlText += startTagPrefix + element.tagName | ||
} else { | ||
buildInfo.xmlText += startTagSuffix; | ||
} | ||
//add attributes | ||
for (let i = 0; i < element.attributes.length; i++) { | ||
const attr = element.attributes[i]; | ||
buildInfo.xmlText += ' ' + attr.name + '=' + '"' + attr.textContent + '"'; | ||
} | ||
if (elementHasItsValue) { | ||
buildInfo.xmlText += valueOfElement; | ||
} else { | ||
if (isEmptyElement && useSelfClosingElement) { | ||
buildInfo.xmlText += startTagSuffixEmpty; | ||
if (isEmptyElement && !useSelfClosingElement) { | ||
} else { | ||
buildInfo.xmlText += '\n'; | ||
} | ||
} else { | ||
buildInfo.xmlText += startTagSuffix; | ||
} | ||
} | ||
if (elementHasItsValue) { | ||
buildInfo.xmlText += valueOfElement; | ||
} else { | ||
buildInfo.indentLevel++; | ||
if (isEmptyElement && !useSelfClosingElement) { | ||
} else { | ||
buildInfo.xmlText += '\n'; | ||
} | ||
for (var i = 0; i < element.children.length; i++) { | ||
var child = element.children[i]; | ||
} | ||
me._parseInternally(child, buildInfo); | ||
} | ||
buildInfo.indentLevel--; | ||
buildInfo.indentLevel++; | ||
if (isEmptyElement) { | ||
let lengOfChildren; | ||
if (useSelfClosingElement) { | ||
if (me.userExternalParser) { | ||
lengOfChildren = me._children(element).length; | ||
} else { | ||
lengOfChildren = element.children.length; | ||
} | ||
} else { | ||
var endTag = endTagPrefix + element.tagName + endTagSuffix; | ||
buildInfo.xmlText += endTag; | ||
buildInfo.xmlText += '\n'; | ||
} | ||
} else { | ||
var endTag = endTagPrefix + element.tagName + endTagSuffix; | ||
for (let i = 0; i < lengOfChildren; i++) { | ||
let child; | ||
if (me.userExternalParser) { | ||
child = me._children(element)[i]; | ||
} else { | ||
child = element.children[i]; | ||
} | ||
me._parseInternally(child, buildInfo); | ||
} | ||
buildInfo.indentLevel--; | ||
if (!(elementHasNoChildren && elementHasValueOrChildren)) { | ||
buildInfo.xmlText += indentText; | ||
} | ||
buildInfo.xmlText += endTag; | ||
buildInfo.xmlText += '\n'; | ||
} | ||
if (isEmptyElement) { | ||
if (useSelfClosingElement) { | ||
}; | ||
} else { | ||
const endTag = endTagPrefix + element.tagName + endTagSuffix; | ||
buildInfo.xmlText += endTag; | ||
buildInfo.xmlText += '\n'; | ||
} | ||
} else { | ||
const endTag = endTagPrefix + element.tagName + endTagSuffix; | ||
return XmlBeautify; | ||
})(); | ||
module.exports = XmlBeautify; | ||
if (!(elementHasNoChildren && elementHasValueOrChildren)) { | ||
buildInfo.xmlText += indentText; | ||
} | ||
buildInfo.xmlText += endTag; | ||
buildInfo.xmlText += '\n'; | ||
} | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
149886
25
876
162
0
10
1