From 0a49fc570bced4ec68d8f9e8eeb50c4c429b4bbb Mon Sep 17 00:00:00 2001 From: Ansgar Schulte Date: Tue, 10 Dec 2019 18:14:30 +0100 Subject: [PATCH 1/2] emptyNodesOnSameLine: true --- src/index.js | 13 ++++++++++++- test/main.js | 14 +++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index ce21283..56cab76 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,7 @@ const isSelfClosingTag = str => /<[^>]+\/>/.test(str); const isOpeningTag = str => isTag(str) && !isClosingTag(str) && !isSelfClosingTag(str); module.exports = (xml, config = {}) => { - let { indentor, textNodesOnSameLine } = config + let { indentor, textNodesOnSameLine, emptyNodesOnSameLine } = config let depth = 0 const indicesToRemove = [] indentor = indentor || ' ' @@ -39,6 +39,17 @@ module.exports = (xml, config = {}) => { } } + if(emptyNodesOnSameLine) { + // Lookbehind for [OpeningTag][Text][ClosingTag] + const oneBefore = arr[i-1] + + if(type === "ClosingTag" && oneBefore.type === "OpeningTag") { + // collapse into a single line + line = `${indentation}${oneBefore.value}${value}` + indicesToRemove.push(i-1, i) + } + } + return line; }) diff --git a/test/main.js b/test/main.js index 7a54cfe..182b221 100644 --- a/test/main.js +++ b/test/main.js @@ -68,7 +68,8 @@ describe("xml-beautifier", () => { ` expect(beautify(ori, { - textNodesOnSameLine: true + textNodesOnSameLine: true, + emptyNodesOnSameLine: true })).toEqual(expected) }) @@ -93,5 +94,16 @@ describe("xml-beautifier", () => { expect(endTime - startTime).toBeLessThan(5000) }) + it("should process 2MB of XML quickly with `emptyNodesOnSameLine`", () => { + const xml = fs.readFileSync(__dirname + '/huge.xml', 'utf8') + const startTime = Date.now() + beautify(xml, { + textNodesOnSameLine: true, + emptyNodesOnSameLine: true + }) + const endTime = Date.now() + expect(endTime - startTime).toBeLessThan(5000) + }) + }) }) From 973ea8a9729ec432f34345cfeaf61dac7529894c Mon Sep 17 00:00:00 2001 From: Ansgar Schulte Date: Tue, 10 Dec 2019 18:33:28 +0100 Subject: [PATCH 2/2] emptyNodesOnSameLine: true --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 56cab76..da559c3 100644 --- a/src/index.js +++ b/src/index.js @@ -46,7 +46,7 @@ module.exports = (xml, config = {}) => { if(type === "ClosingTag" && oneBefore.type === "OpeningTag") { // collapse into a single line line = `${indentation}${oneBefore.value}${value}` - indicesToRemove.push(i-1, i) + indicesToRemove.push(i-1) } }