Skip to content

Commit fb7e102

Browse files
identified some stuff; now uses less interpolation for enum values
- corrected two `attributionsrc` value types - added some todos
1 parent 3a53852 commit fb7e102

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

Sources/HTMLKit/HTMLKit.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public macro a(
4646
virtualkeyboardpolicy: HTMLElementAttribute.virtualkeyboardpolicy? = nil,
4747
writingsuggestions: HTMLElementAttribute.writingsuggestions? = nil,
4848

49-
attributionsrc: [String] = [],
49+
attributionsrc: [String]? = nil,
5050
download: HTMLElementAttribute.download? = nil,
5151
href: String? = nil,
5252
hreflang: String? = nil,
@@ -3401,7 +3401,7 @@ public macro script(
34013401
writingsuggestions: HTMLElementAttribute.writingsuggestions? = nil,
34023402

34033403
async: Bool = false,
3404-
attributionsrc: [String] = [],
3404+
attributionsrc: [String]? = nil,
34053405
blocking: HTMLElementAttribute.blocking? = nil,
34063406
crossorigin: HTMLElementAttribute.crossorigin? = nil,
34073407
defer: Bool = false,
@@ -4729,23 +4729,6 @@ public extension HTMLElementAttribute {
47294729
case refresh
47304730
}
47314731

4732-
enum sandbox : String {
4733-
case allowDownloads = "allow-downloads"
4734-
case allowForms = "allow-forms"
4735-
case allowModals = "allow-modals"
4736-
case allowOrientationLock = "allow-orientation-lock"
4737-
case allowPointerLock = "allow-pointer-lock"
4738-
case allowPopups = "allow-popups"
4739-
case allowPopupsToEscapeSandbox = "allow-popups-to-escape-sandbox"
4740-
case allowPresentation = "allow-presentation"
4741-
case allowSameOrigin = "allow-same-origin"
4742-
case allowScripts = "allow-scripts"
4743-
case allowStorageAccessByUserActiviation = "allow-storage-access-by-user-activation"
4744-
case allowTopNavigation = "allow-top-navigation"
4745-
case allowTopNavigationByUserActivation = "allow-top-navigation-by-user-activation"
4746-
case allowTopNavigationToCustomProtocols = "allow-top-navigation-to-custom-protocols"
4747-
}
4748-
47494732
enum inputmode {
47504733
case none, text, decimal, numeric, tel, search, email, url
47514734
}
@@ -4921,6 +4904,23 @@ public extension HTMLElementAttribute {
49214904
}
49224905
}
49234906

4907+
enum sandbox : String {
4908+
case allowDownloads = "allow-downloads"
4909+
case allowForms = "allow-forms"
4910+
case allowModals = "allow-modals"
4911+
case allowOrientationLock = "allow-orientation-lock"
4912+
case allowPointerLock = "allow-pointer-lock"
4913+
case allowPopups = "allow-popups"
4914+
case allowPopupsToEscapeSandbox = "allow-popups-to-escape-sandbox"
4915+
case allowPresentation = "allow-presentation"
4916+
case allowSameOrigin = "allow-same-origin"
4917+
case allowScripts = "allow-scripts"
4918+
case allowStorageAccessByUserActiviation = "allow-storage-access-by-user-activation"
4919+
case allowTopNavigation = "allow-top-navigation"
4920+
case allowTopNavigationByUserActivation = "allow-top-navigation-by-user-activation"
4921+
case allowTopNavigationToCustomProtocols = "allow-top-navigation-to-custom-protocols"
4922+
}
4923+
49244924
enum scripttype : String {
49254925
case classic = ""
49264926
case importmap, module, speculationrules

Sources/HTMLKitMacros/HTMLElement.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,14 @@ private extension HTMLElement {
8181
if let float:String = expression.as(FloatLiteralExprSyntax.self)?.literal.text {
8282
return yup(float)
8383
}
84-
if let value:String = expression.as(ArrayExprSyntax.self)?.elements.map({ $0.expression.as(StringLiteralExprSyntax.self)!.string }).joined(separator: " ") { // TODO: fix: [Int], [HTMLElementAttribute.controlslist], [HTMLElementAttribute.sandbox]
84+
// TODO: fix: [Int], [HTMLElementAttribute.controlslist], [HTMLElementAttribute.sandbox]
85+
// TODO: fix: attributionsrc attribute has two possible return values (Bool, [String])
86+
// TODO: fix: some attributes return a comma separated string instead of space separated
87+
if let value:String = expression.as(ArrayExprSyntax.self)?.elements.map({ $0.expression.as(StringLiteralExprSyntax.self)!.string }).joined(separator: " ") {
8588
return yup(value)
8689
}
8790
func member(_ value: String) -> String {
88-
var enumName:String = key
91+
let enumName:String
8992
switch elementType.rawValue + key { // better performance than switching key, than switching elementType
9093
case "buttontype":
9194
enumName = "buttontype"
@@ -100,9 +103,20 @@ private extension HTMLElement {
100103
enumName = "scripttype"
101104
break
102105
default:
106+
enumName = key
103107
break
104108
}
105-
return yup("\\(HTMLElementAttribute." + enumName + value + ")")
109+
let string:String
110+
switch enumName {
111+
case "contenteditable", "crossorigin", "download", "formenctype", "hidden", "httpequiv", "numberingtype", "referrerpolicy", "sandbox", "scripttype",
112+
"width", "height":
113+
string = "\\(HTMLElementAttribute." + enumName + value + ")"
114+
break
115+
default:
116+
string = String(value[value.index(after: value.startIndex)...])
117+
break
118+
}
119+
return yup(string)
106120
}
107121
if let function:FunctionCallExprSyntax = expression.as(FunctionCallExprSyntax.self) {
108122
return member("\(function)")

0 commit comments

Comments
 (0)