(el: Element, text: string)
| 253 | |
| 254 | /** Replace the SDK text target without destroying multi-child element structure. */ |
| 255 | export function setOwnText(el: Element, text: string): void { |
| 256 | const singleChild = resolveSingleChildTextTarget(el); |
| 257 | if (singleChild) { |
| 258 | singleChild.textContent = text; |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | const doc = el.ownerDocument; |
| 263 | const children = Array.from(el.childNodes); |
| 264 | // Track original position of the first text node so we restore there, not at firstChild. |
| 265 | let firstTextIdx = -1; |
| 266 | for (let i = 0; i < children.length; i++) { |
| 267 | if (children[i]?.nodeType === 3) { |
| 268 | firstTextIdx = i; |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | for (const child of children) { |
| 273 | if (child.nodeType === 3) el.removeChild(child); |
| 274 | } |
| 275 | if (text) { |
| 276 | // No text nodes before firstTextIdx (it's the first one), so index is stable. |
| 277 | const current = Array.from(el.childNodes); |
| 278 | const ref = firstTextIdx >= 0 ? (current[firstTextIdx] ?? null) : null; |
| 279 | el.insertBefore(doc.createTextNode(text), ref); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // ─── CSS style helpers ──────────────────────────────────────────────────────── |
| 284 |
no test coverage detected