Skip to content

Commit 0eb0c5b

Browse files
author
ArthurHub
committed
* add stylesheet overwrite by CssData object
1 parent 3592e99 commit 0eb0c5b

4 files changed

Lines changed: 68 additions & 16 deletions

File tree

‎Source/HtmlRenderer/CssData.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Collections.Generic;
1515
using HtmlRenderer.Entities;
1616
using HtmlRenderer.Parse;
17+
using HtmlRenderer.Utils;
1718

1819
namespace HtmlRenderer
1920
{
@@ -158,6 +159,31 @@ public void AddCssBlock(string media, CssBlock cssBlock)
158159
}
159160
}
160161

162+
/// <summary>
163+
/// Combine this CSS data blocks with <paramref name="other"/> CSS blocks for each media.<br/>
164+
/// Merge blocks if exists in both.
165+
/// </summary>
166+
/// <param name="other">the CSS data to combine with</param>
167+
public void Combine(CssData other)
168+
{
169+
ArgChecker.AssertArgNotNull(other, "other");
170+
171+
// for each media block
172+
foreach(var mediaBlock in other.MediaBlocks)
173+
{
174+
// for each css class in the media block
175+
foreach(var bla in mediaBlock.Value)
176+
{
177+
// for each css block of the css class
178+
foreach(var cssBlock in bla.Value)
179+
{
180+
// combine with this
181+
AddCssBlock(mediaBlock.Key, cssBlock);
182+
}
183+
}
184+
}
185+
}
186+
161187
/// <summary>
162188
/// Create deep copy of the css data with cloned css blocks.
163189
/// </summary>

‎Source/HtmlRenderer/Entities/HtmlStylesheetLoadEventArgs.cs‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace HtmlRenderer.Entities
1717
{
1818
/// <summary>
19-
/// Raised when aa stylesheet is about to be loaded by file path or URI by link element.<br/>
19+
/// Raised when a stylesheet is about to be loaded by file path or URL in link element.<br/>
2020
/// This event allows to provide the stylesheet manually or provide new source (file or uri) to load from.<br/>
2121
/// If no alternative data is provided the original source will be used.<br/>
2222
/// </summary>
@@ -25,7 +25,7 @@ public sealed class HtmlStylesheetLoadEventArgs : EventArgs
2525
#region Fields and Consts
2626

2727
/// <summary>
28-
/// the source of the stylesheet (file path or uri)
28+
/// the source of the stylesheet as found in the HTML (file path or URL)
2929
/// </summary>
3030
private readonly string _src;
3131

@@ -35,7 +35,7 @@ public sealed class HtmlStylesheetLoadEventArgs : EventArgs
3535
private readonly Dictionary<string, string> _attributes;
3636

3737
/// <summary>
38-
/// provide the new source (file path or uri) to load stylesheet from
38+
/// provide the new source (file path or URL) to load stylesheet from
3939
/// </summary>
4040
private string _setSrc;
4141

@@ -44,13 +44,18 @@ public sealed class HtmlStylesheetLoadEventArgs : EventArgs
4444
/// </summary>
4545
private string _setStyleSheet;
4646

47+
/// <summary>
48+
/// provide the stylesheet data to load
49+
/// </summary>
50+
private CssData _setStyleSheetData;
51+
4752
#endregion
4853

4954

5055
/// <summary>
5156
/// Init.
5257
/// </summary>
53-
/// <param name="src">the source of the image (file path or uri)</param>
58+
/// <param name="src">the source of the image (file path or URL)</param>
5459
/// <param name="attributes">collection of all the attributes that are defined on the image element</param>
5560
public HtmlStylesheetLoadEventArgs(string src, Dictionary<string, string> attributes)
5661
{
@@ -59,23 +64,23 @@ public HtmlStylesheetLoadEventArgs(string src, Dictionary<string, string> attrib
5964
}
6065

6166
/// <summary>
62-
/// the source of the image (file path or uri)
67+
/// the source of the stylesheet as found in the HTML (file path or URL)
6368
/// </summary>
6469
public string Src
6570
{
6671
get { return _src; }
6772
}
6873

6974
/// <summary>
70-
/// collection of all the attributes that are defined on the image element
75+
/// collection of all the attributes that are defined on the link element
7176
/// </summary>
7277
public Dictionary<string, string> Attributes
7378
{
7479
get { return _attributes; }
7580
}
7681

7782
/// <summary>
78-
/// provide the new source (file path or uri) to load stylesheet from
83+
/// provide the new source (file path or URL) to load stylesheet from
7984
/// </summary>
8085
public string SetSrc
8186
{
@@ -91,5 +96,14 @@ public string SetStyleSheet
9196
get { return _setStyleSheet; }
9297
set { _setStyleSheet = value; }
9398
}
99+
100+
/// <summary>
101+
/// provide the stylesheet data to load
102+
/// </summary>
103+
public CssData SetStyleSheetData
104+
{
105+
get { return _setStyleSheetData; }
106+
set { _setStyleSheetData = value; }
107+
}
94108
}
95109
}

