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
28 changes: 28 additions & 0 deletions src/AngleSharp.Js.Tests/EcmaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ public async Task ModuleScriptWithScopedImportMapShouldRunCorrectScript()
Assert.IsNotNull(document2.GetElementById("test1"));
}

[Test]
public async Task ModuleScriptWithQuoteInImportMapShouldRun()
{
var config =
Configuration.Default
.WithJs()
.With(new MockHttpClientRequester(new Dictionary<string, string>()
{
{ "/example-module.js", "export function test() { document.getElementById('test').remove(); }" }
}))
.WithDefaultLoader(new LoaderOptions() { IsResourceLoadingEnabled = true });

var context = BrowsingContext.New(config);
var html = "<!doctype html><div id=test>Test</div><script type=importmap>{ \"imports\": { \"o'clock\": \"/example-module.js\" } }</script><script type=module>import { test } from \"o'clock\"; test();</script>";
var document = await context.OpenAsync(r => r.Content(html));
Assert.IsNull(document.GetElementById("test"));
}

[Test]
public async Task ImportMapContentIsNotEvaluatedAsScript()
{
var config = Configuration.Default.WithJs();
var context = BrowsingContext.New(config);
var html = "<!doctype html><div id=test>Test</div><script type=importmap>{ \"imports\": {} }'); document.getElementById('test').remove(); ('</script>";
var document = await context.OpenAsync(r => r.Content(html));
Assert.IsNotNull(document.GetElementById("test"));
}

[Test]
public async Task ModuleScriptWithAbsoluteUrlImportMapShouldRun()
{
Expand Down
6 changes: 5 additions & 1 deletion src/AngleSharp.Js/EngineInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace AngleSharp.Js
using AngleSharp.Text;
using Jint;
using Jint.Native;
using Jint.Native.Json;
using Jint.Native.Object;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -117,7 +118,10 @@ public JsValue RunScript(String source, String type, String sourceUrl, JsValue c

private JsValue LoadImportMap(String source)
{
var importMap = _engine.Evaluate($"JSON.parse('{source}')").AsObject();
// The source is page content, so it must be handed to a JSON parser rather
// than pasted into a script: a single quote already breaks the parse, and
// anything after a closing quote would run as script.
var importMap = new JsonParser(_engine).Parse(source).AsObject();

if (importMap.TryGetValue("scopes", out var scopes))
{
Expand Down
Loading