diff --git a/src/index.js b/src/index.js index ce21283..da559c3 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) + } + } + 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) + }) + }) })