|
| 1 | +using System.Collections.Generic; |
| 2 | + |
| 3 | +namespace TheArtOfDev.HtmlRenderer.Core.CssEngine |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Shared grammar for the CSS <c>background-position</c> value (the 1/2/3/4-token forms, |
| 7 | + /// including the edge-relative offset syntax, e.g. "right 20px bottom 10px"). Used by both |
| 8 | + /// <see cref="Converters.PointConverter"/> (CSS-OM grammar validation/serialization) and |
| 9 | + /// PeachPDF.Html.Core.Utils.BackgroundLayerResolver (render-time pixel-offset arithmetic) so |
| 10 | + /// there is one parser for this grammar, not two independent re-implementations - mirroring how |
| 11 | + /// CalcParser is shared between CalcValueConverter and CssValueParser rather than re-parsed twice. |
| 12 | + /// </summary> |
| 13 | + internal static class BackgroundPositionGrammar |
| 14 | + { |
| 15 | + internal enum AxisKeyword { None, Left, Right, Top, Bottom, Center } |
| 16 | + |
| 17 | + internal readonly struct Component |
| 18 | + { |
| 19 | + public AxisKeyword Keyword { get; } |
| 20 | + |
| 21 | + /// <summary>The length/percentage/calc() token for this component, or null when there is |
| 22 | + /// none (a keyword-only component, or the implicit "center" filled in for a 1-token value).</summary> |
| 23 | + public Token Offset { get; } |
| 24 | + |
| 25 | + public Component(AxisKeyword keyword, Token offset) |
| 26 | + { |
| 27 | + Keyword = keyword; |
| 28 | + Offset = offset; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + internal sealed class ParsedPosition |
| 33 | + { |
| 34 | + public Component X { get; } |
| 35 | + public Component Y { get; } |
| 36 | + |
| 37 | + /// <summary>The 1-2 authored components in original left-to-right order, needed to |
| 38 | + /// re-serialize in the exact order/form the author wrote (CSS round-trips verbatim).</summary> |
| 39 | + public IReadOnlyList<Component> AuthoredOrder { get; } |
| 40 | + |
| 41 | + public ParsedPosition(Component x, Component y, IReadOnlyList<Component> authoredOrder) |
| 42 | + { |
| 43 | + X = x; |
| 44 | + Y = y; |
| 45 | + AuthoredOrder = authoredOrder; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + internal static ParsedPosition TryParse(IReadOnlyList<Token> tokens) |
| 50 | + { |
| 51 | + switch (tokens.Count) |
| 52 | + { |
| 53 | + case 1: |
| 54 | + return ParseSingle(tokens[0]); |
| 55 | + case 2: |
| 56 | + return ParseDouble(tokens[0], tokens[1]); |
| 57 | + case 3: |
| 58 | + return ParseTriple(tokens[0], tokens[1], tokens[2]); |
| 59 | + case 4: |
| 60 | + return ParseQuadruple(tokens[0], tokens[1], tokens[2], tokens[3]); |
| 61 | + default: |
| 62 | + return null; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static ParsedPosition ParseSingle(Token token) |
| 67 | + { |
| 68 | + var keyword = KeywordOf(token); |
| 69 | + if (keyword == AxisKeyword.None) |
| 70 | + { |
| 71 | + if (!IsLengthOrPercent(token)) return null; |
| 72 | + |
| 73 | + var bare = new Component(AxisKeyword.None, token); |
| 74 | + var center = new Component(AxisKeyword.Center, null); |
| 75 | + return new ParsedPosition(bare, center, new[] { bare }); |
| 76 | + } |
| 77 | + |
| 78 | + if (IsVerticalOnly(keyword)) |
| 79 | + { |
| 80 | + var y = new Component(keyword, null); |
| 81 | + var x = new Component(AxisKeyword.Center, null); |
| 82 | + return new ParsedPosition(x, y, new[] { y }); |
| 83 | + } |
| 84 | + |
| 85 | + var xComp = new Component(keyword, null); |
| 86 | + var yComp = new Component(AxisKeyword.Center, null); |
| 87 | + return new ParsedPosition(xComp, yComp, new[] { xComp }); |
| 88 | + } |
| 89 | + |
| 90 | + private static ParsedPosition ParseDouble(Token t0, Token t1) |
| 91 | + { |
| 92 | + // "h then v" (horizontal-ish first): left/right/center/bare-length, then top/bottom/center/bare-length. |
| 93 | + if (IsHSlotValid(t0) && IsVSlotValid(t1)) |
| 94 | + { |
| 95 | + var x = ToComponent(t0); |
| 96 | + var y = ToComponent(t1); |
| 97 | + return new ParsedPosition(x, y, new[] { x, y }); |
| 98 | + } |
| 99 | + |
| 100 | + // "v then h" (vertical-ish first, e.g. "bottom center", "top left"). |
| 101 | + if (IsVSlotValid(t0) && IsHSlotValid(t1)) |
| 102 | + { |
| 103 | + var y = ToComponent(t0); |
| 104 | + var x = ToComponent(t1); |
| 105 | + return new ParsedPosition(x, y, new[] { y, x }); |
| 106 | + } |
| 107 | + |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + private static ParsedPosition ParseTriple(Token t0, Token t1, Token t2) |
| 112 | + { |
| 113 | + // hi, vi, offset (offset applies to the vertical group): e.g. "right bottom 20px". |
| 114 | + if (IsEdgeKeywordSlot(t0, horizontal: true) && IsEdgeKeywordSlot(t1, horizontal: false) && IsLengthOrPercent(t2)) |
| 115 | + { |
| 116 | + var x = new Component(KeywordOf(t0), null); |
| 117 | + var y = new Component(KeywordOf(t1), t2); |
| 118 | + return new ParsedPosition(x, y, new[] { x, y }); |
| 119 | + } |
| 120 | + |
| 121 | + // hi, offset, vi (offset applies to the horizontal group): e.g. "right 20px bottom". |
| 122 | + if (IsEdgeKeywordSlot(t0, horizontal: true) && IsLengthOrPercent(t1) && IsEdgeKeywordSlot(t2, horizontal: false)) |
| 123 | + { |
| 124 | + var x = new Component(KeywordOf(t0), t1); |
| 125 | + var y = new Component(KeywordOf(t2), null); |
| 126 | + return new ParsedPosition(x, y, new[] { x, y }); |
| 127 | + } |
| 128 | + |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + private static ParsedPosition ParseQuadruple(Token t0, Token t1, Token t2, Token t3) |
| 133 | + { |
| 134 | + // hi, offset, vi, offset: e.g. "right 20px bottom 10px". |
| 135 | + if (IsEdgeKeywordSlot(t0, horizontal: true) && IsLengthOrPercent(t1) && |
| 136 | + IsEdgeKeywordSlot(t2, horizontal: false) && IsLengthOrPercent(t3)) |
| 137 | + { |
| 138 | + var x = new Component(KeywordOf(t0), t1); |
| 139 | + var y = new Component(KeywordOf(t2), t3); |
| 140 | + return new ParsedPosition(x, y, new[] { x, y }); |
| 141 | + } |
| 142 | + |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + private static Component ToComponent(Token token) => |
| 147 | + new Component(KeywordOf(token), KeywordOf(token) == AxisKeyword.None ? token : null); |
| 148 | + |
| 149 | + private static AxisKeyword KeywordOf(Token token) |
| 150 | + { |
| 151 | + if (token.Type != TokenType.Ident) return AxisKeyword.None; |
| 152 | + if (token.Data.Isi(Keywords.Left)) return AxisKeyword.Left; |
| 153 | + if (token.Data.Isi(Keywords.Right)) return AxisKeyword.Right; |
| 154 | + if (token.Data.Isi(Keywords.Top)) return AxisKeyword.Top; |
| 155 | + if (token.Data.Isi(Keywords.Bottom)) return AxisKeyword.Bottom; |
| 156 | + if (token.Data.Isi(Keywords.Center)) return AxisKeyword.Center; |
| 157 | + return AxisKeyword.None; |
| 158 | + } |
| 159 | + |
| 160 | + private static bool IsHorizontalOnly(AxisKeyword k) => k == AxisKeyword.Left || k == AxisKeyword.Right; |
| 161 | + private static bool IsVerticalOnly(AxisKeyword k) => k == AxisKeyword.Top || k == AxisKeyword.Bottom; |
| 162 | + |
| 163 | + private static bool IsLengthOrPercent(Token token) => |
| 164 | + Converters.LengthOrPercentConverter.Convert(new[] { token }) != null; |
| 165 | + |
| 166 | + /// <summary>Valid for the 2-value "h" slot: left/right/center keywords, or a bare length/percent - not top/bottom.</summary> |
| 167 | + private static bool IsHSlotValid(Token token) |
| 168 | + { |
| 169 | + var k = KeywordOf(token); |
| 170 | + return k != AxisKeyword.None ? !IsVerticalOnly(k) : IsLengthOrPercent(token); |
| 171 | + } |
| 172 | + |
| 173 | + /// <summary>Valid for the 2-value "v" slot: top/bottom/center keywords, or a bare length/percent - not left/right.</summary> |
| 174 | + private static bool IsVSlotValid(Token token) |
| 175 | + { |
| 176 | + var k = KeywordOf(token); |
| 177 | + return k != AxisKeyword.None ? !IsHorizontalOnly(k) : IsLengthOrPercent(token); |
| 178 | + } |
| 179 | + |
| 180 | + /// <summary>Valid for the 3/4-value "hi"/"vi" edge-keyword slots: keyword only, no bare-length fallback.</summary> |
| 181 | + private static bool IsEdgeKeywordSlot(Token token, bool horizontal) |
| 182 | + { |
| 183 | + var k = KeywordOf(token); |
| 184 | + if (k == AxisKeyword.Center) return true; |
| 185 | + if (k == AxisKeyword.None) return false; |
| 186 | + return horizontal ? IsHorizontalOnly(k) : IsVerticalOnly(k); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments