|
| 1 | +// "Therefore those skilled at the unorthodox |
| 2 | +// are infinite as heaven and earth, |
| 3 | +// inexhaustible as the great rivers. |
| 4 | +// When they come to an end, |
| 5 | +// they begin again, |
| 6 | +// like the days and months; |
| 7 | +// they die and are reborn, |
| 8 | +// like the four seasons." |
| 9 | +// |
| 10 | +// - Sun Tsu, |
| 11 | +// "The Art of War" |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | +using System.Globalization; |
| 16 | +using TheArtOfDev.HtmlRenderer.Core.Utils; |
| 17 | + |
| 18 | +namespace TheArtOfDev.HtmlRenderer.Core.Dom |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Resolves CSS 2.1 §12.4 named counters (<c>counter-reset</c>/<c>counter-increment</c>) into a |
| 22 | + /// per-box snapshot of counter values, consulted by <c>content: counter()</c> |
| 23 | + /// (<see cref="CssContentEngine"/>).<br/> |
| 24 | + /// Deliberately a subset of the full spec algorithm: counters thread forward through document order |
| 25 | + /// (a box inherits its parent's counters for its first child, and each child's resulting counters |
| 26 | + /// feed the next sibling) which covers ordinary sequential/nested counter usage, but |
| 27 | + /// <c>reversed(<name>)</c> and <c>counter-set</c> are not implemented, and a nested |
| 28 | + /// <c>counter-reset</c> for a name already in scope always starts a fresh inner value rather than |
| 29 | + /// the spec's more precise "new scope only among this element's own following siblings" rule - |
| 30 | + /// in practice indistinguishable from the full rule for the common case of one counter-reset per |
| 31 | + /// counter name in a document, which is what this port's test coverage exercises. |
| 32 | + /// </summary> |
| 33 | + internal static class CssCounterEngine |
| 34 | + { |
| 35 | + /// <summary> |
| 36 | + /// Walks <paramref name="box"/> and its descendants in document order, applying each box's own |
| 37 | + /// <c>counter-reset</c>/<c>counter-increment</c> (plus the implicit <c>list-item</c> counter a |
| 38 | + /// <c>display:list-item</c> box always increments) on top of <paramref name="inherited"/>, and |
| 39 | + /// storing the result on <see cref="CssBoxProperties.Counters"/>. Must run once, before layout, |
| 40 | + /// after every box's CSS properties are resolved (<c>counter-reset</c>/<c>counter-increment</c> |
| 41 | + /// need their final cascaded values). |
| 42 | + /// </summary> |
| 43 | + /// <returns> |
| 44 | + /// The counters visible immediately after <paramref name="box"/> and its whole subtree - what a |
| 45 | + /// following sibling should inherit. |
| 46 | + /// </returns> |
| 47 | + public static Dictionary<string, int> ResolveCounters(CssBox box, Dictionary<string, int> inherited) |
| 48 | + { |
| 49 | + var counters = new Dictionary<string, int>(inherited); |
| 50 | + |
| 51 | + ApplyCounterReset(box, counters); |
| 52 | + ApplyCounterIncrement(box, counters); |
| 53 | + |
| 54 | + box.Counters = new Dictionary<string, int>(counters); |
| 55 | + |
| 56 | + foreach (var child in box.Boxes) |
| 57 | + { |
| 58 | + counters = ResolveCounters(child, counters); |
| 59 | + } |
| 60 | + |
| 61 | + return counters; |
| 62 | + } |
| 63 | + |
| 64 | + private static void ApplyCounterReset(CssBox box, Dictionary<string, int> counters) |
| 65 | + { |
| 66 | + if (string.IsNullOrEmpty(box.CounterReset) || box.CounterReset == CssConstants.None) return; |
| 67 | + |
| 68 | + ForEachNameValuePair(box.CounterReset, defaultValue: 0, (name, value) => counters[name] = value); |
| 69 | + } |
| 70 | + |
| 71 | + private static void ApplyCounterIncrement(CssBox box, Dictionary<string, int> counters) |
| 72 | + { |
| 73 | + if (box.Display == CssConstants.ListItem && !CounterNameAppears(box.CounterIncrement, CssConstants.ListItem)) |
| 74 | + { |
| 75 | + counters[CssConstants.ListItem] = GetOrDefault(counters, CssConstants.ListItem) + 1; |
| 76 | + } |
| 77 | + |
| 78 | + if (string.IsNullOrEmpty(box.CounterIncrement) || box.CounterIncrement == CssConstants.None) return; |
| 79 | + |
| 80 | + ForEachNameValuePair(box.CounterIncrement, defaultValue: 1, |
| 81 | + (name, value) => counters[name] = GetOrDefault(counters, name) + value); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// <see cref="Dictionary{TKey,TValue}.GetValueOrDefault(TKey)"/> stand-in: this project also |
| 86 | + /// targets netstandard2.0/net462, where that method doesn't exist. |
| 87 | + /// </summary> |
| 88 | + private static int GetOrDefault(Dictionary<string, int> counters, string name) |
| 89 | + { |
| 90 | + int value; |
| 91 | + return counters.TryGetValue(name, out value) ? value : 0; |
| 92 | + } |
| 93 | + |
| 94 | + private static bool CounterNameAppears(string counterPropertyValue, string counterName) |
| 95 | + { |
| 96 | + if (string.IsNullOrEmpty(counterPropertyValue) || counterPropertyValue == CssConstants.None) return false; |
| 97 | + |
| 98 | + var found = false; |
| 99 | + ForEachNameValuePair(counterPropertyValue, 0, (name, _) => found |= name == counterName); |
| 100 | + return found; |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Parses a <c>counter-reset</c>/<c>counter-increment</c> value - a space-separated sequence of |
| 105 | + /// <c><name> [<integer>]?</c> pairs - invoking <paramref name="apply"/> once per name, |
| 106 | + /// with <paramref name="defaultValue"/> when no integer follows a given name. |
| 107 | + /// </summary> |
| 108 | + private static void ForEachNameValuePair(string value, int defaultValue, Action<string, int> apply) |
| 109 | + { |
| 110 | + var parts = value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); |
| 111 | + |
| 112 | + for (var i = 0; i < parts.Length; i++) |
| 113 | + { |
| 114 | + var name = parts[i]; |
| 115 | + var next = i + 1 < parts.Length ? parts[i + 1] : null; |
| 116 | + |
| 117 | + if (next != null && int.TryParse(next, NumberStyles.Integer, CultureInfo.InvariantCulture, out var explicitValue)) |
| 118 | + { |
| 119 | + apply(name, explicitValue); |
| 120 | + i++; |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + apply(name, defaultValue); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Formats a resolved counter value as a string using the given CSS counter style (<c>decimal</c>, |
| 131 | + /// <c>decimal-leading-zero</c>, <c>lower-roman</c>, <c>upper-alpha</c>, etc.) - the single |
| 132 | + /// counter-style resolver <c>content: counter()</c> uses. Per |
| 133 | + /// <see href="https://www.w3.org/TR/css-counter-styles-3/">CSS Counter Styles Level 3 §2</see>, an |
| 134 | + /// unknown/invalid style, or a value an alphabetic/symbolic style cannot represent (0 or negative), |
| 135 | + /// falls back to <c>decimal</c> rather than rendering nothing. |
| 136 | + /// </summary> |
| 137 | + public static string FormatCounterValue(int number, string style) |
| 138 | + { |
| 139 | + if (string.IsNullOrEmpty(style) || style.Equals(CssConstants.Decimal, StringComparison.OrdinalIgnoreCase)) |
| 140 | + { |
| 141 | + return number.ToString(CultureInfo.InvariantCulture); |
| 142 | + } |
| 143 | + |
| 144 | + if (style.Equals(CssConstants.DecimalLeadingZero, StringComparison.OrdinalIgnoreCase)) |
| 145 | + { |
| 146 | + return number.ToString("00", CultureInfo.InvariantCulture); |
| 147 | + } |
| 148 | + |
| 149 | + if (IsAlphabeticCounterStyle(style)) |
| 150 | + { |
| 151 | + var formatted = CommonUtils.ConvertToAlphaNumber(number, style); |
| 152 | + |
| 153 | + // Alphabetic/symbolic styles have no representation for values outside their range (e.g. |
| 154 | + // 0 or negatives, for which ConvertToAlphaNumber yields the empty string) - such values |
| 155 | + // fall back to decimal too, per CSS Counter Styles Level 3 §2. |
| 156 | + return formatted.Length > 0 ? formatted : number.ToString(CultureInfo.InvariantCulture); |
| 157 | + } |
| 158 | + |
| 159 | + // Any other unknown/invalid style falls back to decimal. |
| 160 | + return number.ToString(CultureInfo.InvariantCulture); |
| 161 | + } |
| 162 | + |
| 163 | + private static bool IsAlphabeticCounterStyle(string style) |
| 164 | + { |
| 165 | + return style.Equals(CssConstants.LowerRoman, StringComparison.OrdinalIgnoreCase) |
| 166 | + || style.Equals(CssConstants.UpperRoman, StringComparison.OrdinalIgnoreCase) |
| 167 | + || style.Equals(CssConstants.LowerAlpha, StringComparison.OrdinalIgnoreCase) |
| 168 | + || style.Equals(CssConstants.UpperAlpha, StringComparison.OrdinalIgnoreCase) |
| 169 | + || style.Equals(CssConstants.LowerLatin, StringComparison.OrdinalIgnoreCase) |
| 170 | + || style.Equals(CssConstants.UpperLatin, StringComparison.OrdinalIgnoreCase) |
| 171 | + || style.Equals(CssConstants.LowerGreek, StringComparison.OrdinalIgnoreCase) |
| 172 | + || style.Equals(CssConstants.Armenian, StringComparison.OrdinalIgnoreCase) |
| 173 | + || style.Equals(CssConstants.Georgian, StringComparison.OrdinalIgnoreCase) |
| 174 | + || style.Equals(CssConstants.Hebrew, StringComparison.OrdinalIgnoreCase); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments