Preserve Line Breaks for Readability

#59
This commit is contained in:
Josh Johnson 2018-02-01 20:46:01 -05:00
parent 27433b47ce
commit ea5416b506
6 changed files with 67 additions and 6 deletions

View File

@ -5,7 +5,18 @@ import { ClassicXmlFormatter } from "./classic-xml-formatter";
/* tslint:disable no-use-before-declare */
export class V2XmlFormatter implements XmlFormatter {
formatXml(xml: string, options: XmlFormattingOptions): string {
xml = this.minifyXml(xml, options);
// 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
return match.replace(/[^\S\r\n]/g, "");
});
// do some light minification to get rid of insignificant whitespace
xml = xml.replace(/"\s+(?=[^\s]+=)/g, "\" "); // spaces between attributes
xml = xml.replace(/"\s+(?=>)/g, "\""); // spaces between the last attribute and tag close (>)
xml = xml.replace(/"\s+(?=\/>)/g, "\" "); // spaces between the last attribute and tag close (/>)
xml = xml.replace(/[^ <>="]\s+[^ <>="]+=/g, (match: string) => { // spaces between the node name and the first attribute
return match.replace(/\s+/g, " ");
});
let output = "";
@ -133,15 +144,15 @@ export class V2XmlFormatter implements XmlFormatter {
else if (location === Location.Text && cc === "<" && nc === "/") {
indentLevel--;
// if the end tag immediately follows another end tag, add a line break and indent
// if the end tag immediately follows a line break, just add an indentation
// if the end tag immediately follows another end tag, add a line break and indent
// otherwise, this should be treated as a same-line end tag(ex. <element>text</element>)
if (lastNonTextLocation === Location.EndTag) {
output += `${options.newLine}${this._getIndent(options, indentLevel)}<`;
if (pc === "\n") {
output += `${this._getIndent(options, indentLevel)}<`;
}
else if (pc === "\n") {
output += `${this._getIndent(options, indentLevel)}<`;
else if (lastNonTextLocation === Location.EndTag) {
output += `${options.newLine}${this._getIndent(options, indentLevel)}<`;
}
else {

View File

@ -40,6 +40,14 @@ describe("V2XmlFormatter", () => {
testFormatter(xmlFormatter, options, "text-only-line");
});
it("should handle preformatted xml", () => {
testFormatter(xmlFormatter, options, "preformatted");
});
it ("should preserve line breaks between elements", () => {
testFormatter(xmlFormatter, options, "preserve-breaks");
});
});
});

View File

@ -0,0 +1,9 @@
<root>
<element>text</element>
<element>text</element>
<element>text</element>
<element>text</element>
<element>
<element2>text</element2>
</element2>
</root>

View File

@ -0,0 +1,9 @@
<root>
<element>text</element>
<element>text</element>
<element>text</element>
<element>text</element>
<element>
<element2>text</element2>
</element2>
</root>

View File

@ -0,0 +1,12 @@
<root>
<element>text</element>
<element>text</element>
<element>text</element>
<element>text</element>
<element>
<element2>text</element2>
</element2>
</root>

View File

@ -0,0 +1,12 @@
<root>
<element>text</element>
<element>text</element>
<element>text</element>
<element>text</element>
<element>
<element2>text</element2>
</element2>
</root>