123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429(* Js_of_ocaml library
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2010 Jérôme Vouillon
* Laboratoire PPS - CNRS Université Paris Diderot
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)openJsopen!Importclasstype['node]nodeList=objectmethoditem:int->'nodetoptmethmethodlength:intreadonly_propendletlist_of_nodeList(nodeList:'anodeListt)=letlength=nodeList##.lengthinletrecadd_itemacci=ifi<lengththenmatchOpt.to_option(nodeList##itemi)with|None->add_itemacc(i+1)|Somee->add_item(e::acc)(i+1)elseList.revaccinadd_item[]0typenodeType=|OTHER(* Will not happen *)|ELEMENT|ATTRIBUTE|TEXT|CDATA_SECTION|ENTITY_REFERENCE|ENTITY|PROCESSING_INSTRUCTION|COMMENT|DOCUMENT|DOCUMENT_TYPE|DOCUMENT_FRAGMENT|NOTATIONmoduleDocumentPosition=structtypet=inttypemask=intletdisconnected=0x01letpreceding=0x02letfollowing=0x04letcontains=0x08letcontained_by=0x10letimplementation_specific=0x20lethastmask=tlandmask=maskletaddxy=xlorylet(+)=addendclasstypenode=objectmethodnodeName:js_stringtreadonly_propmethodnodeValue:js_stringtoptreadonly_propmethodnodeType:nodeTypereadonly_propmethodparentNode:nodetoptpropmethodchildNodes:nodenodeListtpropmethodfirstChild:nodetoptpropmethodlastChild:nodetoptpropmethodpreviousSibling:nodetoptpropmethodnextSibling:nodetoptpropmethodnamespaceURI:js_stringtoptpropmethodinsertBefore:nodet->nodetopt->nodetmethmethodreplaceChild:nodet->nodet->nodetmethmethodremoveChild:nodet->nodetmethmethodappendChild:nodet->nodetmethmethodhasChildNodes:booltmethmethodcloneNode:boolt->nodetmethmethodcompareDocumentPosition:nodet->DocumentPosition.tmethmethodlookupNamespaceURI:js_stringt->js_stringtoptmethmethodlookupPrefix:js_stringt->js_stringtoptmethendletappendChild(p:#nodet)(n:#nodet)=ignore(p##appendChild(n:>nodet))letremoveChild(p:#nodet)(n:#nodet)=ignore(p##removeChild(n:>nodet))letreplaceChild(p:#nodet)(n:#nodet)(o:#nodet)=ignore(p##replaceChild(n:>nodet)(o:>nodet))letinsertBefore(p:#nodet)(n:#nodet)(o:#nodetopt)=ignore(p##insertBefore(n:>nodet)(o:>nodetopt))(** Specification of [Attr] objects. *)classtypeattr=objectinheritnodemethodname:js_stringtreadonly_propmethodspecified:booltreadonly_propmethodvalue:js_stringtpropmethodownerElement:elementtpropend(** Specification of [NamedNodeMap] objects. *)and['node]namedNodeMap=objectmethodgetNamedItem:js_stringt->'nodetoptmethmethodsetNamedItem:'nodet->'nodetoptmethmethodremoveNamedItem:js_stringt->'nodetoptmethmethoditem:int->'nodetoptmethmethodlength:intreadonly_propend(** Specification of [Element] objects. *)andelement=objectinheritnodemethodtagName:js_stringtreadonly_propmethodgetAttribute:js_stringt->js_stringtoptmethmethodsetAttribute:js_stringt->js_stringt->unitmethmethodremoveAttribute:js_stringt->unitmethmethodhasAttribute:js_stringt->booltmethmethodgetAttributeNS:js_stringt->js_stringt->js_stringtoptmethmethodsetAttributeNS:js_stringt->js_stringt->js_stringt->unitmethmethodremoveAttributeNS:js_stringt->js_stringt->unitmethmethodhasAttributeNS:js_stringt->js_stringt->booltmethmethodgetAttributeNode:js_stringt->attrtoptmethmethodsetAttributeNode:attrt->attrtoptmethmethodremoveAttributeNode:attrt->attrtmethmethodgetAttributeNodeNS:js_stringt->js_stringt->attrtoptmethmethodsetAttributeNodeNS:attrt->attrtoptmethmethodgetElementsByTagName:js_stringt->elementnodeListtmethmethodattributes:attrnamedNodeMaptreadonly_propendclasstypecharacterData=objectinheritnodemethoddata:js_stringtpropmethodlength:intreadonly_propmethodsubjs_stringData:int->int->js_stringtmethmethodappendData:js_stringt->unitmethmethodinsertData:int->js_stringt->unitmethmethoddeleteData:int->int->unitmethmethodreplaceData:int->int->js_stringt->unitmethendclasstypecomment=characterDataclasstypetext=characterDataclasstypedocumentFragment=nodeclasstype['element]document=objectinheritnodemethoddocumentElement:'elementtreadonly_propmethodcreateDocumentFragment:documentFragmenttmethmethodcreateElement:js_stringt->'elementtmethmethodcreateElementNS:js_stringt->js_stringt->'elementtmethmethodcreateTextNode:js_stringt->texttmethmethodcreateAttribute:js_stringt->attrtmethmethodcreateComment:js_stringt->commenttmethmethodgetElementById:js_stringt->'elementtoptmethmethodgetElementsByTagName:js_stringt->'elementnodeListtmethmethodimportNode:elementt->boolt->'elementtmethmethodadoptNode:elementt->'elementtmethendtypenode_type=|Elementofelementt|Attrofattrt|Textoftextt|OtherofnodetletnodeTypee=matche##.nodeTypewith|ELEMENT->Element(Js.Unsafe.coercee)|ATTRIBUTE->Attr(Js.Unsafe.coercee)|CDATA_SECTION|TEXT->Text(Js.Unsafe.coercee)|_->Other(e:>nodet)moduleCoerceTo=structletcast(e:#nodeJs.t)t=ife##.nodeType==tthenJs.some(Js.Unsafe.coercee)elseJs.nullletelemente:elementJs.tJs.opt=casteELEMENTlettexte:textJs.tJs.opt=ife##.nodeType==TEXT||e##.nodeType==CDATA_SECTIONthenJs.some(Js.Unsafe.coercee)elseJs.nullletattre:attrJs.tJs.opt=casteATTRIBUTEendtype('a,'b)event_listener=('a,'b->boolt)meth_callbackopt(** The type of event listener functions. The first type parameter
['a] is the type of the target object; the second parameter
['b] is the type of the event object. *)classtype['a]event=objectmethod_type:js_stringtreadonly_propmethodtarget:'atoptreadonly_propmethodcurrentTarget:'atoptreadonly_prop(* Legacy methods *)methodsrcElement:'atoptreadonly_propendclasstype['a,'b]customEvent=objectinherit['a]eventmethoddetail:'bJs.optJs.readonly_propendletno_handler:('a,'b)event_listener=Js.nullletwindow_event():'a#eventt=Js.Unsafe.pure_js_expr"event"(* The function preventDefault must be called explicitly when
using addEventListener... *)lethandlerf=Js.some(Js.Unsafe.callback(fune->(* depending on the internet explorer version, e can be null or undefined. *)ifnot(Js.Opt.test(somee))then(lete=window_event()inletres=feinifnot(Js.to_boolres)thene##.returnValue:=res;res)elseletres=feinifnot(Js.to_boolres)then(Js.Unsafe.coercee)##preventDefault;res))letfull_handlerf=Js.some(Js.Unsafe.meth_callback(funthise->(* depending on the internet explorer version, e can be null or undefined *)ifnot(Js.Opt.test(somee))then(lete=window_event()inletres=fthiseinifnot(Js.to_boolres)thene##.returnValue:=res;res)elseletres=fthiseinifnot(Js.to_boolres)then(Js.Unsafe.coercee)##preventDefault;res))letinvoke_handler(f:('a,'b)event_listener)(this:'a)(event:'b):boolt=Js.Unsafe.callfthis[|Js.Unsafe.injectevent|]leteventTarget(e:(<..>as'a)#eventt):'at=lettarget=Opt.gete##.target(fun()->Opt.gete##.srcElement(fun()->raiseNot_found))inifJs.instanceoftargetJs.Unsafe.global##._Nodethen(* Workaround for Safari bug *)lettarget':nodeJs.t=Js.Unsafe.coercetargetiniftarget'##.nodeType==TEXTthenJs.Unsafe.coerce(Opt.gettarget'##.parentNode(fun()->assertfalse))elsetargetelsetargetmoduleEvent=structtype'atyp=Js.js_stringJs.tletmakes=Js.stringsendtypeevent_listener_id=unit->unitclasstypeevent_listener_options=objectmethodcapture:booltwriteonly_propmethodonce:booltwriteonly_propmethodpassive:booltwriteonly_propendletaddEventListenerWithOptions(e:(<..>as'a)t)typ?capture?once?passiveh=if(Js.Unsafe.coercee)##.addEventListener==Js.undefinedthenletev=(Js.string"on")##concattypinletcallbacke=Js.Unsafe.call(h,e,[||])inlet()=(Js.Unsafe.coercee)##attachEventevcallbackinfun()->(Js.Unsafe.coercee)##detachEventevcallbackelseletopts:event_listener_optionst=Js.Unsafe.obj[||]inletitertf=matchtwith|None->()|Someb->fbinitercapture(funb->opts##.capture:=b);iteronce(funb->opts##.once:=b);iterpassive(funb->opts##.passive:=b);let()=(Js.Unsafe.coercee)##addEventListenertyphoptsinfun()->(Js.Unsafe.coercee)##removeEventListenertyphoptsletaddEventListener(e:(<..>as'a)t)typhcapt=addEventListenerWithOptionsetyp~capture:capthletremoveEventListenerid=id()letpreventDefaultev=ifJs.Optdef.test(Js.Unsafe.coerceev)##.preventDefault(* IE hack *)then(Js.Unsafe.coerceev)##preventDefaultelse(Js.Unsafe.coerceev)##.returnValue:=Js.boolfalseletcreateCustomEvent?bubbles?cancelable?detailtyp=letopt_iterf=function|None->()|Somex->fxinletopts=Unsafe.obj[||]inopt_iter(funx->opts##.bubbles:=boolx)bubbles;opt_iter(funx->opts##.cancelable:=boolx)cancelable;opt_iter(funx->opts##.detail:=somex)detail;letconstr:(('a,'b)#customEventJs.tEvent.typ-><detail:'boptprop>t->('a,'b)customEventt)constr=Unsafe.global##._CustomEventinnew%jsconstrtypopts(* IE < 9 *)classtypestringList=objectmethoditem:int->js_stringtoptmethmethodlength:intreadonly_propmethodcontains:js_stringt->booltmethend