Skip to content

Commit 8bd7c27

Browse files
optimized macro implementation
1 parent 8268373 commit 8bd7c27

File tree

2 files changed

+22
-91
lines changed

2 files changed

+22
-91
lines changed

Sources/HTMLKitMacros/HTMLElement.swift

Lines changed: 21 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,22 @@ extension HTMLElement {
3131
if let key:String = child.label?.text {
3232
switch key {
3333
case "attributes":
34-
attributes = parse_attributes(child: child)
34+
let elements:ArrayElementListSyntax = child.expression.as(ArrayExprSyntax.self)!.elements
35+
for attribute in elements {
36+
let function:FunctionCallExprSyntax = attribute.expression.as(FunctionCallExprSyntax.self)!
37+
var functionName:String = function.calledExpression.as(MemberAccessExprSyntax.self)!.declName.baseName.text
38+
var expression:ExprSyntax = function.arguments.first!.expression
39+
if functionName == "data" {
40+
functionName += "-" + function.arguments.first!.expression.as(StringLiteralExprSyntax.self)!.string
41+
expression = function.arguments.last!.expression
42+
}
43+
if let string:String = parse_attribute(elementType: elementType, key: functionName, expression: expression) {
44+
attributes.append(string)
45+
}
46+
}
3547
break
3648
default: // extra attribute
37-
if let string:String = parse_extra_attribute(elementType: elementType, child: child) {
49+
if let string:String = parse_attribute(elementType: elementType, child: child) {
3850
attributes.append(string)
3951
}
4052
break
@@ -77,96 +89,13 @@ struct ElementData {
7789

7890
// MARK: Parse Attribute
7991
private extension HTMLElement {
80-
static func parse_attributes(child: LabeledExprSyntax) -> [String] {
81-
let elements:ArrayElementListSyntax = child.expression.as(ArrayExprSyntax.self)!.elements
82-
var attributes:[String] = []
83-
for attribute in elements {
84-
let function:FunctionCallExprSyntax = attribute.expression.as(FunctionCallExprSyntax.self)!
85-
var functionName:String = function.calledExpression.as(MemberAccessExprSyntax.self)!.declName.baseName.text
86-
var string:String = ""
87-
switch functionName {
88-
case "accesskey",
89-
"contentEditable",
90-
"dir",
91-
"draggable",
92-
"enterKeyHint",
93-
"hidden",
94-
"id",
95-
"inputMode",
96-
"is",
97-
"itemId",
98-
"itemProp",
99-
"itemRef",
100-
//"itemScope",
101-
"itemType",
102-
"lang",
103-
"nonce",
104-
"popover",
105-
"role",
106-
"slot",
107-
"spellcheck",
108-
"style",
109-
"title",
110-
"translate",
111-
"virtualKeyboardPolicy",
112-
"writingSuggestions":
113-
string = parse_attribute_string(functionName, function)
114-
break
115-
case "class",
116-
"exportParts",
117-
"part":
118-
string = parse_attribute_array(function).joined(separator: " ")
119-
break
120-
case "tabIndex": // TODO: fix
121-
break
122-
case "data":
123-
functionName = "data-" + function.arguments.first!.expression.as(StringLiteralExprSyntax.self)!.string
124-
string = function.arguments.last!.expression.as(StringLiteralExprSyntax.self)!.string
125-
break
126-
default:
127-
break
128-
}
129-
if !string.isEmpty {
130-
attributes.append(functionName.lowercased() + "=\\\"" + string + "\\\"")
131-
}
132-
}
133-
return attributes
134-
}
135-
static func parse_attribute_string(_ key : String, _ function: FunctionCallExprSyntax) -> String {
136-
let argument = function.arguments.first!
137-
let expression:ExprSyntax = argument.expression
138-
if let string:String = expression.as(StringLiteralExprSyntax.self)?.string {
139-
return string
140-
}
141-
if let member = expression.as(MemberAccessExprSyntax.self) {
142-
var token:[String] = []
143-
var base:ExprSyntax? = member.base
144-
while base != nil {
145-
if let member:MemberAccessExprSyntax = base!.as(MemberAccessExprSyntax.self) {
146-
token.append(member.declName.baseName.text)
147-
base = member.base
148-
} else if let decl:String = base!.as(DeclReferenceExprSyntax.self)?.baseName.text {
149-
token.append(decl)
150-
base = nil
151-
} else {
152-
base = nil
153-
}
154-
}
155-
if token.isEmpty {
156-
token.append("HTMLElementAttribute." + key[key.startIndex].uppercased() + key[key.index(after: key.startIndex)...])
157-
}
158-
return "\\(" + token.reversed().joined(separator: ".") + "." + member.declName.baseName.text + ")"
159-
}
160-
return "?"
161-
}
162-
static func parse_attribute_array(_ function: FunctionCallExprSyntax) -> [String] {
163-
return function.arguments.first!.expression.as(ArrayExprSyntax.self)!.elements.map({ $0.expression.as(StringLiteralExprSyntax.self)!.string })
92+
static func parse_attribute(elementType: HTMLElementType, child: LabeledExprSyntax) -> String? {
93+
return parse_attribute(elementType: elementType, key: child.label!.text, expression: child.expression)
16494
}
16595

166-
static func parse_extra_attribute(elementType: HTMLElementType, child: LabeledExprSyntax) -> String? {
167-
let key:String = child.label!.text
96+
static func parse_attribute(elementType: HTMLElementType, key: String, expression: ExprSyntax) -> String? {
16897
func yup(_ value: String) -> String {
169-
return key + "=\\\"" + value + "\\\""
98+
return key.lowercased() + "=\\\"" + value + "\\\""
17099
}
171100
func member(_ value: String) -> String {
172101
let inner:String
@@ -179,7 +108,6 @@ private extension HTMLElement {
179108
}
180109
return yup("\\(HTMLElementAttribute." + inner + value + ")")
181110
}
182-
let expression:ExprSyntax = child.expression
183111
if let boolean:String = expression.as(BooleanLiteralExprSyntax.self)?.literal.text {
184112
return boolean.elementsEqual("true") ? key : nil
185113
}
@@ -201,6 +129,9 @@ private extension HTMLElement {
201129
if let function:FunctionCallExprSyntax = expression.as(FunctionCallExprSyntax.self) {
202130
return member("\(function)")
203131
}
132+
if let array:[String] = expression.as(ArrayExprSyntax.self)?.elements.map({ $0.expression.as(StringLiteralExprSyntax.self)!.string }) {
133+
return yup(array.joined(separator: " "))
134+
}
204135
return nil
205136
}
206137
}

Tests/HTMLKitTests/HTMLKitTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension HTMLKitTests {
5151
}
5252

5353
extension HTMLKitTests {
54-
func testExample1() {
54+
func test_example_1() {
5555
let test:String = #html([
5656
#body([
5757
#div(

0 commit comments

Comments
 (0)