‎Source/HtmlRenderer/Handlers/StylesheetLoadHandler.cs‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,39 @@ internal static class StylesheetLoadHandler
3333
/// <param name="htmlContainer">the container of the html to handle load stylesheet for</param>
3434
/// <param name="src">the source of the element to load the stylesheet by</param>
3535
/// <param name="attributes">the attributes of the link element</param>
36-
public static string LoadStylesheet(HtmlContainer htmlContainer, string src, Dictionary<string, string> attributes)
36+
/// <param name="stylesheet">return the stylesheet string that has been loaded (null if failed or <paramref name="stylesheetData"/> is given)</param>
37+
/// <param name="stylesheetData">return stylesheet data object that was provided by overwrite (null if failed or <paramref name="stylesheet"/> is given)</param>
38+
public static void LoadStylesheet(HtmlContainer htmlContainer, string src, Dictionary<string, string> attributes, out string stylesheet, out CssData stylesheetData)
3739
{
3840
ArgChecker.AssertArgNotNull(htmlContainer, "htmlContainer");
3941

42+
stylesheet = null;
43+
stylesheetData = null;
4044
try
4145
{
4246
var args = new HtmlStylesheetLoadEventArgs(src, attributes);
4347
htmlContainer.RaiseHtmlStylesheetLoadEvent(args);
4448

45-
if(args.SetStyleSheet != null)
49+
if (!string.IsNullOrEmpty(args.SetStyleSheet))
4650
{
47-
return args.SetStyleSheet;
51+
stylesheet = args.SetStyleSheet;
4852
}
49-
else if(args.SetSrc != null)
53+
else if (args.SetStyleSheetData != null)
5054
{
51-
return LoadStylesheet(htmlContainer, args.SetSrc);
55+
stylesheetData = args.SetStyleSheetData;
56+
}
57+
else if (args.SetSrc != null)
58+
{
59+
stylesheet = LoadStylesheet(htmlContainer, args.SetSrc);
5260
}
5361
else
5462
{
55-
return LoadStylesheet(htmlContainer, src);
63+
stylesheet = LoadStylesheet(htmlContainer, src);
5664
}
5765
}
5866
catch (Exception ex)
5967
{
6068
htmlContainer.ReportError(HtmlRenderErrorType.CssParsing, "Exception in handling stylesheet source", ex);
61-
return string.Empty;
6269
}
6370
}
6471

‎Source/HtmlRenderer/Parse/DomParser.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,13 @@ private static void CascadeStyles(CssBox box, HtmlContainer htmlContainer, ref C
117117
box.GetAttribute("rel", string.Empty).Equals("stylesheet", StringComparison.CurrentCultureIgnoreCase))
118118
{
119119
CloneCssData(ref cssData, ref cssDataChanged);
120-
var styleSheet = StylesheetLoadHandler.LoadStylesheet(htmlContainer, box.GetAttribute("href", string.Empty), box.HtmlTag.Attributes);
121-
CssParser.ParseStyleSheet(cssData, styleSheet);
120+
string stylesheet;
121+
CssData stylesheetData;
122+
StylesheetLoadHandler.LoadStylesheet(htmlContainer, box.GetAttribute("href", string.Empty), box.HtmlTag.Attributes,out stylesheet, out stylesheetData);
123+
if (stylesheet != null)
124+
CssParser.ParseStyleSheet(cssData, stylesheet);
125+
else if( stylesheetData != null )
126+
cssData.Combine(stylesheetData);
122127
}
123128
}
124129

0 commit comments

Comments
 (0)