|
12 | 12 |
|
13 | 13 | using System; |
14 | 14 | using System.Collections.Generic; |
| 15 | +using System.Globalization; |
15 | 16 | using System.IO; |
16 | 17 | using System.Linq; |
| 18 | +using System.Text; |
17 | 19 | using System.Threading.Tasks; |
18 | 20 | using TheArtOfDev.HtmlRenderer.Core.Entities; |
19 | 21 | using TheArtOfDev.HtmlRenderer.Core.Network; |
@@ -142,37 +144,121 @@ private static string CorrectRelativeUrls(string stylesheet, Uri baseUri) |
142 | 144 | while (idx >= 0 && idx < stylesheet.Length) |
143 | 145 | { |
144 | 146 | idx = stylesheet.IndexOf("url(", idx, StringComparison.OrdinalIgnoreCase); |
145 | | - if (idx >= 0) |
| 147 | + if (idx < 0) |
| 148 | + break; |
| 149 | + |
| 150 | + if (TryReadUrl(stylesheet, idx + 4, out var urlStr, out var endIdx) && |
| 151 | + Uri.TryCreate(urlStr, UriKind.Relative, out var url)) |
| 152 | + { |
| 153 | + // Written back quoted, unquoted it couldn't hold the ' ( ) an absolute URI may contain |
| 154 | + var replacement = "\"" + new Uri(baseUri, url).AbsoluteUri + "\""; |
| 155 | + stylesheet = stylesheet.Remove(idx + 4, endIdx - idx - 4).Insert(idx + 4, replacement); |
| 156 | + idx += 4 + replacement.Length + 1; |
| 157 | + } |
| 158 | + else |
146 | 159 | { |
147 | | - int endIdx = stylesheet.IndexOf(')', idx); |
148 | | - if (endIdx > idx + 4) |
149 | | - { |
150 | | - var offset1 = 4 + (stylesheet[idx + 4] == '\'' ? 1 : 0); |
151 | | - var offset2 = (stylesheet[endIdx - 1] == '\'' ? 1 : 0); |
152 | | - var urlStr = stylesheet.Substring(idx + offset1, endIdx - idx - offset1 - offset2); |
153 | | - Uri url; |
154 | | - if (Uri.TryCreate(urlStr, UriKind.Relative, out url)) |
155 | | - { |
156 | | - url = new Uri(baseUri, url); |
157 | | - stylesheet = stylesheet.Remove(idx + 4, endIdx - idx - 4); |
158 | | - stylesheet = stylesheet.Insert(idx + 4, url.AbsoluteUri); |
159 | | - idx += url.AbsoluteUri.Length + 4; |
160 | | - } |
161 | | - else |
162 | | - { |
163 | | - idx = endIdx + 1; |
164 | | - } |
165 | | - } |
166 | | - else |
167 | | - { |
168 | | - idx += 4; |
169 | | - } |
| 160 | + idx += 4; |
170 | 161 | } |
171 | 162 | } |
172 | 163 |
|
173 | 164 | return stylesheet; |
174 | 165 | } |
175 | 166 |
|
| 167 | + /// <summary> |
| 168 | + /// Read the URL of a url() without its quotes and with its escapes resolved. |
| 169 | + /// </summary> |
| 170 | + /// <param name="stylesheet">the stylesheet to read from</param> |
| 171 | + /// <param name="start">the index right after "url("</param> |
| 172 | + /// <param name="url">the URL</param> |
| 173 | + /// <param name="endIdx">the index of the closing ')'</param> |
| 174 | + /// <returns>false if it isn't a well-formed url()</returns> |
| 175 | + private static bool TryReadUrl(string stylesheet, int start, out string url, out int endIdx) |
| 176 | + { |
| 177 | + // CSS 2.1 4.3.4: optional whitespace and a pair of matching ' or " quotes around the URL |
| 178 | + url = null; |
| 179 | + endIdx = -1; |
| 180 | + |
| 181 | + int i = SkipWhitespace(stylesheet, start); |
| 182 | + char quote = i < stylesheet.Length && (stylesheet[i] == '"' || stylesheet[i] == '\'') ? stylesheet[i++] : '\0'; |
| 183 | + |
| 184 | + var sb = new StringBuilder(); |
| 185 | + while (true) |
| 186 | + { |
| 187 | + if (i >= stylesheet.Length) |
| 188 | + return false; |
| 189 | + |
| 190 | + char c = stylesheet[i]; |
| 191 | + if (quote != '\0' ? c == quote : c == ')' || IsWhitespace(c)) |
| 192 | + break; |
| 193 | + if (quote != '\0' ? c == '\n' || c == '\r' || c == '\f' : c == '"' || c == '\'' || c == '(') |
| 194 | + return false; |
| 195 | + |
| 196 | + if (c == '\\') |
| 197 | + { |
| 198 | + i = Unescape(stylesheet, i + 1, sb); |
| 199 | + } |
| 200 | + else |
| 201 | + { |
| 202 | + sb.Append(c); |
| 203 | + i++; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + if (quote != '\0') |
| 208 | + i++; |
| 209 | + |
| 210 | + i = SkipWhitespace(stylesheet, i); |
| 211 | + if (i >= stylesheet.Length || stylesheet[i] != ')' || sb.Length == 0) |
| 212 | + return false; |
| 213 | + |
| 214 | + url = sb.ToString(); |
| 215 | + endIdx = i; |
| 216 | + return true; |
| 217 | + } |
| 218 | + |
| 219 | + /// <summary> |
| 220 | + /// Append what the escape after a backslash stands for (CSS 2.1 4.1.3). |
| 221 | + /// </summary> |
| 222 | + /// <param name="stylesheet">the stylesheet to read from</param> |
| 223 | + /// <param name="i">the index right after the backslash</param> |
| 224 | + /// <param name="sb">the builder to append to</param> |
| 225 | + /// <returns>the index after the escape</returns> |
| 226 | + private static int Unescape(string stylesheet, int i, StringBuilder sb) |
| 227 | + { |
| 228 | + if (i >= stylesheet.Length) |
| 229 | + return i; |
| 230 | + |
| 231 | + int hexEnd = i; |
| 232 | + while (hexEnd < stylesheet.Length && hexEnd - i < 6 && Uri.IsHexDigit(stylesheet[hexEnd])) |
| 233 | + hexEnd++; |
| 234 | + |
| 235 | + if (hexEnd == i) |
| 236 | + { |
| 237 | + // A backslash before a newline in a string continues the line |
| 238 | + if (stylesheet[i] == '\r' && i + 1 < stylesheet.Length && stylesheet[i + 1] == '\n') |
| 239 | + return i + 2; |
| 240 | + if (stylesheet[i] != '\n' && stylesheet[i] != '\r' && stylesheet[i] != '\f') |
| 241 | + sb.Append(stylesheet[i]); |
| 242 | + return i + 1; |
| 243 | + } |
| 244 | + |
| 245 | + int codePoint = int.Parse(stylesheet.Substring(i, hexEnd - i), NumberStyles.HexNumber, CultureInfo.InvariantCulture); |
| 246 | + sb.Append(codePoint == 0 || codePoint > 0x10FFFF || (codePoint >= 0xD800 && codePoint <= 0xDFFF) ? "�" : char.ConvertFromUtf32(codePoint)); |
| 247 | + |
| 248 | + if (hexEnd + 1 < stylesheet.Length && stylesheet[hexEnd] == '\r' && stylesheet[hexEnd + 1] == '\n') |
| 249 | + return hexEnd + 2; |
| 250 | + return hexEnd < stylesheet.Length && IsWhitespace(stylesheet[hexEnd]) ? hexEnd + 1 : hexEnd; |
| 251 | + } |
| 252 | + |
| 253 | + private static int SkipWhitespace(string stylesheet, int i) |
| 254 | + { |
| 255 | + while (i < stylesheet.Length && IsWhitespace(stylesheet[i])) |
| 256 | + i++; |
| 257 | + return i; |
| 258 | + } |
| 259 | + |
| 260 | + private static bool IsWhitespace(char c) => c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'; |
| 261 | + |
176 | 262 | #endregion |
177 | 263 | } |
178 | 264 | } |
0 commit comments