Skip to content

JavaScript exception when trying to call JavaScript function from C# code with Invoke, passing in element node created in C# #76

Open
@martin-honnen

Description

@martin-honnen

Bug Report

Prerequisites

  • Can you reproduce the problem in a MWE?
  • Are you running the latest version of AngleSharp? 0.15 of AngleSharp, 0.14 of AngleSharp.Js
  • Did you check the FAQs to see if that helps you?
  • Are you reporting to the correct repository? (there are multiple AngleSharp libraries, e.g., AngleSharp.Css for CSS support) As I get a JavaScript exception, I file this on AngleSharp.Js
  • Did you perform a search in the issues?

For more information, see the CONTRIBUTING guide.

Description

[Description of the bug]

I get a JavaScript exception in a sample trying to Invoke a JavaScript function from C# code, passing in an element node created on the C# side of the code.

Steps to Reproduce

  1. [First Step]
    Run the code
using System;
using System.Threading.Tasks;
using AngleSharp;
using AngleSharp.Scripting;
using Jint.Native;

namespace JavaScriptExceptionMWE
{
    class Program
    {
        static async Task Main()
        {
            var jsService = new JsScriptingService();

            var config = Configuration.Default.With(jsService);

            var context = BrowsingContext.New(config);

            var source = "<!DOCTYPE html><html><head><title>Test</title><script>function f1(el) { document.body.appendChild(el); }</script></head><body><h1>Some example source</h1><p>This is a paragraph element.</p></body></html>";

            var document = await context.OpenAsync(req => req.Content(source));

            Console.WriteLine("Serializing the (original) document:");
            Console.WriteLine(document.DocumentElement.OuterHtml);

            var f1 = jsService.GetOrCreateJint(document).GetValue("f1");

            var sectionElement = document.CreateElement("section");
            sectionElement.TextContent = "This is a section.";

            var jsSectionElement = JsValue.FromObject(jsService.GetOrCreateJint(document), sectionElement);

            f1.Invoke(jsSectionElement);

            Console.WriteLine("Serializing the document again:");
            Console.WriteLine(document.DocumentElement.OuterHtml);
        }
    }
}

Expected behavior: On the f1.Invoke(jsSectionElement); call, I get an exception

Jint.Runtime.JavaScriptException
  HResult=0x80131500
  Message=
  Source=jint
  StackTrace:
   at Jint.Native.Function.ScriptFunctionInstance.Call(JsValue thisArg, JsValue[] arguments)
   at Jint.Native.JsValue.Invoke(JsValue thisObj, JsValue[] arguments)
   at Jint.Native.JsValue.Invoke(JsValue[] arguments)
   at JavaScriptExceptionMWE.Program.<Main>d__0.MoveNext() in C:\Users\marti\source\repos\JavaScriptExceptionMWE\Program.cs:line 33

Actual behavior: I would expect the code to run without throwing any error and the C# created element to be appended as a child of the HTML's body element so that the final Console.WriteLine(document.DocumentElement.OuterHtml); would show <section>This is a section.</section> as the last child of the body element.

Environment details: [OS, .NET Runtime, ...] Windows 10 20H2, .NET 5 (Core) VS 2019 console project

Possible Solution

[Optionally, share your idea to fix the issue]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions