Skip to content

Commit 2ddcf8e

Browse files
committed
special-case CustomElementConstructor
1 parent 3f7febf commit 2ddcf8e

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

Sources/DOMKit/ECMAScript/Support.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ public let globalThis = Window(from: JSObject.global.jsValue())!
1616
public typealias Uint8ClampedArray = JSUInt8ClampedArray
1717
public typealias CSSColorValue_or_CSSStyleValue = CSSStyleValue
1818
public typealias Any_CSSColorValue_or_CSSStyleValue = CSSStyleValue
19+
public typealias CustomElementConstructor = JSFunction

Sources/DOMKit/WebIDL/CustomElementRegistry.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,27 @@ public class CustomElementRegistry: JSBridgedClass {
1212
self.jsObject = jsObject
1313
}
1414

15-
// XXX: member 'define' is ignored
15+
@inlinable public func define(name: String, constructor: CustomElementConstructor, options: ElementDefinitionOptions? = nil) {
16+
let this = jsObject
17+
_ = this[Strings.define].function!(this: this, arguments: [name.jsValue(), constructor.jsValue(), options?.jsValue() ?? .undefined])
18+
}
1619

1720
@inlinable public func get(name: String) -> CustomElementConstructor? {
1821
let this = jsObject
1922
return this[Strings.get].function!(this: this, arguments: [name.jsValue()]).fromJSValue()!
2023
}
2124

22-
// XXX: member 'whenDefined' is ignored
25+
@inlinable public func whenDefined(name: String) -> JSPromise {
26+
let this = jsObject
27+
return this[Strings.whenDefined].function!(this: this, arguments: [name.jsValue()]).fromJSValue()!
28+
}
2329

24-
// XXX: member 'whenDefined' is ignored
30+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
31+
@inlinable public func whenDefined(name: String) async throws -> CustomElementConstructor {
32+
let this = jsObject
33+
let _promise: JSPromise = this[Strings.whenDefined].function!(this: this, arguments: [name.jsValue()]).fromJSValue()!
34+
return try await _promise.get().fromJSValue()!
35+
}
2536

2637
@inlinable public func upgrade(root: Node) {
2738
let this = jsObject

Sources/DOMKit/WebIDL/Strings.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,7 @@ import JavaScriptKit
18551855
@usableFromInline static let defaultValue: JSString = "defaultValue"
18561856
@usableFromInline static let defaultView: JSString = "defaultView"
18571857
@usableFromInline static let `defer`: JSString = "defer"
1858+
@usableFromInline static let define: JSString = "define"
18581859
@usableFromInline static let deg: JSString = "deg"
18591860
@usableFromInline static let degradationPreference: JSString = "degradationPreference"
18601861
@usableFromInline static let delay: JSString = "delay"
@@ -4975,6 +4976,7 @@ import JavaScriptKit
49754976
@usableFromInline static let webkitdirectory: JSString = "webkitdirectory"
49764977
@usableFromInline static let weight: JSString = "weight"
49774978
@usableFromInline static let whatToShow: JSString = "whatToShow"
4979+
@usableFromInline static let whenDefined: JSString = "whenDefined"
49784980
@usableFromInline static let which: JSString = "which"
49794981
@usableFromInline static let whiteBalanceMode: JSString = "whiteBalanceMode"
49804982
@usableFromInline static let wholeText: JSString = "wholeText"

Sources/DOMKit/WebIDL/Typedefs.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ public typealias FileCallback = (File) -> Void
146146
public typealias PositionCallback = (GeolocationPosition) -> Void
147147
public typealias PositionErrorCallback = (GeolocationPositionError) -> Void
148148
public typealias BlobCallback = (Blob?) -> Void
149-
public typealias CustomElementConstructor = () -> HTMLElement
150149
public typealias FunctionStringCallback = (String) -> Void
151150
public typealias EventHandlerNonNull = (Event) -> JSValue
152151
public typealias OnErrorEventHandlerNonNull = (Event_or_String, String, UInt32, UInt32, JSValue) -> JSValue

Sources/WebIDLToSwift/IDLBuilder.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ enum IDLBuilder {
1010
\n
1111
"""
1212

13-
// dictionaries that depend on types not exposed to Window environments
1413
static let ignoredNames: Set = [
14+
// dictionaries that depend on types not exposed to Window environments
1515
"BreakTokenOptions", "TrustedTypePolicyOptions", "FragmentResultOptions",
1616
"Client_or_MessagePort_or_ServiceWorker", "ExtendableMessageEventInit",
17+
// redundant unions
1718
"CSSColorValue_or_CSSStyleValue",
1819
]
1920

@@ -81,8 +82,6 @@ enum IDLBuilder {
8182
"XRSession": ["requestAnimationFrame"],
8283
// variadic callbacks are unsupported
8384
"TrustedTypePolicyFactory": ["createPolicy"],
84-
// functions as return types are unsupported
85-
"CustomElementRegistry": ["define", "whenDefined"],
8685
// NodeFilter
8786
"Document": ["createNodeIterator", "createTreeWalker"],
8887
"NodeIterator": ["filter"],

Sources/WebIDLToSwift/MergeDeclarations.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import WebIDL
22

33
enum DeclarationMerger {
4-
static let ignoredTypedefs: Set<String> = ["Function", "AudioWorkletProcessorConstructor"]
4+
static let ignoredTypedefs: Set<String> = [
5+
"Function",
6+
"AudioWorkletProcessorConstructor",
7+
"CustomElementConstructor",
8+
]
59
static let validExposures: Set<String> = ["Window"]
610

711
private static func addAsync(_ members: [IDLNode]) -> [IDLNode] {

0 commit comments

Comments
 (0)