Skip to content

Commit 08a37f5

Browse files
fixed attribute rel behavior for 4 macros
1 parent 4ef36a6 commit 08a37f5

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

Sources/HTMLKit/HTMLKit.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public macro a<T: ExpressibleByStringLiteral>(
6464
hreflang: T? = nil,
6565
ping: [T] = [],
6666
referrerpolicy: HTMLElementAttribute.Extra.referrerpolicy? = nil,
67-
rel: T? = nil,
67+
rel: [HTMLElementAttribute.Extra.rel] = [],
6868
target: HTMLElementAttribute.Extra.target? = nil,
6969
type: T? = nil,
7070
_ innerHTML: T...
@@ -93,7 +93,7 @@ public macro area<T: ExpressibleByStringLiteral>(
9393
shape: HTMLElementAttribute.Extra.shape? = nil,
9494
ping: [T] = [],
9595
referrerpolicy: HTMLElementAttribute.Extra.referrerpolicy? = nil,
96-
rel: T? = nil,
96+
rel: [HTMLElementAttribute.Extra.rel] = [],
9797
target: HTMLElementAttribute.Extra.formtarget? = nil,
9898
_ innerHTML: T...
9999
) -> T = #externalMacro(module: "HTMLKitMacros", type: "HTMLElement")
@@ -378,7 +378,7 @@ public macro form<T: ExpressibleByStringLiteral>(
378378
method: T? = nil,
379379
name: T? = nil,
380380
novalidate: Bool = false,
381-
rel: T? = nil,
381+
rel: [HTMLElementAttribute.Extra.rel] = [],
382382
target: HTMLElementAttribute.Extra.target? = nil,
383383
_ innerHTML: T...
384384
) -> T = #externalMacro(module: "HTMLKitMacros", type: "HTMLElement")
@@ -587,7 +587,7 @@ public macro link<T: ExpressibleByStringLiteral>(
587587
integrity: T? = nil,
588588
media: T? = nil,
589589
referrerpolicy: HTMLElementAttribute.Extra.referrerpolicy? = nil,
590-
rel: [T] = [],
590+
rel: [HTMLElementAttribute.Extra.rel] = [],
591591
sizes: [T] = [],
592592
type: T? = nil,
593593
_ innerHTML: T...

Sources/HTMLKitMacros/HTMLElement.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ extension HTMLElementAttribute.Extra {
642642
case "inputtype": return inputtype(rawValue: enumCase)!.htmlValue
643643
case "numberingtype": return numberingtype(rawValue: enumCase)!.htmlValue
644644
case "referrerpolicy": return referrerpolicy(rawValue: enumCase)!.htmlValue
645+
case "rel": return rel(rawValue: enumCase)!.htmlValue
645646
case "sandbox": return sandbox(rawValue: enumCase)!.htmlValue
646647
case "height", "width":
647648
let values:[Substring] = enumCase.split(separator: "("), key:String = String(values[0]), value:String = String(values[1])

Sources/HTMLKitUtilities/HTMLKitUtilities.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ public extension HTMLElementAttribute.Extra {
296296
case high, low, auto
297297
}
298298

299+
// MARK: formenctype
299300
enum formenctype : String {
300301
case applicationXWWWFormURLEncoded
301302
case multipartFormData
@@ -310,14 +311,17 @@ public extension HTMLElementAttribute.Extra {
310311
}
311312
}
312313

314+
// MARK: formmethod
313315
enum formmethod : String {
314316
case get, post, dialog
315317
}
316318

319+
// MARK: formtarget
317320
enum formtarget : String {
318321
case _self, _blank, _parent, _top
319322
}
320323

324+
// MARK: hidden
321325
enum hidden : String {
322326
case `true`
323327
case untilFound
@@ -330,6 +334,7 @@ public extension HTMLElementAttribute.Extra {
330334
}
331335
}
332336

337+
// MARK: httpequiv
333338
enum httpequiv : String {
334339
case contentSecurityPolicy
335340
case contentType
@@ -348,10 +353,12 @@ public extension HTMLElementAttribute.Extra {
348353
}
349354
}
350355

356+
// MARK: inputmode
351357
enum inputmode : String {
352358
case none, text, decimal, numeric, tel, search, email, url
353359
}
354360

361+
// MARK: inputtype
355362
enum inputtype : String {
356363
case button, checkbox, color, date
357364
case datetimeLocal
@@ -365,10 +372,12 @@ public extension HTMLElementAttribute.Extra {
365372
}
366373
}
367374

375+
// MARK: loading
368376
enum loading : String {
369377
case eager, lazy
370378
}
371379

380+
// MARK: numberingtype
372381
enum numberingtype : String {
373382
case a, A, i, I, one
374383

@@ -380,17 +389,22 @@ public extension HTMLElementAttribute.Extra {
380389
}
381390
}
382391

392+
// MARK: popover
383393
enum popover : String {
384394
case auto, manual
385395
}
396+
397+
// MARK: popovertargetaction
386398
enum popovertargetaction : String {
387399
case hide, show, toggle
388400
}
389401

402+
// MARK: preload
390403
enum preload : String {
391404
case none, metadata, auto
392405
}
393406

407+
// MARK: referrerpolicy
394408
enum referrerpolicy : String {
395409
case noReferrer
396410
case noReferrerWhenDowngrade
@@ -414,6 +428,28 @@ public extension HTMLElementAttribute.Extra {
414428
}
415429
}
416430

431+
// MARK: rel
432+
enum rel : String {
433+
case alternate, author, bookmark, canonical
434+
case dnsPrefetch
435+
case external, expect, help, icon, license
436+
case manifest, me, modulepreload, next, nofollow, noopener, noreferrer
437+
case opener, pingback, preconnect, prefetch, preload, prerender, prev
438+
case privacyPolicy
439+
case search, stylesheet, tag
440+
case termsOfService
441+
442+
public var htmlValue : String {
443+
switch self {
444+
case .dnsPrefetch: return "dns-prefetch"
445+
case .privacyPolicy: return "privacy-policy"
446+
case .termsOfService: return "terms-of-service"
447+
default: return rawValue
448+
}
449+
}
450+
}
451+
452+
// MARK: role
417453
/// [The first rule](https://www.w3.org/TR/using-aria/#rule1) of ARIA use is "If you can use a native HTML element or attribute with the semantics and behavior you require already built in, instead of re-purposing an element and adding an ARIA role, state or property to make it accessible, then do so."
418454
///
419455
/// - Note: There is a saying "No ARIA is better than bad ARIA." In [WebAim's survey of over one million home pages](https://webaim.org/projects/million/#aria), they found that Home pages with ARIA present averaged 41% more detected errors than those without ARIA. While ARIA is designed to make web pages more accessible, if used incorrectly, it can do more harm than good.

Tests/HTMLKitTests/ElementTests.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ extension ElementTests {
8282

8383
string = #a(target: ._blank)
8484
#expect(string == "<a target=\"_blank\"></a>")
85+
86+
string = #a(rel: [.stylesheet, .alternate, .privacyPolicy, .termsOfService, .dnsPrefetch])
87+
#expect(string == "<a rel=\"stylesheet alternate privacy-policy terms-of-service dns-prefetch\"></a>")
8588
}
8689

8790
// MARK: area
@@ -218,7 +221,15 @@ extension ElementTests {
218221
#expect(string == "<iframe allow=\"geolocation;test\"></iframe>")
219222
}
220223

224+
// MARK: img
225+
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img
226+
@Test func img() {
227+
let string:StaticString = #img(sizes: ["(max-height: 500px) 1000px", "(min-height: 25rem)"], srcset: ["https://paradigm-app.com", "https://litleagues.com"])
228+
#expect(string == "<img sizes=\"(max-height: 500px) 1000px,(min-height: 25rem)\" srcset=\"https://paradigm-app.com,https://litleagues.com\">")
229+
}
230+
221231
// MARK: input
232+
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
222233
@Test func input() {
223234
var string:StaticString = #input(autocomplete: ["email", "password"], type: .text)
224235
#expect(string == "<input autocomplete=\"email password\" type=\"text\">")
@@ -228,18 +239,19 @@ extension ElementTests {
228239

229240
string = #input(type: .datetimeLocal)
230241
#expect(string == "<input type=\"datetime-local\">")
231-
}
232242

233-
// MARK: img
234-
@Test func img() {
235-
let string:StaticString = #img(sizes: ["(max-height: 500px) 1000px", "(min-height: 25rem)"], srcset: ["https://paradigm-app.com", "https://litleagues.com"])
236-
#expect(string == "<img sizes=\"(max-height: 500px) 1000px,(min-height: 25rem)\" srcset=\"https://paradigm-app.com,https://litleagues.com\">")
243+
string = #input(accept: [".docx", ".json"])
244+
#expect(string == "<input accept=\".docx,.json\">")
237245
}
238246

239247
// MARK: link
248+
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
240249
@Test func link() {
241-
let string:StaticString = #link(as: .document, imagesizes: ["lmno", "p"])
250+
var string:StaticString = #link(as: .document, imagesizes: ["lmno", "p"])
242251
#expect(string == "<link as=\"document\" imagesizes=\"lmno,p\">")
252+
253+
string = #link(imagesrcset: ["blah", "bling"])
254+
#expect(string == "<link imagesrcset=\"blah,bling\">")
243255
}
244256

245257
// MARK: ol

0 commit comments

Comments
 (0)