Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/AngleSharp.Js.Tests/DomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,19 @@ public async Task NumericIndexerOfTokenListYieldsTheToken()
var result = await "new DOMParser().parseFromString(`<div class='a b'></div>`, '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);
}
}
}
10 changes: 8 additions & 2 deletions src/AngleSharp.Js/Dom/WindowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ namespace AngleSharp.Js.Dom
using AngleSharp.Html.Dom;
using AngleSharp.Js.Attributes;
using System;
using System.Runtime.CompilerServices;

/// <summary>
/// Defines a set of extensions for the window object.
/// </summary>
[DomExposed("Window")]
public static class WindowExtensions
{
private static readonly ConditionalWeakTable<IWindow, Console> Consoles =
new ConditionalWeakTable<IWindow, Console>();

/// <summary>
/// Posts a message.
/// </summary>
Expand All @@ -34,15 +38,17 @@ public static void PostMessage(this IWindow window, String message, String targe
public static IWindow Top(this IWindow window) => window.Document.Context?.Creator?.DefaultView;

/// <summary>
/// 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.
/// </summary>
/// <param name="window"></param>
/// <returns></returns>
[DomName("console")]
[DomAccessor(Accessors.Getter)]
public static Console Console(this IWindow window)
{
return new Console(window);
return Consoles.GetValue(window, w => new Console(w));
}

/// <summary>
Expand Down
Loading