diff --git a/CHANGELOG.md b/CHANGELOG.md
index bb42ac8..97458da 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,10 @@
+# 1.2.1
+
+Released on Saturday, November 7 2026.
+
+- Fixed a script inserted by script (e.g. `head.appendChild(script)` with its text) running after the script inserting it instead of before the insertion returns (MediaWiki ran the modules cached in `localStorage` twice: "module already implemented")
+- Fixed `Function.prototype.toString()` giving `function name() { [native code] }` for the functions of scripts instead of their source (MediaWiki stores its modules as the source of their functions); `JsScriptingOptions.RetainFunctionSourceText` turns keeping the source off
+
# 1.2.0
Released on Friday, November 6 2026.
diff --git a/docs/general/01-Basics.md b/docs/general/01-Basics.md
index 3e1d826..9993162 100644
--- a/docs/general/01-Basics.md
+++ b/docs/general/01-Basics.md
@@ -77,6 +77,22 @@ var configuration = Configuration.Default
});
```
+### Keep the source of functions
+
+Like in browsers, `Function.prototype.toString()` gives the source of a function written in a
+script, which some pages rely on (e.g. MediaWiki stores the code of its modules this way). This
+keeps the source of the scripts in memory as long as their functions live; setting
+`JsScriptingOptions.RetainFunctionSourceText` to `false` turns it off, and `toString()` then gives
+`function name() { [native code] }`.
+
+```cs
+var configuration = Configuration.Default
+ .WithJs(new JsScriptingOptions
+ {
+ RetainFunctionSourceText = false,
+ });
+```
+
## Execute JavaScript
HTML `"));
+
+ StringAssert.Contains("[native code]", document.ExecuteScript("String(f)")?.ToString());
+ }
+ }
+}
diff --git a/src/AngleSharp.Js.Tests/ScriptEvalTests.cs b/src/AngleSharp.Js.Tests/ScriptEvalTests.cs
index e8d645b..07c0fa8 100644
--- a/src/AngleSharp.Js.Tests/ScriptEvalTests.cs
+++ b/src/AngleSharp.Js.Tests/ScriptEvalTests.cs
@@ -323,6 +323,47 @@ public async Task RunMainScriptFromHtml5Test()
Assert.AreNotEqual("undefined", result);
}
+ [Test]
+ public async Task InsertedScriptRunsBeforeAppendChildReturns()
+ {
+ // MediaWiki runs the modules cached in localStorage like this (domEval), and asks for
+ // the ones still missing right after
+ var result = await EvaluateComplexScriptAsync(
+ "var order = []; var s = document.createElement('script'); s.text = 'order.push(1)'; document.head.appendChild(s); order.push(2); s.remove(); document.body.appendChild(s); order.push(3);",
+ SetResult("order.join()"));
+ Assert.AreEqual("1,2,3", result);
+ }
+
+ [Test]
+ public async Task ScriptInsertedByAListenerRunsBeforeAppendChildReturns()
+ {
+ var result = await EvaluateComplexScriptAsync(
+ "var order = []; document.addEventListener('x', function () { var s = document.createElement('script'); s.text = 'order.push(1)'; document.head.appendChild(s); order.push(2); }); document.dispatchEvent(new Event('x'));",
+ SetResult("order.join()"));
+ Assert.AreEqual("1,2", result);
+ }
+
+ [Test]
+ public async Task InsertedScriptRunsOnlyOnce()
+ {
+ // AngleSharp evaluates the script later as well, which is skipped
+ var cfg = Configuration.Default.WithJs().WithEventLoop();
+ var html = "";
+ var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html));
+ await Task.Delay(300);
+
+ Assert.AreEqual(1.0, document.ExecuteScript("count"));
+ }
+
+ [Test]
+ public async Task ErrorOfAnInsertedScriptDoesNotStopTheOneInsertingIt()
+ {
+ var result = await EvaluateComplexScriptAsync(
+ "var order = []; var s = document.createElement('script'); s.text = 'order.push(1); throw new Error(\"inner\")'; document.head.appendChild(s); order.push(2);",
+ SetResult("order.join()"));
+ Assert.AreEqual("1,2", result);
+ }
+
[Test]
public async Task QueryUserAgentShouldMatchAgent()
{
diff --git a/src/AngleSharp.Js/Cache/ScriptCache.cs b/src/AngleSharp.Js/Cache/ScriptCache.cs
index 286b24a..c025734 100644
--- a/src/AngleSharp.Js/Cache/ScriptCache.cs
+++ b/src/AngleSharp.Js/Cache/ScriptCache.cs
@@ -10,7 +10,8 @@ namespace AngleSharp.Js.Cache
///
/// Caches the parsed and analyzed form of scripts. A prepared script is
/// documented by Jint as reusable and thread-safe, so the same one can serve
- /// every document that runs the same source with the same regex timeout - a
+ /// every document that runs the same source with the same parsing options (the regex
+ /// timeout, and if the source of functions is kept) - a
/// page loading a library ends up parsing it once instead of once per document.
///
static class ScriptCache
@@ -28,9 +29,9 @@ static class ScriptCache
/// source could not be prepared and should be handed to the engine as text,
/// so that the engine reports the syntax error itself.
///
- public static Prepared