Skip to content

Commit

Permalink
Merge pull request #29 from KristofferStrube/fix/self-closing-tags-no…
Browse files Browse the repository at this point in the history
…t-supported

Fixed that self-closing SVG tags were not supported.
  • Loading branch information
KristofferStrube authored Mar 15, 2024
2 parents 1c24c72 + 1332896 commit 1191a9c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

protected List<SupportedElement> SupportedElements { get; set; } = new()
{
new(typeof(Node), element => element.TagName is "CIRCLE" && element.GetAttribute("data-elementtype") == "node"),
new(typeof(Connector), element => element.TagName is "LINE" && element.GetAttribute("data-elementtype") == "connector"),
new(typeof(Node), element => element.TagName.ToUpper() is "CIRCLE" && element.GetAttribute("data-elementtype") == "node"),
new(typeof(Connector), element => element.TagName.ToUpper() is "LINE" && element.GetAttribute("data-elementtype") == "connector"),
};

protected List<SupportedAddNewSVGElementMenuItem> AddNewSVGElementMenuItems { get; set; } = new()
Expand Down
2 changes: 1 addition & 1 deletion src/KristofferStrube.Blazor.SVGEditor/Gradients/Defs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Defs(IElement element, SVGEditor svg)
{
ISVGElement? sVGElement = SVG.SupportedElements.FirstOrDefault(se => se.CanHandle(child))?.ElementType is Type type
? Activator.CreateInstance(type, child, SVG) as Shape
: GradientTypes.TryGetValue(child.TagName, out Type? gradientType)
: GradientTypes.TryGetValue(child.TagName.ToUpper(), out Type? gradientType)
? Activator.CreateInstance(gradientType, child, SVG) as ISVGElement
: throw new NotImplementedException($"Tag not supported:\n {child.OuterHtml}");
if (sVGElement is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public LinearGradient(IElement element, SVGEditor svg)
Element = element;
SVG = svg;

if (Id is not null && Element.ParentElement?.TagName == "DEFS")
if (Id is not null && Element.ParentElement?.TagName.ToUpper() == "DEFS")
{
SVG.Definitions[Id] = this;
}
Stops = Element.Children
.Where(child => child.TagName == "STOP")
.Where(child => child.TagName.ToUpper() == "STOP")
.Select(child => new Stop(child, this, svg))
.ToList();
AnimationElements = Element.Children
.Where(child => child.TagName == "ANIMATE" && child.HasAttribute("attributename"))
.Where(child => child.TagName.ToUpper() == "ANIMATE" && child.HasAttribute("attributename"))
.Select(child =>
{
string? attributeName = child.GetAttribute("attributename");
Expand Down
20 changes: 10 additions & 10 deletions src/KristofferStrube.Blazor.SVGEditor/SVGEditor.razor.Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ public partial class SVGEditor

[Parameter]
public List<SupportedElement> SupportedElements { get; set; } = new() {
new(typeof(Rect), (IElement element) => element.TagName == "RECT"),
new(typeof(Circle), (IElement element) => element.TagName == "CIRCLE"),
new(typeof(Ellipse), (IElement element) => element.TagName == "ELLIPSE"),
new(typeof(Polygon), (IElement element) => element.TagName == "POLYGON"),
new(typeof(Polyline), (IElement element) => element.TagName == "POLYLINE"),
new(typeof(Line), (IElement element) => element.TagName == "LINE"),
new(typeof(Path), (IElement element) => element.TagName == "PATH"),
new(typeof(Text), (IElement element) => element.TagName == "TEXT"),
new(typeof(G), (IElement element) => element.TagName == "G"),
new(typeof(Defs), (IElement element) => element.TagName == "DEFS"),
new(typeof(Rect), (IElement element) => element.TagName.ToUpper() == "RECT"),
new(typeof(Circle), (IElement element) => element.TagName.ToUpper() == "CIRCLE"),
new(typeof(Ellipse), (IElement element) => element.TagName.ToUpper() == "ELLIPSE"),
new(typeof(Polygon), (IElement element) => element.TagName.ToUpper() == "POLYGON"),
new(typeof(Polyline), (IElement element) => element.TagName.ToUpper() == "POLYLINE"),
new(typeof(Line), (IElement element) => element.TagName.ToUpper() == "LINE"),
new(typeof(Path), (IElement element) => element.TagName.ToUpper() == "PATH"),
new(typeof(Text), (IElement element) => element.TagName.ToUpper() == "TEXT"),
new(typeof(G), (IElement element) => element.TagName.ToUpper() == "G"),
new(typeof(Defs), (IElement element) => element.TagName.ToUpper() == "DEFS"),
};

[Parameter]
Expand Down
5 changes: 2 additions & 3 deletions src/KristofferStrube.Blazor.SVGEditor/SVGEditor.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,10 @@ protected override async Task OnParametersSetAsync()
Definitions.Clear();
ClearSelectedShapes();

IBrowsingContext context = BrowsingContext.New();
Document = await context.OpenAsync(req => req.Content(Input));
Document = await BrowsingContext.New().OpenAsync(res => res.Content($"<svg>{input}</svg>"));

Elements = [];
foreach (IElement child in Document.GetElementsByTagName("BODY")[0].Children)
foreach (IElement child in Document.GetElementsByTagName("BODY")[0].Children.First().Children)
{
ISVGElement? sVGElement = SupportedElements.FirstOrDefault(se => se.CanHandle(child))?.ElementType is Type type
? Activator.CreateInstance(type, child, this) as ISVGElement
Expand Down
4 changes: 2 additions & 2 deletions src/KristofferStrube.Blazor.SVGEditor/Shapes/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public Shape(IElement element, SVGEditor svg)
SVG = svg;

AnimationElements = Element.Children
.Where(child => child.TagName == "ANIMATE" && child.HasAttribute("attributename"))
.Where(child => child.TagName.ToUpper() == "ANIMATE" && child.HasAttribute("attributename"))
.Select(child =>
{
string? attributeName = child.GetAttribute("attributename");
Expand Down Expand Up @@ -82,7 +82,7 @@ public virtual double StrokeDashoffset
public Box BoundingBox { get; set; } = new();
public Action<ISVGElement>? Changed { get; set; }
public bool Selected => SVG.VisibleSelectionShapes.Contains(this);
public bool IsChildElement => Element.ParentElement?.TagName is "G" or null;
public bool IsChildElement => Element.ParentElement?.TagName.ToUpper() is "G" or null;
public bool ShouldTriggerContextMenu => SVG.ShouldShowContextMenu(this);
public virtual string StateRepresentation =>
string.Join("-", Element.Attributes.Select(a => a.Value)) +
Expand Down

0 comments on commit 1191a9c

Please sign in to comment.