Skip to content

Commit

Permalink
Css parameter follow up (#1110)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
inforithmics authored Jan 14, 2024
1 parent e9778e4 commit d5e5059
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
42 changes: 34 additions & 8 deletions Source/SvgDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public virtual TSvgElement GetElementById<TSvgElement>(string id) where TSvgElem
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
public static SvgDocument Open(string path)
{
return Open<SvgDocument>(path, null, null);
return Open<SvgDocument>(path, new SvgOptions());
}

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

/// <summary>
Expand All @@ -247,7 +247,20 @@ public static SvgDocument Open(string path)
/// <param name="entities">A dictionary of custom entity definitions to be used when resolving XML entities within the document.</param>
/// <returns>An <see cref="SvgDocument"/> with the contents loaded.</returns>
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
public static T Open<T>(string path, Dictionary<string, string> entities, string css = null) where T : SvgDocument, new()
[Obsolete("Use Open<T>(string path, SvgOptions svgOptions)")]
public static T Open<T>(string path, Dictionary<string, string> entities) where T : SvgDocument, new()
{
return Open<T>(path, new SvgOptions(entities));
}

/// <summary>
/// Opens the document at the specified path and loads the SVG contents.
/// </summary>
/// <param name="path">A <see cref="string"/> containing the path of the file to open.</param>
/// <param name="svgOptions">A dictionary of custom entity definitions to be used when resolving XML entities within the document.</param>
/// <returns>A <see cref="SvgDocument"/> with the contents loaded.</returns>
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
public static T Open<T>(string path, SvgOptions svgOptions) where T : SvgDocument, new()
{
if (string.IsNullOrEmpty(path))
{
Expand All @@ -261,7 +274,7 @@ public static SvgDocument Open(string path)

using (var stream = File.OpenRead(path))
{
var doc = Open<T>(stream, entities, css);
var doc = Open<T>(stream, svgOptions);
doc.BaseUri = new Uri(System.IO.Path.GetFullPath(path));
return doc;
}
Expand All @@ -273,7 +286,7 @@ public static SvgDocument Open(string path)
/// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
public static T Open<T>(Stream stream) where T : SvgDocument, new()
{
return Open<T>(stream, null);
return Open<T>(stream, new SvgOptions());
}

/// <summary>
Expand All @@ -282,21 +295,34 @@ public static SvgDocument Open(string path)
/// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
/// <param name="entities">Custom entity definitions.</param>
/// <exception cref="ArgumentNullException">The <paramref name="stream"/> parameter cannot be <c>null</c>.</exception>
public static T Open<T>(Stream stream, Dictionary<string, string> entities, string css = null) where T : SvgDocument, new()
[Obsolete("Use Open<T>(Stream stream, SvgOptions svgOptions)")]
public static T Open<T>(Stream stream, Dictionary<string, string> entities)
where T : SvgDocument, new()
{
return Open<T>(stream, new SvgOptions(entities));
}

/// <summary>
/// Opens an SVG document from the specified <see cref="Stream"/> and adds the specified entities.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
/// <param name="svgOptions">Css Style that will be applied to the Svg Document</param>
/// <exception cref="ArgumentNullException">The <paramref name="stream"/> parameter cannot be <c>null</c>.</exception>
public static T Open<T>(Stream stream, SvgOptions svgOptions) where T : SvgDocument, new()
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}

// Don't close the stream via a dispose: that is the client's job.
var reader = new SvgTextReader(stream, entities)
var reader = new SvgTextReader(stream, svgOptions.Entities)
{
XmlResolver = new SvgDtdResolver(),
WhitespaceHandling = WhitespaceHandling.Significant,
DtdProcessing = DisableDtdProcessing ? DtdProcessing.Ignore : DtdProcessing.Parse,
};
return Create<T>(reader, css);
return Create<T>(reader, svgOptions.Css);
}

/// <summary>
Expand Down
31 changes: 31 additions & 0 deletions Source/SvgOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;

#nullable enable

namespace Svg;

public class SvgOptions
{
public SvgOptions()
{
}

public SvgOptions(Dictionary<string, string> entities)
{
Entities = entities;
}

public SvgOptions(Dictionary<string, string> entities, string css)
{
Entities = entities;
Css = css;
}

public SvgOptions(string css)
{
Css = css;
}

public Dictionary<string, string>? Entities { get; set; }
public string? Css { get; set; }
}
2 changes: 1 addition & 1 deletion Tests/Svg.UnitTests/EntitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestOpenWithEntities()
public void TestOpenWithoutEntities()
{
var svgPath = Path.Combine(TestsRootPath, EntitiesSampleSvgPath);
Assert.That(() => { SvgDocument.Open<SvgDocument>(svgPath, null); },
Assert.That(() => { SvgDocument.Open<SvgDocument>(svgPath, (Dictionary<string,string>)null); },

Check warning on line 37 in Tests/Svg.UnitTests/EntitiesTests.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, netcoreapp3.1)

'SvgDocument.Open<T>(string, Dictionary<string, string>)' is obsolete: 'Use Open<T>(string path, SvgOptions svgOptions)'

Check warning on line 37 in Tests/Svg.UnitTests/EntitiesTests.cs

View workflow job for this annotation

GitHub Actions / benchmark (SvgDocument, windows-latest, netcoreapp3.1)

'SvgDocument.Open<T>(string, Dictionary<string, string>)' is obsolete: 'Use Open<T>(string path, SvgOptions svgOptions)'

Check warning on line 37 in Tests/Svg.UnitTests/EntitiesTests.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net6.0)

'SvgDocument.Open<T>(string, Dictionary<string, string>)' is obsolete: 'Use Open<T>(string path, SvgOptions svgOptions)'

Check warning on line 37 in Tests/Svg.UnitTests/EntitiesTests.cs

View workflow job for this annotation

GitHub Actions / benchmark (SvgPathBuilder, windows-latest, netcoreapp3.1)

'SvgDocument.Open<T>(string, Dictionary<string, string>)' is obsolete: 'Use Open<T>(string path, SvgOptions svgOptions)'

Check warning on line 37 in Tests/Svg.UnitTests/EntitiesTests.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net462)

'SvgDocument.Open<T>(string, Dictionary<string, string>)' is obsolete: 'Use Open<T>(string path, SvgOptions svgOptions)'

Check warning on line 37 in Tests/Svg.UnitTests/EntitiesTests.cs

View workflow job for this annotation

GitHub Actions / benchmark (SvgTransformConverter, windows-latest, netcoreapp3.1)

'SvgDocument.Open<T>(string, Dictionary<string, string>)' is obsolete: 'Use Open<T>(string path, SvgOptions svgOptions)'

Check warning on line 37 in Tests/Svg.UnitTests/EntitiesTests.cs

View workflow job for this annotation

GitHub Actions / benchmark (CoordinateParser, windows-latest, netcoreapp3.1)

'SvgDocument.Open<T>(string, Dictionary<string, string>)' is obsolete: 'Use Open<T>(string path, SvgOptions svgOptions)'
Throws.TypeOf<System.Xml.XmlException>().With.Message.Contains("entity"));
}

Expand Down
2 changes: 2 additions & 0 deletions doc/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ The release versions are NuGet releases.

## Unreleased

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

Expand Down

0 comments on commit d5e5059

Please sign in to comment.