private static IEnumerable <T> BlockDescendantsInternal <T>(ContainerBlock block) where T : MarkdownObject { Debug.Assert(typeof(T).IsSubclassOf(typeof(Block))); Stack <Block> stack = new Stack <Block>(); int childrenCount = block.Count; while (childrenCount-- > 0) { stack.Push(block[childrenCount]); } while (stack.Count > 0) { var subBlock = stack.Pop(); if (subBlock is T subBlockT) { yield return(subBlockT); } if (subBlock is ContainerBlock subBlockContainer) { childrenCount = subBlockContainer.Count; while (childrenCount-- > 0) { stack.Push(subBlockContainer[childrenCount]); } } } }
/// <summary> /// Iterates over the descendant elements for the specified markdown <see cref="Block" /> element and filters by the type {T}. /// </summary> /// <typeparam name="T">Type to use for filtering the descendants</typeparam> /// <param name="block">The markdown object.</param> /// <returns> /// An iteration over the descendant elements /// </returns> public static IEnumerable <T> Descendants <T>(this ContainerBlock block) where T : Block { // Fast-path an empty container to avoid allocating a Stack if (block.Count == 0) { yield break; } Stack <Block> stack = new Stack <Block>(); int childrenCount = block.Count; while (childrenCount-- > 0) { stack.Push(block[childrenCount]); } while (stack.Count > 0) { var subBlock = stack.Pop(); if (subBlock is T subBlockT) { yield return(subBlockT); } if (subBlock is ContainerBlock subBlockContainer) { childrenCount = subBlockContainer.Count; while (childrenCount-- > 0) { stack.Push(subBlockContainer[childrenCount]); } } } }
/// <summary> /// Iterates over the descendant elements for the specified markdown <see cref="Block" /> element and filters by the type <typeparamref name="T"/>. /// </summary> /// <typeparam name="T">Type to use for filtering the descendants</typeparam> /// <param name="block">The markdown object.</param> /// <returns> /// An iteration over the descendant elements /// </returns> public static IEnumerable <T> Descendants <T>(this ContainerBlock block) where T : Block { if (block != null && block.Count > 0) { return(BlockDescendantsInternal <T>(block)); } else { return(ArrayHelper <T> .Empty); } }
private static IEnumerable <T> InlineDescendantsInternal <T>(ContainerBlock block) where T : MarkdownObject { Debug.Assert(typeof(T).IsSubclassOf(typeof(Inline))); foreach (MarkdownObject descendant in block.Descendants()) { if (descendant is T descendantT) { yield return(descendantT); } } }
/// <summary> /// Iterates over the descendant elements for the specified markdown <see cref="Block" /> element and filters by the type {T}. /// </summary> /// <typeparam name="T">Type to use for filtering the descendants</typeparam> /// <param name="block">The markdown object.</param> /// <returns> /// An iteration over the descendant elements /// </returns> public static IEnumerable <T> Descendants <T>(this ContainerBlock block) where T : Block { foreach (var subBlock in block) { var subBlockT = subBlock as T; if (subBlockT != null) { yield return(subBlockT); } var subBlockContainer = subBlock as ContainerBlock; if (subBlockContainer != null) { foreach (var sub in subBlockContainer.Descendants <T>()) { yield return(sub); } } } }
internal Enumerator(ContainerBlock block) { this.block = block; index = 0; current = null; }
/// <summary> /// Iterates over the descendant elements for the specified markdown <see cref="Block" /> element and filters by the type <typeparamref name="T"/>. /// </summary> /// <typeparam name="T">Type to use for filtering the descendants</typeparam> /// <param name="block">The markdown object.</param> /// <returns> /// An iteration over the descendant elements /// </returns> public static IEnumerable <T> Descendants <T>(this ContainerBlock block) where T : Block { if (block is { Count : > 0 })