Skip to content

Commit c9f9335

Browse files
reorganized some code
1 parent fdd2779 commit c9f9335

File tree

9 files changed

+427
-418
lines changed

9 files changed

+427
-418
lines changed

Sources/HTMLKitUtilities/HTMLElementValueType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ package indirect enum HTMLElementValueType {
2424
let key:String
2525
if let member:MemberAccessExprSyntax = called_expression.memberAccess, member.base?.declRef?.baseName.text == "HTMLKit" {
2626
key = member.declName.baseName.text
27-
} else if let ref = called_expression.declRef {
27+
} else if let ref:DeclReferenceExprSyntax = called_expression.declRef {
2828
key = ref.baseName.text
2929
} else {
3030
return nil

Sources/HTMLKitUtilities/HTMLKitUtilities.swift

Lines changed: 0 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -68,185 +68,4 @@ public extension String {
6868
self.replace("\"", with: """)
6969
self.replace("'", with: "&#39")
7070
}
71-
}
72-
73-
// MARK: CSSUnit
74-
public extension HTMLElementAttribute {
75-
enum CSSUnit : HTMLInitializable { // https://www.w3schools.com/cssref/css_units.php
76-
// absolute
77-
case centimeters(_ value: Float?)
78-
case millimeters(_ value: Float?)
79-
/// 1 inch = 96px = 2.54cm
80-
case inches(_ value: Float?)
81-
/// 1 pixel = 1/96th of 1inch
82-
case pixels(_ value: Float?)
83-
/// 1 point = 1/72 of 1inch
84-
case points(_ value: Float?)
85-
/// 1 pica = 12 points
86-
case picas(_ value: Float?)
87-
88-
// relative
89-
/// Relative to the font-size of the element (2em means 2 times the size of the current font)
90-
case em(_ value: Float?)
91-
/// Relative to the x-height of the current font (rarely used)
92-
case ex(_ value: Float?)
93-
/// Relative to the width of the "0" (zero)
94-
case ch(_ value: Float?)
95-
/// Relative to font-size of the root element
96-
case rem(_ value: Float?)
97-
/// Relative to 1% of the width of the viewport
98-
case viewportWidth(_ value: Float?)
99-
/// Relative to 1% of the height of the viewport
100-
case viewportHeight(_ value: Float?)
101-
/// Relative to 1% of viewport's smaller dimension
102-
case viewportMin(_ value: Float?)
103-
/// Relative to 1% of viewport's larger dimension
104-
case viewportMax(_ value: Float?)
105-
/// Relative to the parent element
106-
case percent(_ value: Float?)
107-
108-
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
109-
let expression:ExprSyntax = arguments.first!.expression
110-
func float() -> Float? {
111-
guard let s:String = expression.integerLiteral?.literal.text ?? expression.floatLiteral?.literal.text else { return nil }
112-
return Float(s)
113-
}
114-
switch key {
115-
case "centimeters": self = .centimeters(float())
116-
case "millimeters": self = .millimeters(float())
117-
case "inches": self = .inches(float())
118-
case "pixels": self = .pixels(float())
119-
case "points": self = .points(float())
120-
case "picas": self = .picas(float())
121-
122-
case "em": self = .em(float())
123-
case "ex": self = .ex(float())
124-
case "ch": self = .ch(float())
125-
case "rem": self = .rem(float())
126-
case "viewportWidth": self = .viewportWidth(float())
127-
case "viewportHeight": self = .viewportHeight(float())
128-
case "viewportMin": self = .viewportMin(float())
129-
case "viewportMax": self = .viewportMax(float())
130-
case "percent": self = .percent(float())
131-
default: return nil
132-
}
133-
}
134-
135-
public var key : String {
136-
switch self {
137-
case .centimeters(_): return "centimeters"
138-
case .millimeters(_): return "millimeters"
139-
case .inches(_): return "inches"
140-
case .pixels(_): return "pixels"
141-
case .points(_): return "points"
142-
case .picas(_): return "picas"
143-
144-
case .em(_): return "em"
145-
case .ex(_): return "ex"
146-
case .ch(_): return "ch"
147-
case .rem(_): return "rem"
148-
case .viewportWidth(_): return "viewportWidth"
149-
case .viewportHeight(_): return "viewportHeight"
150-
case .viewportMin(_): return "viewportMin"
151-
case .viewportMax(_): return "viewportMax"
152-
case .percent(_): return "percent"
153-
}
154-
}
155-
156-
public var htmlValue : String? {
157-
switch self {
158-
case .centimeters(let v),
159-
.millimeters(let v),
160-
.inches(let v),
161-
.pixels(let v),
162-
.points(let v),
163-
.picas(let v),
164-
165-
.em(let v),
166-
.ex(let v),
167-
.ch(let v),
168-
.rem(let v),
169-
.viewportWidth(let v),
170-
.viewportHeight(let v),
171-
.viewportMin(let v),
172-
.viewportMax(let v),
173-
.percent(let v):
174-
guard let v:Float = v else { return nil }
175-
var s:String = String(describing: v)
176-
while s.last == "0" {
177-
s.removeLast()
178-
}
179-
if s.last == "." {
180-
s.removeLast()
181-
}
182-
return s + suffix
183-
}
184-
}
185-
186-
public var htmlValueIsVoidable : Bool { false }
187-
188-
public var suffix : String {
189-
switch self {
190-
case .centimeters(_): return "cm"
191-
case .millimeters(_): return "mm"
192-
case .inches(_): return "in"
193-
case .pixels(_): return "px"
194-
case .points(_): return "pt"
195-
case .picas(_): return "pc"
196-
197-
case .em(_): return "em"
198-
case .ex(_): return "ex"
199-
case .ch(_): return "ch"
200-
case .rem(_): return "rem"
201-
case .viewportWidth(_): return "vw"
202-
case .viewportHeight(_): return "vh"
203-
case .viewportMin(_): return "vmin"
204-
case .viewportMax(_): return "vmax"
205-
case .percent(_): return "%"
206-
}
207-
}
208-
}
209-
}
210-
211-
// MARK: LiteralReturnType
212-
public enum LiteralReturnType {
213-
case boolean(Bool)
214-
case string(String)
215-
case int(Int)
216-
case float(Float)
217-
case interpolation(String)
218-
case array([Any])
219-
220-
public var isInterpolation : Bool {
221-
switch self {
222-
case .interpolation(_): return true
223-
default: return false
224-
}
225-
}
226-
public var isString : Bool {
227-
switch self {
228-
case .string(_): return true
229-
default: return false
230-
}
231-
}
232-
233-
public func value(key: String) -> String? {
234-
switch self {
235-
case .boolean(let b): return b ? key : nil
236-
case .string(var string):
237-
if string.isEmpty && key == "attributionsrc" {
238-
return ""
239-
}
240-
string.escapeHTML(escapeAttributes: true)
241-
return string
242-
case .int(let int):
243-
return String(describing: int)
244-
case .float(let float):
245-
return String(describing: float)
246-
case .interpolation(let string):
247-
return string
248-
case .array(_):
249-
return nil
250-
}
251-
}
25271
}

0 commit comments

Comments
 (0)