Skip to content

Commit d5e5059

Browse files
authored
Css parameter follow up (#1110)
* make adding string css parameter binary compatible * added release notes * introduce svgoptions for css paramater * added obsolete to unnecessary overloads * added more svgoptions overloads * fixing build * Improved release notes
1 parent e9778e4 commit d5e5059

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

Source/SvgDocument.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public virtual TSvgElement GetElementById<TSvgElement>(string id) where TSvgElem
226226
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
227227
public static SvgDocument Open(string path)
228228
{
229-
return Open<SvgDocument>(path, null, null);
229+
return Open<SvgDocument>(path, new SvgOptions());
230230
}
231231

232232
/// <summary>
@@ -237,7 +237,7 @@ public static SvgDocument Open(string path)
237237
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
238238
public static T Open<T>(string path) where T : SvgDocument, new()
239239
{
240-
return Open<T>(path, null, null);
240+
return Open<T>(path, new SvgOptions());
241241
}
242242

243243
/// <summary>
@@ -247,7 +247,20 @@ public static SvgDocument Open(string path)
247247
/// <param name="entities">A dictionary of custom entity definitions to be used when resolving XML entities within the document.</param>
248248
/// <returns>An <see cref="SvgDocument"/> with the contents loaded.</returns>
249249
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
250-
public static T Open<T>(string path, Dictionary<string, string> entities, string css = null) where T : SvgDocument, new()
250+
[Obsolete("Use Open<T>(string path, SvgOptions svgOptions)")]
251+
public static T Open<T>(string path, Dictionary<string, string> entities) where T : SvgDocument, new()
252+
{
253+
return Open<T>(path, new SvgOptions(entities));
254+
}
255+
256+
/// <summary>
257+
/// Opens the document at the specified path and loads the SVG contents.
258+
/// </summary>
259+
/// <param name="path">A <see cref="string"/> containing the path of the file to open.</param>
260+
/// <param name="svgOptions">A dictionary of custom entity definitions to be used when resolving XML entities within the document.</param>
261+
/// <returns>A <see cref="SvgDocument"/> with the contents loaded.</returns>
262+
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
263+
public static T Open<T>(string path, SvgOptions svgOptions) where T : SvgDocument, new()
251264
{
252265
if (string.IsNullOrEmpty(path))
253266
{
@@ -261,7 +274,7 @@ public static SvgDocument Open(string path)
261274

262275
using (var stream = File.OpenRead(path))
263276
{
264-
var doc = Open<T>(stream, entities, css);
277+
var doc = Open<T>(stream, svgOptions);
265278
doc.BaseUri = new Uri(System.IO.Path.GetFullPath(path));
266279
return doc;
267280
}
@@ -273,7 +286,7 @@ public static SvgDocument Open(string path)
273286
/// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
274287
public static T Open<T>(Stream stream) where T : SvgDocument, new()
275288
{
276-
return Open<T>(stream, null);
289+
return Open<T>(stream, new SvgOptions());
277290
}
278291

279292
/// <summary>
@@ -282,21 +295,34 @@ public static SvgDocument Open(string path)
282295
/// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
283296
/// <param name="entities">Custom entity definitions.</param>
284297
/// <exception cref="ArgumentNullException">The <paramref name="stream"/> parameter cannot be <c>null</c>.</exception>
285-
public static T Open<T>(Stream stream, Dictionary<string, string> entities, string css = null) where T : SvgDocument, new()
298+
[Obsolete("Use Open<T>(Stream stream, SvgOptions svgOptions)")]
299+
public static T Open<T>(Stream stream, Dictionary<string, string> entities)
300+
where T : SvgDocument, new()
301+
{
302+
return Open<T>(stream, new SvgOptions(entities));
303+
}
304+
305+
/// <summary>
306+
/// Opens an SVG document from the specified <see cref="Stream"/> and adds the specified entities.
307+
/// </summary>
308+
/// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
309+
/// <param name="svgOptions">Css Style that will be applied to the Svg Document</param>
310+
/// <exception cref="ArgumentNullException">The <paramref name="stream"/> parameter cannot be <c>null</c>.</exception>
311+
public static T Open<T>(Stream stream, SvgOptions svgOptions) where T : SvgDocument, new()
286312
{
287313
if (stream == null)
288314
{
289315
throw new ArgumentNullException("stream");
290316
}
291317

292318
// Don't close the stream via a dispose: that is the client's job.
293-
var reader = new SvgTextReader(stream, entities)
319+
var reader = new SvgTextReader(stream, svgOptions.Entities)
294320
{
295321
XmlResolver = new SvgDtdResolver(),
296322
WhitespaceHandling = WhitespaceHandling.Significant,
297323
DtdProcessing = DisableDtdProcessing ? DtdProcessing.Ignore : DtdProcessing.Parse,
298324
};
299-
return Create<T>(reader, css);
325+
return Create<T>(reader, svgOptions.Css);
300326
}
301327

302328
/// <summary>

Source/SvgOptions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections.Generic;
2+
3+
#nullable enable
4+
5+
namespace Svg;
6+
7+
public class SvgOptions
8+
{
9+
public SvgOptions()
10+
{
11+
}
12+
13+
public SvgOptions(Dictionary<string, string> entities)
14+
{
15+
Entities = entities;
16+
}
17+
18+
public SvgOptions(Dictionary<string, string> entities, string css)
19+
{
20+
Entities = entities;
21+
Css = css;
22+
}
23+
24+
public SvgOptions(string css)
25+
{
26+
Css = css;
27+
}
28+
29+
public Dictionary<string, string>? Entities { get; set; }
30+
public string? Css { get; set; }
31+
}

Tests/Svg.UnitTests/EntitiesTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void TestOpenWithEntities()
3434
public void TestOpenWithoutEntities()
3535
{
3636
var svgPath = Path.Combine(TestsRootPath, EntitiesSampleSvgPath);
37-
Assert.That(() => { SvgDocument.Open<SvgDocument>(svgPath, null); },
37+
Assert.That(() => { SvgDocument.Open<SvgDocument>(svgPath, (Dictionary<string,string>)null); },
3838
Throws.TypeOf<System.Xml.XmlException>().With.Message.Contains("entity"));
3939
}
4040

doc/ReleaseNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ The release versions are NuGet releases.
33

44
## Unreleased
55

6+
### Changes
7+
* added SvgOptions with css parameter to Open so that this can be used for transforming the svgdocument.
68
### Enhancements
79
* made exceptions serializable to be able to cross AppDomain boundaries (see [#826](https://github.com/svg-net/SVG/pull/826))
810

0 commit comments

Comments
 (0)