From 69a7a9164bede4989d5644f84a8ca2f710667a2f Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 12 Jun 2018 20:11:52 -0400 Subject: [PATCH 1/7] Add Failing Test Issue: #193 --- src/test/extension.test.ts | 4 ++++ src/test/test-data/issue-193.formatted.xml | 12 ++++++++++++ src/test/test-data/issue-193.unformatted.xml | 12 ++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/test/test-data/issue-193.formatted.xml create mode 100644 src/test/test-data/issue-193.unformatted.xml diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index 45666e7..a53a598 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -80,6 +80,10 @@ describe("V2XmlFormatter", () => { testFormatter(xmlFormatter, options, "issue-189"); }); + it("should not add extra line breaks before closing tags", () => { + testFormatter(xmlFormatter, options, "issue-193"); + }); + }); }); diff --git a/src/test/test-data/issue-193.formatted.xml b/src/test/test-data/issue-193.formatted.xml new file mode 100644 index 0000000..18b4d70 --- /dev/null +++ b/src/test/test-data/issue-193.formatted.xml @@ -0,0 +1,12 @@ + + + \ No newline at end of file diff --git a/src/test/test-data/issue-193.unformatted.xml b/src/test/test-data/issue-193.unformatted.xml new file mode 100644 index 0000000..e157813 --- /dev/null +++ b/src/test/test-data/issue-193.unformatted.xml @@ -0,0 +1,12 @@ + + + \ No newline at end of file From a99ba2ea41c43af989e20590c6c5cc840a1b2d13 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 12 Jun 2018 20:27:39 -0400 Subject: [PATCH 2/7] Eliminate Extra Line Breaks This only solves half of the problem. Issue: #193 --- src/formatting/formatters/v2-xml-formatter.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/formatting/formatters/v2-xml-formatter.ts b/src/formatting/formatters/v2-xml-formatter.ts index 0d8af2a..1f8179e 100644 --- a/src/formatting/formatters/v2-xml-formatter.ts +++ b/src/formatting/formatters/v2-xml-formatter.ts @@ -33,6 +33,7 @@ export class V2XmlFormatter implements XmlFormatter { let location = Location.Text; let lastNonTextLocation = Location.Text; // hah let attributeQuote = ""; + let lineBreakSpree = false; // NOTE: all "exiting" checks should appear after their associated "entering" checks for (let i = 0; i < xml.length; i++) { @@ -176,8 +177,9 @@ export class V2XmlFormatter implements XmlFormatter { // if the end tag immediately follows a line break, just add an indentation // if the end tag immediately follows another end tag or a self-closing tag (issue #185), add a line break and indent // otherwise, this should be treated as a same-line end tag(ex. text) - if (pc === "\n") { + if (pc === "\n" || lineBreakSpree) { output += `${this._getIndent(options, indentLevel)}<`; + lineBreakSpree = false; } else if (lastNonTextLocation === Location.EndTag) { @@ -204,6 +206,14 @@ export class V2XmlFormatter implements XmlFormatter { // Text else { + if (cc === "\n") { + lineBreakSpree = true; + } + + else if (lineBreakSpree && /\S/.test(cc)) { + lineBreakSpree = false; + } + output += cc; } } From 97afac00312a2a8579dd01c7229873a28d0b07f4 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 12 Jun 2018 21:05:02 -0400 Subject: [PATCH 3/7] Prevent Endless Indentation Issue: #193 --- src/formatting/formatters/v2-xml-formatter.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/formatting/formatters/v2-xml-formatter.ts b/src/formatting/formatters/v2-xml-formatter.ts index 1f8179e..975a2c0 100644 --- a/src/formatting/formatters/v2-xml-formatter.ts +++ b/src/formatting/formatters/v2-xml-formatter.ts @@ -95,6 +95,8 @@ export class V2XmlFormatter implements XmlFormatter { } else { + // removing trailing non-breaking whitespace here prevents endless indentations (issue #193) + output = this._removeTrailingNonBreakingWhitespace(output); output += `${this._getIndent(options, indentLevel)}<`; } @@ -178,6 +180,8 @@ export class V2XmlFormatter implements XmlFormatter { // if the end tag immediately follows another end tag or a self-closing tag (issue #185), add a line break and indent // otherwise, this should be treated as a same-line end tag(ex. text) if (pc === "\n" || lineBreakSpree) { + // removing trailing non-breaking whitespace here prevents endless indentations (issue #193) + output = this._removeTrailingNonBreakingWhitespace(output); output += `${this._getIndent(options, indentLevel)}<`; lineBreakSpree = false; } @@ -229,6 +233,10 @@ export class V2XmlFormatter implements XmlFormatter { return ((options.editorOptions.insertSpaces) ? " ".repeat(options.editorOptions.tabSize) : "\t").repeat(indentLevel); } + private _removeTrailingNonBreakingWhitespace(text: string): string { + return text.replace(/[^\r\n\S]+$/, ""); + } + private _sanitizeComments(xml: string): string { let output = ""; let inComment = false; From 9a2147683c290ddd835a6feacc35807dff28be65 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 12 Jun 2018 21:22:21 -0400 Subject: [PATCH 4/7] Add Failing Test Issue: #194 --- src/test/extension.test.ts | 4 ++++ src/test/test-data/issue-194.formatted.xml | 7 +++++++ src/test/test-data/issue-194.unformatted.xml | 1 + 3 files changed, 12 insertions(+) create mode 100644 src/test/test-data/issue-194.formatted.xml create mode 100644 src/test/test-data/issue-194.unformatted.xml diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index a53a598..c09df83 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -84,6 +84,10 @@ describe("V2XmlFormatter", () => { testFormatter(xmlFormatter, options, "issue-193"); }); + it("should not add extra whitespace before CDATA", () => { + testFormatter(xmlFormatter, options, "issue-194"); + }); + }); }); diff --git a/src/test/test-data/issue-194.formatted.xml b/src/test/test-data/issue-194.formatted.xml new file mode 100644 index 0000000..fb6e5a3 --- /dev/null +++ b/src/test/test-data/issue-194.formatted.xml @@ -0,0 +1,7 @@ + + + + This is ok + + + \ No newline at end of file diff --git a/src/test/test-data/issue-194.unformatted.xml b/src/test/test-data/issue-194.unformatted.xml new file mode 100644 index 0000000..7700a30 --- /dev/null +++ b/src/test/test-data/issue-194.unformatted.xml @@ -0,0 +1 @@ +This is ok \ No newline at end of file From 61a07adb59c3da3386e323f4df6d9b7412416afa Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 12 Jun 2018 21:23:31 -0400 Subject: [PATCH 5/7] Prevent Indentation for Inline CDATA Issue: #194 --- src/formatting/formatters/v2-xml-formatter.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/formatting/formatters/v2-xml-formatter.ts b/src/formatting/formatters/v2-xml-formatter.ts index 975a2c0..ab60c13 100644 --- a/src/formatting/formatters/v2-xml-formatter.ts +++ b/src/formatting/formatters/v2-xml-formatter.ts @@ -45,7 +45,14 @@ export class V2XmlFormatter implements XmlFormatter { // entering CData if (location === Location.Text && cc === "<" && nc === "!" && nnc === "[") { - output += `${this._getIndent(options, indentLevel)}<`; + if (pc === ">" && ppc !== "/") { + output += "<"; + } + + else { + output += `${this._getIndent(options, indentLevel)}<`; + } + location = Location.CData; } From 289202d64b2cc88310665cc010f2e22648b56c29 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 12 Jun 2018 21:46:48 -0400 Subject: [PATCH 6/7] Remove Sibling Node Mixing Issue: #197 --- src/common/xml-traverser.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/common/xml-traverser.ts b/src/common/xml-traverser.ts index 9273b77..e4aa5c6 100644 --- a/src/common/xml-traverser.ts +++ b/src/common/xml-traverser.ts @@ -64,7 +64,19 @@ export class XmlTraverser { } getSiblings(node: Node): Node[] { - return [...this.getChildAttributeArray(node.parentNode), ...this.getChildElementArray(node.parentNode)]; + if (this.isElement(node)) { + return this.getSiblingElements(node); + } + + return this.getSiblingAttributes(node); + } + + getSiblingAttributes(node: Node): Node[] { + return this.getChildAttributeArray(node.parentNode); + } + + getSiblingElements(node: Node): Node[] { + return this.getChildElementArray(node.parentNode); } hasSimilarSiblings(node: Node): boolean { From 1641e7abbfa73be84ed46a34648f1abb55f6678f Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Fri, 15 Jun 2018 22:27:40 -0400 Subject: [PATCH 7/7] v2.3.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 009eb8c..b560753 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "xml", "displayName": "XML Tools", "description": "XML Formatting, XQuery, and XPath Tools for Visual Studio Code", - "version": "2.3.0", + "version": "2.3.1", "preview": false, "publisher": "DotJoshJohnson", "author": {