Skip to content

Commit 12bb905

Browse files
committed
Svg & line support
1 parent a3ce262 commit 12bb905

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

Sources/SwiftHtml/Svg/Line.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Tibor Bodecs on 2021. 11. 29..
6+
//
7+
8+
public final class Line: Tag {
9+
10+
public init(x1: Int, y1: Int, x2: Int, y2: Int) {
11+
super.init(Node(type: .standard, name: "line", attributes: [
12+
.init(key: "x1", value: String(x1)),
13+
.init(key: "y1", value: String(y1)),
14+
.init(key: "x2", value: String(x2)),
15+
.init(key: "y2", value: String(y2)),
16+
]))
17+
18+
}
19+
}

Sources/SwiftHtml/Svg/Svg.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Tibor Bodecs on 2021. 11. 29..
6+
//
7+
8+
public final class Svg: Tag {
9+
10+
public init(@TagBuilder _ builder: () -> [Tag]) {
11+
super.init(Node(type: .standard, name: "svg"), children: builder())
12+
}
13+
}
14+
15+
public extension Svg {
16+
17+
func width(_ value: Int) -> Self {
18+
node.upsert(Attribute(key: "width", value: String(value)))
19+
return self
20+
}
21+
22+
func height(_ value: Int) -> Self {
23+
node.upsert(Attribute(key: "height", value: String(value)))
24+
return self
25+
}
26+
27+
func viewBox(minX: Int, minY: Int, width: Int, height: Int) -> Self {
28+
let value = [minX, minY, width, height].map(String.init).joined(separator: " ")
29+
node.upsert(Attribute(key: "viewBox", value: value))
30+
return self
31+
}
32+
33+
func fill(_ value: String) -> Self {
34+
node.upsert(Attribute(key: "fill", value: value))
35+
return self
36+
}
37+
38+
func stroke(_ value: String) -> Self {
39+
node.upsert(Attribute(key: "stroke", value: value))
40+
return self
41+
}
42+
43+
func strokeWidth(_ value: Int) -> Self {
44+
node.upsert(Attribute(key: "stroke-width", value: String(value)))
45+
return self
46+
}
47+
48+
func strokeLinecap(_ value: String) -> Self {
49+
node.upsert(Attribute(key: "stroke-linecap", value: value))
50+
return self
51+
}
52+
53+
func strokeLinejoin(_ value: String) -> Self {
54+
node.upsert(Attribute(key: "stroke-linejoin", value: value))
55+
return self
56+
}
57+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Tibor Bodecs on 2021. 11. 29..
6+
//
7+
8+
import XCTest
9+
@testable import SwiftHtml
10+
11+
final class SvgTests: XCTestCase {
12+
13+
func testSvg() {
14+
15+
let doc = Document(.unspecified) {
16+
Svg {
17+
Line(x1: 1, y1: 2, x2: 3, y2: 4)
18+
}
19+
.width(24)
20+
.height(24)
21+
.viewBox(minX: 0, minY: 0, width: 24, height: 24)
22+
.fill("none")
23+
.strokeWidth(2)
24+
.strokeLinecap("round")
25+
.strokeLinejoin("round")
26+
27+
}
28+
XCTAssertEqual(DocumentRenderer(minify: true).render(doc), #"<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="1" y1="2" x2="3" y2="4"></line></svg>"#)
29+
}
30+
31+
}

0 commit comments

Comments
 (0)