ParentNodeExtensions
Useful methods for parent node objects.
using AngleSharp.DOM;
using System.Diagnostics;
namespace AngleSharp.Extensions
{
[DebuggerStepThrough]
internal static class ParentNodeExtensions
{
public static INode MutationMacro(this INode[] nodes)
{
if (nodes.Length > 1) {
DocumentFragment documentFragment = new DocumentFragment();
for (int i = 0; i < nodes.Length; i++) {
documentFragment.AppendChild(nodes[i]);
}
return documentFragment;
}
return nodes[0];
}
public static void PrependNodes(this INode parent, params INode[] nodes)
{
INode node = nodes.MutationMacro();
parent.PreInsert(node, parent.FirstChild);
}
public static void AppendNodes(this INode parent, params INode[] nodes)
{
INode node = nodes.MutationMacro();
parent.PreInsert(node, null);
}
public static void InsertBefore(this INode child, params INode[] nodes)
{
INode parent = child.Parent;
if (parent != null && nodes.Length > 0) {
INode node = nodes.MutationMacro();
parent.PreInsert(node, child);
}
}
public static void InsertAfter(this INode child, params INode[] nodes)
{
INode parent = child.Parent;
if (parent != null && nodes.Length > 0) {
INode node = nodes.MutationMacro();
parent.PreInsert(node, child.NextSibling);
}
}
public static void ReplaceWith(this INode child, params INode[] nodes)
{
INode parent = child.Parent;
if (parent != null && nodes.Length > 0) {
INode newChild = nodes.MutationMacro();
parent.ReplaceChild(newChild, child);
}
}
public static void RemoveFromParent(this INode child)
{
child.Parent?.PreRemove(child);
}
}
}