diff --git a/src/AngleSharp.Js.Tests/DomTests.cs b/src/AngleSharp.Js.Tests/DomTests.cs index 88b41b7..e14fe45 100644 --- a/src/AngleSharp.Js.Tests/DomTests.cs +++ b/src/AngleSharp.Js.Tests/DomTests.cs @@ -68,5 +68,19 @@ public async Task NumericIndexerOfTokenListYieldsTheToken() var result = await "new DOMParser().parseFromString(`
`, 'text/html').body.firstChild.classList[1]".EvalScriptAsync(); Assert.AreEqual("b", result); } + + [Test] + public async Task ConsoleIsTheSameObjectOnEveryAccess() + { + var result = await "window.console === window.console".EvalScriptAsync(); + Assert.AreEqual("True", result); + } + + [Test] + public async Task ConsoleKeepsPropertiesAssignedToIt() + { + var result = await "(function () { window.console.marker = 'kept'; return window.console.marker; })()".EvalScriptAsync(); + Assert.AreEqual("kept", result); + } } } diff --git a/src/AngleSharp.Js/Dom/WindowExtensions.cs b/src/AngleSharp.Js/Dom/WindowExtensions.cs index 27835b9..679ae9d 100644 --- a/src/AngleSharp.Js/Dom/WindowExtensions.cs +++ b/src/AngleSharp.Js/Dom/WindowExtensions.cs @@ -7,6 +7,7 @@ namespace AngleSharp.Js.Dom using AngleSharp.Html.Dom; using AngleSharp.Js.Attributes; using System; + using System.Runtime.CompilerServices; /// /// Defines a set of extensions for the window object. @@ -14,6 +15,9 @@ namespace AngleSharp.Js.Dom [DomExposed("Window")] public static class WindowExtensions { + private static readonly ConditionalWeakTable Consoles = + new ConditionalWeakTable(); + /// /// Posts a message. /// @@ -34,7 +38,9 @@ public static void PostMessage(this IWindow window, String message, String targe public static IWindow Top(this IWindow window) => window.Document.Context?.Creator?.DefaultView; /// - /// Gets the console instance. + /// Gets the console instance. The same instance is returned for the same + /// window, so that `window.console === window.console` holds and anything + /// script puts on the console is still there on the next access. /// /// /// @@ -42,7 +48,7 @@ public static void PostMessage(this IWindow window, String message, String targe [DomAccessor(Accessors.Getter)] public static Console Console(this IWindow window) { - return new Console(window); + return Consoles.GetValue(window, w => new Console(w)); } ///