Skip to content

Instantly share code, notes, and snippets.

@jglinsek
Created October 17, 2014 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jglinsek/dab7f950a3054cf97301 to your computer and use it in GitHub Desktop.
Save jglinsek/dab7f950a3054cf97301 to your computer and use it in GitHub Desktop.
AngleSharp - clone an element, append it somewhere - the cloned element has a parent but all of it's children do not
public void replace_the_parent_with_a_clone_and_the_children_of_the_cloned_parent_should_have_a_parent()
{
const string html = @"
<html>
<body>
<div class='parent'>
<div class='child'>
</div>
</div>
</body>
</html>
";
var doc = DocumentBuilder.Html(html);
var originalParent = doc.QuerySelector(".parent");
//clone the parent
var clonedParent = originalParent.Clone();
clonedParent.Parent.ShouldBe(null);
//remove the original parent
var grandparent = originalParent.Parent;
originalParent.Remove();
originalParent.Parent.ShouldBe(null);
grandparent.ShouldNotBe(null);
//replace the original parent with the cloned parent
grandparent.AppendChild(clonedParent);
//the clone itself has the correct parent
clonedParent.Parent.ShouldBe(grandparent);
//all the children (and grandchildren) of the cloned element have no parent?
var cloneElement = (IElement) clonedParent;
cloneElement.FirstChild.Parent.ShouldNotBe(null); //FAILS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment