Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html kernel evals script tags #2605

Merged
merged 1 commit into from
Jan 10, 2023
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
24 changes: 24 additions & 0 deletions src/Microsoft.DotNet.Interactive.Browser.Tests/HtmlKernelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ public async Task JavaScript_kernel_can_see_DOM_changes_made_by_HTML_kernel()
.ContainSingle(v => v.Value.Contains("<div>hey there!</div>"));
}

[FactSkipLinux("Requires Playwright installed")]
public async Task html_kernel_evaluates_script_tags()
{
var connector = new PlaywrightKernelConnector(!Debugger.IsAttached);

using var htmlKernel = await connector.CreateKernelAsync("html", BrowserKernelLanguage.Html);
using var javascriptKernel = await connector.CreateKernelAsync("javascript", BrowserKernelLanguage.JavaScript);
await htmlKernel.SendAsync(new SubmitCode("<div><script>myValue = 123;</script></div>"));

var result = await javascriptKernel.SendAsync(new SubmitCode("return myValue;"));

var events = result.KernelEvents.ToSubscribedList();

events.Should().NotContainErrors();

events
.Should()
.ContainSingle<ReturnValueProduced>()
.Which
.FormattedValues
.Should()
.ContainSingle(v => v.Value.Contains("123"));
}

private async Task<Kernel> CreateHtmlProxyKernelAsync()
{
var connector = new PlaywrightKernelConnector(!Debugger.IsAttached);
Expand Down
6 changes: 5 additions & 1 deletion src/microsoft-dotnet-interactive/src/htmlKernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ export function htmlDomFragmentInserter(htmlFragment: string, configuration?: Ht
const updateContainerContent = configuration?.updateContainerContent ?? ((container, htmlFragment) => container.innerHTML = htmlFragment);
const createMutationObserver = configuration?.createMutationObserver ?? (callback => new MutationObserver(callback));
let jsEvaluator: (js: string) => Promise<void>;

if (configuration?.jsEvaluator) {
jsEvaluator = configuration.jsEvaluator;
} else {
const AsyncFunction = eval(`Object.getPrototypeOf(async function(){}).constructor`);
jsEvaluator = (code) => AsyncFunction("console", code);
jsEvaluator = (code) => {
const evaluator = AsyncFunction(code);
return (<() => Promise<void>>evaluator)();
};
}
let container = getOrCreateContainer();

Expand Down
21 changes: 12 additions & 9 deletions src/microsoft-dotnet-interactive/tests/htmlKernel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,26 @@ describe("htmlKernel", () => {
events.push(e);
});

await kernel.send({ commandType: contracts.SubmitCodeType, command: <contracts.SubmitCode>{ code: '<div id="0"><script>foo = 122;</script></div>' } });
await kernel.send({ commandType: contracts.SubmitCodeType, command: <contracts.SubmitCode>{ code: '<div id="0"><script type="module">foo = 122;</script><div id="1"><script type="module">bar = 211;</script></div></div>' } });

const lastDisplayedValueProducedEvent = events.filter(e => e.eventType === contracts.DisplayedValueProducedType).at(-1)?.event as contracts.DisplayedValueProduced;
expect(lastDisplayedValueProducedEvent).to.not.be.undefined;

expect(lastDisplayedValueProducedEvent).to.deep.equal({
displayedValueProduced:
expect(lastDisplayedValueProducedEvent).to.deep.equal(
{
formattedValues:
[{
mimeType: 'text/html',
value: '<div id="0"><script>foo = 122;</script></div>'
}]
displayedValueProduced:
{
formattedValues:
[{
mimeType: 'text/html',
value: '<div id="0"><script type="module">foo = 122;</script><div id="1"><script type="module">bar = 211;</script></div></div>'
}]
}
}
});
);

expect(dom.window.globalThis["foo"]).to.be.equal(122);
expect(dom.window.globalThis["bar"]).to.be.equal(211);
});

it("can reuse container", async () => {
Expand Down