AngleSharp / AngleSharp.Js

:angel: Extends AngleSharp with a .NET-based JavaScript engine.
https://anglesharp.github.io
MIT License
105 stars 23 forks source link

AngleSharp doesn't seem to work with jQuery #43

Closed DmitriiShamrikov closed 5 years ago

DmitriiShamrikov commented 7 years ago

That's my test page:

<html>
<head>
    <title>test page</title>
    <script type='text/javascript' src='https://code.jquery.com/jquery-2.2.4.js'></script>
</head>
<body></body>
</html>

And that's my code:

var config = Configuration.Default
    .WithDefaultLoader( _ => _.IsResourceLoadingEnabled = true, new[] { new HttpClientRequester() } )
    .WithJavaScript();
var browser = BrowsingContext.New( config );
var doc = browser.OpenAsync( url ).Result;
var jsp = config.Services.OfType<JavaScriptProvider>().First();
jsp.Engine.EvaluateScript( doc, "jQuery" );

It throws an exception "Jint.Runtime.JavaScriptException : jQuery is not defined". I tried different jQuery versions (1.12.4, 2.2.4, 3.2.1, minified and uncompressed) - nothing worked. AngleSharp version: 0.9.9 AngleSharp.Scripting version: 0.5.1 jint version: 2.10.4

Help please

FlorianRappl commented 7 years ago

Are the tests running for you? Question is of course related to the fact that the tests use jQuery. What I imagine is that you need WithCss() depending on the version of jQuery (some make some tests against it and refuse to work without a proper CSSOM).

DmitriiShamrikov commented 7 years ago

Yes, JqueryTests are running. But it uses jquery 1.11.2 and 2.1.4. If I run your tests with 1.12.4 and 2.2.4, latest for this moment, it behaves unsatable - only random 0-3 of 7 tests pass.

Also i tried to change jquery version in my example to one that you use and it became working (adding WithCss()).

So i think there is something in latest jquery versions that AngleSharp can't handle.

FlorianRappl commented 7 years ago

Yes it could be. jQuery uses a lot of DOM APIs and if some are not exposed from AngleSharp.Scripting (or even missing, e.g., layout specific ones, which are not part of the AngleSharp.Core package) then jQuery will unfortunately fail to load.

I am unsure if this should be closed. On the one hand this is a known thing and the AngleSharp.Scripting project is still far away from production ready or claiming that it can run any script. On the other hand running jQuery (any version) should be / was the goal for this POC.

FlorianRappl commented 5 years ago

There are multiple issues. Some of them are fixed and all noted versions are working sometimes. The reason for the hiccup is somewhere in the event loop (most likely related to setTimeout after some initial investigation) and have to be fixed directly in the core.

AngleSharp v0.13 will come with a new event loop that already satisfies the requirements for modern JS stacks.

Landed in devel.