Skip to content

Commit 78e5d5a

Browse files
committed
feat(xml): added indents to xml
1 parent 19f20da commit 78e5d5a

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

src/XML/common.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
export class baseXMLBUilder {
22
protected _data: string[] = [];
3+
private tab: string = " ";
4+
private indent: number = 0;
35

46
protected putTag(name: string, attr: {[key: string]: string},
57
contents?: (() => void) | string) {
6-
const attrStr =
7-
Object.entries(attr)
8+
const attrStr = (() => {
9+
const a = Object.entries(attr)
810
.map(([key, value]) => {return `${key}="${value}"`;})
911
.join(' ');
12+
return a ? " " + a : "";
13+
})();
1014

1115
if (!contents) {
12-
this._data.push(`<${name} ${attrStr}/>`);
16+
this._data.push(`${this.tab.repeat(this.indent)}<${name}${attrStr} />`);
1317
return;
1418
}
1519

16-
this._data.push(`<${name} ${attrStr}>`);
20+
this._data.push(`${this.tab.repeat(this.indent)}<${name}${attrStr}>`);
1721
if (typeof contents === "function") {
22+
this.indent++;
1823
contents();
19-
this._data.push(`</${name}>`);
24+
this.indent--
25+
this._data.push(`${this.tab.repeat(this.indent)}</${name}>`);
2026
} else {
21-
const tmp: string[] = [contents, `</${name}>`];
22-
this._data[this._data.length - 1] += tmp.join("");
27+
this._data[this._data.length - 1] += `${contents}</${name}>`;
2328
}
2429
}
2530

0 commit comments

Comments
 (0)