This project has moved and is read-only. For the latest updates, please go here.
3

Closed

InnerHtml property not calculated properly

description

A DocumentNode is a tree of HtmlNode's. Each node has an InnerHtml (and
InnerText, but I'll talk only of InnerHtml) property. This property is
cached and is controlled by a private field in the HtmlNode class called
"_innerchanged". (There's also a private field called "_outerchanged".)
 
Suppose you're, say, three levels deep in the tree. You replace this node via the ReplaceChild API. It sets the
_innerchanged field to true, so that the next time you refer to that node's
InnerHtml property it will recalculate the inner HTHML (since _innerchanged
is true). Which is fine.

But if you want the HTML for the entire document, and ask for
doc.DocumentNode.InnerHtml, that node (doc.DocumentNode) doesn't have
the _innerchanged field set. So what you get is the cached (i.e. old)
version of the HTML, which doesn't reflect the changes made to the child
node several levels down.
 
The solution is a source mod to HtmlNode.cs, method ReplaceChild, which
propogates the _innerchanged (and _outerchanged) values of the replacement
node upwards to its parent (and its parent (and its parent, etc)). The code
to do this as follows...
 
private void PropogateInnerOuterChanged(bool InnerChanged, bool OuterChanged) {
    HtmlNode node = this;
    while (node.ParentNode != null && node.ParentNode is HtmlNode) {
        node = node.ParentNode; 
        node._innerchanged = InnerChanged;
        node._outerchanged = OuterChanged; 
}
 
This routine should be called at the very end of ReplaceChild, just before
the "return" statement -- PropogateInnerOuterChange(_innerchanged,
_outerchanged);
 
Note: There seems to be a related bug in HtmlNode.ReplaceChild(int index,
HtmlNode node), which doesn't even set the _innerchanged/_outerchanged
fields, much less propogate the values upwards. But since we don't use that
API, I haven't changed it.
Closed Sep 24, 2012 at 10:01 AM by simonm

comments

wrote Sep 24, 2012 at 10:01 AM

Resolved with changeset 99803.

wrote Feb 22, 2013 at 2:47 AM

wrote May 16, 2013 at 1:32 PM

wrote Apr 30, 2017 at 12:06 PM