From 3a89f737a55e71feec1d72397f3ceba60742564e Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 20 Jun 2017 22:29:17 -0400 Subject: [PATCH 1/5] Support Colon Start Character Fixes #94 --- src/services/XmlFormatter.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/services/XmlFormatter.ts b/src/services/XmlFormatter.ts index a1aac00..7ad1a2c 100644 --- a/src/services/XmlFormatter.ts +++ b/src/services/XmlFormatter.ts @@ -60,7 +60,7 @@ export class XmlFormatter { } // - else if (/^<\w/.test(parts[i - 1]) && /^<\/\w/.test(parts[i]) + else if (/^<(\w|:)/.test(parts[i - 1]) && /^<\/(\w|:)/.test(parts[i]) && /^<[\w:\-\.\,]+/.exec(parts[i - 1])[0] == /^<\/[\w:\-\.\,]+/.exec(parts[i])[0].replace('/', '')) { output += parts[i]; @@ -68,12 +68,12 @@ export class XmlFormatter { } // - else if (parts[i].search(/<\w/) > -1 && parts[i].search(/<\//) == -1 && parts[i].search(/\/>/) == -1) { + else if (parts[i].search(/<(\w|:)/) > -1 && parts[i].search(/<\//) == -1 && parts[i].search(/\/>/) == -1) { output = (!inComment) ? output += this._getIndent(level++, parts[i]) : output += parts[i]; } // ... - else if (parts[i].search(/<\w/) > -1 && parts[i].search(/<\//) > -1) { + else if (parts[i].search(/<(\w|:)/) > -1 && parts[i].search(/<\//) > -1) { output = (!inComment) ? output += this._getIndent(level, parts[i]) : output += parts[i]; } From fd5749a49ac0acea9483a38bb55f91b3bb00d2d4 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 20 Jun 2017 22:50:21 -0400 Subject: [PATCH 2/5] Fix Eager Line Break Stripping This change ensures line breaks are not stripped outside of CDATA nodes if the line break is not preceded or followed by another whitespace character. Fixes #92 --- src/services/XmlFormatter.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/services/XmlFormatter.ts b/src/services/XmlFormatter.ts index 7ad1a2c..c23e145 100644 --- a/src/services/XmlFormatter.ts +++ b/src/services/XmlFormatter.ts @@ -152,6 +152,8 @@ export class XmlFormatter { for (let i = 0; i < xml.length; i++) { let char: string = xml.charAt(i); + let prev: string = xml.charAt(i - 1); + let next: string = xml.charAt(i + 1); if (char == '!' && (xml.substr(i, 8) == '![CDATA[' || xml.substr(i, 3) == '!--')) { inCdata = true; @@ -166,6 +168,14 @@ export class XmlFormatter { } else if (char.search(/[\r\n]/g) > -1 && !inCdata) { + if (/\r/.test(char) && /\S|\r|\n/.test(prev) && /\S|\r|\n/.test(xml.charAt(i + this.newLine.length))) { + output += char; + } + + else if (/\n/.test(char) && /\S|\r|\n/.test(xml.charAt(i - this.newLine.length)) && /\S|\r|\n/.test(next)) { + output += char; + } + continue; } From 3f7ee2773be813fe50accb15b48a9b9fb117ed3a Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 20 Jun 2017 23:04:45 -0400 Subject: [PATCH 3/5] Improve Closing Tag Identification The previous code would cause a self-closing tag to appear as the opening tag if the following tag was a closing tag of the same name. Fixes #90 --- src/services/XmlFormatter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/XmlFormatter.ts b/src/services/XmlFormatter.ts index c23e145..b6f9598 100644 --- a/src/services/XmlFormatter.ts +++ b/src/services/XmlFormatter.ts @@ -61,7 +61,7 @@ export class XmlFormatter { // else if (/^<(\w|:)/.test(parts[i - 1]) && /^<\/(\w|:)/.test(parts[i]) - && /^<[\w:\-\.\,]+/.exec(parts[i - 1])[0] == /^<\/[\w:\-\.\,]+/.exec(parts[i])[0].replace('/', '')) { + && /^<[\w:\-\.\,\/ ]+/.exec(parts[i - 1])[0] == /^<\/[\w:\-\.\, ]+/.exec(parts[i])[0].replace('/', '')) { output += parts[i]; if (!inComment) level--; From 157bafa5b93af8dcba3b3706b66a17b0d0100a7a Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 20 Jun 2017 23:30:54 -0400 Subject: [PATCH 4/5] Fix "xmlns" Identification The previous code did not account for "xmlns=" in self-closing elements (it only identified "xmlns:"). But the indent counter identified both, which caused the odd nesting behavior. Fixes #87 --- src/services/XmlFormatter.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/XmlFormatter.ts b/src/services/XmlFormatter.ts index b6f9598..6f4e314 100644 --- a/src/services/XmlFormatter.ts +++ b/src/services/XmlFormatter.ts @@ -83,12 +83,12 @@ export class XmlFormatter { } // - else if (parts[i].search(/\/>/) > -1 && (!this.splitNamespaces || parts[i].search(/xmlns\:/) == -1)) { + else if (parts[i].search(/\/>/) > -1 && (!this.splitNamespaces || parts[i].search(/xmlns(:|=)/) == -1)) { output = (!inComment) ? output += this._getIndent(level, parts[i]) : output += parts[i]; } // xmlns /> - else if (parts[i].search(/\/>/) > -1 && parts[i].search(/xmlns\:/) > -1 && this.splitNamespaces) { + else if (parts[i].search(/\/>/) > -1 && parts[i].search(/xmlns(:|=)/) > -1 && this.splitNamespaces) { output = (!inComment) ? output += this._getIndent(level--, parts[i]) : output += parts[i]; } From 79116359d8e66b1795773f16d7373560216db2b7 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 20 Jun 2017 23:49:59 -0400 Subject: [PATCH 5/5] v1.9.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f4839b3..2bea686 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": "1.9.0", + "version": "1.9.1", "publisher": "DotJoshJohnson", "author": { "name": "Josh Johnson",