From 97afac00312a2a8579dd01c7229873a28d0b07f4 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 12 Jun 2018 21:05:02 -0400 Subject: [PATCH] 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;