From 6a640ba2cd0db75b38d42a7a512981ad500df349 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Tue, 8 Nov 2022 02:42:11 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B4=20XmlParser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/xml/XmlParser.js | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/server/core/xml/XmlParser.js b/server/core/xml/XmlParser.js index daaa52d..1a89e58 100644 --- a/server/core/xml/XmlParser.js +++ b/server/core/xml/XmlParser.js @@ -399,6 +399,8 @@ class XmlParser extends NodeBase { const nodesToString = (nodes, depth = 0) => { let result = ''; + + const indent = '\n' + ' '.repeat(depth); let lastType = 0; for (const n of nodes) { @@ -424,22 +426,23 @@ class XmlParser extends NodeBase { } } - open = `<${node.name}${attrs}>`; + + open = (format && lastType !== TEXT ? indent : ''); + open += `<${node.name}${attrs}>`; if (node.value) body = nodesToString(node.value, depth + 2); - close = ``; - if (format) { - open = (lastType !== TEXT ? '\n' + ' '.repeat(depth) : '') + open; - close = (deepType === NODE ? '\n' + ' '.repeat(depth) : '') + close; - } + close = (format && deepType && deepType !== TEXT ? indent : ''); + close += ``; } else if (node.type === TEXT) { body = node.value || ''; } else if (node.type === CDATA) { - body = ``; + body = (format && lastType !== TEXT ? indent : ''); + body += ``; } else if (node.type === COMMENT) { - body = ``; + body = (format && lastType !== TEXT ? indent : ''); + body += ``; } result += `${open}${body}${close}`; @@ -528,9 +531,23 @@ class XmlParser extends NodeBase { }; const onCdata = (tagData, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars + if (ignoreNode) + return; + + if (!node.value) + node.value = []; + + node.value.push(this.createCdata(tagData).raw); } const onComment = (tagData, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars + if (ignoreNode) + return; + + if (!node.value) + node.value = []; + + node.value.push(this.createComment(tagData).raw); } sax.parseSync(xmlString, {