/tryocaml/js_of_ocaml-patched/lib/dom_html.ml

http://github.com/cago/tryocaml · OCaml · 1430 lines · 1249 code · 146 blank · 35 comment · 29 complexity · d3069ea0220d148ddec475cf1d347af2 MD5 · raw file

  1. (* Js_of_ocaml library
  2. * http://www.ocsigen.org/js_of_ocaml/
  3. * Copyright (C) 2010 Jérôme Vouillon
  4. * Laboratoire PPS - CNRS Université Paris Diderot
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, with linking exception;
  9. * either version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *)
  20. open Js
  21. external caml_js_on_ie : unit -> bool t = "caml_js_on_ie"
  22. let onIE = Js.to_bool (caml_js_on_ie ())
  23. external html_escape : js_string t -> js_string t = "caml_js_html_escape"
  24. class type cssStyleDeclaration = object
  25. method background : js_string t prop
  26. method backgroundAttachment : js_string t prop
  27. method backgroundColor : js_string t prop
  28. method backgroundImage : js_string t prop
  29. method backgroundPosition : js_string t prop
  30. method backgroundRepeat : js_string t prop
  31. method border : js_string t prop
  32. method borderBottom : js_string t prop
  33. method borderBottomColor : js_string t prop
  34. method borderBottomStyle : js_string t prop
  35. method borderBottomWidth : js_string t prop
  36. method borderCollapse : js_string t prop
  37. method borderColor : js_string t prop
  38. method borderLeft : js_string t prop
  39. method borderLeftColor : js_string t prop
  40. method borderLeftStyle : js_string t prop
  41. method borderLeftWidth : js_string t prop
  42. method borderRight : js_string t prop
  43. method borderRightColor : js_string t prop
  44. method borderRightStyle : js_string t prop
  45. method borderRightWidth : js_string t prop
  46. method borderSpacing : js_string t prop
  47. method borderStyle : js_string t prop
  48. method borderTop : js_string t prop
  49. method borderTopColor : js_string t prop
  50. method borderTopStyle : js_string t prop
  51. method borderTopWidth : js_string t prop
  52. method borderWidth : js_string t prop
  53. method bottom : js_string t prop
  54. method captionSide : js_string t prop
  55. method clear : js_string t prop
  56. method clip : js_string t prop
  57. method color : js_string t prop
  58. method content : js_string t prop
  59. method counterIncrement : js_string t prop
  60. method counterReset : js_string t prop
  61. method cssText : js_string t prop
  62. method cursor : js_string t prop
  63. method direction : js_string t prop
  64. method display : js_string t prop
  65. method emptyCells : js_string t prop
  66. method font : js_string t prop
  67. method fontFamily : js_string t prop
  68. method fontSize : js_string t prop
  69. method fontStyle : js_string t prop
  70. method fontVariant : js_string t prop
  71. method fontWeight : js_string t prop
  72. method height : js_string t prop
  73. method left : js_string t prop
  74. method letterSpacing : js_string t prop
  75. method lineHeight : js_string t prop
  76. method listStyle : js_string t prop
  77. method listStyleImage : js_string t prop
  78. method listStylePosition : js_string t prop
  79. method listStyleType : js_string t prop
  80. method margin : js_string t prop
  81. method marginBottom : js_string t prop
  82. method marginLeft : js_string t prop
  83. method marginRight : js_string t prop
  84. method marginTop : js_string t prop
  85. method maxHeight : js_string t prop
  86. method maxWidth : js_string t prop
  87. method minHeight : js_string t prop
  88. method minWidth : js_string t prop
  89. method opacity : js_string t optdef prop
  90. method outline : js_string t prop
  91. method outlineColor : js_string t prop
  92. method outlineOffset : js_string t prop
  93. method outlineStyle : js_string t prop
  94. method outlineWidth : js_string t prop
  95. method overflow : js_string t prop
  96. method overflowX : js_string t prop
  97. method overflowY : js_string t prop
  98. method padding : js_string t prop
  99. method paddingBottom : js_string t prop
  100. method paddingLeft : js_string t prop
  101. method paddingRight : js_string t prop
  102. method paddingTop : js_string t prop
  103. method pageBreakAfter : js_string t prop
  104. method pageBreakBefore : js_string t prop
  105. method position : js_string t prop
  106. method right : js_string t prop
  107. method tableLayout : js_string t prop
  108. method textAlign : js_string t prop
  109. method textDecoration : js_string t prop
  110. method textIndent : js_string t prop
  111. method textTransform : js_string t prop
  112. method top : js_string t prop
  113. method verticalAlign : js_string t prop
  114. method visibility : js_string t prop
  115. method whiteSpace : js_string t prop
  116. method width : js_string t prop
  117. method wordSpacing : js_string t prop
  118. method zIndex : js_string t prop
  119. end
  120. type ('a, 'b) event_listener = ('a, 'b optdef -> bool t) meth_callback opt
  121. type mouse_button =
  122. | No_button
  123. | Left_button
  124. | Middle_button
  125. | Right_button
  126. class type event = object
  127. method _type : js_string t readonly_prop
  128. method target : element t optdef readonly_prop
  129. method srcElement : element t optdef readonly_prop
  130. end
  131. and mouseEvent = object
  132. inherit event
  133. method relatedTarget : element t opt optdef readonly_prop
  134. method clientX : int readonly_prop
  135. method clientY : int readonly_prop
  136. method screenX : int readonly_prop
  137. method screenY : int readonly_prop
  138. method ctrlKey : bool t readonly_prop
  139. method shiftKey : bool t readonly_prop
  140. method altKey : bool t readonly_prop
  141. method metaKey : bool t readonly_prop
  142. method button : int readonly_prop
  143. method which : mouse_button optdef readonly_prop
  144. method fromElement : element t opt optdef readonly_prop
  145. method toElement : element t opt optdef readonly_prop
  146. method pageX : int optdef readonly_prop
  147. method pageY : int optdef readonly_prop
  148. end
  149. and keyboardEvent = object
  150. inherit event
  151. method charCode : int optdef readonly_prop
  152. method keyCode : int readonly_prop
  153. method keyIdentifier : js_string t optdef readonly_prop
  154. method altKey : bool t readonly_prop
  155. method shiftKey : bool t readonly_prop
  156. method ctrlKey : bool t readonly_prop
  157. method metaKey : bool t readonly_prop
  158. end
  159. and wheelEvent = object (* All browsers but Firefox *)
  160. inherit mouseEvent
  161. method delta : int readonly_prop
  162. method deltaX : int optdef readonly_prop
  163. method deltaY : int optdef readonly_prop
  164. end
  165. and mouseScrollEvent = object (* Firefox *)
  166. inherit mouseEvent
  167. method detail : int readonly_prop
  168. method axis : int optdef readonly_prop
  169. method _HORIZONTAL_AXIS : int optdef readonly_prop
  170. method _VERTICAL_AXIS : int optdef readonly_prop
  171. end
  172. and eventTarget = object ('self)
  173. method onclick : ('self t, mouseEvent t) event_listener writeonly_prop
  174. method ondblclick : ('self t, mouseEvent t) event_listener writeonly_prop
  175. method onmousedown : ('self t, mouseEvent t) event_listener writeonly_prop
  176. method onmouseup : ('self t, mouseEvent t) event_listener writeonly_prop
  177. method onmouseover : ('self t, mouseEvent t) event_listener writeonly_prop
  178. method onmousemove : ('self t, mouseEvent t) event_listener writeonly_prop
  179. method onmouseout : ('self t, mouseEvent t) event_listener writeonly_prop
  180. method onkeypress : ('self t, keyboardEvent t) event_listener writeonly_prop
  181. method onkeydown : ('self t, keyboardEvent t) event_listener writeonly_prop
  182. method onkeyup : ('self t, keyboardEvent t) event_listener writeonly_prop
  183. end
  184. and popStateEvent = object
  185. inherit event
  186. method state : Js.Unsafe.any readonly_prop
  187. end
  188. and storageEvent = object
  189. inherit event
  190. method key : js_string t readonly_prop
  191. method oldValue : js_string t optdef readonly_prop
  192. method keynewValue : js_string t optdef readonly_prop
  193. method url : js_string t readonly_prop
  194. method storageArea : storage t optdef readonly_prop
  195. end
  196. and storage = object
  197. method length : int readonly_prop
  198. method key : int -> js_string t optdef meth
  199. method getItem : js_string t -> js_string t optdef meth
  200. method setItem : js_string t -> js_string t -> unit meth
  201. method removeItem : js_string t -> unit meth
  202. method clear : unit meth
  203. end
  204. and element = object
  205. inherit Dom.element
  206. method id : js_string t prop
  207. method title : js_string t prop
  208. method lang : js_string t prop
  209. method dir : js_string t prop
  210. method className : js_string t prop
  211. method style : cssStyleDeclaration t prop
  212. method innerHTML : js_string t prop
  213. method clientLeft : int readonly_prop
  214. method clientTop : int readonly_prop
  215. method clientWidth : int readonly_prop
  216. method clientHeight : int readonly_prop
  217. method offsetLeft : int readonly_prop
  218. method offsetTop : int readonly_prop
  219. method offsetParent : element t opt readonly_prop
  220. method offsetWidth : int readonly_prop
  221. method offsetHeight : int readonly_prop
  222. method scrollLeft : int prop
  223. method scrollTop : int prop
  224. method scrollWidth : int prop
  225. method scrollHeight : int prop
  226. method getClientRects : clientRectList t meth
  227. method getBoundingClientRect : clientRect t meth
  228. method scrollIntoView: bool t -> unit meth
  229. inherit eventTarget
  230. end
  231. and clientRect = object
  232. method top : float t readonly_prop
  233. method right : float t readonly_prop
  234. method bottom : float t readonly_prop
  235. method left : float t readonly_prop
  236. method width : float t optdef readonly_prop
  237. method height : float t optdef readonly_prop
  238. end
  239. and clientRectList = object
  240. method length : int readonly_prop
  241. method item : int -> clientRect t optdef meth
  242. end
  243. let no_handler : ('a, 'b) event_listener = Js.null
  244. let window_event () : #event t = Js.Unsafe.variable "event"
  245. (* The function preventDefault must be called explicitely when
  246. using addEventListener... *)
  247. let handler f =
  248. Js.some (Js.wrap_callback
  249. (fun e ->
  250. Optdef.case e
  251. (fun () ->
  252. let e = window_event () in
  253. let res = f e in
  254. e##returnValue <- res; res)
  255. (fun e ->
  256. let res = f e in
  257. if not (Js.to_bool res) then
  258. (Js.Unsafe.coerce e)##preventDefault ();
  259. res)))
  260. let full_handler f =
  261. Js.some (Js.wrap_meth_callback
  262. (fun this e ->
  263. Optdef.case e
  264. (fun () ->
  265. let e = window_event () in
  266. let res = f this e in
  267. e##returnValue <- res; res)
  268. (fun e ->
  269. let res = f this e in
  270. if not (Js.to_bool res) then
  271. (Js.Unsafe.coerce e)##preventDefault ();
  272. res)))
  273. let invoke_handler
  274. (f : ('a, 'b) event_listener) (this : 'a) (event : 'b) : bool t =
  275. Js.Unsafe.call f this [|Js.Unsafe.inject event|]
  276. module Event = struct
  277. type 'a typ = js_string t
  278. let click = Js.string "click"
  279. let dblclick = Js.string "dblclick"
  280. let mousedown = Js.string "mousedown"
  281. let mouseup = Js.string "mouseup"
  282. let mouseover = Js.string "mouseover"
  283. let mousemove = Js.string "mousemove"
  284. let mouseout = Js.string "mouseout"
  285. let keypress = Js.string "keypress"
  286. let keydown = Js.string "keydown"
  287. let keyup = Js.string "keyup"
  288. let mousewheel = Js.string "mousewheel"
  289. let _DOMMouseScroll = Js.string "DOMMouseScroll"
  290. end
  291. type event_listener_id = unit -> unit
  292. let addEventListener (e : #eventTarget t) typ h capt =
  293. if (Js.Unsafe.coerce e)##addEventListener == Js.undefined then begin
  294. let ev = (Js.string "on")##concat(typ) in
  295. let callback = fun e -> Js.Unsafe.call (h, e, [||]) in
  296. (Js.Unsafe.coerce e)##attachEvent(ev, callback);
  297. fun () -> (Js.Unsafe.coerce e)##detachEvent(ev, callback)
  298. end else begin
  299. (Js.Unsafe.coerce e)##addEventListener(typ, h, capt);
  300. fun () -> (Js.Unsafe.coerce e)##removeEventListener (typ, h, capt)
  301. end
  302. let removeEventListener id = id ()
  303. class type ['node] collection = object
  304. method length : int readonly_prop
  305. method item : int -> 'node t optdef meth
  306. method namedItem : js_string t -> 'node t opt meth
  307. end
  308. class type htmlElement = element
  309. class type headElement = object
  310. inherit element
  311. method profile : js_string t prop
  312. end
  313. class type linkElement = object
  314. inherit element
  315. method disabled : bool t prop
  316. method charset : js_string t prop
  317. method href : js_string t prop
  318. method hreflang : js_string t prop
  319. method media : js_string t prop
  320. method rel : js_string t prop
  321. method rev : js_string t prop
  322. method target : js_string t prop
  323. method _type : js_string t prop
  324. end
  325. class type titleElement = object
  326. inherit element
  327. method text : js_string t prop
  328. end
  329. class type metaElement = object
  330. inherit element
  331. method content : js_string t prop
  332. method httpEquiv : js_string t prop
  333. method name : js_string t prop
  334. method scheme : js_string t prop
  335. end
  336. class type baseElement = object
  337. inherit element
  338. method href : js_string t prop
  339. method target : js_string t prop
  340. end
  341. class type styleElement = object
  342. inherit element
  343. method disabled : bool t prop
  344. method media : js_string t prop
  345. method _type : js_string t prop
  346. end
  347. class type bodyElement = element
  348. class type formElement = object
  349. inherit element
  350. method elements : element collection t readonly_prop
  351. method length : int readonly_prop
  352. method acceptCharset : js_string t prop
  353. method action : js_string t prop
  354. method enctype : js_string t prop
  355. method _method : js_string t prop
  356. method target : js_string t prop
  357. method submit : unit meth
  358. method reset : unit meth
  359. method onsubmit : ('self t, event t) event_listener writeonly_prop
  360. end
  361. class type optGroupElement = object
  362. inherit element
  363. method disabled : bool t prop
  364. method label : js_string t prop
  365. end
  366. class type optionElement = object
  367. inherit optGroupElement
  368. method form : formElement t opt readonly_prop
  369. method defaultSelected : bool t prop
  370. method text : js_string t readonly_prop
  371. method index : int readonly_prop
  372. method selected : bool prop
  373. method value : js_string t prop
  374. end
  375. class type selectElement = object ('self)
  376. inherit element
  377. method _type : js_string t readonly_prop
  378. method selectedIndex : int prop
  379. method value : js_string t prop
  380. method length : int prop
  381. method form : formElement t opt readonly_prop
  382. method options : optionElement collection t readonly_prop
  383. method disabled : bool t prop
  384. method multiple : bool t prop
  385. method name : js_string t readonly_prop
  386. method size : int prop
  387. method tabIndex : int prop
  388. method add : #optGroupElement t -> #optGroupElement t opt -> unit meth
  389. method remove : int -> unit meth
  390. method blur : unit meth
  391. method focus : unit meth
  392. method onchange : ('self t, event t) event_listener prop
  393. end
  394. class type inputElement = object ('self)
  395. inherit element
  396. method defaultValue : js_string t prop
  397. method defaultChecked : js_string t prop
  398. method form : formElement t opt readonly_prop
  399. method accept : js_string t prop
  400. method accessKey : js_string t prop
  401. method align : js_string t prop
  402. method alt : js_string t prop
  403. method checked : bool t prop
  404. method disabled : bool t prop
  405. method maxLength : int prop
  406. method name : js_string t readonly_prop
  407. method readOnly : bool t prop
  408. method size : int prop
  409. method src : js_string t prop
  410. method tabIndex : int prop
  411. method _type : js_string t readonly_prop
  412. method useMap : js_string t prop
  413. method value : js_string t prop
  414. method blur : unit meth
  415. method focus : unit meth
  416. method select : unit meth
  417. method click : unit meth
  418. method onselect : ('self t, event t) event_listener prop
  419. method onchange : ('self t, event t) event_listener prop
  420. end
  421. class type textAreaElement = object ('self)
  422. inherit element
  423. method defaultValue : js_string t prop
  424. method form : formElement t opt readonly_prop
  425. method accessKey : js_string t prop
  426. method cols : int prop
  427. method disabled : bool t prop
  428. method name : js_string t readonly_prop
  429. method readOnly : bool t prop
  430. method rows : int prop
  431. method tabIndex : int prop
  432. method _type : js_string t readonly_prop
  433. method value : js_string t prop
  434. method blur : unit meth
  435. method focus : unit meth
  436. method select : unit meth
  437. method onselect : ('self t, event t) event_listener prop
  438. method onchange : ('self t, event t) event_listener prop
  439. end
  440. class type buttonElement = object
  441. inherit element
  442. method form : formElement t opt readonly_prop
  443. method accessKey : js_string t prop
  444. method disabled : bool t prop
  445. method name : js_string t readonly_prop
  446. method tabIndex : int prop
  447. method _type : js_string t readonly_prop
  448. method value : js_string t prop
  449. end
  450. class type labelElement = object
  451. inherit element
  452. method form : formElement t opt readonly_prop
  453. method accessKey : js_string t prop
  454. method htmlFor : js_string t prop
  455. end
  456. class type fieldSetElement = object
  457. inherit element
  458. method form : formElement t opt readonly_prop
  459. end
  460. class type legendElement = object
  461. inherit element
  462. method form : formElement t opt readonly_prop
  463. method accessKey : js_string t prop
  464. end
  465. class type uListElement = element
  466. class type oListElement = element
  467. class type dListElement = element
  468. class type liElement = element
  469. class type divElement = element
  470. class type paragraphElement = element
  471. class type headingElement = element
  472. class type quoteElement = object
  473. inherit element
  474. method cite : js_string t prop
  475. end
  476. class type preElement = element
  477. class type brElement = element
  478. class type hrElement = element
  479. class type modElement = object
  480. inherit element
  481. method cite : js_string t prop
  482. method dateTime : js_string t prop
  483. end
  484. class type anchorElement = object
  485. inherit element
  486. method accessKey : js_string t prop
  487. method charset : js_string t prop
  488. method coords : js_string t prop
  489. method href : js_string t prop
  490. method hreflang : js_string t prop
  491. method name : js_string t prop
  492. method rel : js_string t prop
  493. method rev : js_string t prop
  494. method shape : js_string t prop
  495. method tabIndex : int prop
  496. method target : js_string t prop
  497. method _type : js_string t prop
  498. method blur : unit meth
  499. method focus : unit meth
  500. end
  501. class type imageElement = object ('self)
  502. inherit element
  503. method alt : js_string t prop
  504. method src : js_string t prop
  505. method useMap : js_string t prop
  506. method isMap : bool t prop
  507. method width : int prop
  508. method height : int prop
  509. method naturalWidth : int optdef readonly_prop
  510. method naturalHeight : int optdef readonly_prop
  511. method complete : bool t prop
  512. method onload : ('self t, event t) event_listener prop
  513. end
  514. class type objectElement = object
  515. inherit element
  516. method form : formElement t opt readonly_prop
  517. method code : js_string t prop
  518. method archive : js_string t prop
  519. method codeBase : js_string t prop
  520. method codeType : js_string t prop
  521. method data : js_string t prop
  522. method declare : bool t prop
  523. method height : js_string t prop
  524. method name : js_string t prop
  525. method standby : js_string t prop
  526. method tabIndex : int prop
  527. method _type : js_string t prop
  528. method useMap : js_string t prop
  529. method width : js_string t prop
  530. method document : Dom.element Dom.document t opt readonly_prop
  531. end
  532. class type paramElement = object
  533. inherit element
  534. method name : js_string t prop
  535. method _type : js_string t prop
  536. method value : js_string t prop
  537. method valueType : js_string t prop
  538. end
  539. class type areaElement = object
  540. inherit element
  541. method accessKey : js_string t prop
  542. method alt : js_string t prop
  543. method coords : js_string t prop
  544. method href : js_string t prop
  545. method noHref : bool t prop
  546. method shape : js_string t prop
  547. method tabIndex : int prop
  548. method target : js_string t prop
  549. end
  550. class type mapElement = object
  551. inherit element
  552. method areas : areaElement collection t readonly_prop
  553. method name : js_string t prop
  554. end
  555. class type scriptElement = object
  556. inherit element
  557. method text : js_string t prop
  558. method charset : js_string t prop
  559. method defer : bool t prop
  560. method src : js_string t prop
  561. method _type : js_string t prop
  562. end
  563. class type tableCellElement = object
  564. inherit element
  565. method cellIndex : int readonly_prop
  566. method abbr : js_string t prop
  567. method align : js_string t prop
  568. method axis : js_string t prop
  569. method ch : js_string t prop
  570. method chOff : js_string t prop
  571. method colSpan : int prop
  572. method headers : js_string t prop
  573. method rowSpan : int prop
  574. method scope : js_string t prop
  575. method vAlign : js_string t prop
  576. end
  577. class type tableRowElement = object
  578. inherit element
  579. method rowIndex : int readonly_prop
  580. method sectionRowIndex : int readonly_prop
  581. method cells : tableCellElement collection t readonly_prop
  582. method align : js_string t prop
  583. method ch : js_string t prop
  584. method chOff : js_string t prop
  585. method vAlign : js_string t prop
  586. method insertCell : int -> tableCellElement t meth
  587. method deleteCell : int -> unit meth
  588. end
  589. class type tableColElement = object
  590. inherit element
  591. method align : js_string t prop
  592. method ch : js_string t prop
  593. method chOff : js_string t prop
  594. method span : int prop
  595. method vAlign : js_string t prop
  596. method width : js_string t prop
  597. end
  598. class type tableSectionElement = object
  599. inherit element
  600. method align : js_string t prop
  601. method ch : js_string t prop
  602. method chOff : js_string t prop
  603. method vAlign : js_string t prop
  604. method rows : tableRowElement collection t readonly_prop
  605. method insertRow : int -> tableRowElement t meth
  606. method deleteRow : int -> unit meth
  607. end
  608. class type tableCaptionElement = element
  609. class type tableElement = object
  610. inherit element
  611. method caption : tableCaptionElement t prop
  612. method tHead : tableSectionElement t prop
  613. method tFood : tableSectionElement t prop
  614. method rows : tableRowElement collection t readonly_prop
  615. method tbodies : tableSectionElement collection t readonly_prop
  616. method align : js_string t prop
  617. method border : js_string t prop
  618. method cellPadding : js_string t prop
  619. method cellSpacing : js_string t prop
  620. method frame : js_string t prop
  621. method rules : js_string t prop
  622. method summary : js_string t prop
  623. method width : js_string t prop
  624. method createTHead : tableSectionElement t meth
  625. method deleteTHead : unit meth
  626. method createTFoot : tableSectionElement t meth
  627. method deleteTFoot : unit meth
  628. method createCaption : tableCaptionElement t meth
  629. method deleteCaption : unit meth
  630. method insertRow : int -> tableRowElement t meth
  631. method deleteRow : int -> unit meth
  632. end
  633. type context = js_string t
  634. let _2d_ = Js.string "2d"
  635. type canvasPattern
  636. class type canvasElement = object
  637. inherit element
  638. method width : int prop
  639. method height : int prop
  640. method toDataURL : js_string t meth
  641. method getContext : js_string t -> canvasRenderingContext2D t meth
  642. end
  643. and canvasRenderingContext2D = object
  644. method canvas : canvasElement t readonly_prop
  645. method save : unit meth
  646. method restore : unit meth
  647. method scale : float -> float -> unit meth
  648. method rotate : float -> unit meth
  649. method translate : float -> float -> unit meth
  650. method transform :
  651. float -> float -> float -> float -> float -> float -> unit meth
  652. method setTransform :
  653. float -> float -> float -> float -> float -> float -> unit meth
  654. method globalAlpha : float_prop
  655. method globalCompositeOperation : js_string t prop
  656. method strokeStyle : js_string t writeonly_prop
  657. method strokeStyle_gradient : canvasGradient t writeonly_prop
  658. method strokeStyle_pattern : canvasPattern t writeonly_prop
  659. method fillStyle : js_string t writeonly_prop
  660. method fillStyle_gradient : canvasGradient t writeonly_prop
  661. method fillStyle_pattern : canvasPattern t writeonly_prop
  662. method createLinearGradient :
  663. float -> float -> float -> float -> canvasGradient t meth
  664. method createRadialGradient :
  665. float -> float -> float -> float -> float -> float ->
  666. canvasGradient t meth
  667. method createPattern : imageElement t -> js_string t -> canvasPattern t meth
  668. method createPattern_fromCanvas :
  669. canvasElement t -> js_string t -> canvasPattern t meth
  670. (*
  671. CanvasPattern createPattern(in HTMLVideoElement image, in DOMJs_String repetition);
  672. *)
  673. method lineWidth : float_prop
  674. method lineCap : js_string t prop
  675. method lineJoin : js_string t prop
  676. method miterLimit : float_prop
  677. method shadowOffsetX : float_prop
  678. method shadowOffsetY : float_prop
  679. method shadowBlur : float_prop
  680. method shadowColor : js_string t prop
  681. method clearRect : float -> float -> float -> float -> unit meth
  682. method fillRect : float -> float -> float -> float -> unit meth
  683. method strokeRect : float -> float -> float -> float -> unit meth
  684. method beginPath : unit meth
  685. method closePath : unit meth
  686. method moveTo : float -> float -> unit meth
  687. method lineTo : float -> float -> unit meth
  688. method quadraticCurveTo : float -> float -> float -> float -> unit meth
  689. method bezierCurveTo :
  690. float -> float -> float -> float -> float -> float -> unit meth
  691. method arcTo : float -> float -> float -> float -> float -> unit meth
  692. method rect : float -> float -> float -> float -> unit meth
  693. method arc :
  694. float -> float -> float -> float -> float -> bool t -> unit meth
  695. method fill : unit meth
  696. method stroke : unit meth
  697. method clip : unit meth
  698. method isPointInPath : float -> float -> bool t meth
  699. method drawFocusRing : #element t -> float -> float -> bool t -> bool t meth
  700. method font : js_string t prop
  701. method textAlign : js_string t prop
  702. method textBaseline : js_string t prop
  703. method fillText : js_string t -> float -> float -> unit meth
  704. method fillText_withWidth :
  705. js_string t -> float -> float -> float -> unit meth
  706. method strokeText : js_string t -> float -> float -> unit meth
  707. method strokeText_withWidth :
  708. js_string t -> float -> float -> float -> unit meth
  709. method measureText : js_string t -> textMetrics t meth
  710. method drawImage :
  711. imageElement t -> float -> float -> unit meth
  712. method drawImage_withSize :
  713. imageElement t -> float -> float -> float -> float -> unit meth
  714. method drawImage_full :
  715. imageElement t -> float -> float -> float -> float ->
  716. float -> float -> float -> float -> unit meth
  717. method drawImage_fromCanvas :
  718. canvasElement t -> float -> float -> unit meth
  719. method drawImage_fromCanvasWithSize :
  720. canvasElement t -> float -> float -> float -> float -> unit meth
  721. method drawImage_fullFromCanvas :
  722. canvasElement t -> float -> float -> float -> float ->
  723. float -> float -> float -> float -> unit meth
  724. (*
  725. void drawImage(in HTMLVideoElement image, in float dx, in float dy, in optional float dw, in float dh);
  726. void drawImage(in HTMLVideoElement image, in float sx, in float sy, in float sw, in float sh, in float dx, in float dy, in float dw, in float dh);
  727. *)
  728. method createImageData : int -> int -> imageData t meth
  729. method getImageData : float -> float -> float -> float -> imageData t meth
  730. method putImageData : imageData t -> float -> float -> unit meth
  731. end
  732. and canvasGradient = object
  733. method addColorStop : float -> js_string t -> unit meth
  734. end
  735. and textMetrics = object
  736. method width : float readonly_prop
  737. end
  738. and imageData = object
  739. method width : int readonly_prop
  740. method height : int readonly_prop
  741. method data : canvasPixelArray t readonly_prop
  742. end
  743. and canvasPixelArray = object
  744. method length : int readonly_prop
  745. end
  746. external pixel_get : canvasPixelArray t -> int -> int = "caml_js_get"
  747. external pixel_set : canvasPixelArray t -> int -> int -> unit = "caml_js_set"
  748. class type document = object
  749. inherit [element] Dom.document
  750. method title : js_string t prop
  751. method referrer : js_string t readonly_prop
  752. method domain : js_string t readonly_prop
  753. method _URL : js_string t readonly_prop
  754. method head : headElement t prop
  755. method body : bodyElement t prop
  756. method documentElement : htmlElement t readonly_prop
  757. method images : imageElement collection t readonly_prop
  758. method applets : element collection t readonly_prop
  759. method links : element collection t readonly_prop
  760. method forms : formElement collection t readonly_prop
  761. method anchors : element collection t readonly_prop
  762. method cookie : js_string t prop
  763. method designMode : js_string t prop
  764. method open_ : unit meth
  765. method close : unit meth
  766. method write : js_string t -> unit meth
  767. method execCommand : js_string t -> bool t -> js_string t opt -> unit meth
  768. inherit eventTarget
  769. end
  770. type interval_id
  771. type timeout_id
  772. class type location = object
  773. method href : js_string t prop
  774. method protocol : js_string t prop
  775. method host : js_string t prop
  776. method hostname : js_string t prop
  777. method port : js_string t prop
  778. method pathname : js_string t prop
  779. method search : js_string t prop
  780. method hash : js_string t prop
  781. method assign : js_string t -> unit meth
  782. method replace : js_string t -> unit meth
  783. method reload : unit meth
  784. end
  785. class type history = object
  786. method length : int readonly_prop
  787. method state : Js.Unsafe.any readonly_prop
  788. method go : int opt -> unit meth
  789. method back : unit meth
  790. method forward : unit meth
  791. method pushState : Js.Unsafe.any -> js_string t -> js_string t opt -> unit meth
  792. method replaceState : Js.Unsafe.any -> js_string t -> js_string t opt -> unit meth
  793. end
  794. class type undoManager = object
  795. end
  796. class type selection = object
  797. end
  798. class type window = object
  799. method document : document t readonly_prop
  800. method name : js_string t prop
  801. method location : location t readonly_prop
  802. method history : history t readonly_prop
  803. method undoManager : undoManager t readonly_prop
  804. method getSelection : selection t meth
  805. method close : unit meth
  806. method stop : unit meth
  807. method focus : unit meth
  808. method blur : unit meth
  809. method scroll : int -> int -> unit meth
  810. method sessionStorage : storage t readonly_prop
  811. method localStorage : storage t readonly_prop
  812. method top : window t readonly_prop
  813. method parent : window t readonly_prop
  814. method frameElement : element t opt readonly_prop
  815. method alert : js_string t -> unit meth
  816. method confirm : js_string t -> bool t meth
  817. method prompt : js_string t -> js_string t -> js_string t meth
  818. method print : unit meth
  819. method setInterval : (unit -> unit) Js.callback -> float -> interval_id meth
  820. method clearInterval : interval_id -> unit meth
  821. method setTimeout : (unit -> unit) Js.callback -> float -> timeout_id meth
  822. method clearTimeout : timeout_id -> unit meth
  823. method onload : (window t, event t) event_listener prop
  824. method onbeforeunload : (window t, event t) event_listener prop
  825. method onblur : (window t, event t) event_listener prop
  826. method onfocus : (window t, event t) event_listener prop
  827. method onresize : (window t, event t) event_listener prop
  828. method onpopstate : (window t, popStateEvent t) event_listener prop
  829. method open_ : js_string t -> js_string t -> js_string t opt -> unit meth
  830. method close : unit meth
  831. end
  832. let window : window t = Js.Unsafe.variable "window"
  833. let document = window##document
  834. (****)
  835. class type frameSetElement = object
  836. inherit element
  837. method cols : js_string t prop
  838. method rows : js_string t prop
  839. end
  840. class type frameElement = object
  841. inherit element
  842. method frameBorder : js_string t prop
  843. method longDesc : js_string t prop
  844. method marginHeight : js_string t prop
  845. method marginWidth : js_string t prop
  846. method name : js_string t prop
  847. method noResize : bool t prop
  848. method scrolling : js_string t prop
  849. method src : js_string t prop
  850. method contentDocument : document t opt readonly_prop
  851. end
  852. class type iFrameElement = object
  853. inherit element
  854. method frameBorder : js_string t prop
  855. method height : js_string t prop
  856. method width : js_string t prop
  857. method longDesc : js_string t prop
  858. method marginHeight : js_string t prop
  859. method marginWidth : js_string t prop
  860. method name : js_string t prop
  861. method scrolling : js_string t prop
  862. method src : js_string t prop
  863. method contentDocument : document t opt readonly_prop
  864. method contentWindow : window t readonly_prop
  865. end
  866. (****)
  867. (*XXX Should provide creation functions a la lablgtk... *)
  868. let opt_iter x f = match x with None -> () | Some v -> f v
  869. let createElement (doc : document t) name = doc##createElement(Js.string name)
  870. let unsafeCreateElement doc name = Js.Unsafe.coerce (createElement doc name)
  871. let unsafeCreateElementEx ?_type ?name doc elt =
  872. if _type = None && name = None then
  873. Js.Unsafe.coerce (createElement doc elt)
  874. else if not onIE then begin
  875. let res = Js.Unsafe.coerce (createElement doc elt) in
  876. opt_iter _type (fun t -> res##_type <- t);
  877. opt_iter name (fun n -> res##name <- n);
  878. res
  879. end else begin
  880. let a = jsnew Js.array_empty () in
  881. ignore (a##push_2(Js.string "<", Js.string elt));
  882. opt_iter _type (fun t ->
  883. ignore (a##push_3(Js.string " type=\"", html_escape t, Js.string "\"")));
  884. opt_iter name (fun n ->
  885. ignore (a##push_3(Js.string " name=\"", html_escape n, Js.string "\"")));
  886. ignore (a##push(Js.string ">"));
  887. Js.Unsafe.coerce (doc##createElement (a##join (Js.string "")))
  888. end
  889. let createHtml doc : htmlElement t = unsafeCreateElement doc "html"
  890. let createHead doc : headElement t = unsafeCreateElement doc "head"
  891. let createLink doc : linkElement t = unsafeCreateElement doc "link"
  892. let createTitle doc : titleElement t = unsafeCreateElement doc "title"
  893. let createMeta doc : metaElement t = unsafeCreateElement doc "meta"
  894. let createBase doc : baseElement t = unsafeCreateElement doc "base"
  895. let createStyle doc : styleElement t = unsafeCreateElement doc "style"
  896. let createBody doc : bodyElement t = unsafeCreateElement doc "body"
  897. let createForm doc : formElement t = unsafeCreateElement doc "form"
  898. let createOptgroup doc : optGroupElement t = unsafeCreateElement doc "optgroup"
  899. let createOption doc : optionElement t = unsafeCreateElement doc "option"
  900. let createSelect ?_type ?name doc : selectElement t =
  901. unsafeCreateElementEx ?_type ?name doc "select"
  902. let createInput ?_type ?name doc : inputElement t =
  903. unsafeCreateElementEx ?_type ?name doc "input"
  904. let createTextarea ?_type ?name doc : textAreaElement t =
  905. unsafeCreateElementEx ?_type ?name doc "textarea"
  906. let createButton ?_type ?name doc : buttonElement t =
  907. unsafeCreateElementEx ?_type ?name doc "button"
  908. let createLabel doc : labelElement t = unsafeCreateElement doc "label"
  909. let createFieldset doc : fieldSetElement t = unsafeCreateElement doc "fieldset"
  910. let createLegend doc : legendElement t = unsafeCreateElement doc "legend"
  911. let createUl doc : uListElement t = unsafeCreateElement doc "ul"
  912. let createOl doc : oListElement t = unsafeCreateElement doc "ol"
  913. let createDl doc : dListElement t = unsafeCreateElement doc "dl"
  914. let createLi doc : liElement t = unsafeCreateElement doc "li"
  915. let createDiv doc : divElement t = unsafeCreateElement doc "div"
  916. let createP doc : paragraphElement t = unsafeCreateElement doc "p"
  917. let createH1 doc : headingElement t = unsafeCreateElement doc "h1"
  918. let createH2 doc : headingElement t = unsafeCreateElement doc "h2"
  919. let createH3 doc : headingElement t = unsafeCreateElement doc "h3"
  920. let createH4 doc : headingElement t = unsafeCreateElement doc "h4"
  921. let createH5 doc : headingElement t = unsafeCreateElement doc "h5"
  922. let createH6 doc : headingElement t = unsafeCreateElement doc "h6"
  923. let createQ doc : quoteElement t = unsafeCreateElement doc "q"
  924. let createBlockquote doc : quoteElement t =
  925. unsafeCreateElement doc "blockquote"
  926. let createPre doc : preElement t = unsafeCreateElement doc "pre"
  927. let createBr doc : brElement t = unsafeCreateElement doc "br"
  928. let createHr doc : hrElement t = unsafeCreateElement doc "hr"
  929. let createIns doc : modElement t = unsafeCreateElement doc "ins"
  930. let createDel doc : modElement t = unsafeCreateElement doc "del"
  931. let createA doc : anchorElement t = unsafeCreateElement doc "a"
  932. let createImg doc : imageElement t = unsafeCreateElement doc "img"
  933. let createObject doc : objectElement t = unsafeCreateElement doc "object"
  934. let createParam doc : paramElement t = unsafeCreateElement doc "param"
  935. let createMap doc : mapElement t = unsafeCreateElement doc "map"
  936. let createArea doc : areaElement t = unsafeCreateElement doc "area"
  937. let createScript doc : scriptElement t = unsafeCreateElement doc "script"
  938. let createTable doc : tableElement t = unsafeCreateElement doc "table"
  939. let createCaption doc : tableCaptionElement t =
  940. unsafeCreateElement doc "caption"
  941. let createCol doc : tableColElement t = unsafeCreateElement doc "col"
  942. let createColgroup doc : tableColElement t = unsafeCreateElement doc "colgroup"
  943. let createThead doc : tableSectionElement t = unsafeCreateElement doc "thead"
  944. let createTfoot doc : tableSectionElement t = unsafeCreateElement doc "tfoot"
  945. let createTbody doc : tableSectionElement t = unsafeCreateElement doc "tbody"
  946. let createTr doc : tableRowElement t = unsafeCreateElement doc "tr"
  947. let createTh doc : tableCellElement t = unsafeCreateElement doc "th"
  948. let createTd doc : tableCellElement t = unsafeCreateElement doc "td"
  949. let createSub doc = createElement doc "sub"
  950. let createSup doc = createElement doc "sup"
  951. let createSpan doc = createElement doc "span"
  952. let createTt doc = createElement doc "tt"
  953. let createI doc = createElement doc "i"
  954. let createB doc = createElement doc "b"
  955. let createBig doc = createElement doc "big"
  956. let createSmall doc = createElement doc "small"
  957. let createEm doc = createElement doc "em"
  958. let createStrong doc = createElement doc "strong"
  959. let createCite doc = createElement doc "cite"
  960. let createDfn doc = createElement doc "dfn"
  961. let createCode doc = createElement doc "code"
  962. let createSamp doc = createElement doc "samp"
  963. let createKbd doc = createElement doc "kbd"
  964. let createVar doc = createElement doc "var"
  965. let createAbbr doc = createElement doc "abbr"
  966. let createDd doc = createElement doc "dd"
  967. let createDt doc = createElement doc "dt"
  968. let createNoscript doc = createElement doc "noscript"
  969. let createAddress doc = createElement doc "address"
  970. let createFrameset doc : frameSetElement t = unsafeCreateElement doc "frameset"
  971. let createFrame doc : frameElement t = unsafeCreateElement doc "frame"
  972. let createIframe doc : iFrameElement t = unsafeCreateElement doc "iframe"
  973. exception Canvas_not_available
  974. let createCanvas doc : canvasElement t =
  975. let c = unsafeCreateElement doc "canvas" in
  976. if not (Opt.test c##getContext) then raise Canvas_not_available;
  977. c
  978. let html_element : htmlElement t constr = Js.Unsafe.variable "window.HTMLElement"
  979. module CoerceTo = struct
  980. let element : #Dom.node Js.t -> element Js.t Js.opt =
  981. if def html_element == undefined then
  982. (* ie < 9 does not have HTMLElement: we have to cheat to check
  983. that something is an html element *)
  984. (fun e ->
  985. if def ((Js.Unsafe.coerce e)##innerHTML) == undefined then
  986. Js.null
  987. else Js.some (Js.Unsafe.coerce e))
  988. else
  989. (fun e ->
  990. if Js.instanceof e html_element then
  991. Js.some (Js.Unsafe.coerce e)
  992. else Js.null)
  993. let unsafeCoerce tag (e : #element t) =
  994. if e##tagName##toLowerCase() == Js.string tag then
  995. Js.some (Js.Unsafe.coerce e)
  996. else
  997. Js.null
  998. let a e = unsafeCoerce "a" e
  999. let area e = unsafeCoerce "area" e
  1000. let base e = unsafeCoerce "base" e
  1001. let blockquote e = unsafeCoerce "blockquote" e
  1002. let body e = unsafeCoerce "body" e
  1003. let br e = unsafeCoerce "br" e
  1004. let button e = unsafeCoerce "button" e
  1005. let canvas e = unsafeCoerce "canvas" e
  1006. let caption e = unsafeCoerce "caption" e
  1007. let col e = unsafeCoerce "col" e
  1008. let colgroup e = unsafeCoerce "colgroup" e
  1009. let del e = unsafeCoerce "del" e
  1010. let div e = unsafeCoerce "div" e
  1011. let dl e = unsafeCoerce "dl" e
  1012. let fieldset e = unsafeCoerce "fieldset" e
  1013. let form e = unsafeCoerce "form" e
  1014. let frameset e = unsafeCoerce "frameset" e
  1015. let frame e = unsafeCoerce "frame" e
  1016. let h1 e = unsafeCoerce "h1" e
  1017. let h2 e = unsafeCoerce "h2" e
  1018. let h3 e = unsafeCoerce "h3" e
  1019. let h4 e = unsafeCoerce "h4" e
  1020. let h5 e = unsafeCoerce "h5" e
  1021. let h6 e = unsafeCoerce "h6" e
  1022. let head e = unsafeCoerce "head" e
  1023. let hr e = unsafeCoerce "hr" e
  1024. let html e = unsafeCoerce "html" e
  1025. let iframe e = unsafeCoerce "iframe" e
  1026. let img e = unsafeCoerce "img" e
  1027. let input e = unsafeCoerce "input" e
  1028. let ins e = unsafeCoerce "ins" e
  1029. let label e = unsafeCoerce "label" e
  1030. let legend e = unsafeCoerce "legend" e
  1031. let li e = unsafeCoerce "li" e
  1032. let link e = unsafeCoerce "link" e
  1033. let map e = unsafeCoerce "map" e
  1034. let meta e = unsafeCoerce "meta" e
  1035. let _object e = unsafeCoerce "object" e
  1036. let ol e = unsafeCoerce "ol" e
  1037. let optgroup e = unsafeCoerce "optgroup" e
  1038. let option e = unsafeCoerce "option" e
  1039. let p e = unsafeCoerce "p" e
  1040. let param e = unsafeCoerce "param" e
  1041. let pre e = unsafeCoerce "pre" e
  1042. let q e = unsafeCoerce "q" e
  1043. let script e = unsafeCoerce "script" e
  1044. let select e = unsafeCoerce "select" e
  1045. let style e = unsafeCoerce "style" e
  1046. let table e = unsafeCoerce "table" e
  1047. let tbody e = unsafeCoerce "tbody" e
  1048. let td e = unsafeCoerce "td" e
  1049. let textarea e = unsafeCoerce "textarea" e
  1050. let tfoot e = unsafeCoerce "tfoot" e
  1051. let th e = unsafeCoerce "th" e
  1052. let thead e = unsafeCoerce "thead" e
  1053. let title e = unsafeCoerce "title" e
  1054. let tr e = unsafeCoerce "tr" e
  1055. let ul e = unsafeCoerce "ul" e
  1056. let unsafeCoerceEvent name (ev : #event t) =
  1057. let constr = Js.Unsafe.variable name in
  1058. if def constr != undefined && Js.instanceof ev constr then
  1059. Js.some (Js.Unsafe.coerce ev)
  1060. else Js.null
  1061. let mouseEvent ev = unsafeCoerceEvent "window.MouseEvent" ev
  1062. let keyboardEvent ev = unsafeCoerceEvent "window.KeyboardEvent" ev
  1063. let wheelEvent ev = unsafeCoerceEvent "window.WheelEvent" ev
  1064. let mouseScrollEvent ev = unsafeCoerceEvent "window.MouseScrollEvent" ev
  1065. let popStateEvent ev = unsafeCoerceEvent "window.PopStateEvent" ev
  1066. end
  1067. (****)
  1068. let eventTarget (e : #event t) =
  1069. let target =
  1070. Optdef.get (e##target) (fun () ->
  1071. Optdef.get (e##srcElement) (fun () -> assert false))
  1072. in
  1073. (* Workaround for Safari bug *)
  1074. if target##nodeType == Dom.TEXT then
  1075. Js.Unsafe.coerce (Opt.get (target##parentNode) (fun () -> assert false))
  1076. else
  1077. target
  1078. let eventRelatedTarget (e : #mouseEvent t) =
  1079. Optdef.get (e##relatedTarget) (fun () ->
  1080. match Js.to_string (e##_type) with
  1081. "mouseover" -> Optdef.get (e##fromElement) (fun () -> assert false)
  1082. | "mouseout" -> Optdef.get (e##toElement) (fun () -> assert false)
  1083. | _ -> Js.null)
  1084. let eventAbsolutePosition' (e : #mouseEvent t) =
  1085. let body = document##body in
  1086. let html = document##documentElement in
  1087. (e##clientX + body##scrollLeft + html##scrollLeft,
  1088. e##clientY + body##scrollTop + html##scrollTop)
  1089. let eventAbsolutePosition (e : #mouseEvent t) =
  1090. Optdef.case (e##pageX) (fun () -> eventAbsolutePosition' e) (fun x ->
  1091. Optdef.case (e##pageY) (fun () -> eventAbsolutePosition' e) (fun y ->
  1092. (x, y)))
  1093. let elementClientPosition (e : #element t) =
  1094. let r = e##getBoundingClientRect () in
  1095. let body = document##body in
  1096. let html = document##documentElement in
  1097. (truncate (Js.to_float r##left) - body##clientLeft - html##clientLeft,
  1098. truncate (Js.to_float r##top) - body##clientTop - html##clientTop)
  1099. let getDocumentScroll () =
  1100. let body = document##body in
  1101. let html = document##documentElement in
  1102. (body##scrollLeft + html##scrollLeft, body##scrollTop + html##scrollTop)
  1103. let buttonPressed (ev : #mouseEvent Js.t) =
  1104. Js.Optdef.case (ev##which)
  1105. (fun () ->
  1106. match ev##button with
  1107. | 1 -> Left_button
  1108. | 2 -> Right_button
  1109. | 4 -> Middle_button
  1110. | _ -> No_button)
  1111. (fun x -> x)
  1112. let hasMousewheelEvents () =
  1113. let d = createDiv document in
  1114. d##setAttribute(Js.string "onmousewheel", Js.string "return;");
  1115. Js.typeof (Js.Unsafe.get d (Js.string "onmousewheel")) ==
  1116. Js.string "function"
  1117. let addMousewheelEventListener e h capt =
  1118. if hasMousewheelEvents () then
  1119. addEventListener e Event.mousewheel
  1120. (handler
  1121. (fun (e : wheelEvent t) ->
  1122. let dx = - Optdef.get (e##deltaX) (fun () -> 0) / 40 in
  1123. let dy =
  1124. - Optdef.get (e##deltaY) (fun () -> e##delta) / 40 in
  1125. h (e :> mouseEvent t) ~dx ~dy))
  1126. capt
  1127. else
  1128. addEventListener e Event._DOMMouseScroll
  1129. (handler
  1130. (fun (e : mouseScrollEvent t) ->
  1131. let d = e##detail in
  1132. if e##axis == e##_HORIZONTAL_AXIS then
  1133. h (e :> mouseEvent t) ~dx:d ~dy:0
  1134. else
  1135. h (e :> mouseEvent t) ~dx:0 ~dy:d))
  1136. capt
  1137. type taggedElement =
  1138. | A of anchorElement t
  1139. | Area of areaElement t
  1140. | Base of baseElement t
  1141. | Blockquote of quoteElement t
  1142. | Body of bodyElement t
  1143. | Br of brElement t
  1144. | Button of buttonElement t
  1145. | Canvas of canvasElement t
  1146. | Caption of tableCaptionElement t
  1147. | Col of tableColElement t
  1148. | Colgroup of tableColElement t
  1149. | Del of modElement t
  1150. | Div of divElement t
  1151. | Dl of dListElement t
  1152. | Fieldset of fieldSetElement t
  1153. | Form of formElement t
  1154. | Frameset of frameSetElement t
  1155. | Frame of frameElement t
  1156. | H1 of headingElement t
  1157. | H2 of headingElement t
  1158. | H3 of headingElement t
  1159. | H4 of headingElement t
  1160. | H5 of headingElement t
  1161. | H6 of headingElement t
  1162. | Head of headElement t
  1163. | Hr of hrElement t
  1164. | Html of htmlElement t
  1165. | Iframe of iFrameElement t
  1166. | Img of imageElement t
  1167. | Input of inputElement t
  1168. | Ins of modElement t
  1169. | Label of labelElement t
  1170. | Legend of legendElement t
  1171. | Li of liElement t
  1172. | Link of linkElement t
  1173. | Map of mapElement t
  1174. | Meta of metaElement t
  1175. | Object of objectElement t
  1176. | Ol of oListElement t
  1177. | Optgroup of optGroupElement t
  1178. | Option of optionElement t
  1179. | P of paramElement t
  1180. | Param of paramElement t
  1181. | Pre of preElement t
  1182. | Q of quoteElement t
  1183. | Script of scriptElement t
  1184. | Select of selectElement t
  1185. | Style of styleElement t
  1186. | Table of tableElement t
  1187. | Tbody of tableSectionElement t
  1188. | Td of tableColElement t
  1189. | Textarea of textAreaElement t
  1190. | Tfoot of tableSectionElement t
  1191. | Th of tableColElement t
  1192. | Thead of tableSectionElement t
  1193. | Title of titleElement t
  1194. | Tr of tableRowElement t
  1195. | Ul of uListElement t
  1196. | Other of element t
  1197. let tagged (e : #element t) =
  1198. match Js.to_string (e##tagName##toLowerCase()) with
  1199. | "a" -> A (Js.Unsafe.coerce e)
  1200. | "area" -> Area (Js.Unsafe.coerce e)
  1201. | "base" -> Base (Js.Unsafe.coerce e)
  1202. | "blockquote" -> Blockquote (Js.Unsafe.coerce e)
  1203. | "body" -> Body (Js.Unsafe.coerce e)
  1204. | "br" -> Br (Js.Unsafe.coerce e)
  1205. | "button" -> Button (Js.Unsafe.coerce e)
  1206. | "canvas" -> Canvas (Js.Unsafe.coerce e)
  1207. | "caption" -> Caption (Js.Unsafe.coerce e)
  1208. | "col" -> Col (Js.Unsafe.coerce e)
  1209. | "colgroup" -> Colgroup (Js.Unsafe.coerce e)
  1210. | "del" -> Del (Js.Unsafe.coerce e)
  1211. | "div" -> Div (Js.Unsafe.coerce e)
  1212. | "dl" -> Dl (Js.Unsafe.coerce e)
  1213. | "fieldset" -> Fieldset (Js.Unsafe.coerce e)
  1214. | "form" -> Form (Js.Unsafe.coerce e)
  1215. | "frameset" -> Frameset (Js.Unsafe.coerce e)
  1216. | "frame" -> Frame (Js.Unsafe.coerce e)
  1217. | "h1" -> H1 (Js.Unsafe.coerce e)
  1218. | "h2" -> H2 (Js.Unsafe.coerce e)
  1219. | "h3" -> H3 (Js.Unsafe.coerce e)
  1220. | "h4" -> H4 (Js.Unsafe.coerce e)
  1221. | "h5" -> H5 (Js.Unsafe.coerce e)
  1222. | "h6" -> H6 (Js.Unsafe.coerce e)
  1223. | "head" -> Head (Js.Unsafe.coerce e)
  1224. | "hr" -> Hr (Js.Unsafe.coerce e)
  1225. | "html" -> Html (Js.Unsafe.coerce e)
  1226. | "iframe" -> Iframe (Js.Unsafe.coerce e)
  1227. | "img" -> Img (Js.Unsafe.coerce e)
  1228. | "input" -> Input (Js.Unsafe.coerce e)
  1229. | "ins" -> Ins (Js.Unsafe.coerce e)
  1230. | "label" -> Label (Js.Unsafe.coerce e)
  1231. | "legend" -> Legend (Js.Unsafe.coerce e)
  1232. | "li" -> Li (Js.Unsafe.coerce e)
  1233. | "link" -> Link (Js.Unsafe.coerce e)
  1234. | "map" -> Map (Js.Unsafe.coerce e)
  1235. | "meta" -> Meta (Js.Unsafe.coerce e)
  1236. | "object" -> Object (Js.Unsafe.coerce e)
  1237. | "ol" -> Ol (Js.Unsafe.coerce e)
  1238. | "optgroup" -> Optgroup (Js.Unsafe.coerce e)
  1239. | "option" -> Option (Js.Unsafe.coerce e)
  1240. | "p" -> P (Js.Unsafe.coerce e)
  1241. | "param" -> Param (Js.Unsafe.coerce e)
  1242. | "pre" -> Pre (Js.Unsafe.coerce e)
  1243. | "q" -> Q (Js.Unsafe.coerce e)
  1244. | "script" -> Script (Js.Unsafe.coerce e)
  1245. | "select" -> Select (Js.Unsafe.coerce e)
  1246. | "style" -> Style (Js.Unsafe.coerce e)
  1247. | "table" -> Table (Js.Unsafe.coerce e)
  1248. | "tbody" -> Tbody (Js.Unsafe.coerce e)
  1249. | "td" -> Td (Js.Unsafe.coerce e)
  1250. | "textarea" -> Textarea (Js.Unsafe.coerce e)
  1251. | "tfoot" -> Tfoot (Js.Unsafe.coerce e)
  1252. | "th" -> Th (Js.Unsafe.coerce e)
  1253. | "thead" -> Thead (Js.Unsafe.coerce e)
  1254. | "title" -> Title (Js.Unsafe.coerce e)
  1255. | "tr" -> Tr (Js.Unsafe.coerce e)
  1256. | "ul" -> Ul (Js.Unsafe.coerce e)
  1257. | _ -> Other (e : #element t :> element t)
  1258. let opt_tagged e = Opt.case e (fun () -> None) (fun e -> Some (tagged e))
  1259. type taggedEvent =
  1260. | MouseEvent of mouseEvent t
  1261. | KeyboardEvent of keyboardEvent t
  1262. | WheelEvent of wheelEvent t
  1263. | MouseScrollEvent of mouseScrollEvent t
  1264. | PopStateEvent of popStateEvent t
  1265. | OtherEvent of event t
  1266. let taggedEvent (ev : #event Js.t) =
  1267. Js.Opt.case (CoerceTo.mouseEvent ev)
  1268. (fun () -> Js.Opt.case (CoerceTo.keyboardEvent ev)
  1269. (fun () -> Js.Opt.case (CoerceTo.wheelEvent ev)
  1270. (fun () -> Js.Opt.case (CoerceTo.mouseScrollEvent ev)
  1271. (fun () -> Js.Opt.case (CoerceTo.popStateEvent ev)
  1272. (fun () -> OtherEvent (ev :> event t))
  1273. (fun ev -> PopStateEvent ev))
  1274. (fun ev -> MouseScrollEvent ev))
  1275. (fun ev -> WheelEvent ev))
  1276. (fun ev -> KeyboardEvent ev))
  1277. (fun ev -> MouseEvent ev)
  1278. let opt_taggedEvent ev = Opt.case ev (fun () -> None) (fun ev -> Some (taggedEvent ev))
  1279. let stopPropagation ev =
  1280. let e = Js.Unsafe.coerce ev in
  1281. Optdef.case
  1282. (e##stopPropagation)
  1283. (fun () -> e##cancelBubble <- Js._true)
  1284. (fun _ -> e##_stopPropagation())