Saving HTML DOM to a stream
I am going to use AngleSharp for loading, processing and saving HTML documents. Those documents can be quite big (up to 10 MB). But the only way to save an edited HTML tree is to call IHtmlDocument.ToHtml (or IHtmlDocument.DocumentElement.OuterHtml which is equivalent) and receive the whole HTML document as a string. It worries me since it produces quite big objects in LOH, that are not even needed for saving content to a stream. In the source code I can see that ToHtml method recursively calls overloaded ToHtml methods on child elements. Wouldn't be good to have a method Save(Stream stream) similar to ToHtml method? That would save a lot of memory during serialization.
Yes, I think that would save quite a lot of memory - and yes, it would also beneficial in other scenarios.
But thinking outside of this proposal: Wouldn't your scenario benefit more from saving directly the underlying source? I mean what you get is a serialization of the DOM - and not the original source code (they may differ in some areas). If the TextSource would be public and accessible, would that help you?
Do you mean, saving not to Stream directly, but to TextSource in order to abstract from text encoding? If so, yes, that would be even better.
Not saving to TextSource but from it. TextSource already contains the raw buffer and therefore the original source. It would dump the content (a reference) without having to create many (intermediate) strings.
Yes, but the buffer inside TextSource is not updated when DOM is edited. If I open some HTML document, edit it and save TextSource content - original content will be saved. Then, if I create an empty HTML document using BrowsingContext.OpenNewAsync(), add some elements and save TextSource content - nothing will be saved. However I would like to save the edited content, not the original one.
Alright, I missed the edited point. Yes, the TextSource won't change, i.e., this won't be a solution for you. I'll include this feature (saving to Stream) in v0.9.6!
By the way: Welcome to GitHub! In GitHub "stars" are the currency to show interest in projects. If the project is starred it will be further developed. Otherwise, the maintainer might lose interest. If you want to support the development of AngleSharp then please "star it" (upper right corner).
That would be fantastic, thanks a lot. Then I can safely start using AngleSharp in my project already now.
This will be released with v0.9.6, see PR #294.