* Serializes the element and its children to an HTML string. * All attribute values and text content are properly escaped. * @returns The generated HTML string.
()
| 65 | * @returns The generated HTML string. |
| 66 | */ |
| 67 | toString(): string { |
| 68 | const childrenStr = this.children |
| 69 | .map((child) => (typeof child === "string" ? escape(child) : child.toString())) |
| 70 | .join(""); |
| 71 | |
| 72 | if (this.tag === "") { |
| 73 | // If the tag is empty, it acts as a fragment. |
| 74 | return childrenStr; |
| 75 | } else { |
| 76 | const attrs = []; |
| 77 | if (this.attrId) attrs.push(`id="${escape(this.attrId)}"`); |
| 78 | if (this.attrClass.length) attrs.push(`class="${escape(this.attrClass.join(" "))}"`); |
| 79 | if (this.attrTitle) attrs.push(`title="${escape(this.attrTitle)}"`); |
| 80 | if (this.attrStyle) attrs.push(`style="${escape(this.attrStyle)}"`); |
| 81 | return `<${this.tag} ${attrs.join(" ")}>${childrenStr}</${this.tag}>`; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Converts the `H` instance into a live DOM element or a DocumentFragment. |
no test coverage detected