mirror of
https://github.com/xoofx/markdig.git
synced 2026-02-04 05:44:50 +00:00
Rewrite Descendants(ContainerInline) iteratively
This commit is contained in:
@@ -94,25 +94,36 @@ namespace Markdig.Syntax.Inlines
|
||||
/// <returns>An enumeration of T</returns>
|
||||
public IEnumerable<T> FindDescendants<T>() where T : Inline
|
||||
{
|
||||
var child = FirstChild;
|
||||
while (child != null)
|
||||
{
|
||||
var next = child.NextSibling;
|
||||
// Fast-path an empty container to avoid allocating a Stack
|
||||
if (LastChild == null) yield break;
|
||||
|
||||
if (child is T)
|
||||
{
|
||||
yield return (T)child;
|
||||
}
|
||||
Stack<Inline> stack = new Stack<Inline>();
|
||||
|
||||
if (child is ContainerInline)
|
||||
{
|
||||
foreach (var subChild in ((ContainerInline) child).FindDescendants<T>())
|
||||
{
|
||||
yield return subChild;
|
||||
}
|
||||
}
|
||||
|
||||
child = next;
|
||||
var child = LastChild;
|
||||
while (child != null)
|
||||
{
|
||||
stack.Push(child);
|
||||
child = child.PreviousSibling;
|
||||
}
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
child = stack.Pop();
|
||||
|
||||
if (child is T childT)
|
||||
{
|
||||
yield return childT;
|
||||
}
|
||||
|
||||
if (child is ContainerInline containerInline)
|
||||
{
|
||||
child = containerInline.LastChild;
|
||||
while (child != null)
|
||||
{
|
||||
stack.Push(child);
|
||||
child = child.PreviousSibling;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user