fix(js): coerce non-string primitives in before/after/replaceWith

before(), after() and replaceWith() only converted string arguments to Text
nodes; other primitives (numbers, booleans, null) were passed straight to
insert_before with no _nid, corrupting the call. Route them through the shared
_convertNodes helper (the same path append/prepend use), so every non-node
argument becomes document.createTextNode(String(n)).

Fixes #498
This commit is contained in:
Andrey Mnatsakanov committed 2026-07-26 13:30:07 +02:00
1 parent 8078cc3070
commit 499a5ef983
2 files changed
+26 -12

No files matched your search

+6 -12
View File
@@ -4122,13 +4122,13 @@ if (typeof Response === 'undefined') {
}
if (!Element.prototype.replaceWith) {
// _convertNodes turns any non-node argument (numbers, booleans, null, …) into
// a Text node via String(n), matching the spec and append()/prepend(); the
// old `typeof n === 'string'` check corrupted insert_before for other types.
Element.prototype.replaceWith = function(...nodes) {
const parent = this.parentNode;
if (!parent) return;
for (const n of nodes) {
if (typeof n === 'string') parent.insertBefore(document.createTextNode(n), this);
else parent.insertBefore(n, this);
}
for (const n of _convertNodes(nodes)) parent.insertBefore(n, this);
parent.removeChild(this);
};
_markNative(Element.prototype.replaceWith);
@@ -4137,10 +4137,7 @@ if (!Element.prototype.before) {
Element.prototype.before = function(...nodes) {
const parent = this.parentNode;
if (!parent) return;
for (const n of nodes) {
if (typeof n === 'string') parent.insertBefore(document.createTextNode(n), this);
else parent.insertBefore(n, this);
}
for (const n of _convertNodes(nodes)) parent.insertBefore(n, this);
};
_markNative(Element.prototype.before);
}
@@ -4149,10 +4146,7 @@ if (!Element.prototype.after) {
const parent = this.parentNode;
if (!parent) return;
const ref = this.nextSibling;
for (const n of nodes) {
if (typeof n === 'string') parent.insertBefore(document.createTextNode(n), ref);
else parent.insertBefore(n, ref);
}
for (const n of _convertNodes(nodes)) parent.insertBefore(n, ref);
};
_markNative(Element.prototype.after);
}