| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a CSS <c>white-space</c> value within the Allyaria theming system. Provides a strongly typed wrapper for |
| | | 5 | | /// controlling how whitespace inside an element is handled, including collapsing, preservation, and line-breaking |
| | | 6 | | /// behavior. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed record StyleWhiteSpace : StyleValueBase |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Initializes a new instance of the <see cref="StyleWhiteSpace" /> record using the specified <see cref="Kind" /> |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="kind">The white-space handling behavior to represent.</param> |
| | | 14 | | public StyleWhiteSpace(Kind kind) |
| | 54 | 15 | | : base(value: kind.GetDescription()) { } |
| | | 16 | | |
| | | 17 | | /// <summary>Defines the supported CSS <c>white-space</c> property values.</summary> |
| | | 18 | | public enum Kind |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Preserves spaces, tabs, and line breaks; text may wrap to a new line only at line breaks or explicit wrap po |
| | | 22 | | /// Trailing spaces are preserved. |
| | | 23 | | /// </summary> |
| | | 24 | | [Description(description: "break-spaces")] |
| | | 25 | | BreakSpaces, |
| | | 26 | | |
| | | 27 | | /// <summary>Collapses consecutive spaces and tabs into a single space; line breaks may occur where allowed.</su |
| | | 28 | | [Description(description: "collapse")] |
| | | 29 | | Collapse, |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Collapses consecutive whitespace characters and allows text wrapping at normal break points (default behavio |
| | | 33 | | /// </summary> |
| | | 34 | | [Description(description: "normal")] |
| | | 35 | | Normal, |
| | | 36 | | |
| | | 37 | | /// <summary>Collapses whitespace but prevents text from wrapping onto multiple lines.</summary> |
| | | 38 | | [Description(description: "nowrap")] |
| | | 39 | | Nowrap, |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Preserves spaces and line breaks exactly as they appear in the source. Text does not wrap automatically. |
| | | 43 | | /// </summary> |
| | | 44 | | [Description(description: "pre")] |
| | | 45 | | Pre, |
| | | 46 | | |
| | | 47 | | /// <summary>Collapses spaces as in normal, but preserves line breaks.</summary> |
| | | 48 | | [Description(description: "pre-line")] |
| | | 49 | | PreLine, |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Preserves spaces and tabs but prevents automatic line wrapping. Equivalent to legacy <c>white-space: pre now |
| | | 53 | | /// combination. |
| | | 54 | | /// </summary> |
| | | 55 | | [Description(description: "preserve nowrap")] |
| | | 56 | | PreserveNowrap, |
| | | 57 | | |
| | | 58 | | /// <summary>Preserves whitespace but allows text to wrap when needed.</summary> |
| | | 59 | | [Description(description: "pre-wrap")] |
| | | 60 | | PreWrap |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Parses a string representation of a CSS <c>white-space</c> value into a <see cref="StyleWhiteSpace" /> instance. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="value">The string representation of the white-space value.</param> |
| | | 67 | | /// <returns>A new <see cref="StyleWhiteSpace" /> instance representing the parsed value.</returns> |
| | | 68 | | /// <exception cref="AryArgumentException"> |
| | | 69 | | /// Thrown when the provided <paramref name="value" /> does not correspond to a valid <see cref="Kind" />. |
| | | 70 | | /// </exception> |
| | | 71 | | public static StyleWhiteSpace Parse(string? value) |
| | 15 | 72 | | => value.TryParseEnum<Kind>(result: out var kind) |
| | 15 | 73 | | ? new StyleWhiteSpace(kind: kind) |
| | 15 | 74 | | : throw new AryArgumentException(message: $"Invalid style: {value}", argName: nameof(value)); |
| | | 75 | | |
| | | 76 | | /// <summary>Attempts to parse a string into a <see cref="StyleWhiteSpace" /> instance.</summary> |
| | | 77 | | /// <param name="value">The string representation of the white-space value to parse.</param> |
| | | 78 | | /// <param name="result"> |
| | | 79 | | /// When this method returns, contains the parsed <see cref="StyleWhiteSpace" /> instance or <see langword="null" /> |
| | | 80 | | /// parsing failed. |
| | | 81 | | /// </param> |
| | | 82 | | /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns> |
| | | 83 | | public static bool TryParse(string? value, out StyleWhiteSpace? result) |
| | | 84 | | { |
| | | 85 | | try |
| | | 86 | | { |
| | 3 | 87 | | result = Parse(value: value); |
| | | 88 | | |
| | 1 | 89 | | return true; |
| | | 90 | | } |
| | 2 | 91 | | catch |
| | | 92 | | { |
| | 2 | 93 | | result = null; |
| | | 94 | | |
| | 2 | 95 | | return false; |
| | | 96 | | } |
| | 3 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary>Implicitly converts a string into a <see cref="StyleWhiteSpace" /> instance.</summary> |
| | | 100 | | /// <param name="value">The string representation of the white-space value.</param> |
| | | 101 | | /// <returns>A <see cref="StyleWhiteSpace" /> instance representing the provided value.</returns> |
| | | 102 | | /// <exception cref="AryArgumentException"> |
| | | 103 | | /// Thrown when the provided string cannot be parsed into a valid |
| | | 104 | | /// <see cref="Kind" />. |
| | | 105 | | /// </exception> |
| | 2 | 106 | | public static implicit operator StyleWhiteSpace(string? value) => Parse(value: value); |
| | | 107 | | |
| | | 108 | | /// <summary>Implicitly converts a <see cref="StyleWhiteSpace" /> instance to its string representation.</summary> |
| | | 109 | | /// <param name="value">The <see cref="StyleWhiteSpace" /> instance to convert.</param> |
| | | 110 | | /// <returns> |
| | | 111 | | /// The underlying CSS <c>white-space</c> string value, or an empty string if <paramref name="value" /> is |
| | | 112 | | /// <see langword="null" />. |
| | | 113 | | /// </returns> |
| | 2 | 114 | | public static implicit operator string(StyleWhiteSpace? value) => (value?.Value).OrDefaultIfEmpty(); |
| | | 115 | | } |