|
| 1 | +using System.Text.RegularExpressions; |
| 2 | +using AngleSharp.Dom; |
| 3 | +using Bunit.Web.AngleSharp; |
| 4 | + |
| 5 | +namespace Bunit; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Extension methods for querying <see cref="IRenderedComponent{TComponent}" /> by text content |
| 9 | +/// </summary> |
| 10 | +public static partial class TextQueryExtensions |
| 11 | +{ |
| 12 | + private static readonly HashSet<string> IgnoredNodeNames = new(StringComparer.OrdinalIgnoreCase) { "SCRIPT", "STYLE" }; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Returns the first element whose text content matches the given text. |
| 16 | + /// </summary> |
| 17 | + /// <param name="renderedComponent">The rendered fragment to search.</param> |
| 18 | + /// <param name="searchText">The text content to search for.</param> |
| 19 | + /// <param name="configureOptions">Method used to override the default behavior of FindByText.</param> |
| 20 | + /// <returns>The first element matching the specified text.</returns> |
| 21 | + /// <exception cref="TextNotFoundException">Thrown when no element matching the provided text is found.</exception> |
| 22 | + public static IElement FindByText(this IRenderedComponent<IComponent> renderedComponent, string searchText, Action<ByTextOptions>? configureOptions = null) |
| 23 | + { |
| 24 | + ArgumentNullException.ThrowIfNull(renderedComponent); |
| 25 | + ArgumentNullException.ThrowIfNull(searchText); |
| 26 | + |
| 27 | + var options = ByTextOptions.Default; |
| 28 | + if (configureOptions is not null) |
| 29 | + { |
| 30 | + options = options with { }; |
| 31 | + configureOptions.Invoke(options); |
| 32 | + } |
| 33 | + |
| 34 | + return FindByTextInternal(renderedComponent, searchText, options) ?? throw new TextNotFoundException(searchText); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Returns all elements whose text content matches the given text. |
| 39 | + /// </summary> |
| 40 | + /// <param name="renderedComponent">The rendered fragment to search.</param> |
| 41 | + /// <param name="searchText">The text content to search for.</param> |
| 42 | + /// <param name="configureOptions">Method used to override the default behavior of FindAllByText.</param> |
| 43 | + /// <returns>A read-only collection of elements matching the text. Returns an empty collection if no matches are found.</returns> |
| 44 | + public static IReadOnlyList<IElement> FindAllByText(this IRenderedComponent<IComponent> renderedComponent, string searchText, Action<ByTextOptions>? configureOptions = null) |
| 45 | + { |
| 46 | + ArgumentNullException.ThrowIfNull(renderedComponent); |
| 47 | + ArgumentNullException.ThrowIfNull(searchText); |
| 48 | + |
| 49 | + var options = ByTextOptions.Default; |
| 50 | + if (configureOptions is not null) |
| 51 | + { |
| 52 | + options = options with { }; |
| 53 | + configureOptions.Invoke(options); |
| 54 | + } |
| 55 | + |
| 56 | + return FindAllByTextInternal(renderedComponent, searchText, options); |
| 57 | + } |
| 58 | + |
| 59 | + internal static IElement? FindByTextInternal(this IRenderedComponent<IComponent> renderedComponent, string searchText, ByTextOptions options) |
| 60 | + { |
| 61 | + var elements = renderedComponent.Nodes.TryQuerySelectorAll(options.Selector); |
| 62 | + var normalizedSearchText = NormalizeWhitespace(searchText); |
| 63 | + |
| 64 | + foreach (var element in elements) |
| 65 | + { |
| 66 | + if (IgnoredNodeNames.Contains(element.NodeName)) |
| 67 | + continue; |
| 68 | + |
| 69 | + var normalizedTextContent = NormalizeWhitespace(element.TextContent); |
| 70 | + |
| 71 | + if (normalizedTextContent.Equals(normalizedSearchText, options.ComparisonType)) |
| 72 | + return element.WrapUsing(new ByTextElementFactory(renderedComponent, searchText, options)); |
| 73 | + } |
| 74 | + |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + internal static IReadOnlyList<IElement> FindAllByTextInternal(this IRenderedComponent<IComponent> renderedComponent, string searchText, ByTextOptions options) |
| 79 | + { |
| 80 | + var elements = renderedComponent.Nodes.TryQuerySelectorAll(options.Selector); |
| 81 | + var normalizedSearchText = NormalizeWhitespace(searchText); |
| 82 | + var results = new List<IElement>(); |
| 83 | + var seen = new HashSet<IElement>(); |
| 84 | + |
| 85 | + foreach (var element in elements) |
| 86 | + { |
| 87 | + if (IgnoredNodeNames.Contains(element.NodeName)) |
| 88 | + continue; |
| 89 | + |
| 90 | + var normalizedTextContent = NormalizeWhitespace(element.TextContent); |
| 91 | + |
| 92 | + if (!normalizedTextContent.Equals(normalizedSearchText, options.ComparisonType)) |
| 93 | + continue; |
| 94 | + |
| 95 | + var underlyingElement = element.Unwrap(); |
| 96 | + if (seen.Add(underlyingElement)) |
| 97 | + { |
| 98 | + results.Add(element.WrapUsing(new ByTextElementFactory(renderedComponent, searchText, options))); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return results; |
| 103 | + } |
| 104 | + |
| 105 | + internal static string NormalizeWhitespace(string text) |
| 106 | + { |
| 107 | + var trimmed = text.Trim(); |
| 108 | + return CollapseWhitespaceRegex().Replace(trimmed, " "); |
| 109 | + } |
| 110 | + |
| 111 | + [GeneratedRegex(@"\s+")] |
| 112 | + private static partial Regex CollapseWhitespaceRegex(); |
| 113 | +} |
0 commit comments