diff --git a/cjs/html/text-element.js b/cjs/html/text-element.js
index d55b5d3c..2dfe704c 100644
--- a/cjs/html/text-element.js
+++ b/cjs/html/text-element.js
@@ -10,7 +10,11 @@ class TextElement extends HTMLElement {
toString() {
const outerHTML = toString.call(this.cloneNode());
- return outerHTML.replace('><', () => `>${this.textContent}<`);
+ /* c8 ignore next */
+ if (outerHTML.endsWith(' />')) return outerHTML; // self-closed, as in XML documents
+ // the text goes before the end tag, as attribute values can contain ">" too
+ const end = outerHTML.lastIndexOf('>') + 1;
+ return outerHTML.slice(0, end) + this.textContent + outerHTML.slice(end);
}
}
exports.TextElement = TextElement
diff --git a/esm/html/text-element.js b/esm/html/text-element.js
index ae231795..1b64caa0 100644
--- a/esm/html/text-element.js
+++ b/esm/html/text-element.js
@@ -9,6 +9,10 @@ export class TextElement extends HTMLElement {
toString() {
const outerHTML = toString.call(this.cloneNode());
- return outerHTML.replace('><', () => `>${this.textContent}<`);
+ /* c8 ignore next */
+ if (outerHTML.endsWith(' />')) return outerHTML; // self-closed, as in XML documents
+ // the text goes before the end tag, as attribute values can contain ">" too
+ const end = outerHTML.lastIndexOf('>') + 1;
+ return outerHTML.slice(0, end) + this.textContent + outerHTML.slice(end);
}
}
diff --git a/worker.js b/worker.js
index 2671c7f5..84d093f2 100644
--- a/worker.js
+++ b/worker.js
@@ -8482,7 +8482,11 @@ class TextElement extends HTMLElement {
toString() {
const outerHTML = toString.call(this.cloneNode());
- return outerHTML.replace('><', () => `>${this.textContent}<`);
+ /* c8 ignore next */
+ if (outerHTML.endsWith(' />')) return outerHTML; // self-closed, as in XML documents
+ // the text goes before the end tag, as attribute values can contain ">" too
+ const end = outerHTML.lastIndexOf('>') + 1;
+ return outerHTML.slice(0, end) + this.textContent + outerHTML.slice(end);
}
}