|
1 | | -// "Therefore those skilled at the unorthodox |
| 1 | +// "Therefore those skilled at the unorthodox |
2 | 2 | // are infinite as heaven and earth, |
3 | 3 | // inexhaustible as the great rivers. |
4 | 4 | // When they come to an end, |
|
15 | 15 | using System.IO; |
16 | 16 | using System.Net; |
17 | 17 | using HtmlRenderer.Entities; |
| 18 | +using HtmlRenderer.Utils; |
18 | 19 |
|
19 | | -namespace HtmlRenderer.Utils |
| 20 | +namespace HtmlRenderer.Handlers |
20 | 21 | { |
21 | 22 | /// <summary> |
22 | | - /// Helper for loading a stylesheet data. |
| 23 | + /// Handler for loading a stylesheet data. |
23 | 24 | /// </summary> |
24 | | - internal static class StylesheetLoadHelper |
| 25 | + internal static class StylesheetLoadHandler |
25 | 26 | { |
26 | 27 | /// <summary> |
27 | | - /// |
| 28 | + /// Load stylesheet data from the given source.<br/> |
| 29 | + /// The source can be local file or web URI.<br/> |
| 30 | + /// First raise <see cref="HtmlStylesheetLoadEventArgs"/> event to allow the client to overwrite the stylesheet loading.<br/> |
| 31 | + /// If the stylesheet is downloaded from URI we will try to correct local URIs to absolute.<br/> |
28 | 32 | /// </summary> |
29 | 33 | /// <param name="htmlContainer">the container of the html to handle load stylesheet for</param> |
30 | 34 | /// <param name="src">the source of the element to load the stylesheet by</param> |
@@ -70,26 +74,13 @@ public static string LoadStylesheet(HtmlContainer htmlContainer, string src, Dic |
70 | 74 | private static string LoadStylesheet(HtmlContainer htmlContainer, string src) |
71 | 75 | { |
72 | 76 | var uri = CommonUtils.TryGetUri(src); |
73 | | - if (uri != null && uri.Scheme != "file") |
74 | | - { |
75 | | - return LoadStylesheetFromUri(uri); |
76 | | - } |
77 | | - else |
| 77 | + if( uri == null || uri.Scheme == "file" ) |
78 | 78 | { |
79 | 79 | return LoadStylesheetFromFile(htmlContainer, uri != null ? uri.AbsolutePath : src); |
80 | 80 | } |
81 | | - } |
82 | | - |
83 | | - /// <summary> |
84 | | - /// Load the stylesheet from uri by downloading the string. |
85 | | - /// </summary> |
86 | | - /// <param name="uri">the uri to download from</param> |
87 | | - /// <returns>the loaded stylesheet string</returns> |
88 | | - private static string LoadStylesheetFromUri(Uri uri) |
89 | | - { |
90 | | - using (var client = new WebClient()) |
| 81 | + else |
91 | 82 | { |
92 | | - return client.DownloadString(uri); |
| 83 | + return LoadStylesheetFromUri(htmlContainer, uri); |
93 | 84 | } |
94 | 85 | } |
95 | 86 |
|
@@ -123,7 +114,66 @@ private static string LoadStylesheetFromFile(HtmlContainer htmlContainer, string |
123 | 114 | return string.Empty; |
124 | 115 | } |
125 | 116 |
|
126 | | - #endregion |
| 117 | + /// <summary> |
| 118 | + /// Load the stylesheet from uri by downloading the string. |
| 119 | + /// </summary> |
| 120 | + /// <param name="htmlContainer">the container of the html to handle load stylesheet for</param> |
| 121 | + /// <param name="uri">the uri to download from</param> |
| 122 | + /// <returns>the loaded stylesheet string</returns> |
| 123 | + private static string LoadStylesheetFromUri(HtmlContainer htmlContainer, Uri uri) |
| 124 | + { |
| 125 | + using (var client = new WebClient()) |
| 126 | + { |
| 127 | + var stylesheet = client.DownloadString(uri); |
| 128 | + try |
| 129 | + { |
| 130 | + stylesheet = CorrectRelativeUrls(stylesheet, uri); |
| 131 | + } |
| 132 | + catch(Exception ex) |
| 133 | + { |
| 134 | + htmlContainer.ReportError(HtmlRenderErrorType.CssParsing, "Error in correcting relative URL in loaded stylesheet", ex); |
| 135 | + } |
| 136 | + return stylesheet; |
| 137 | + } |
| 138 | + } |
127 | 139 |
|
| 140 | + /// <summary> |
| 141 | + /// Make relative URLs absolute in the stylesheet using the URI of the stylesheet. |
| 142 | + /// </summary> |
| 143 | + /// <param name="stylesheet">the stylesheet to correct</param> |
| 144 | + /// <param name="baseUri">the stylesheet uri to use to create absolute URLs</param> |
| 145 | + /// <returns>Corrected stylesheet</returns> |
| 146 | + private static string CorrectRelativeUrls(string stylesheet, Uri baseUri) |
| 147 | + { |
| 148 | + int idx = 0; |
| 149 | + while( idx != -1 && idx < stylesheet.Length ) |
| 150 | + { |
| 151 | + idx = stylesheet.IndexOf("url", idx, StringComparison.OrdinalIgnoreCase); |
| 152 | + if( idx > 0 ) |
| 153 | + { |
| 154 | + int endIdx = stylesheet.IndexOf(")", idx, StringComparison.OrdinalIgnoreCase); |
| 155 | + if( endIdx > 0 ) |
| 156 | + { |
| 157 | + var urlStr = stylesheet.Substring(idx + 4, endIdx - idx - 4); |
| 158 | + Uri url; |
| 159 | + if( Uri.TryCreate(urlStr, UriKind.Relative, out url) ) |
| 160 | + { |
| 161 | + url = new Uri(baseUri, url); |
| 162 | + stylesheet = stylesheet.Remove(idx + 4, endIdx - idx - 4); |
| 163 | + stylesheet = stylesheet.Insert(idx + 4, url.AbsoluteUri); |
| 164 | + idx += url.AbsoluteUri.Length + 4; |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + idx = endIdx + 1; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return stylesheet; |
| 175 | + } |
| 176 | + |
| 177 | + #endregion |
128 | 178 | } |
129 | 179 | } |
0 commit comments