Skip to content

Commit 4e0246a

Browse files
two bug fixes
- fixed a compile error relating to Swift 6 import access control - fixed enum value types not being parsed when in an array - added 3 unit tests
1 parent cb25d99 commit 4e0246a

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ let package = Package(
3535
.target(
3636
name: "HTMLKit",
3737
dependencies: [
38+
"HTMLKitUtilities",
3839
"HTMLKitMacros"
3940
]
4041
),

Sources/HTMLKitMacros/HTMLElement.swift

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ struct HTMLElement : ExpressionMacro {
2121
}
2222
}
2323

24-
/*extension HTMLElement : BodyMacro {
25-
static func expansion(of node: AttributeSyntax, providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax, in context: some MacroExpansionContext) throws -> [CodeBlockItemSyntax] {
26-
return ["test: String? = nil,"]
27-
}
28-
}*/
29-
3024
private extension HTMLElement {
3125
static func parse_arguments(elementType: HTMLElementType, arguments: LabeledExprListSyntax) -> ElementData {
3226
var attributes:[String] = [], innerHTML:[String] = []
@@ -87,34 +81,32 @@ private extension HTMLElement {
8781
if let float:String = expression.as(FloatLiteralExprSyntax.self)?.literal.text {
8882
return yup(float)
8983
}
90-
// TODO: fix: [HTMLElementAttribute.controlslist], [HTMLElementAttribute.sandbox]
91-
if let value:String = expression.as(ArrayExprSyntax.self)?.elements.map({
92-
$0.expression.as(StringLiteralExprSyntax.self)?.string
93-
?? $0.expression.as(IntegerLiteralExprSyntax.self)!.literal.text
84+
func enumName() -> String {
85+
switch elementType.rawValue + key { // better performance than switching key, than switching elementType
86+
case "buttontype": return "buttontype"
87+
case "inputtype": return "inputmode"
88+
case "oltype": return "numberingtype"
89+
case "scripttype": return "scripttype"
90+
default: return key
91+
}
92+
}
93+
if let value:String = expression.as(ArrayExprSyntax.self)?.elements.compactMap({
94+
if let string:String = $0.expression.as(StringLiteralExprSyntax.self)?.string {
95+
return string
96+
}
97+
if let string:String = $0.expression.as(IntegerLiteralExprSyntax.self)?.literal.text {
98+
return string
99+
}
100+
if let string:String = $0.expression.as(MemberAccessExprSyntax.self)?.declName.baseName.text {
101+
return HTMLElementAttribute.htmlValue(enumName: enumName(), for: string)
102+
}
103+
return nil
94104
}).joined(separator: get_separator(key: key)) {
95105
return yup(value)
96106
}
97107
func member(_ value: String) -> String {
98-
let enumName:String
99-
switch elementType.rawValue + key { // better performance than switching key, than switching elementType
100-
case "buttontype":
101-
enumName = "buttontype"
102-
break
103-
case "inputtype":
104-
enumName = "inputmode"
105-
break
106-
case "oltype":
107-
enumName = "numberingtype"
108-
break
109-
case "scripttype":
110-
enumName = "scripttype"
111-
break
112-
default:
113-
enumName = key
114-
break
115-
}
116108
var string:String = String(value[value.index(after: value.startIndex)...])
117-
string = HTMLElementAttribute.htmlValue(enumName: enumName, for: string)
109+
string = HTMLElementAttribute.htmlValue(enumName: enumName(), for: string)
118110
return yup(string)
119111
}
120112
if let function:FunctionCallExprSyntax = expression.as(FunctionCallExprSyntax.self) {

Tests/HTMLKitTests/HTMLKitTests.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
import Testing
9-
@testable import HTMLKit
9+
import HTMLKit
1010

1111
/*extension StaticString : Equatable {
1212
public static func == (left: Self, right: Self) -> Bool {
@@ -42,6 +42,9 @@ extension HTMLKitTests {
4242
@Test func test_element_area() {
4343
#expect(#area(coords: [1, 2, 3]) == "<area coords=\"1,2,3\">")
4444
}
45+
@Test func test_element_audio() {
46+
#expect(#audio(controlslist: .nodownload) == "<audio controlslist=\"nodownload\"></audio>")
47+
}
4548
@Test func test_element_button() {
4649
#expect(#button(type: .submit) == "<button type=\"submit\"></button>")
4750
}
@@ -51,6 +54,9 @@ extension HTMLKitTests {
5154
@Test func test_element_form() {
5255
#expect(#form(acceptCharset: ["utf-8"], autocomplete: .on) == "<form accept-charset=\"utf-8\" autocomplete=\"on\"></form>")
5356
}
57+
@Test func test_element_iframe() {
58+
#expect(#iframe(sandbox: [.allowDownloads, .allowForms]) == "<iframe sandbox=\"allow-downloads allow-forms\"></iframe>")
59+
}
5460
@Test func test_element_input() {
5561
#expect(#input(autocomplete: ["email", "password"], type: .text) == "<input autocomplete=\"email password\" type=\"text\">")
5662
}
@@ -77,6 +83,9 @@ extension HTMLKitTests {
7783
@Test func test_element_text_area() {
7884
#expect(#textarea(autocomplete: ["email", "password"], rows: 5) == "<textarea autocomplete=\"email password\" rows=\"5\"></textarea>")
7985
}
86+
@Test func test_element_video() {
87+
#expect(#video(controlslist: [.nodownload, .nofullscreen, .noremoteplayback]) == "<video controlslist=\"nodownload nofullscreen noremoteplayback\"></video>")
88+
}
8089
}
8190

8291
extension HTMLKitTests {

0 commit comments

Comments
 (0)