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

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

Open
5 tasks done
martin-honnen opened this issue Apr 30, 2021 · 0 comments
Labels
Milestone

Comments

@martin-honnen
Copy link

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]

@FlorianRappl FlorianRappl added this to the v1.0 milestone Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants