Skip to content

Commit a2f7fa9

Browse files
committed
Add SVGElementType protocol and element conformances
Add SVGElementType protocol to define common requirements for all SVG element types (tagName and isSelfClosing properties). Update all 20 W3C_SVG2 element types to conform to this protocol: - Document elements: Group, SVG, Defs, Use, Symbol - Shape elements: Circle, Ellipse, Line, Rectangle, Polygon, Polyline - Text elements: Text, TSpan - Painting elements: ClipPath, Mask, Marker - PaintServer elements: LinearGradient, RadialGradient, Pattern, Stop This enables type-safe SVG element rendering with protocol-based polymorphism while maintaining the existing element structure. All tests pass (79 tests in 29 suites).
1 parent 08a079d commit a2f7fa9

21 files changed

+43
-20
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// SVGElementType.swift
3+
// swift-w3c-svg
4+
//
5+
// Created by Coen ten Thije Boonkkamp
6+
//
7+
8+
/// Protocol that all SVG element types must conform to.
9+
///
10+
/// This protocol defines the basic requirements for SVG elements,
11+
/// allowing for type-safe element construction with specific parameters.
12+
public protocol SVGElementType: Sendable {
13+
/// The tag name of the SVG element (e.g., "rect", "circle", "path")
14+
static var tagName: String { get }
15+
16+
/// Whether this element is self-closing (has no children)
17+
static var isSelfClosing: Bool { get }
18+
}
19+
20+
extension SVGElementType {
21+
/// Default implementation - most SVG elements can have children
22+
public static var isSelfClosing: Bool { false }
23+
}

Sources/W3C SVG/W3C_SVG2.Document.Defs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension W3C_SVG2.Document {
4040
/// - ``Symbol``
4141
/// - ``Use``
4242
/// - ``Group``
43-
public struct Defs: Sendable, Equatable {
43+
public struct Defs: SVGElementType, Sendable, Equatable {
4444
/// Optional identifier for the defs element
4545
public let id: String?
4646

Sources/W3C SVG/W3C_SVG2.Document.Group.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension W3C_SVG2.Document {
3939
///
4040
/// - ``SVG``
4141
/// - ``Defs``
42-
public struct Group: Sendable, Equatable {
42+
public struct Group: SVGElementType, Sendable, Equatable {
4343
/// Optional identifier for the group
4444
///
4545
/// Used for referencing, animation, or styling

Sources/W3C SVG/W3C_SVG2.Document.SVG.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension W3C_SVG2.Document {
4040
///
4141
/// - ``Group``
4242
/// - ``Symbol``
43-
public struct SVG: Sendable, Equatable {
43+
public struct SVG: SVGElementType, Sendable, Equatable {
4444
/// The x-axis coordinate for embedded svg elements
4545
///
4646
/// Default value: 0

Sources/W3C SVG/W3C_SVG2.Document.Symbol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension W3C_SVG2.Document {
5454
/// - ``Use``
5555
/// - ``Defs``
5656
/// - ``SVG``
57-
public struct Symbol: Sendable, Equatable {
57+
public struct Symbol: SVGElementType, Sendable, Equatable {
5858
/// Identifier for referencing the symbol
5959
public let id: String?
6060

Sources/W3C SVG/W3C_SVG2.Document.Use.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension W3C_SVG2.Document {
5454
/// - ``Symbol``
5555
/// - ``Defs``
5656
/// - ``Group``
57-
public struct Use: Sendable, Equatable {
57+
public struct Use: SVGElementType, Sendable, Equatable {
5858
/// URL reference to target element
5959
///
6060
/// Format: "#elementId" for same-document references

Sources/W3C SVG/W3C_SVG2.PaintServers.LinearGradient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extension W3C_SVG2.PaintServers {
4848
///
4949
/// - ``RadialGradient``
5050
/// - ``Stop``
51-
public struct LinearGradient: Sendable, Equatable {
51+
public struct LinearGradient: SVGElementType, Sendable, Equatable {
5252
/// Identifier for referencing the gradient
5353
public let id: String?
5454

Sources/W3C SVG/W3C_SVG2.PaintServers.Pattern.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension W3C_SVG2.PaintServers {
5151
///
5252
/// - ``LinearGradient``
5353
/// - ``RadialGradient``
54-
public struct Pattern: Sendable, Equatable {
54+
public struct Pattern: SVGElementType, Sendable, Equatable {
5555
/// Identifier for referencing the pattern
5656
public let id: String?
5757

Sources/W3C SVG/W3C_SVG2.PaintServers.RadialGradient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extension W3C_SVG2.PaintServers {
5252
///
5353
/// - ``LinearGradient``
5454
/// - ``Stop``
55-
public struct RadialGradient: Sendable, Equatable {
55+
public struct RadialGradient: SVGElementType, Sendable, Equatable {
5656
/// Identifier for referencing the gradient
5757
public let id: String?
5858

Sources/W3C SVG/W3C_SVG2.PaintServers.Stop.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension W3C_SVG2.PaintServers {
3838
///
3939
/// - ``LinearGradient``
4040
/// - ``RadialGradient``
41-
public struct Stop: Sendable, Equatable {
41+
public struct Stop: SVGElementType, Sendable, Equatable {
4242
/// Position along gradient vector
4343
///
4444
/// Format: number (0–1) or percentage (0%–100%)

0 commit comments

Comments
 (0)