This commit is contained in:
Josh Johnson 2016-01-09 00:35:50 -05:00
parent 4dcac61936
commit 5e637bb185

View File

@ -119,8 +119,10 @@ export class XmlFormatter {
removeComments = false; removeComments = false;
} }
xml = this._stripLineBreaks(xml);
xml = (removeComments) ? xml.replace(/\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>/g, '') : xml; xml = (removeComments) ? xml.replace(/\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>/g, '') : xml;
xml = xml.replace(/>\s{0,}</g, '><'); xml = xml.replace(/>\s{0,}</g, '><');
xml = xml.replace(/"\s+(?=[^\s]+=)/g, '" ');
return xml; return xml;
} }
@ -130,6 +132,34 @@ export class XmlFormatter {
return `${this.newLine}${this.indentPattern.repeat(level)}${trailingValue}`; return `${this.newLine}${this.indentPattern.repeat(level)}${trailingValue}`;
} }
private _stripLineBreaks(xml: string): string {
let output: string = '';
let inTag: boolean = false;
let inTagName: boolean = false;
let inCdata: boolean = false;
let inAttribute: boolean = false;
for (let i = 0; i < xml.length; i++) {
let char: string = xml.charAt(i);
if (char == '!' && (xml.substr(i, 8) == '![CDATA[' || xml.substr(i, 3) == '!--')) {
inCdata = true;
}
else if (char == ']' && (xml.substr(i, 3) == ']]>' || xml.substr(i, 3) == '-->')) {
inCdata = false;
}
else if (char.search(/[\r\n]/g) > -1 && !inCdata) {
continue;
}
output += char;
}
return output;
}
} }
export interface IXmlFormatterOptions { export interface IXmlFormatterOptions {