(event: Event)
| 2580 | } |
| 2581 | |
| 2582 | private write(event: Event): string { |
| 2583 | if (event._tag === "EndElement") { |
| 2584 | const open = this.stack.length > 0 ? this.stack[this.stack.length - 1] : undefined |
| 2585 | if (open === undefined || event.name !== open) { |
| 2586 | // Only valid names are written into messages |
| 2587 | checkName("element name", event.name) |
| 2588 | throw invalidEvent( |
| 2589 | open === undefined |
| 2590 | ? `end tag </${event.name}> without a start tag` |
| 2591 | : `end tag </${event.name}> does not match <${open}>` |
| 2592 | ) |
| 2593 | } |
| 2594 | this.stack.pop() |
| 2595 | if (this.stack.length === 0) this.state = EPILOG |
| 2596 | return `</${open}>` |
| 2597 | } |
| 2598 | switch (event._tag) { |
| 2599 | case "StartElement": { |
| 2600 | this.checkName("element name", event.name) |
| 2601 | if (this.state === EPILOG) throw invalidEvent(`second root element <${event.name}>`) |
| 2602 | let tag = "<" + event.name |
| 2603 | let valid = true |
| 2604 | for (const name in event.attributes) { |
| 2605 | if (!Object.hasOwn(event.attributes, name)) continue |
| 2606 | const value = event.attributes[name] |
| 2607 | if (typeof value !== "string" || !this.isName(name)) { |
| 2608 | valid = false |
| 2609 | break |
| 2610 | } |
| 2611 | tag += ` ${name}="${escapeAttribute(value)}"` |
| 2612 | } |
| 2613 | // Escaping keeps invalid characters, so one search of the tag checks |
| 2614 | // every value; the first error is then found in attribute order |
| 2615 | if (!valid || (tag.length > event.name.length + 1 && searchInvalid(tag) !== -1)) { |
| 2616 | this.checkAttributes(event.attributes) |
| 2617 | } |
| 2618 | this.stack.push(event.name) |
| 2619 | this.state = CONTENT |
| 2620 | return tag + ">" |
| 2621 | } |
| 2622 | case "Text": { |
| 2623 | if (this.state !== CONTENT) { |
| 2624 | checkChars("text", event.text) |
| 2625 | if (!isWhitespace(event.text)) throw invalidEvent("text outside the root element must be whitespace") |
| 2626 | if (this.state === NOTHING_WRITTEN && event.text.length > 0) this.state = PROLOG |
| 2627 | return event.text |
| 2628 | } |
| 2629 | checkChars("text", event.text) |
| 2630 | return escapeText(event.text) |
| 2631 | } |
| 2632 | case "Comment": { |
| 2633 | checkChars("comment", event.text) |
| 2634 | if (event.text.includes("--") || event.text.endsWith("-")) { |
| 2635 | throw invalidEvent("comments cannot contain \"--\" or end with \"-\"") |
| 2636 | } |
| 2637 | if (this.state === NOTHING_WRITTEN) this.state = PROLOG |
| 2638 | return `<!--${event.text}-->` |
| 2639 | } |
no test coverage detected