Skip to content

Commit f2cfe16

Browse files
Merge pull request #198 from DotJoshJohnson/release-2.3.1
Release v2.3.1
2 parents 0a17f59 + 1641e7a commit f2cfe16

File tree

8 files changed

+81
-4
lines changed

8 files changed

+81
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "xml",
33
"displayName": "XML Tools",
44
"description": "XML Formatting, XQuery, and XPath Tools for Visual Studio Code",
5-
"version": "2.3.0",
5+
"version": "2.3.1",
66
"preview": false,
77
"publisher": "DotJoshJohnson",
88
"author": {

src/common/xml-traverser.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,19 @@ export class XmlTraverser {
6464
}
6565

6666
getSiblings(node: Node): Node[] {
67-
return [...this.getChildAttributeArray(<Element>node.parentNode), ...this.getChildElementArray(node.parentNode)];
67+
if (this.isElement(node)) {
68+
return this.getSiblingElements(node);
69+
}
70+
71+
return this.getSiblingAttributes(node);
72+
}
73+
74+
getSiblingAttributes(node: Node): Node[] {
75+
return this.getChildAttributeArray(<Element>node.parentNode);
76+
}
77+
78+
getSiblingElements(node: Node): Node[] {
79+
return this.getChildElementArray(node.parentNode);
6880
}
6981

7082
hasSimilarSiblings(node: Node): boolean {

src/formatting/formatters/v2-xml-formatter.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class V2XmlFormatter implements XmlFormatter {
3333
let location = Location.Text;
3434
let lastNonTextLocation = Location.Text; // hah
3535
let attributeQuote = "";
36+
let lineBreakSpree = false;
3637

3738
// NOTE: all "exiting" checks should appear after their associated "entering" checks
3839
for (let i = 0; i < xml.length; i++) {
@@ -44,7 +45,14 @@ export class V2XmlFormatter implements XmlFormatter {
4445

4546
// entering CData
4647
if (location === Location.Text && cc === "<" && nc === "!" && nnc === "[") {
47-
output += `${this._getIndent(options, indentLevel)}<`;
48+
if (pc === ">" && ppc !== "/") {
49+
output += "<";
50+
}
51+
52+
else {
53+
output += `${this._getIndent(options, indentLevel)}<`;
54+
}
55+
4856
location = Location.CData;
4957
}
5058

@@ -94,6 +102,8 @@ export class V2XmlFormatter implements XmlFormatter {
94102
}
95103

96104
else {
105+
// removing trailing non-breaking whitespace here prevents endless indentations (issue #193)
106+
output = this._removeTrailingNonBreakingWhitespace(output);
97107
output += `${this._getIndent(options, indentLevel)}<`;
98108
}
99109

@@ -176,8 +186,11 @@ export class V2XmlFormatter implements XmlFormatter {
176186
// if the end tag immediately follows a line break, just add an indentation
177187
// if the end tag immediately follows another end tag or a self-closing tag (issue #185), add a line break and indent
178188
// otherwise, this should be treated as a same-line end tag(ex. <element>text</element>)
179-
if (pc === "\n") {
189+
if (pc === "\n" || lineBreakSpree) {
190+
// removing trailing non-breaking whitespace here prevents endless indentations (issue #193)
191+
output = this._removeTrailingNonBreakingWhitespace(output);
180192
output += `${this._getIndent(options, indentLevel)}<`;
193+
lineBreakSpree = false;
181194
}
182195

183196
else if (lastNonTextLocation === Location.EndTag) {
@@ -204,6 +217,14 @@ export class V2XmlFormatter implements XmlFormatter {
204217

205218
// Text
206219
else {
220+
if (cc === "\n") {
221+
lineBreakSpree = true;
222+
}
223+
224+
else if (lineBreakSpree && /\S/.test(cc)) {
225+
lineBreakSpree = false;
226+
}
227+
207228
output += cc;
208229
}
209230
}
@@ -219,6 +240,10 @@ export class V2XmlFormatter implements XmlFormatter {
219240
return ((options.editorOptions.insertSpaces) ? " ".repeat(options.editorOptions.tabSize) : "\t").repeat(indentLevel);
220241
}
221242

243+
private _removeTrailingNonBreakingWhitespace(text: string): string {
244+
return text.replace(/[^\r\n\S]+$/, "");
245+
}
246+
222247
private _sanitizeComments(xml: string): string {
223248
let output = "";
224249
let inComment = false;

src/test/extension.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ describe("V2XmlFormatter", () => {
8080
testFormatter(xmlFormatter, options, "issue-189");
8181
});
8282

83+
it("should not add extra line breaks before closing tags", () => {
84+
testFormatter(xmlFormatter, options, "issue-193");
85+
});
86+
87+
it("should not add extra whitespace before CDATA", () => {
88+
testFormatter(xmlFormatter, options, "issue-194");
89+
});
90+
8391
});
8492

8593
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<xsl:template name="btn-export-excel-pdf">
2+
<div class="row">
3+
<div class="col-md button-wrapper text-center">
4+
<a class="btn btn-outline-info" role="button" href="javascript:template.printToPdf()" target="_blank">
5+
<i class="far fa-file-pdf">&#160;</i> Export to PDF
6+
</a> &#160;
7+
<a class="btn btn-outline-info" role="button" href="javascript:template.exportToExcel()" target="_blank">
8+
<i class="far fa-file-excel">&#160;</i> Export to EXCEL
9+
</a>
10+
</div>
11+
</div>
12+
</xsl:template>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<xsl:template name="btn-export-excel-pdf">
2+
<div class="row">
3+
<div class="col-md button-wrapper text-center">
4+
<a class="btn btn-outline-info" role="button" href="javascript:template.printToPdf()" target="_blank">
5+
<i class="far fa-file-pdf">&#160;</i> Export to PDF
6+
</a> &#160;
7+
<a class="btn btn-outline-info" role="button" href="javascript:template.exportToExcel()" target="_blank">
8+
<i class="far fa-file-excel">&#160;</i> Export to EXCEL
9+
</a>
10+
</div>
11+
</div>
12+
</xsl:template>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<madeup>
3+
<some>
4+
<element>This is ok</element>
5+
<other><![CDATA[Here is my cdata]]></other>
6+
</some>
7+
</madeup>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="utf-8"?><madeup><some><element>This is ok</element><other><![CDATA[Here is my cdata]]></other></some></madeup>

0 commit comments

Comments
 (0)