You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/HTMLKitUtilities/HTMLEncoding.swift
+22-10Lines changed: 22 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -7,29 +7,29 @@
7
7
8
8
/// The value type the data should be encoded to when returned from the macro.
9
9
///
10
-
/// ## Interpolation Promotion
11
-
/// Swift HTMLKit tries to [promote](https://github.com/RandomHashTags/swift-htmlkit/blob/94793984763308ef5275dd9f71ea0b5e83fea417/Sources/HTMLKitMacros/HTMLElement.swift#L423) known interpolation at compile time with an equivalent `StaticString` for the best performance.
10
+
/// ### Interpolation Promotion
11
+
/// Swift HTMLKit tries to [promote](https://github.com/RandomHashTags/swift-htmlkit/blob/94793984763308ef5275dd9f71ea0b5e83fea417/Sources/HTMLKitMacros/HTMLElement.swift#L423) known interpolation at compile time with an equivalent string literal for the best performance, regardless of encoding.
12
12
/// It is currently limited due to macro expansions being sandboxed and lexical contexts/AST not being available for macro arguments.
13
-
/// This means referencing content known at compile time in a html macro won't get replaced by its literal contents.
13
+
/// This means referencing content known at compile time in a html macro won't get promoted to its expected value.
14
14
/// [Read more about this limitation](https://forums.swift.org/t/swift-lexical-lookup-for-referenced-stuff-located-outside-scope-current-file/75776/6).
15
15
///
16
-
/// ### Promotion Example
16
+
/// #### Promotion Example
17
17
///
18
18
/// ```swift
19
19
/// let _:StaticString = #html(div("\("string")")) // ✅ promotion makes this "<div>string</div>"
20
-
/// let _:StaticString = #html("\(5)") // ✅ promotion makes this "<div>5</div>"
21
-
/// let _:StaticString = #html(5) // ✅ promotion makes this "<div>5</div>"
20
+
/// let _:StaticString = #html(div("\(5)")) // ✅ promotion makes this "<div>5</div>"
21
+
/// let _:StaticString = #html(div(5)) // ✅ promotion makes this "<div>5</div>"
22
22
/// ````
23
23
///
24
-
/// ### Promotion Limitation
24
+
/// #### Promotion Limitation
25
25
///
26
26
/// ```swift
27
27
/// let string:StaticString = "Test"
28
28
/// let _:StaticString = #html(div(string)) // ❌ promotion cannot be applied; StaticString not allowed
29
29
/// let _:String = #html(div(string)) // ⚠️ promotion cannot be applied; compiled as "<div>\(string)</div>"
30
-
/// ````
30
+
/// ```
31
31
///
32
-
publicenumHTMLEncoding:String{
32
+
publicenumHTMLEncoding{
33
33
/// `String`/`StaticString`
34
34
case string
35
35
@@ -47,6 +47,18 @@ public enum HTMLEncoding : String {
47
47
case foundationData
48
48
49
49
/// `ByteBuffer`
50
-
/// - Warning: Swift HTMLKit does not depend on `swift-nio`. You need to import `NIOCore` to use this!
50
+
/// - Warning: You need to import `NIOCore` to use this! Swift HTMLKit does not depend on `swift-nio`!
51
51
case byteBuffer
52
+
53
+
/// Encode the HTML into a custom type.
54
+
///
55
+
/// Use `$0` for the compiled HTML.
56
+
/// - Parameters:
57
+
/// - logic: The encoding logic, represented as a string.
58
+
/// ### Example Usage
59
+
/// ```swift
60
+
/// let _:String = #html(encoding: .custom(#"String("$0")"#), p(5)) // String("<p>5</p>")
0 commit comments