Mutation Observer throws when connecting to a document
See original GitHub issueBug Report
Prerequisites
- [y] Can you reproduce the problem in a MWE?
- [y] Are you running the latest version of AngleSharp?
- [y] Did you check the FAQs to see if that helps you?
- [y] Are you reporting to the correct repository? (there are multiple AngleSharp libraries, e.g.,
AngleSharp.Css
for CSS support) - [y] Did you perform a search in the issues?
For more information, see the CONTRIBUTING
guide.
Description
I’m trying to use a mutation observer to watch an entire document. Unfortunately when I try to do that I get a null reference exception.
Steps to Reproduce
var document = ... //from somewhere
var observer = new MutationObserver((mutations, observer) => {});
observer.Connect(document);
Expected behavior: no exception should be thrown
Actual behavior: a null reference exception was thrown when the connect method tries to access the document owner which is always null
Environment details: Windows .NET 5
Possible Solution
In this file: https://github.com/AngleSharp/AngleSharp/blob/devel/src/AngleSharp/Dom/MutationObserver.cs#L221 the following line in the issue
node.Owner.Mutations.Register(this);
the problem is that a document always returns null for it’s owner. I see 2 ways to fix this, the first is to change the document so returns itself as it’s owner. The other way would be to refactor this line to check if the node is a document and if it is then just use the Mutations Property directly. Something like this
if (node is Document d)
d.Mutations.Register(this);
else
node.Owner.Mutations.Register(this);
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Fix released in preview https://www.nuget.org/packages/AngleSharp/0.16.1-alpha-120.
I’m sorry, I think my example was incorrect, instead of this:
observer.Connect(document);
it should have been thisobserver.Connect(document, true);
so I wouldn’t get an error about any of those settings being false.I did a little testing (in Firefox and Chrome) and it seems perfectly happy to observe the document without any errors. If you want to test it out copy this into your browser console and modify something in the html.
Like you said a document is a node, in AngleSharp and the browser, there’s just a minor detail in AngleSharp that seems to prevent it from working currently. As for step 3 of the algorithm I don’t see what this has to do with a document not being accepted.