forked from external/vscode-xml
parent
ea5416b506
commit
2c6a5ceaa0
@ -2,11 +2,17 @@ import { XmlFormatter } from "../xml-formatter";
|
|||||||
import { XmlFormattingOptions } from "../xml-formatting-options";
|
import { XmlFormattingOptions } from "../xml-formatting-options";
|
||||||
import { ClassicXmlFormatter } from "./classic-xml-formatter";
|
import { ClassicXmlFormatter } from "./classic-xml-formatter";
|
||||||
|
|
||||||
|
const MagicalStringOfWonders = "~::~MAAAGIC~::~";
|
||||||
|
|
||||||
/* tslint:disable no-use-before-declare */
|
/* tslint:disable no-use-before-declare */
|
||||||
export class V2XmlFormatter implements XmlFormatter {
|
export class V2XmlFormatter implements XmlFormatter {
|
||||||
formatXml(xml: string, options: XmlFormattingOptions): string {
|
formatXml(xml: string, options: XmlFormattingOptions): string {
|
||||||
|
// this replaces all "<" brackets inside of comments to a magical string
|
||||||
|
// so the following minification steps don't mess with comment formatting
|
||||||
|
xml = this._sanitizeComments(xml);
|
||||||
|
|
||||||
// remove whitespace from between tags, except for line breaks
|
// remove whitespace from between tags, except for line breaks
|
||||||
xml = xml.replace(/>\s{0,}</g, (match: string) => { // spaces between the node name and the first attribute
|
xml = xml.replace(/>\s{0,}</g, (match: string) => {
|
||||||
return match.replace(/[^\S\r\n]/g, "");
|
return match.replace(/[^\S\r\n]/g, "");
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -18,6 +24,9 @@ export class V2XmlFormatter implements XmlFormatter {
|
|||||||
return match.replace(/\s+/g, " ");
|
return match.replace(/\s+/g, " ");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// the coast is clear - we can drop those "<" brackets back in
|
||||||
|
xml = this._unsanitizeComments(xml);
|
||||||
|
|
||||||
let output = "";
|
let output = "";
|
||||||
|
|
||||||
let indentLevel = 0;
|
let indentLevel = 0;
|
||||||
@ -185,6 +194,46 @@ export class V2XmlFormatter implements XmlFormatter {
|
|||||||
private _getIndent(options: XmlFormattingOptions, indentLevel: number): string {
|
private _getIndent(options: XmlFormattingOptions, indentLevel: number): string {
|
||||||
return ((options.editorOptions.insertSpaces) ? " ".repeat(options.editorOptions.tabSize) : "\t").repeat(indentLevel);
|
return ((options.editorOptions.insertSpaces) ? " ".repeat(options.editorOptions.tabSize) : "\t").repeat(indentLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _sanitizeComments(xml: string): string {
|
||||||
|
let output = "";
|
||||||
|
let inComment = false;
|
||||||
|
|
||||||
|
for (let i = 0; i < xml.length; i++) {
|
||||||
|
const cc = xml[i];
|
||||||
|
const nc = xml.charAt(i + 1);
|
||||||
|
const nnc = xml.charAt(i + 2);
|
||||||
|
const pc = xml.charAt(i - 1);
|
||||||
|
|
||||||
|
if (!inComment && cc === "<" && nc === "!" && nnc === "-") {
|
||||||
|
inComment = true;
|
||||||
|
output += "<!--";
|
||||||
|
|
||||||
|
i += 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (inComment && cc === "<") {
|
||||||
|
output += MagicalStringOfWonders;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (inComment && cc === "-" && nc === "-" && nnc === ">") {
|
||||||
|
inComment = false;
|
||||||
|
output += "-->";
|
||||||
|
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
output += cc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _unsanitizeComments(xml: string): string {
|
||||||
|
return xml.replace(new RegExp(MagicalStringOfWonders, "g"), "<");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Location {
|
enum Location {
|
||||||
|
@ -48,6 +48,10 @@ describe("V2XmlFormatter", () => {
|
|||||||
testFormatter(xmlFormatter, options, "preserve-breaks");
|
testFormatter(xmlFormatter, options, "preserve-breaks");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should maintain comment formatting", () => {
|
||||||
|
testFormatter(xmlFormatter, options, "maintain-comment-formatting");
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
13
src/test/test-data/maintain-comment-formatting.formatted.xml
Normal file
13
src/test/test-data/maintain-comment-formatting.formatted.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<root>
|
||||||
|
<element>text</element>
|
||||||
|
<!--
|
||||||
|
<Description>
|
||||||
|
Any description.
|
||||||
|
</Description>
|
||||||
|
<Keyword UUID="fd8e6b13-9865-4d8c-9ecd-5ff08a0cf2e2"/>
|
||||||
|
<Parameters>
|
||||||
|
<Parameter Name="ParameterName1">The Parameter Name.</Parameter>
|
||||||
|
<Parameter Name="ParameterName2" PossibleValues="Val1,Val2" DefaultValue="Val1"/>
|
||||||
|
</Parameters>
|
||||||
|
-->
|
||||||
|
</root>
|
@ -0,0 +1,13 @@
|
|||||||
|
<root>
|
||||||
|
<element>text</element>
|
||||||
|
<!--
|
||||||
|
<Description>
|
||||||
|
Any description.
|
||||||
|
</Description>
|
||||||
|
<Keyword UUID="fd8e6b13-9865-4d8c-9ecd-5ff08a0cf2e2"/>
|
||||||
|
<Parameters>
|
||||||
|
<Parameter Name="ParameterName1">The Parameter Name.</Parameter>
|
||||||
|
<Parameter Name="ParameterName2" PossibleValues="Val1,Val2" DefaultValue="Val1"/>
|
||||||
|
</Parameters>
|
||||||
|
-->
|
||||||
|
</root>
|
Loading…
Reference in New Issue
Block a user