(indent = 0)
| 537 | } |
| 538 | |
| 539 | toString(indent = 0): string { |
| 540 | const spaces = " ".repeat(indent); |
| 541 | const attrs = Object.entries(this.attributes) |
| 542 | .map(([key, val]) => `${key}="${this.escape(String(val))}"`) |
| 543 | .join(" "); |
| 544 | |
| 545 | const attrStr = attrs ? " " + attrs : ""; |
| 546 | |
| 547 | if (this.children.length === 0) { |
| 548 | return `${spaces}<${this.name}${attrStr} />`; |
| 549 | } |
| 550 | |
| 551 | const isAllText = this.children.every((c) => typeof c === "string"); |
| 552 | |
| 553 | if (isAllText) { |
| 554 | const textContent = this.children.map((c) => this.escape(c as string)).join(""); |
| 555 | return `${spaces}<${this.name}${attrStr}>${textContent}</${this.name}>`; |
| 556 | } |
| 557 | |
| 558 | const childrenStr = this.children |
| 559 | .map((c) => (typeof c === "string" ? this.escape(c) : c.toString(indent + 2))) |
| 560 | .join("\n"); |
| 561 | |
| 562 | return `${spaces}<${this.name}${attrStr}>\n${childrenStr}\n${spaces}</${this.name}>`; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
no test coverage detected