Skip to content

Commit 374f47c

Browse files
committed
Add example about querySelector usage in inherited interfaces.
1 parent 5f5b585 commit 374f47c

9 files changed

+69
-33
lines changed

src/DOMAPI/Document.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DOMAPI/HTMLCanvasElement.js

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DOMAPI/HTMLCanvasElement.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ external asHTMLElement: htmlCanvasElement => htmlElement = "%identity"
88
external asElement: htmlCanvasElement => element = "%identity"
99
external asNode: htmlCanvasElement => node = "%identity"
1010
external asEventTarget: htmlCanvasElement => eventTarget = "%identity"
11-
let isInstanceOf = (_: 't): bool => %raw(`param instanceof HTMLCanvasElement`)
11+
1212
/**
1313
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/focus)
1414
*/

src/DOMAPI/HTMLDivElement.js

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DOMAPI/HTMLDivElement.res

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,9 @@ Throws a "HierarchyRequestError" DOMException if the constraints of the node tre
7272
@send
7373
external replaceChildren2: (htmlDivElement, string) => unit = "replaceChildren"
7474

75-
/**
76-
Returns the first element that is a descendant of node that matches selectors.
77-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Document/querySelector)
78-
*/
79-
@send
80-
external querySelector: (htmlDivElement, string) => element = "querySelector"
75+
include QuerySelector.Impl({
76+
type t = htmlDivElement
77+
})
8178

8279
/**
8380
Returns all element descendants of node that match selectors.

src/DOMAPI/QuerySelector.js

Lines changed: 19 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DOMAPI/QuerySelector.res

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
open Prelude
22
open DOMAPI
33

4+
let isInstanceOfHTMLCanvasElement = (_: 't): bool => %raw(`param instanceof HTMLCanvasElement`)
5+
let isInstanceOfHTMLDivElement = (_: 't): bool => %raw(`param instanceof HTMLDivElement`)
6+
47
module Impl = (
58
T: {
69
type t
@@ -21,16 +24,19 @@ t->querySelector("#myCanvas")
2124
@send
2225
external querySelector: (T.t, string) => null<element> = "querySelector"
2326

24-
let querySelector_htmlCanvasElement = (t: T.t, selector: string): option<htmlCanvasElement> => {
27+
let safeQuerySelector = (predicate: T.t => bool, t: T.t, selector: string): option<'return> => {
2528
let e = querySelector(t, selector)
2629
switch e {
2730
| Null.Null => None
28-
| Null.Value(e) =>
29-
if HTMLCanvasElement.isInstanceOf(e) {
30-
Some(unsafeConversation(e))
31-
} else {
32-
None
33-
}
31+
| Null.Value(e) => predicate(t) ? Some(unsafeConversation(e)) : None
3432
}
3533
}
34+
35+
let querySelector_htmlCanvasElement = (t: T.t, selector: string): option<htmlCanvasElement> => {
36+
safeQuerySelector(isInstanceOfHTMLCanvasElement, t, selector)
37+
}
38+
39+
let querySelector_htmlDivElement = (t: T.t, selector: string): option<htmlDivElement> => {
40+
safeQuerySelector(isInstanceOfHTMLDivElement, t, selector)
41+
}
3642
}

tests/DOMAPI/HTMLCanvasElement__tes.js

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/DOMAPI/HTMLCanvasElement__tes.res

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
open WebAPI.Global
22

3+
let myDiv = document->Document.querySelector_htmlDivElement("#myDiv")
4+
35
let myCanvas: option<DOMAPI.htmlCanvasElement> =
4-
document->Document.querySelector_htmlCanvasElement("#myCanvas")
6+
myDiv->Option.flatMap(div => div->HTMLDivElement.querySelector_htmlCanvasElement("#myCanvas"))
57

68
myCanvas->Option.forEach(myCanvas => {
79
let ctx = myCanvas->HTMLCanvasElement.getContext_2D

0 commit comments

Comments
 (0)