Skip to content

Commit 61144ad

Browse files
committed
Add EventListener support
1 parent 3263934 commit 61144ad

File tree

5 files changed

+130
-13
lines changed

5 files changed

+130
-13
lines changed

Sources/CSSOM/Generated.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,9 +1747,15 @@ public class MediaQueryList: EventTarget {
17471747
@ReadonlyAttribute
17481748
public var matches: Bool
17491749

1750-
// XXX: member 'addListener' is ignored
1750+
@inlinable public func addListener(callback: EventListener?) {
1751+
let this = jsObject
1752+
_ = this[Strings.addListener].function!(this: this, arguments: [_toJSValue(callback)])
1753+
}
17511754

1752-
// XXX: member 'removeListener' is ignored
1755+
@inlinable public func removeListener(callback: EventListener?) {
1756+
let this = jsObject
1757+
_ = this[Strings.removeListener].function!(this: this, arguments: [_toJSValue(callback)])
1758+
}
17531759

17541760
@ClosureAttribute1Optional
17551761
public var onchange: EventHandler
@@ -2114,6 +2120,7 @@ public typealias CSSColorAngle = CSSColorRGBComp
21142120
@usableFromInline static let StyleSheetList: JSString = "StyleSheetList"
21152121
@usableFromInline static let a: JSString = "a"
21162122
@usableFromInline static let add: JSString = "add"
2123+
@usableFromInline static let addListener: JSString = "addListener"
21172124
@usableFromInline static let addRule: JSString = "addRule"
21182125
@usableFromInline static let adoptedStyleSheets: JSString = "adoptedStyleSheets"
21192126
@usableFromInline static let alpha: JSString = "alpha"
@@ -2236,6 +2243,7 @@ public typealias CSSColorAngle = CSSColorRGBComp
22362243
@usableFromInline static let rad: JSString = "rad"
22372244
@usableFromInline static let relativeTo: JSString = "relativeTo"
22382245
@usableFromInline static let rem: JSString = "rem"
2246+
@usableFromInline static let removeListener: JSString = "removeListener"
22392247
@usableFromInline static let removeProperty: JSString = "removeProperty"
22402248
@usableFromInline static let removeRule: JSString = "removeRule"
22412249
@usableFromInline static let replace: JSString = "replace"

Sources/DOM/Generated.swift

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5285,6 +5285,7 @@ public class EventInit: BridgedDictionary {
52855285
public var composed: Bool
52865286
}
52875287

5288+
public typealias EventListener = (Event) -> Void
52885289
public class EventListenerOptions: BridgedDictionary {
52895290
public convenience init(capture: Bool) {
52905291
let object = JSObject.global[Strings.Object].function!.new()
@@ -5458,9 +5459,15 @@ open class EventTarget: JSBridgedClass {
54585459
self.init(unsafelyWrapping: Self.constructor!.new(arguments: []))
54595460
}
54605461

5461-
// XXX: member 'addEventListener' is ignored
5462+
@inlinable public func addEventListener(type: String, callback: EventListener?, options: AddEventListenerOptions_or_Bool? = nil) {
5463+
let this = jsObject
5464+
_ = this[Strings.addEventListener].function!(this: this, arguments: [_toJSValue(type), _toJSValue(callback), _toJSValue(options)])
5465+
}
54625466

5463-
// XXX: member 'removeEventListener' is ignored
5467+
@inlinable public func removeEventListener(type: String, callback: EventListener?, options: Bool_or_EventListenerOptions? = nil) {
5468+
let this = jsObject
5469+
_ = this[Strings.removeEventListener].function!(this: this, arguments: [_toJSValue(type), _toJSValue(callback), _toJSValue(options)])
5470+
}
54645471

54655472
@inlinable public func dispatchEvent(event: Event) -> Bool {
54665473
let this = jsObject
@@ -10662,9 +10669,15 @@ public class MediaQueryList: EventTarget {
1066210669
@ReadonlyAttribute
1066310670
public var matches: Bool
1066410671

10665-
// XXX: member 'addListener' is ignored
10672+
@inlinable public func addListener(callback: EventListener?) {
10673+
let this = jsObject
10674+
_ = this[Strings.addListener].function!(this: this, arguments: [_toJSValue(callback)])
10675+
}
1066610676

10667-
// XXX: member 'removeListener' is ignored
10677+
@inlinable public func removeListener(callback: EventListener?) {
10678+
let this = jsObject
10679+
_ = this[Strings.removeListener].function!(this: this, arguments: [_toJSValue(callback)])
10680+
}
1066810681

1066910682
@ClosureAttribute1Optional
1067010683
public var onchange: EventHandler
@@ -18173,6 +18186,8 @@ public class XSLTProcessor: JSBridgedClass {
1817318186
@usableFromInline static let addAll: JSString = "addAll"
1817418187
@usableFromInline static let addColorStop: JSString = "addColorStop"
1817518188
@usableFromInline static let addCue: JSString = "addCue"
18189+
@usableFromInline static let addEventListener: JSString = "addEventListener"
18190+
@usableFromInline static let addListener: JSString = "addListener"
1817618191
@usableFromInline static let addModule: JSString = "addModule"
1817718192
@usableFromInline static let addPath: JSString = "addPath"
1817818193
@usableFromInline static let addSourceBuffer: JSString = "addSourceBuffer"
@@ -19180,6 +19195,8 @@ public class XSLTProcessor: JSBridgedClass {
1918019195
@usableFromInline static let removeAttributeNode: JSString = "removeAttributeNode"
1918119196
@usableFromInline static let removeChild: JSString = "removeChild"
1918219197
@usableFromInline static let removeCue: JSString = "removeCue"
19198+
@usableFromInline static let removeEventListener: JSString = "removeEventListener"
19199+
@usableFromInline static let removeListener: JSString = "removeListener"
1918319200
@usableFromInline static let removeNamedItem: JSString = "removeNamedItem"
1918419201
@usableFromInline static let removeNamedItemNS: JSString = "removeNamedItemNS"
1918519202
@usableFromInline static let removeParameter: JSString = "removeParameter"
@@ -19515,6 +19532,48 @@ public class XSLTProcessor: JSBridgedClass {
1951519532
@usableFromInline static let z: JSString = "z"
1951619533
}
1951719534

19535+
public protocol Any_AddEventListenerOptions_or_Bool: ConvertibleToJSValue {}
19536+
extension AddEventListenerOptions: Any_AddEventListenerOptions_or_Bool {}
19537+
extension Bool: Any_AddEventListenerOptions_or_Bool {}
19538+
19539+
public enum AddEventListenerOptions_or_Bool: JSValueCompatible, Any_AddEventListenerOptions_or_Bool {
19540+
case addEventListenerOptions(AddEventListenerOptions)
19541+
case bool(Bool)
19542+
19543+
public var addEventListenerOptions: AddEventListenerOptions? {
19544+
switch self {
19545+
case let .addEventListenerOptions(addEventListenerOptions): return addEventListenerOptions
19546+
default: return nil
19547+
}
19548+
}
19549+
19550+
public var bool: Bool? {
19551+
switch self {
19552+
case let .bool(bool): return bool
19553+
default: return nil
19554+
}
19555+
}
19556+
19557+
public static func construct(from value: JSValue) -> Self? {
19558+
if let addEventListenerOptions: AddEventListenerOptions = value.fromJSValue() {
19559+
return .addEventListenerOptions(addEventListenerOptions)
19560+
}
19561+
if let bool: Bool = value.fromJSValue() {
19562+
return .bool(bool)
19563+
}
19564+
return nil
19565+
}
19566+
19567+
public var jsValue: JSValue {
19568+
switch self {
19569+
case let .addEventListenerOptions(addEventListenerOptions):
19570+
return addEventListenerOptions.jsValue
19571+
case let .bool(bool):
19572+
return bool.jsValue
19573+
}
19574+
}
19575+
}
19576+
1951819577
public protocol Any_ArrayBuffer_or_String: ConvertibleToJSValue {}
1951919578
extension ArrayBuffer: Any_ArrayBuffer_or_String {}
1952019579
extension String: Any_ArrayBuffer_or_String {}
@@ -19669,6 +19728,48 @@ public enum BlobPart: JSValueCompatible, Any_BlobPart {
1966919728
}
1967019729
}
1967119730

19731+
public protocol Any_Bool_or_EventListenerOptions: ConvertibleToJSValue {}
19732+
extension Bool: Any_Bool_or_EventListenerOptions {}
19733+
extension EventListenerOptions: Any_Bool_or_EventListenerOptions {}
19734+
19735+
public enum Bool_or_EventListenerOptions: JSValueCompatible, Any_Bool_or_EventListenerOptions {
19736+
case bool(Bool)
19737+
case eventListenerOptions(EventListenerOptions)
19738+
19739+
public var bool: Bool? {
19740+
switch self {
19741+
case let .bool(bool): return bool
19742+
default: return nil
19743+
}
19744+
}
19745+
19746+
public var eventListenerOptions: EventListenerOptions? {
19747+
switch self {
19748+
case let .eventListenerOptions(eventListenerOptions): return eventListenerOptions
19749+
default: return nil
19750+
}
19751+
}
19752+
19753+
public static func construct(from value: JSValue) -> Self? {
19754+
if let bool: Bool = value.fromJSValue() {
19755+
return .bool(bool)
19756+
}
19757+
if let eventListenerOptions: EventListenerOptions = value.fromJSValue() {
19758+
return .eventListenerOptions(eventListenerOptions)
19759+
}
19760+
return nil
19761+
}
19762+
19763+
public var jsValue: JSValue {
19764+
switch self {
19765+
case let .bool(bool):
19766+
return bool.jsValue
19767+
case let .eventListenerOptions(eventListenerOptions):
19768+
return eventListenerOptions.jsValue
19769+
}
19770+
}
19771+
}
19772+
1967219773
public protocol Any_Bool_or_MediaTrackConstraints: ConvertibleToJSValue {}
1967319774
extension Bool: Any_Bool_or_MediaTrackConstraints {}
1967419775
extension MediaTrackConstraints: Any_Bool_or_MediaTrackConstraints {}

Sources/WebIDLToSwift/IDLBuilder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ enum IDLBuilder {
1919
// need types from specs not yet included
2020
"ShadowAnimation", "Blob_or_MediaSource",
2121
"HTMLOrSVGImageElement", "HTMLOrSVGScriptElement", "BodyInit",
22+
// Need better callback interface support
23+
"NodeFilter",
24+
"XPathNSResolver",
2225
// implemented manually
2326
// ArrayBufferView
2427
"BigInt64Array_or_BigUint64Array_or_DataView_or_Float32Array_or_Float64Array_or_Int16Array_or_Int32Array_or_Int8Array_or_Uint16Array_or_Uint32Array_or_Uint8Array_or_Uint8ClampedArray",
@@ -59,9 +62,6 @@ enum IDLBuilder {
5962
"Document": ["createNodeIterator", "createTreeWalker"],
6063
"NodeIterator": ["filter"],
6164
"TreeWalker": ["filter"],
62-
// EventListener
63-
"EventTarget": ["addEventListener", "removeEventListener"],
64-
"MediaQueryList": ["addListener", "removeListener"],
6565
// XPathNSResolver
6666
"XPathEvaluatorBase": ["createExpression", "createNSResolver", "evaluate"],
6767
// manually implemented

Sources/WebIDLToSwift/MergeDeclarations.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ enum DeclarationMerger {
180180
}
181181
}
182182

183-
print("unhandled callback interfaces", allNodes(ofType: IDLCallbackInterface.self).map(\.name))
184-
185183
var allTypes: [IDLTypealias] = allNodes(ofType: IDLTypedef.self) + allNodes(ofType: IDLCallback.self)
186184
allTypes.removeAll(where: { ignoredTypedefs.contains($0.name) })
187185
let mergedTypes = Dictionary(uniqueKeysWithValues: allTypes.map { ($0.name, $0) })
@@ -213,7 +211,8 @@ enum DeclarationMerger {
213211
return MergeResult(
214212
declarations: arrays
215213
+ [Typedefs(typedefs: allTypes)]
216-
+ allNodes(ofType: IDLEnum.self),
214+
+ allNodes(ofType: IDLEnum.self)
215+
+ allNodes(ofType: IDLCallbackInterface.self),
217216
interfaces: mergedInterfaces,
218217
types: mergedTypes
219218
// unions: unions
@@ -233,6 +232,7 @@ protocol DeclarationFile {
233232
}
234233

235234
extension IDLEnum: DeclarationFile {}
235+
extension IDLCallbackInterface: DeclarationFile {}
236236

237237
struct AsyncOperation: IDLNode, IDLNamespaceMember, IDLInterfaceMember, IDLInterfaceMixinMember, IDLNamed {
238238
static var type: String { "" }

Sources/WebIDLToSwift/WebIDL+SwiftRepresentation.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,15 @@ extension IDLCallback: SwiftRepresentable {
182182

183183
extension IDLCallbackInterface: SwiftRepresentable {
184184
var swiftRepresentation: SwiftSource {
185-
"// XXX: unsupported callback interface: \(name)"
185+
let operations = members.compactMap { $0 as? IDLOperation }
186+
precondition(operations.count == 1)
187+
let callback = operations[0]
188+
return """
189+
\(members.count > 1 ? "// XXX: members other than the operation are ignored" : "")
190+
public typealias \(name) = (\(sequence: callback.arguments.map {
191+
"\($0.idlType)\($0.variadic ? "..." : "")"
192+
})) -> \(callback.idlType!)
193+
"""
186194
}
187195
}
188196

0 commit comments

Comments
 (0)