Skip to content

Commit fe837e0

Browse files
authored
added form, mouse, and keyboard events (#6)
1 parent 1cc0cf6 commit fe837e0

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
public extension HTMLAttribute where Tag: HTMLTrait.Attributes.Global {
2+
static func on(_ event: HTMLAttributeValue.MouseEvent, _ script: String) -> Self { .init(on: event, script: script) }
3+
static func on(_ event: HTMLAttributeValue.FormEvent, _ script: String) -> Self { .init(on: event, script: script) }
4+
static func on(_ event: HTMLAttributeValue.KeyboardEvent, _ script: String) -> Self { .init(on: event, script: script) }
5+
}
6+
7+
// TODO: window events, drag events, media events (more scoped)
8+
9+
public extension HTMLAttributeValue {
10+
struct MouseEvent: HTMLEventName {
11+
public var rawValue: String
12+
public init(rawValue: String) {
13+
self.rawValue = rawValue
14+
}
15+
16+
public static var click: Self { .init(rawValue: "click") }
17+
public static var dblclick: Self { .init(rawValue: "dblclick") }
18+
public static var mousedown: Self { .init(rawValue: "mousedown") }
19+
public static var mousemove: Self { .init(rawValue: "mousemove") }
20+
public static var mouseout: Self { .init(rawValue: "mouseout") }
21+
public static var mouseover: Self { .init(rawValue: "mouseover") }
22+
public static var mouseup: Self { .init(rawValue: "mouseup") }
23+
public static var wheel: Self { .init(rawValue: "wheel") }
24+
}
25+
26+
struct KeyboardEvent: HTMLEventName {
27+
public var rawValue: String
28+
public init(rawValue: String) {
29+
self.rawValue = rawValue
30+
}
31+
32+
public static var keydown: Self { .init(rawValue: "keydown") }
33+
public static var keypress: Self { .init(rawValue: "keypress") }
34+
public static var keyup: Self { .init(rawValue: "keyup") }
35+
}
36+
37+
struct FormEvent: HTMLEventName {
38+
public var rawValue: String
39+
public init(rawValue: String) {
40+
self.rawValue = rawValue
41+
}
42+
43+
public static var blur: Self { .init(rawValue: "blur") }
44+
public static var change: Self { .init(rawValue: "change") }
45+
public static var contextmenu: Self { .init(rawValue: "contextmenu") }
46+
public static var focus: Self { .init(rawValue: "focus") }
47+
public static var input: Self { .init(rawValue: "input") }
48+
public static var invalid: Self { .init(rawValue: "invalid") }
49+
public static var reset: Self { .init(rawValue: "reset") }
50+
public static var search: Self { .init(rawValue: "search") }
51+
public static var select: Self { .init(rawValue: "select") }
52+
public static var submit: Self { .init(rawValue: "submit") }
53+
}
54+
}
55+
56+
protocol HTMLEventName: RawRepresentable {}
57+
58+
extension HTMLAttribute {
59+
init(on eventName: some HTMLEventName, script: String) {
60+
self.init(name: "on\(eventName.rawValue)", value: script)
61+
}
62+
}

Tests/ElementaryTests/AttributeRenderingTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,25 @@ final class AttributeRenderingTests: XCTestCase {
6767
#"<br id="1-2" data-bar="bazbaq">"#
6868
)
6969
}
70+
71+
func testRendersMouseEventAttribute() {
72+
HTMLAssertEqual(
73+
p(.on(.click, "doIt()")) {},
74+
#"<p onclick="doIt()"></p>"#
75+
)
76+
}
77+
78+
func testRendersKeyboardEventAttribute() {
79+
HTMLAssertEqual(
80+
p(.on(.keydown, "doIt()")) {},
81+
#"<p onkeydown="doIt()"></p>"#
82+
)
83+
}
84+
85+
func testRendersFormEventAttribute() {
86+
HTMLAssertEqual(
87+
p(.on(.blur, "doIt()")) {},
88+
#"<p onblur="doIt()"></p>"#
89+
)
90+
}
7091
}

0 commit comments

Comments
 (0)