BabylonJS/Babylon.js · error

text() without open element

Error message

text() without open element

What it means

XmlBuilder.text() writes character data inside an element, so an element context must be open. When _peekContext() returns undefined (no open element — e.g. before the root element or after everything was closed), the builder throws this error rather than emitting orphan text outside the document tree.

Source

Thrown at packages/dev/serializers/src/3MF/core/xml/xml.builder.ts:206

        let qns = n;
        if (ns) {
            const p = this._lookupPrefix(ns) ?? ns;
            qns = `${p}:${n}`;
        }
        this._pushContext(qns, ++this._d);
        this._w.write(XmlSyntax.OpenTag, qns);
        return this;
    }

    /**
     *
     * @param txt
     * @returns
     */
    public text(txt: string): IXmlBuilder {
        const ctx = this._peekContext();
        if (!ctx) {
            throw new Error("text() without open element");
        }
        this._closeOpenTagIfNeeded(ctx);
        ctx.lastToken = TokenType.Text;
        this._w.write(this._escText(txt));
        return this;
    }

    /**
     *
     * @returns
     */
    public end(): IXmlBuilder {
        const ctx = this._popContext();
        if (ctx) {
            this._d--;
            if (!ctx.closed) {
                this._w.write(XmlSyntax.Slash, XmlSyntax.CloseTag);
            } else {

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Open an element before calling text(); ensure the call order is el(...) then text(...).
  2. Check that the element containing the text wasn't closed earlier by a stray at()/close call.
  3. For text outside any element, wrap it in a container element or skip it (XML has no root-level text).
  4. Trace the builder sequence for premature closes that emptied the element stack.

Example fix

// before
b.text("hello"); // throws
// after
b.el("root").text("hello");
Defensive patterns

Strategy: try-catch

Try / catch

try {
    b.text(txt);
} catch (e) {
    if (e instanceof Error && e.message === 'text() without open element') {
        b.el('root').text(txt); // recover by opening a container element
    } else throw e;
}

Prevention

When it happens

Trigger: Calling text() before any el()/element() call, after the root element was closed, or after an unbalanced at()el() sequence emptied the context stack.

Common situations: Writing top-level text outside a root element; continuing to write after a closeAll/root close; error-handling paths that resume writing after the tree was finalized; misordered serialize pipeline writing text before the opening tag.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/0998a843773bcf87. Report an issue: GitHub.