parent
							
								
									27433b47ce
								
							
						
					
					
						commit
						ea5416b506
					
				
					 6 changed files with 67 additions and 6 deletions
				
			
		| 
						 | 
					@ -5,7 +5,18 @@ import { ClassicXmlFormatter } from "./classic-xml-formatter";
 | 
				
			||||||
/* 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 {
 | 
				
			||||||
        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 = "";
 | 
					        let output = "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -133,15 +144,15 @@ export class V2XmlFormatter implements XmlFormatter {
 | 
				
			||||||
            else if (location === Location.Text && cc === "<" && nc === "/") {
 | 
					            else if (location === Location.Text && cc === "<" && nc === "/") {
 | 
				
			||||||
                indentLevel--;
 | 
					                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 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>)
 | 
					                // otherwise, this should be treated as a same-line end tag(ex. <element>text</element>)
 | 
				
			||||||
                if (lastNonTextLocation === Location.EndTag) {
 | 
					                if (pc === "\n") {
 | 
				
			||||||
                    output += `${options.newLine}${this._getIndent(options, indentLevel)}<`;
 | 
					                    output += `${this._getIndent(options, indentLevel)}<`;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                else if (pc === "\n") {
 | 
					                else if (lastNonTextLocation === Location.EndTag) {
 | 
				
			||||||
                    output += `${this._getIndent(options, indentLevel)}<`;
 | 
					                    output += `${options.newLine}${this._getIndent(options, indentLevel)}<`;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                else {
 | 
					                else {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -40,6 +40,14 @@ describe("V2XmlFormatter", () => {
 | 
				
			||||||
            testFormatter(xmlFormatter, options, "text-only-line");
 | 
					            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");
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										9
									
								
								src/test/test-data/preformatted.formatted.xml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/test/test-data/preformatted.formatted.xml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,9 @@
 | 
				
			||||||
 | 
					<root>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>
 | 
				
			||||||
 | 
					        <element2>text</element2>
 | 
				
			||||||
 | 
					    </element2>
 | 
				
			||||||
 | 
					</root>
 | 
				
			||||||
							
								
								
									
										9
									
								
								src/test/test-data/preformatted.unformatted.xml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/test/test-data/preformatted.unformatted.xml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,9 @@
 | 
				
			||||||
 | 
					<root>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>
 | 
				
			||||||
 | 
					        <element2>text</element2>
 | 
				
			||||||
 | 
					    </element2>
 | 
				
			||||||
 | 
					</root>
 | 
				
			||||||
							
								
								
									
										12
									
								
								src/test/test-data/preserve-breaks.formatted.xml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/test/test-data/preserve-breaks.formatted.xml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,12 @@
 | 
				
			||||||
 | 
					<root>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <element>
 | 
				
			||||||
 | 
					        <element2>text</element2>
 | 
				
			||||||
 | 
					    </element2>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					</root>
 | 
				
			||||||
							
								
								
									
										12
									
								
								src/test/test-data/preserve-breaks.unformatted.xml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/test/test-data/preserve-breaks.unformatted.xml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,12 @@
 | 
				
			||||||
 | 
					<root>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					    <element>text</element>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <element>
 | 
				
			||||||
 | 
					        <element2>text</element2>
 | 
				
			||||||
 | 
					    </element2>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					</root>
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue