Add First Test
This commit is contained in:
parent
e1d41f6025
commit
b9b6aec528
6 changed files with 88 additions and 29 deletions
|
|
@ -25,7 +25,7 @@ export class V2XmlFormatter implements XmlFormatter {
|
|||
output += `${this._getIndent(options, indentLevel)}<`;
|
||||
location = Location.CData;
|
||||
}
|
||||
|
||||
|
||||
// exiting CData
|
||||
else if (location === Location.CData && cc === "]" && nc === "]" && nnc === ">") {
|
||||
output += "]]>";
|
||||
|
|
@ -34,13 +34,13 @@ export class V2XmlFormatter implements XmlFormatter {
|
|||
lastNonTextLocation = location;
|
||||
location = Location.Text;
|
||||
}
|
||||
|
||||
|
||||
// entering Comment
|
||||
else if (location === Location.Text && cc === "<" && nc === "!" && nnc === "-") {
|
||||
output += `${this._getIndent(options, indentLevel)}<`;
|
||||
location = Location.Comment;
|
||||
}
|
||||
|
||||
|
||||
// exiting Comment
|
||||
else if (location === Location.Comment && cc === "-" && nc === "-" && nnc === ">") {
|
||||
output += "-->";
|
||||
|
|
@ -49,20 +49,20 @@ export class V2XmlFormatter implements XmlFormatter {
|
|||
lastNonTextLocation = location;
|
||||
location = Location.Text;
|
||||
}
|
||||
|
||||
|
||||
// entering SpecialTag
|
||||
else if (location === Location.Text && cc === "<" && (nc === "!" || nc === "?")) {
|
||||
output += `${this._getIndent(options, indentLevel)}<`;
|
||||
location = Location.SpecialTag;
|
||||
}
|
||||
|
||||
|
||||
// exiting SpecialTag
|
||||
else if (location === Location.SpecialTag && cc === ">") {
|
||||
output += `>`;
|
||||
lastNonTextLocation = location;
|
||||
location = Location.Text;
|
||||
}
|
||||
|
||||
|
||||
// entering StartTag.StartTagName
|
||||
else if (location === Location.Text && cc === "<" && ["/", "!"].indexOf(nc) === -1) {
|
||||
// if this occurs after another tag, prepend a line break
|
||||
|
|
@ -77,17 +77,20 @@ export class V2XmlFormatter implements XmlFormatter {
|
|||
indentLevel++;
|
||||
location = Location.StartTagName;
|
||||
}
|
||||
|
||||
|
||||
// exiting StartTag.StartTagName, enter StartTag
|
||||
else if (location === Location.StartTagName && cc === " ") {
|
||||
output += " ";
|
||||
lastNonTextLocation = location;
|
||||
location = Location.StartTag;
|
||||
}
|
||||
|
||||
|
||||
// entering StartTag.Attribute
|
||||
else if (location === Location.StartTag && [" ", "/", ">"].indexOf(cc) === -1) {
|
||||
if (lastNonTextLocation === Location.AttributeValue && ((options.splitXmlnsOnFormat && xml.substr(i, 5).toLowerCase() === "xmlns") || options.splitAttributesOnFormat)) {
|
||||
if (lastNonTextLocation === Location.AttributeValue
|
||||
&& ((options.splitXmlnsOnFormat
|
||||
&& xml.substr(i, 5).toLowerCase() === "xmlns")
|
||||
|| options.splitAttributesOnFormat)) {
|
||||
output += `${options.newLine}${this._getIndent(options, indentLevel)}`;
|
||||
}
|
||||
|
||||
|
|
@ -95,14 +98,14 @@ export class V2XmlFormatter implements XmlFormatter {
|
|||
lastNonTextLocation = location;
|
||||
location = Location.Attribute;
|
||||
}
|
||||
|
||||
|
||||
// entering StartTag.Attribute.AttributeValue
|
||||
else if (location === Location.Attribute && cc === "\"") {
|
||||
output += "\"";
|
||||
lastNonTextLocation = location;
|
||||
location = Location.AttributeValue;
|
||||
}
|
||||
|
||||
|
||||
// exiting StartTag.Attribute.AttributeValue, entering StartTag
|
||||
else if (location === Location.AttributeValue && cc === "\"") {
|
||||
output += "\"";
|
||||
|
|
@ -118,6 +121,21 @@ export class V2XmlFormatter implements XmlFormatter {
|
|||
output += `>${options.newLine}`;
|
||||
}
|
||||
|
||||
// if this is an open tag followed by a line break, add an indent before the text (after the line break)
|
||||
// TODO: there could be multiple lines of text here, so we'll need a less naive implementation at some point
|
||||
else if (nc === "\r" || nc === "\n") {
|
||||
output += `>${options.newLine}${this._getIndent(options, indentLevel)}`;
|
||||
|
||||
// fast-forward based on what type of line break was used
|
||||
if (nc === "\r") {
|
||||
i += 2;
|
||||
}
|
||||
|
||||
else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
output += ">";
|
||||
}
|
||||
|
|
@ -125,31 +143,36 @@ export class V2XmlFormatter implements XmlFormatter {
|
|||
lastNonTextLocation = location;
|
||||
location = Location.Text;
|
||||
}
|
||||
|
||||
|
||||
// entering EndTag
|
||||
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
|
||||
// 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)}<`;
|
||||
}
|
||||
|
||||
|
||||
else if (pc === "\n") {
|
||||
output += `${this._getIndent(options, indentLevel)}<`;
|
||||
}
|
||||
|
||||
else {
|
||||
output += "<";
|
||||
}
|
||||
|
||||
location = Location.EndTag;
|
||||
}
|
||||
|
||||
|
||||
// exiting EndTag, entering Text
|
||||
else if (location === Location.EndTag && cc === ">") {
|
||||
output += ">";
|
||||
lastNonTextLocation = location;
|
||||
location = Location.Text;
|
||||
}
|
||||
|
||||
|
||||
// Text
|
||||
else {
|
||||
output += cc;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue