Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 67a593d

Browse files
committed
Improvements
WIP: Selectors & At-Rules support
1 parent 03dbcba commit 67a593d

File tree

10 files changed

+302
-191
lines changed

10 files changed

+302
-191
lines changed

Sources/CSSKit/Group.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
// Copyright © 2019 MakeupStudio. All rights reserved.
77
//
88

9-
//public struct Group: Renderable {
10-
// public var selector: ErasedSelector
9+
//struct AtRuleGroup {
10+
// var rule: NestingAtRule
11+
// var nestedGroups: [StyleGroup]
12+
//}
13+
14+
//public struct StyleGroup: Cus {
15+
// public var selector: AnySelector
1116
// public var styles = StylesCollection()
12-
// public func render() -> String {
13-
//
14-
// }
17+
// public func render() -> String {}
1518
//}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Verification.swift
3+
// CSSKit
4+
//
5+
// Created by Maxim Krouk on 5/29/20.
6+
//
7+
8+
import Foundation
9+
10+
internal func verifyIdent(_ string: String) -> Bool {
11+
string.range(
12+
of: #"^(([\\a-zA-Z_-]{1}[^\d])|[\\a-zA-Z_]{1})([\\a-zA-Z0-9_ -]|(\\.))*$"#,
13+
options: .regularExpression
14+
) != nil
15+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// AtRule+Namespace.swift
3+
// CSSKit
4+
//
5+
// Created by Maxim Krouk on 5/29/20.
6+
//
7+
8+
extension AtRule {
9+
public struct Namespace: CSSAtRule {
10+
static private var prefix: String { "@namespace" }
11+
public var label: Label
12+
public var target: Target
13+
14+
public init?(rawValue: RawAtRule) {
15+
let string = rawValue.render()
16+
guard string.hasPrefix(Self.prefix) else { return nil }
17+
let noPrefix = string.components(separatedBy: .whitespaces).dropFirst()
18+
19+
guard let labelString = noPrefix.first else { return nil }
20+
21+
let label = Label(labelString)
22+
23+
let targetString = noPrefix.dropFirst().joined(separator: " ")
24+
guard !targetString.isEmpty else { return nil }
25+
26+
let target = Target(targetString)
27+
28+
self.init(label: label, target: target)
29+
}
30+
31+
public init(label: Label, target: Target) {
32+
self.label = label
33+
self.target = target
34+
}
35+
36+
public var rawValue: RawValue {
37+
RawAtRule([
38+
Self.prefix,
39+
label.rawValue,
40+
target.rawValue
41+
]
42+
.filter { !$0.isEmpty }
43+
.joined(separator: " ")
44+
)
45+
}
46+
}
47+
}
48+
49+
extension AtRule.Namespace {
50+
public struct Label: RawRepresentable, ExpressibleByStringLiteral {
51+
private(set) public var rawValue: String
52+
53+
public init(stringLiteral value: StringLiteralType) {
54+
self.init(value)
55+
}
56+
57+
public init(_ value: String) {
58+
self.init(rawValue: value)
59+
}
60+
61+
public init(rawValue: String) { self.rawValue = rawValue }
62+
63+
static var `default`: Self { .init("") }
64+
}
65+
66+
// TODO: Maybe I should validate target in initializer somehow
67+
public struct Target: RawRepresentable, ExpressibleByStringLiteral {
68+
private(set) public var rawValue: String
69+
70+
public init(stringLiteral value: StringLiteralType) {
71+
self.init(value)
72+
}
73+
74+
public init(_ value: String) {
75+
self.init(rawValue: value)
76+
}
77+
78+
public init(rawValue: String) { self.rawValue = rawValue }
79+
80+
public static func url(_ value: String) -> Self { .init("url(\(value)") }
81+
public static func string(_ value: String) -> Self { .init(value) }
82+
}
83+
}

Sources/CSSKit/Rules/CSSAtRule.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// CSSAtRule.swift
3+
// CSSKit
4+
//
5+
// Created by Maxim Krouk on 9/1/19.
6+
// Copyright © 2019 MakeupStudio. All rights reserved.
7+
//
8+
9+
public enum AtRule {}
10+
11+
public protocol CSSAtRule: Renderable, RawRepresentable where RawValue == RawAtRule {}
12+
13+
extension CSSAtRule {
14+
public func eraseToAnyAtRule() -> AnyAtRule { .init(self) }
15+
16+
public func render() -> String { rawValue.render() }
17+
}
18+
19+
public struct RawAtRule: CSSAtRule, ExpressibleByStringLiteral {
20+
private var _rawValue: String
21+
22+
public init(rawValue: RawAtRule) {
23+
self = rawValue
24+
}
25+
26+
public init(stringLiteral value: String) {
27+
self.init(value)
28+
}
29+
30+
public init(_ value: String) {
31+
self._rawValue = value
32+
}
33+
34+
public func render() -> String { _rawValue }
35+
36+
public var rawValue: RawAtRule { self }
37+
38+
}
39+
40+
public struct AnyAtRule: CSSAtRule {
41+
private var _render: () -> String
42+
43+
public init(rawValue: RawAtRule) {
44+
self.init(rawValue)
45+
}
46+
47+
public init<T: CSSAtRule>(_ rule: T) {
48+
self._render = rule.render
49+
}
50+
51+
public var rawValue: RawAtRule { .init(render()) }
52+
53+
public func render() -> String { _render() }
54+
}

Sources/CSSKit/Selectors/CSSSelector.swift

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,48 @@
66
// Copyright © 2019 MakeupStudio. All rights reserved.
77
//
88

9-
public protocol Selector: RawRepresentable where RawValue == String {
9+
public enum Selector {}
10+
11+
public protocol CSSSelector: Renderable, RawRepresentable where RawValue == RawSelector {}
12+
13+
extension CSSSelector {
14+
func eraseToAnySelector() -> AnySelector { .init(self) }
15+
16+
public func render() -> String { rawValue.render() }
17+
}
18+
19+
public struct RawSelector: CSSSelector, ExpressibleByStringLiteral {
20+
public var _rawValue: String
21+
22+
public init(rawValue: RawSelector) {
23+
self = rawValue
24+
}
1025

11-
static var `operator`: String { get }
26+
public init(stringLiteral value: String) {
27+
self.init(value)
28+
}
1229

30+
public init(_ value: String) {
31+
self._rawValue = value
32+
}
1333

34+
public func render() -> String { _rawValue }
35+
36+
public var rawValue: RawSelector { self }
1437
}
1538

39+
public struct AnySelector: CSSSelector {
40+
var _render: () -> String
41+
42+
public init(rawValue: RawSelector) {
43+
self.init(rawValue)
44+
}
45+
46+
public init<T: CSSSelector>(_ selector: T) {
47+
self._render = selector.render
48+
}
49+
50+
public var rawValue: RawSelector { .init(render())}
51+
52+
public func render() -> String { _render() }
53+
}

Sources/CSSKit/Selectors/ErasedSelector.swift

Lines changed: 0 additions & 20 deletions
This file was deleted.

Sources/CSSKit/Selectors/Selector+Init.swift

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)