Prevent CDATA Formatting

fixes: #293
This commit is contained in:
Josh Johnson 2020-07-06 23:13:40 -04:00
parent 6fd54a2d01
commit 10619ee123

View file

@ -7,9 +7,9 @@ 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 // this replaces all "<" brackets inside of comments and CDATA to a magical string
// so the following minification steps don't mess with comment formatting // so the following minification steps don't mess with comment and CDATA formatting
xml = this._sanitizeComments(xml); xml = this._sanitizeCommentsAndCDATA(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) => { xml = xml.replace(/>\s{0,}</g, (match: string) => {
@ -25,7 +25,7 @@ export class V2XmlFormatter implements XmlFormatter {
}); });
// the coast is clear - we can drop those "<" brackets back in // the coast is clear - we can drop those "<" brackets back in
xml = this._unsanitizeComments(xml); xml = this._unsanitizeCommentsAndCDATA(xml);
let output = ""; let output = "";
@ -298,9 +298,9 @@ export class V2XmlFormatter implements XmlFormatter {
return text.replace(/[^\r\n\S]+$/, ""); return text.replace(/[^\r\n\S]+$/, "");
} }
private _sanitizeComments(xml: string): string { private _sanitizeCommentsAndCDATA(xml: string): string {
let output = ""; let output = "";
let inComment = false; let inCommentOrCDATA = false;
for (let i = 0; i < xml.length; i++) { for (let i = 0; i < xml.length; i++) {
const cc = xml[i]; const cc = xml[i];
@ -308,20 +308,20 @@ export class V2XmlFormatter implements XmlFormatter {
const nnc = xml.charAt(i + 2); const nnc = xml.charAt(i + 2);
const pc = xml.charAt(i - 1); const pc = xml.charAt(i - 1);
if (!inComment && cc === "<" && nc === "!" && nnc === "-") { if (!inCommentOrCDATA && cc === "<" && nc === "!" && (nnc === "-" || nnc === "[")) {
inComment = true; inCommentOrCDATA = true;
output += "<!--"; output += (nnc === "-") ? "<!--" : "<![CDATA[";
i += 3; i += (nnc === "-") ? 3 : 8;
} }
else if (inComment && cc === "<") { else if (inCommentOrCDATA && cc === "<") {
output += MagicalStringOfWonders; output += MagicalStringOfWonders;
} }
else if (inComment && cc === "-" && nc === "-" && nnc === ">") { else if (inCommentOrCDATA && (cc === "-" && nc === "-" && nnc === ">") || (cc === "]" && nc === "]" && nnc === ">")) {
inComment = false; inCommentOrCDATA = false;
output += "-->"; output += (cc === "-") ? "-->" : "]]>";
i += 2; i += 2;
} }
@ -334,7 +334,7 @@ export class V2XmlFormatter implements XmlFormatter {
return output; return output;
} }
private _unsanitizeComments(xml: string): string { private _unsanitizeCommentsAndCDATA(xml: string): string {
return xml.replace(new RegExp(MagicalStringOfWonders, "g"), "<"); return xml.replace(new RegExp(MagicalStringOfWonders, "g"), "<");
} }
} }