diff --git a/README.md b/README.md index bb55a1e3..18f37ad5 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ and add `@rescript/webapi` to your `rescript.json`: ## Usage ```rescript -let location = WebAPI.Window.current->WebAPI.Window.location +let location = WebAPI.DOM.window->WebAPI.Window.location let href = location.href location->WebAPI.Location.reload ``` diff --git a/docs/content/docs/api-surface.mdx b/docs/content/docs/api-surface.mdx index 3dc6ee15..75c225d5 100644 --- a/docs/content/docs/api-surface.mdx +++ b/docs/content/docs/api-surface.mdx @@ -13,12 +13,12 @@ add the package to `rescript.json`: } ``` -Use `WebAPI.Window.current` for the browser `window`. Interface-specific methods live on +Use `WebAPI.DOM.window` for the browser `window`. Interface-specific methods live on public modules such as `WebAPI.Window`, `WebAPI.Location`, `WebAPI.Document`, `WebAPI.Element`, `WebAPI.Request`, and `WebAPI.Response`. ```ReScript -let location = WebAPI.Window.current->WebAPI.Window.location +let location = WebAPI.DOM.window->WebAPI.Window.location let href = location.href location->WebAPI.Location.reload @@ -32,7 +32,7 @@ helper modules. ```ReScript let req: WebAPI.Request.t = WebAPI.Request.fromURL("https://example.com") let headers = WebAPI.Headers.make() -let document = WebAPI.Window.current->WebAPI.Window.document +let document = WebAPI.DOM.window->WebAPI.Window.document let element = document->WebAPI.Document.createElement("button") ``` @@ -243,7 +243,7 @@ let redirect = WebAPI.Response.redirect(~url="/login", ~status=302) DOM values are operated on through public interface modules. ```ReScript -let document = WebAPI.Window.current->WebAPI.Window.document +let document = WebAPI.DOM.window->WebAPI.Window.document let maybeButton = document ->WebAPI.Document.querySelector("button") @@ -271,7 +271,7 @@ let node = element->WebAPI.Element.asNode `Window.visualViewport` returns a nullable `WebAPI.VisualViewport.t`. ```ReScript -let maybeViewport = WebAPI.Window.current +let maybeViewport = WebAPI.DOM.window ->WebAPI.Window.visualViewport ->Null.toOption diff --git a/docs/content/docs/index.mdx b/docs/content/docs/index.mdx index 5e4d0c56..19242735 100644 --- a/docs/content/docs/index.mdx +++ b/docs/content/docs/index.mdx @@ -51,7 +51,7 @@ export const rescriptJson = ` After installing the package , you can use bindings for the various Web APIs as defined in [MDN](https://developer.mozilla.org/en-US/docs/Web/API). export const rescriptSample = ` -let location = WebAPI.Window.current->WebAPI.Window.location +let location = WebAPI.DOM.window->WebAPI.Window.location // Access properties using \`.\` let href = location.href diff --git a/rescript.json b/rescript.json index 965cdcf7..1cbd32f0 100644 --- a/rescript.json +++ b/rescript.json @@ -214,6 +214,7 @@ "AbortController", "AbortSignal", "Event", + "EventType", "EventTarget", "ExtendableEvent" ] diff --git a/src/Base/BaseCSSFontLoading.res b/src/Base/BaseCSSFontLoading.res index f5c6eabf..4a17fe35 100644 --- a/src/Base/BaseCSSFontLoading.res +++ b/src/Base/BaseCSSFontLoading.res @@ -13,7 +13,6 @@ type fontFaceSetLoadStatus = */ @editor.completeFrom(BaseCSSFontLoading.FontFaceSet) type rec fontFaceSet = private { - ...BaseEvent.eventTarget, /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/FontFaceSet/ready) */ diff --git a/src/Base/BaseEncryptedMediaExtensions.res b/src/Base/BaseEncryptedMediaExtensions.res index 7ba05fa4..43dc1f3a 100644 --- a/src/Base/BaseEncryptedMediaExtensions.res +++ b/src/Base/BaseEncryptedMediaExtensions.res @@ -61,7 +61,7 @@ This WebApiEncryptedMediaExtensions API interface represents a context for mess */ @editor.completeFrom(BaseEncryptedMediaExtensions.MediaKeySession) type mediaKeySession = private { - ...BaseEvent.eventTarget, + ...DOM.eventTarget, /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MediaKeySession/sessionId) */ diff --git a/src/Base/BaseEvent.res b/src/Base/BaseEvent.res deleted file mode 100644 index 8b811ed5..00000000 --- a/src/Base/BaseEvent.res +++ /dev/null @@ -1,6 +0,0 @@ -/** -EventTarget is a WebApiDOM interface implemented by objects that can receive events and may have listeners for them. -[See EventTarget on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget) -*/ -@editor.completeFrom(BaseEvent.EventTarget) -type eventTarget = private {} diff --git a/src/Base/DOM.res b/src/Base/DOM.res index 1a954a89..b41c47c0 100644 --- a/src/Base/DOM.res +++ b/src/Base/DOM.res @@ -27,7 +27,9 @@ type domStringList = { length: int, } -type window +@editor.completeFrom(Window) +type window = private {} +external window: window = "window" type shadowRootMode = | @as("closed") Closed @@ -203,7 +205,7 @@ The location (WebApiURL) of the object it is linked to. Changes done on it are r [See Location on MDN](https://developer.mozilla.org/docs/Web/API/Location) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.Location) +@editor.completeFrom(Location) type location = { /** Returns the Location object's URL. @@ -291,11 +293,11 @@ type userActivation = { The state and the identity of the user agent. It allows scripts to query it and to register themselves to carry on some activities. [See Navigator on MDN](https://developer.mozilla.org/docs/Web/API/Navigator) */ -@editor.completeFrom(DOM.Navigator) +@editor.completeFrom(Navigator) type navigator // TODO: mark as private once mutating fields of private records is allowed -@editor.completeFrom(DOM.DOMTokenList) +@editor.completeFrom(DOMTokenList) type domTokenList = { /** Returns the number of tokens. @@ -315,7 +317,7 @@ Can be set, to change the associated attribute. A collection of Attr objects. Objects inside a NamedNodeMap are not in any particular order, unlike NodeList, although they may be accessed by an index as in an array. [See NamedNodeMap on MDN](https://developer.mozilla.org/docs/Web/API/NamedNodeMap) */ -@editor.completeFrom(DOM.NamedNodeMap) +@editor.completeFrom(NamedNodeMap) type namedNodeMap = private { /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/NamedNodeMap/length) @@ -331,7 +333,7 @@ type fragmentDirective = {} /** [See CustomElementRegistry on MDN](https://developer.mozilla.org/docs/Web/API/CustomElementRegistry) */ -@editor.completeFrom(DOM.CustomElementRegistry) +@editor.completeFrom(CustomElementRegistry) type customElementRegistry = private {} /** @@ -344,12 +346,135 @@ type barProp = { visible: bool, } +/** +EventTarget is a WebApiDOM interface implemented by objects that can receive events and may have listeners for them. +[See EventTarget on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget) +*/ +@editor.completeFrom(EventTarget) +type eventTarget = private {} + +/** +An event which takes place in the DOM. +[See WebApiEvent on MDN](https://developer.mozilla.org/docs/Web/API/Event) +*/ +@editor.completeFrom(Event) +type event = private { + /** +Returns the type of event, e.g. "click", "hashchange", or "submit". +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Event/type) +*/ + @as("type") + type_: EventType.t, + /** +Returns the object to which event is dispatched (its target). +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Event/target) +*/ + target: Null.t, + /** +Returns the object whose event listener's callback is currently being invoked. +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Event/currentTarget) +*/ + currentTarget: Null.t, + /** +Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE. +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Event/eventPhase) +*/ + eventPhase: int, + /** +Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise. +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Event/bubbles) +*/ + bubbles: bool, + /** +Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method. +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Event/cancelable) +*/ + cancelable: bool, + /** +Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise. +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Event/defaultPrevented) +*/ + defaultPrevented: bool, + /** +Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise. +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Event/composed) +*/ + composed: bool, + /** +Returns true if event was dispatched by the user agent, and false otherwise. +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Event/isTrusted) +*/ + isTrusted: bool, + /** +Returns the event's timestamp as the number of milliseconds measured relative to the time origin. +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Event/timeStamp) +*/ + timeStamp: float, +} + +type eventInit = { + mutable bubbles?: bool, + mutable cancelable?: bool, + mutable composed?: bool, +} + +/** +The ExtendableEvent interface extends the lifetime of the install and activate events dispatched on the global scope as part of the service worker lifecycle. +[See ExtendableEvent on MDN](https://developer.mozilla.org/docs/Web/API/ExtendableEvent) +*/ +@editor.completeFrom(ExtendableEvent) +type extendableEvent = private { + ...event, +} + +/** +A controller object that allows you to abort one or more WebApiDOM requests as and when desired. +[See AbortController on MDN](https://developer.mozilla.org/docs/Web/API/AbortController) +*/ +@editor.completeFrom(AbortController) +type rec abortController = private { + /** +Returns the AbortSignal object associated with this object. +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/AbortController/signal) +*/ + signal: abortSignal, +} +/** +A signal object that allows you to communicate with a WebApiDOM request (such as a WebApiFetch) and abort it if required via an AbortController object. +[See AbortSignal on MDN](https://developer.mozilla.org/docs/Web/API/AbortSignal) +*/ +@editor.completeFrom(AbortSignal) and abortSignal = private { + ...eventTarget, + /** +Returns true if this AbortSignal's AbortController has signaled to abort, and false otherwise. +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/AbortSignal/aborted) +*/ + aborted: bool, + /** +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/AbortSignal/reason) +*/ + reason: JSON.t, +} + +module EventListener = { + type t<'event> = 'event => unit + + type options = {mutable capture?: bool} + + type addEventListenerOptions = { + ...options, + mutable passive?: bool, + mutable once?: bool, + mutable signal?: abortSignal, + } +} + /** [See ScreenOrientation on MDN](https://developer.mozilla.org/docs/Web/API/ScreenOrientation) */ -@editor.completeFrom(DOM.ScreenOrientation) +@editor.completeFrom(ScreenOrientation) type screenOrientation = private { - ...BaseEvent.eventTarget, + ...eventTarget, /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/ScreenOrientation/type) */ @@ -408,7 +533,7 @@ type offscreenRenderingContext = unknown /** [See AnimationTimeline on MDN](https://developer.mozilla.org/docs/Web/API/AnimationTimeline) */ -@editor.completeFrom(DOM.Animation) +@editor.completeFrom(Animation) type rec animationTimeline = private { /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/AnimationTimeline/currentTime) @@ -419,7 +544,7 @@ type rec animationTimeline = private { /** [See DocumentTimeline on MDN](https://developer.mozilla.org/docs/Web/API/DocumentTimeline) */ -@editor.completeFrom(DOM.DocumentTimeline) and documentTimeline = private { +@editor.completeFrom(DocumentTimeline) and documentTimeline = private { // Base properties from AnimationTimeline /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/AnimationTimeline/currentTime) @@ -432,7 +557,7 @@ type rec animationTimeline = private { [See MediaList on MDN](https://developer.mozilla.org/docs/Web/API/MediaList) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.MediaList) +@editor.completeFrom(MediaList) type mediaList = { /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MediaList/mediaText) @@ -447,7 +572,7 @@ type mediaList = { /** [See StylePropertyMapReadOnly on MDN](https://developer.mozilla.org/docs/Web/API/StylePropertyMapReadOnly) */ -@editor.completeFrom(DOM.StylePropertyMapReadOnly) +@editor.completeFrom(StylePropertyMapReadOnly) type stylePropertyMapReadOnly = private { /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/StylePropertyMapReadOnly/size) @@ -458,7 +583,7 @@ type stylePropertyMapReadOnly = private { /** [See StylePropertyMap on MDN](https://developer.mozilla.org/docs/Web/API/StylePropertyMap) */ -@editor.completeFrom(DOM.StylePropertyMap) +@editor.completeFrom(StylePropertyMap) type stylePropertyMap = private { ...stylePropertyMapReadOnly, } @@ -473,7 +598,7 @@ type domStringMap = {} A list of StyleSheet. [See StyleSheetList on MDN](https://developer.mozilla.org/docs/Web/API/StyleSheetList) */ -@editor.completeFrom(DOM.StyleSheetList) +@editor.completeFrom(StyleSheetList) type rec styleSheetList = private { /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/StyleSheetList/length) @@ -522,7 +647,7 @@ A single CSS style sheet. It inherits properties and methods from its parent, St [See CSSStyleSheet on MDN](https://developer.mozilla.org/docs/Web/API/CSSStyleSheet) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.CSSStyleSheet) and cssStyleSheet = { +@editor.completeFrom(CSSStyleSheet) and cssStyleSheet = { // Base properties from StyleSheet /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/StyleSheet/type) @@ -588,7 +713,7 @@ and cssRule = { A CSSRuleList is an (indirect-modify only) array-like object containing an ordered collection of CSSRule objects. [See CSSRuleList on MDN](https://developer.mozilla.org/docs/Web/API/CSSRuleList) */ -@editor.completeFrom(DOM.CSSRuleList) and cssRuleList = private { +@editor.completeFrom(CSSRuleList) and cssRuleList = private { /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/CSSRuleList/length) */ @@ -600,7 +725,7 @@ An object that is a CSS declaration block, and exposes style information and var [See CSSStyleDeclaration on MDN](https://developer.mozilla.org/docs/Web/API/CSSStyleDeclaration) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.CSSStyleDeclaration) and cssStyleDeclaration = { +@editor.completeFrom(CSSStyleDeclaration) and cssStyleDeclaration = { /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/CSSStyleDeclaration/cssText) */ @@ -2308,9 +2433,9 @@ Node is an interface from which a number of WebApiDOM API object types inherit. [See Node on MDN](https://developer.mozilla.org/docs/Web/API/Node) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.Node) +@editor.completeFrom(Node) type rec node = { - ...BaseEvent.eventTarget, + ...eventTarget, /** Returns the type of node. [Read more on MDN](https://developer.mozilla.org/docs/Web/API/Node/nodeType) @@ -2385,7 +2510,7 @@ type rec node = { NodeList objects are collections of nodes, usually returned by properties such as Node.childNodes and methods such as document.querySelectorAll(). [See NodeList on MDN](https://developer.mozilla.org/docs/Web/API/NodeList) */ -@editor.completeFrom(DOM.NodeList) and nodeList<'tNode> = private { +@editor.completeFrom(NodeList) and nodeList<'tNode> = private { /** Returns the number of nodes in the collection. [Read more on MDN](https://developer.mozilla.org/docs/Web/API/NodeList/length) @@ -2398,7 +2523,7 @@ Element is the most general base class from which all objects in a Document inhe [See Element on MDN](https://developer.mozilla.org/docs/Web/API/Element) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.Element) and element = { +@editor.completeFrom(Element) and element = { // Base properties from Node /** Returns the type of node. @@ -2770,7 +2895,7 @@ TODO: mark as private once mutating fields of private records is allowed [See ShadowRoot on MDN](https://developer.mozilla.org/docs/Web/API/ShadowRoot) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.ShadowRoot) and shadowRoot = { +@editor.completeFrom(ShadowRoot) and shadowRoot = { // Base properties from DocumentFragment // End base properties from DocumentFragment @@ -2910,7 +3035,7 @@ Similarly, when the focused element is in a different node tree than documentOrS A generic collection (array-like object similar to arguments) of elements (in document order) and offers methods and properties for selecting from the list. [See HTMLCollection on MDN](https://developer.mozilla.org/docs/Web/API/HTMLCollection) */ -@editor.completeFrom(DOM.HTMLCollection) and htmlCollection<'t> = private { +@editor.completeFrom(HTMLCollection) and htmlCollection<'t> = private { /** Sets or retrieves the number of objects in a collection. [Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLCollection/length) @@ -2922,7 +3047,7 @@ A generic collection (array-like object similar to arguments) of elements (in do A collection of HTML form control elements. [See HTMLFormControlsCollection on MDN](https://developer.mozilla.org/docs/Web/API/HTMLFormControlsCollection) */ -@editor.completeFrom(DOM.DOM.HTMLFormControlsCollection) and htmlFormControlsCollection = private { +@editor.completeFrom(HTMLFormControlsCollection) and htmlFormControlsCollection = private { // Base properties from HTMLCollection /** Sets or retrieves the number of objects in a collection. @@ -2937,7 +3062,7 @@ Any HTML element. Some elements directly implement this interface, while others [See HTMLElement on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.HTMLElement) and htmlElement = { +@editor.completeFrom(HTMLElement) and htmlElement = { // Base properties from Element /** Returns the namespace. @@ -3232,7 +3357,7 @@ Contains the descriptive information, or metadata, for a document. This object i [See HTMLHeadElement on MDN](https://developer.mozilla.org/docs/Web/API/HTMLHeadElement) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.HTMLHeadElement) and htmlHeadElement = { +@editor.completeFrom(HTMLHeadElement) and htmlHeadElement = { // Base properties from HTMLElement /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/title) @@ -3489,7 +3614,7 @@ A
element in the WebApiDOM; it allows access to and in some cases modific [See HTMLFormElement on MDN](https://developer.mozilla.org/docs/Web/API/HTMLFormElement) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.HTMLFormElement) and htmlFormElement = { +@editor.completeFrom(HTMLFormElement) and htmlFormElement = { // Base properties from HTMLElement /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/title) @@ -3797,7 +3922,7 @@ Provides special properties and methods for manipulating elements. [See HTMLImageElement on MDN](https://developer.mozilla.org/docs/Web/API/HTMLImageElement) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.HTMLImageElement) and htmlImageElement = { +@editor.completeFrom(HTMLImageElement) and htmlImageElement = { // Base properties from HTMLElement /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/title) @@ -4141,7 +4266,7 @@ Provides special properties (beyond the regular HTMLElement interface it also ha [See HTMLEmbedElement on MDN](https://developer.mozilla.org/docs/Web/API/HTMLEmbedElement) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.HTMLEmbedElement) and htmlEmbedElement = { +@editor.completeFrom(HTMLEmbedElement) and htmlEmbedElement = { // Base properties from HTMLElement /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/title) @@ -4414,7 +4539,7 @@ Hyperlink elements and provides special properties and methods (beyond those of [See HTMLAnchorElement on MDN](https://developer.mozilla.org/docs/Web/API/HTMLAnchorElement) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.HTMLAnchorElement) and htmlAnchorElement = { +@editor.completeFrom(HTMLAnchorElement) and htmlAnchorElement = { // Base properties from HTMLElement /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/title) @@ -4788,7 +4913,7 @@ Provides special properties and methods (beyond those of the regular object HTML [See HTMLAreaElement on MDN](https://developer.mozilla.org/docs/Web/API/HTMLAreaElement) TODO: mark as private once mutating fields of private records is allowed */ -@editor.completeFrom(DOM.HTMLAreaElement) and htmlAreaElement = { +@editor.completeFrom(HTMLAreaElement) and htmlAreaElement = { // Base properties from HTMLElement /** [Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/title) @@ -5142,7 +5267,7 @@ HTML