Skip to content

Commit 05c4add

Browse files
author
ArthurHub
committed
* Handle relative URLs in downloaded stylesheet.
1 parent badde02 commit 05c4add

3 files changed

Lines changed: 75 additions & 24 deletions

File tree

Source/HtmlRenderer/Utils/StylesheetLoadHelper.cs renamed to Source/HtmlRenderer/Handlers/StylesheetLoadHandler.cs

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// "Therefore those skilled at the unorthodox
1+
// "Therefore those skilled at the unorthodox
22
// are infinite as heaven and earth,
33
// inexhaustible as the great rivers.
44
// When they come to an end,
@@ -15,16 +15,20 @@
1515
using System.IO;
1616
using System.Net;
1717
using HtmlRenderer.Entities;
18+
using HtmlRenderer.Utils;
1819

19-
namespace HtmlRenderer.Utils
20+
namespace HtmlRenderer.Handlers
2021
{
2122
/// <summary>
22-
/// Helper for loading a stylesheet data.
23+
/// Handler for loading a stylesheet data.
2324
/// </summary>
24-
internal static class StylesheetLoadHelper
25+
internal static class StylesheetLoadHandler
2526
{
2627
/// <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/>
2832
/// </summary>
2933
/// <param name="htmlContainer">the container of the html to handle load stylesheet for</param>
3034
/// <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
7074
private static string LoadStylesheet(HtmlContainer htmlContainer, string src)
7175
{
7276
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" )
7878
{
7979
return LoadStylesheetFromFile(htmlContainer, uri != null ? uri.AbsolutePath : src);
8080
}
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
9182
{
92-
return client.DownloadString(uri);
83+
return LoadStylesheetFromUri(htmlContainer, uri);
9384
}
9485
}
9586

@@ -123,7 +114,66 @@ private static string LoadStylesheetFromFile(HtmlContainer htmlContainer, string
123114
return string.Empty;
124115
}
125116

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+
}
127139

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
128178
}
129179
}

‎Source/HtmlRenderer/HtmlRenderer.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<Compile Include="Handlers\ContextMenuHandler.cs" />
9797
<Compile Include="Handlers\ImageLoadHandler.cs" />
9898
<Compile Include="Handlers\SelectionHandler.cs" />
99+
<Compile Include="Handlers\StylesheetLoadHandler.cs" />
99100
<Compile Include="IGraphics.cs" />
100101
<Compile Include="Handlers\BackgroundImageDrawHandler.cs" />
101102
<Compile Include="Handlers\BordersDrawHandler.cs" />
@@ -132,7 +133,6 @@
132133
<Compile Include="Utils\DomUtils.cs" />
133134
<Compile Include="Utils\HtmlUtils.cs" />
134135
<Compile Include="Utils\CommonUtils.cs" />
135-
<Compile Include="Utils\StylesheetLoadHelper.cs" />
136136
<Compile Include="WinGraphics.cs" />
137137
<Compile Include="Utils\Win32Utils.cs" />
138138
</ItemGroup>

‎Source/HtmlRenderer/Parse/DomParser.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Globalization;
1616
using HtmlRenderer.Dom;
1717
using HtmlRenderer.Entities;
18+
using HtmlRenderer.Handlers;
1819
using HtmlRenderer.Utils;
1920

2021
namespace HtmlRenderer.Parse
@@ -116,7 +117,7 @@ private static void CascadeStyles(CssBox box, HtmlContainer htmlContainer, ref C
116117
box.GetAttribute("rel", string.Empty).Equals("stylesheet", StringComparison.CurrentCultureIgnoreCase))
117118
{
118119
CloneCssData(ref cssData, ref cssDataChanged);
119-
var styleSheet = StylesheetLoadHelper.LoadStylesheet(htmlContainer, box.GetAttribute("href", string.Empty), box.HtmlTag.Attributes);
120+
var styleSheet = StylesheetLoadHandler.LoadStylesheet(htmlContainer, box.GetAttribute("href", string.Empty), box.HtmlTag.Attributes);
120121
CssParser.ParseStyleSheet(cssData, styleSheet);
121122
}
122123
}

0 commit comments

Comments
 (0)