{"record":{"id":"0998a843773bcf87","repo":"BabylonJS/Babylon.js","slug":"text-without-open-element","errorCode":null,"errorMessage":"text() without open element","messagePattern":"text\\(\\) without open element","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/serializers/src/3MF/core/xml/xml.builder.ts","lineNumber":206,"sourceCode":"        let qns = n;\r\n        if (ns) {\r\n            const p = this._lookupPrefix(ns) ?? ns;\r\n            qns = `${p}:${n}`;\r\n        }\r\n        this._pushContext(qns, ++this._d);\r\n        this._w.write(XmlSyntax.OpenTag, qns);\r\n        return this;\r\n    }\r\n\r\n    /**\r\n     *\r\n     * @param txt\r\n     * @returns\r\n     */\r\n    public text(txt: string): IXmlBuilder {\r\n        const ctx = this._peekContext();\r\n        if (!ctx) {\r\n            throw new Error(\"text() without open element\");\r\n        }\r\n        this._closeOpenTagIfNeeded(ctx);\r\n        ctx.lastToken = TokenType.Text;\r\n        this._w.write(this._escText(txt));\r\n        return this;\r\n    }\r\n\r\n    /**\r\n     *\r\n     * @returns\r\n     */\r\n    public end(): IXmlBuilder {\r\n        const ctx = this._popContext();\r\n        if (ctx) {\r\n            this._d--;\r\n            if (!ctx.closed) {\r\n                this._w.write(XmlSyntax.Slash, XmlSyntax.CloseTag);\r\n            } else {\r","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/serializers/src/3MF/core/xml/xml.builder.ts#L188-L224","documentation":"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.","triggerScenarios":"Calling text() before any el()/element() call, after the root element was closed, or after an unbalanced at()el() sequence emptied the context stack.","commonSituations":"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.","solutions":["Open an element before calling text(); ensure the call order is el(...) then text(...).","Check that the element containing the text wasn't closed earlier by a stray at()/close call.","For text outside any element, wrap it in a container element or skip it (XML has no root-level text).","Trace the builder sequence for premature closes that emptied the element stack."],"exampleFix":"// before\nb.text(\"hello\"); // throws\n// after\nb.el(\"root\").text(\"hello\");","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    b.text(txt);\n} catch (e) {\n    if (e instanceof Error && e.message === 'text() without open element') {\n        b.el('root').text(txt); // recover by opening a container element\n    } else throw e;\n}","preventionTips":["Ensure a root element exists before writing text.","Call text() only between el() open and close.","Guard serialization entry points to always open the root first.","Avoid resuming a builder after the document was finalized."],"tags":["xml","api-misuse","builder","state"],"backgroundTag":"invalid-xml-structure","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}