Skip to content

Commit 5ed92bd

Browse files
authored
Stylesheet renderer (#1)
* New stylesheet renderer object * Minification & custom indentation support * Write doubles as int if possible * Updated unit tests & minor fixes
1 parent 581082d commit 5ed92bd

16 files changed

+246
-255
lines changed

Sources/SwiftCss/CSS/CSSRepresentable.swift

-10
This file was deleted.

Sources/SwiftCss/CSS/Stylesheet.swift

-68
This file was deleted.

Sources/SwiftCss/Components/CSSColor.swift

+19-19
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,69 @@
77

88
public struct CSSColor: ExpressibleByStringLiteral {
99

10-
private var css: String
10+
private var colorValue: String
1111

1212
init(raw: String) {
13-
css = raw
13+
colorValue = raw
1414
}
1515

1616
// init(hex: String) {
1717
// css = hex
1818
// }
19-
19+
2020
public init(stringLiteral value: StringLiteralType) {
21-
css = value
21+
colorValue = value
2222
/// check if length is valid (000, #000, cafe00, #cafe00)
2323
assert([3,4,6,7].contains(value.count), "Invalid hex string")
2424

2525
/// add # prefix if missing
26-
if !css.hasPrefix("#") {
27-
css = "#" + css
26+
if !colorValue.hasPrefix("#") {
27+
colorValue = "#" + colorValue
2828
}
2929
}
3030

3131
public init(r: Int, g: Int, b: Int, a: Double? = nil) {
32-
css = "\(r),\(g),\(b)"
32+
colorValue = "\(r),\(g),\(b)"
3333
if let a = a {
34-
css = "rgba(" + css + ", \(a))"
34+
colorValue = "rgba(" + colorValue + ", \(a))"
3535
}
3636
else {
37-
css = "rgb(" + css + ")"
37+
colorValue = "rgb(" + colorValue + ")"
3838
}
3939
}
4040

4141
public init(r: Double, g: Double, b: Double, a: Double? = nil) {
42-
css = "\(r)%,\(g)%,\(b)%"
42+
colorValue = "\(r)%,\(g)%,\(b)%"
4343
if let a = a {
44-
css = "rgba(" + css + ", \(a))"
44+
colorValue = "rgba(" + colorValue + ", \(a))"
4545
}
4646
else {
47-
css = "rgb(" + css + ")"
47+
colorValue = "rgb(" + colorValue + ")"
4848
}
4949
}
5050

5151
public init(h: Int, s: Int, l: Int, a: Double? = nil) {
52-
css = "\(h),\(s),\(l)"
52+
colorValue = "\(h),\(s),\(l)"
5353
if let a = a {
54-
css = "hsla(" + css + ", \(a))"
54+
colorValue = "hsla(" + colorValue + ", \(a))"
5555
}
5656
else {
57-
css = "hsl(" + css + ")"
57+
colorValue = "hsl(" + colorValue + ")"
5858
}
5959
}
6060

6161
public init(h: Double, s: Double, l: Double, a: Double? = nil) {
62-
css = "\(h)%,\(s)%,\(l)%"
62+
colorValue = "\(h)%,\(s)%,\(l)%"
6363
if let a = a {
64-
css = "hsla(" + css + ", \(a))"
64+
colorValue = "hsla(" + colorValue + ", \(a))"
6565
}
6666
else {
67-
css = "hsl(" + css + ")"
67+
colorValue = "hsl(" + colorValue + ")"
6868
}
6969
}
7070

7171
var rawValue: String {
72-
css
72+
colorValue
7373
}
7474
}
7575

Sources/SwiftCss/Components/Property.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
/// https://www.w3schools.com/cssref/
9-
public struct Property: CSSRepresentable {
9+
public struct Property {
1010
var name: String
1111
var value: String
1212
var isImportant: Bool = false
@@ -16,11 +16,7 @@ public struct Property: CSSRepresentable {
1616
self.value = value
1717
self.isImportant = isImportant
1818
}
19-
20-
public var css: String {
21-
"\t" + name + ": " + value + (isImportant ? " !important" : "") + ";\n"
22-
}
23-
19+
2420
public func important() -> Property {
2521
guard !isImportant else {
2622
return self

Sources/SwiftCss/Components/Rule.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// Created by Tibor Bodecs on 2021. 07. 10..
66
//
77

8-
public protocol Rule: CSSRepresentable {
8+
public protocol Rule {
99

1010
}

Sources/SwiftCss/Components/Selector.swift

+1-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
/// https://www.w3schools.com/cssref/css_selectors.asp
9-
public struct Selector: CSSRepresentable {
9+
public struct Selector {
1010
var name: String
1111
var properties: [Property]
1212
var pseudo: String? = nil
@@ -21,12 +21,4 @@ public struct Selector: CSSRepresentable {
2121
self.name = name
2222
self.properties = builder()
2323
}
24-
25-
public var css: String {
26-
var suffix = ""
27-
if let pseudo = pseudo {
28-
suffix = pseudo
29-
}
30-
return name + suffix + " {\n" + properties.map(\.css).joined() + "}\n"
31-
}
3224
}

Sources/SwiftCss/Components/Unit.swift

+26-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
// Created by Tibor Bodecs on 2021. 07. 10..
66
//
77

8+
fileprivate extension Double {
9+
10+
/// converts a double value into an integer if it has no fraction digits, otherwise it uses the double value
11+
var asIntOrDouble: String {
12+
if truncatingRemainder(dividingBy: 1) == 0 {
13+
return String(Int(self))
14+
}
15+
return String(self)
16+
}
17+
}
18+
819
public enum Unit {
920
case zero
1021

@@ -45,35 +56,35 @@ public enum Unit {
4556
case .zero:
4657
return "0"
4758
case .cm(let value):
48-
return "\(value)cm"
59+
return "\(value.asIntOrDouble)cm"
4960
case .mm(let value):
50-
return "\(value)mm"
61+
return "\(value.asIntOrDouble)mm"
5162
case .in(let value):
52-
return "\(value)in"
63+
return "\(value.asIntOrDouble)in"
5364
case .px(let value):
54-
return "\(value)px"
65+
return "\(value.asIntOrDouble)px"
5566
case .pt(let value):
56-
return "\(value)pt"
67+
return "\(value.asIntOrDouble)pt"
5768
case .pc(let value):
58-
return "\(value)pc"
69+
return "\(value.asIntOrDouble)pc"
5970
case .em(let value):
60-
return "\(value)em"
71+
return "\(value.asIntOrDouble)em"
6172
case .ex(let value):
62-
return "\(value)ex"
73+
return "\(value.asIntOrDouble)ex"
6374
case .ch(let value):
64-
return "\(value)ch"
75+
return "\(value.asIntOrDouble)ch"
6576
case .rem(let value):
66-
return "\(value)rem"
77+
return "\(value.asIntOrDouble)rem"
6778
case .vw(let value):
68-
return "\(value)vw"
79+
return "\(value.asIntOrDouble)vw"
6980
case .vh(let value):
70-
return "\(value)vh"
81+
return "\(value.asIntOrDouble)vh"
7182
case .vmin(let value):
72-
return "\(value)vmin"
83+
return "\(value.asIntOrDouble)vmin"
7384
case .vmax(let value):
74-
return "\(value)vmax"
85+
return "\(value.asIntOrDouble)vmax"
7586
case .percent(let value):
76-
return "\(value)%"
87+
return "\(value.asIntOrDouble)%"
7788
}
7889
}
7990
}

Sources/SwiftCss/Rules/Charset.swift

-5
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,4 @@ public struct Charset: Rule {
1212
public init(_ name: String) {
1313
self.name = name
1414
}
15-
16-
/// @charset "UTF-8";
17-
public var css: String {
18-
#"@charset ""# + name + #"";"#
19-
}
2015
}

Sources/SwiftCss/Rules/FontFace.swift

-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,4 @@ public struct FontFace: Rule {
1313
self.properties = builder()
1414
}
1515

16-
public var css: String {
17-
let value = properties.map(\.css).joined()
18-
return "@font-face {\n\t" + value.split(separator: "\n").joined(separator: "\n\t") + "\n}\n"
19-
}
2016
}

Sources/SwiftCss/Rules/Import.swift

-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,4 @@ public struct Import: Rule {
1616
public init(_ name: String) {
1717
self.name = name
1818
}
19-
20-
/// @import "mobstyle.css" screen and (max-width: 768px);
21-
public var css: String {
22-
"@import " + name + ";"
23-
}
2419
}

Sources/SwiftCss/Rules/Keyframes.swift

-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,4 @@ public struct Keyframes: Rule {
1414
self.name = name
1515
self.selectors = builder()
1616
}
17-
18-
public var css: String {
19-
let value = selectors.map(\.css).joined()
20-
return "@keyframes " + name + " {\n\t" + value.split(separator: "\n").joined(separator: "\n\t") + "\n}\n"
21-
}
2217
}

Sources/SwiftCss/Rules/Media.swift

-8
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,4 @@ public struct Media: Rule {
3535
public init(screen: Screen, @SelectorBuilder _ builder: () -> [Selector]) {
3636
self.init(screen.rawValue, builder)
3737
}
38-
39-
public var css: String {
40-
let css = selectors.map(\.css).joined()
41-
guard let query = query else {
42-
return css
43-
}
44-
return "@media " + query + " {\n\t" + css.split(separator: "\n").joined(separator: "\n\t") + "\n}\n"
45-
}
4638
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Stylesheet.swift
3+
// SwiftCss
4+
//
5+
// Created by Tibor Bodecs on 2021. 07. 09..
6+
//
7+
8+
public struct Stylesheet {
9+
let rules: [Rule]
10+
11+
public init(@RuleBuilder _ builder: () -> [Rule]) {
12+
self.rules = builder()
13+
}
14+
}

0 commit comments

Comments
 (0)