| | | 1 | | namespace Allyaria.Theming.Contracts; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Serves as the abstract base class for all style value types within the Allyaria theming system. Implements |
| | | 5 | | /// <see cref="IStyleValue" /> and provides common validation and normalization behavior for CSS-compatible string value |
| | | 6 | | /// </summary> |
| | | 7 | | public abstract record StyleValueBase : IStyleValue |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Initializes a new instance of the <see cref="StyleValueBase" /> record with the specified string value. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="value"> |
| | | 13 | | /// The CSS-compatible string value to assign. The value is automatically validated and trimmed during initializatio |
| | | 14 | | /// </param> |
| | | 15 | | /// <exception cref="AryArgumentException"> |
| | | 16 | | /// Thrown when the provided <paramref name="value" /> contains invalid or control |
| | | 17 | | /// characters. |
| | | 18 | | /// </exception> |
| | 39962 | 19 | | protected StyleValueBase(string value) => Value = ValidateInput(value: value); |
| | | 20 | | |
| | | 21 | | /// <summary>Gets the validated and normalized string value associated with this style instance.</summary> |
| | 38516 | 22 | | public string Value { get; } |
| | | 23 | | |
| | | 24 | | /// <summary>Attempts to validate and normalize a string input for use as a CSS-compatible value.</summary> |
| | | 25 | | /// <param name="value">The input string to validate.</param> |
| | | 26 | | /// <param name="result"> |
| | | 27 | | /// When this method returns, contains the trimmed version of the input string if validation succeeds, or an empty s |
| | | 28 | | /// if the input was <see langword="null" />. |
| | | 29 | | /// </param> |
| | | 30 | | /// <returns> |
| | | 31 | | /// <see langword="true" /> if the input contains no invalid control characters and is suitable for use as a style v |
| | | 32 | | /// otherwise, <see langword="false" />. |
| | | 33 | | /// </returns> |
| | | 34 | | private static bool TryValidateInput(string? value, out string result) |
| | | 35 | | { |
| | 19981 | 36 | | result = value?.Trim() ?? string.Empty; |
| | | 37 | | |
| | 194263 | 38 | | return !result.Any(predicate: static c => char.IsControl(c: c)); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary>Validates and normalizes a string input, ensuring it is safe and CSS-compatible.</summary> |
| | | 42 | | /// <param name="value">The input string to validate and normalize.</param> |
| | | 43 | | /// <returns>The trimmed version of the input string if validation succeeds.</returns> |
| | | 44 | | /// <exception cref="AryArgumentException"> |
| | | 45 | | /// Thrown when the provided <paramref name="value" /> contains control characters or is otherwise deemed invalid fo |
| | | 46 | | /// as a CSS value. |
| | | 47 | | /// </exception> |
| | | 48 | | private static string ValidateInput(string? value) |
| | | 49 | | { |
| | 19981 | 50 | | AryGuard.Check( |
| | 19981 | 51 | | condition: TryValidateInput(value: value, result: out var result), |
| | 19981 | 52 | | argName: nameof(value), |
| | 19981 | 53 | | message: $"Invalid value: {value}" |
| | 19981 | 54 | | ); |
| | | 55 | | |
| | 19979 | 56 | | return result; |
| | | 57 | | } |
| | | 58 | | } |