dom.go 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131
  1. // Package dom provides GopherJS bindings for the JavaScript DOM APIs.
  2. //
  3. // This package is an in progress effort of providing idiomatic Go
  4. // bindings for the DOM, wrapping the JavaScript DOM APIs. The API is
  5. // neither complete nor frozen yet, but a great amount of the DOM is
  6. // already useable.
  7. //
  8. // While the package tries to be idiomatic Go, it also tries to stick
  9. // closely to the JavaScript APIs, so that one does not need to learn
  10. // a new set of APIs if one is already familiar with it.
  11. //
  12. // One decision that hasn't been made yet is what parts exactly should
  13. // be part of this package. It is, for example, possible that the
  14. // canvas APIs will live in a separate package. On the other hand,
  15. // types such as StorageEvent (the event that gets fired when the
  16. // HTML5 storage area changes) will be part of this package, simply
  17. // due to how the DOM is structured – even if the actual storage APIs
  18. // might live in a separate package. This might require special care
  19. // to avoid circular dependencies.
  20. //
  21. // The documentation for some of the identifiers is based on the
  22. // MDN Web Docs by Mozilla Contributors (https://developer.mozilla.org/en-US/docs/Web/API),
  23. // licensed under CC-BY-SA 2.5 (https://creativecommons.org/licenses/by-sa/2.5/).
  24. //
  25. // # Getting started
  26. //
  27. // The usual entry point of using the dom package is by using the
  28. // GetWindow() function which will return a Window, from which you can
  29. // get things such as the current Document.
  30. //
  31. // # Interfaces
  32. //
  33. // The DOM has a big amount of different element and event types, but
  34. // they all follow three interfaces. All functions that work on or
  35. // return generic elements/events will return one of the three
  36. // interfaces Element, HTMLElement or Event. In these interface values
  37. // there will be concrete implementations, such as
  38. // HTMLParagraphElement or FocusEvent. It's also not unusual that
  39. // values of type Element also implement HTMLElement. In all cases,
  40. // type assertions can be used.
  41. //
  42. // Example:
  43. //
  44. // el := dom.GetWindow().Document().QuerySelector(".some-element")
  45. // htmlEl := el.(dom.HTMLElement)
  46. // pEl := el.(*dom.HTMLParagraphElement)
  47. //
  48. // # Live collections
  49. //
  50. // Several functions in the JavaScript DOM return "live"
  51. // collections of elements, that is collections that will be
  52. // automatically updated when elements get removed or added to the
  53. // DOM. Our bindings, however, return static slices of elements that,
  54. // once created, will not automatically reflect updates to the DOM.
  55. // This is primarily done so that slices can actually be used, as
  56. // opposed to a form of iterator, but also because we think that
  57. // magically changing data isn't Go's nature and that snapshots of
  58. // state are a lot easier to reason about.
  59. //
  60. // This does not, however, mean that all objects are snapshots.
  61. // Elements, events and generally objects that aren't slices or maps
  62. // are simple wrappers around JavaScript objects, and as such
  63. // attributes as well as method calls will always return the most
  64. // current data. To reflect this behaviour, these bindings use
  65. // pointers to make the semantics clear. Consider the following
  66. // example:
  67. //
  68. // d := dom.GetWindow().Document()
  69. // e1 := d.GetElementByID("my-element")
  70. // e2 := d.GetElementByID("my-element")
  71. //
  72. // e1.Class().SetString("some-class")
  73. // println(e1.Class().String() == e2.Class().String())
  74. //
  75. // The above example will print `true`.
  76. //
  77. // # DOMTokenList
  78. //
  79. // Some objects in the JS API have two versions of attributes, one
  80. // that returns a string and one that returns a DOMTokenList to ease
  81. // manipulation of string-delimited lists. Some other objects only
  82. // provide DOMTokenList, sometimes DOMSettableTokenList. To simplify
  83. // these bindings, only the DOMTokenList variant will be made
  84. // available, by the type TokenList. In cases where the string
  85. // attribute was the only way to completely replace the value, our
  86. // TokenList will provide Set([]string) and SetString(string) methods,
  87. // which will be able to accomplish the same. Additionally, our
  88. // TokenList will provide methods to convert it to strings and slices.
  89. //
  90. // # Backwards compatibility
  91. //
  92. // This package has a relatively stable API. However, there will be
  93. // backwards incompatible changes from time to time. This is because
  94. // the package isn't complete yet, as well as because the DOM is a
  95. // moving target, and APIs do change sometimes.
  96. //
  97. // While an attempt is made to reduce changing function signatures to
  98. // a minimum, it can't always be guaranteed. Sometimes mistakes in the
  99. // bindings are found that require changing arguments or return
  100. // values.
  101. //
  102. // Interfaces defined in this package may also change on a
  103. // semi-regular basis, as new methods are added to them. This happens
  104. // because the bindings aren't complete and can never really be, as
  105. // new features are added to the DOM.
  106. package dom // import "honnef.co/go/js/dom"
  107. import (
  108. "image"
  109. "image/color"
  110. "strings"
  111. "time"
  112. "github.com/gopherjs/gopherjs/js"
  113. )
  114. // toString returns the string representation of o. If o is nil or
  115. // undefined, the empty string will be returned instead.
  116. func toString(o *js.Object) string {
  117. if o == nil || o == js.Undefined {
  118. return ""
  119. }
  120. return o.String()
  121. }
  122. func callRecover(o *js.Object, fn string, args ...interface{}) (err error) {
  123. defer func() {
  124. e := recover()
  125. if e == nil {
  126. return
  127. }
  128. if panicErr, ok := e.(error); ok && panicErr != nil {
  129. err = panicErr
  130. } else {
  131. panic(e)
  132. }
  133. }()
  134. o.Call(fn, args...)
  135. return nil
  136. }
  137. func elementConstructor(o *js.Object) *js.Object {
  138. if n := o.Get("node"); n != js.Undefined {
  139. // Support elements wrapped in Polymer's DOM APIs.
  140. return n.Get("constructor")
  141. }
  142. return o.Get("constructor")
  143. }
  144. func arrayToObjects(o *js.Object) []*js.Object {
  145. var out []*js.Object
  146. for i := 0; i < o.Length(); i++ {
  147. out = append(out, o.Index(i))
  148. }
  149. return out
  150. }
  151. func nodeListToObjects(o *js.Object) []*js.Object {
  152. if o.Get("constructor") == js.Global.Get("Array") {
  153. // Support Polymer's DOM APIs, which uses Arrays instead of
  154. // NodeLists
  155. return arrayToObjects(o)
  156. }
  157. var out []*js.Object
  158. length := o.Get("length").Int()
  159. for i := 0; i < length; i++ {
  160. out = append(out, o.Call("item", i))
  161. }
  162. return out
  163. }
  164. func nodeListToNodes(o *js.Object) []Node {
  165. var out []Node
  166. for _, obj := range nodeListToObjects(o) {
  167. out = append(out, wrapNode(obj))
  168. }
  169. return out
  170. }
  171. func nodeListToElements(o *js.Object) []Element {
  172. var out []Element
  173. for _, obj := range nodeListToObjects(o) {
  174. out = append(out, wrapElement(obj))
  175. }
  176. return out
  177. }
  178. func nodeListToHTMLElements(o *js.Object) []HTMLElement {
  179. var out []HTMLElement
  180. for _, obj := range nodeListToObjects(o) {
  181. out = append(out, wrapHTMLElement(obj))
  182. }
  183. return out
  184. }
  185. func WrapDocument(o *js.Object) Document {
  186. return wrapDocument(o)
  187. }
  188. func WrapDocumentFragment(o *js.Object) DocumentFragment {
  189. return wrapDocumentFragment(o)
  190. }
  191. func WrapNode(o *js.Object) Node {
  192. return wrapNode(o)
  193. }
  194. func WrapElement(o *js.Object) Element {
  195. return wrapElement(o)
  196. }
  197. func WrapHTMLElement(o *js.Object) HTMLElement {
  198. return wrapHTMLElement(o)
  199. }
  200. func wrapDocument(o *js.Object) Document {
  201. switch elementConstructor(o) {
  202. case js.Global.Get("HTMLDocument"):
  203. return &htmlDocument{&document{&BasicNode{o}}}
  204. default:
  205. return &document{&BasicNode{o}}
  206. }
  207. }
  208. func wrapDocumentFragment(o *js.Object) DocumentFragment {
  209. switch elementConstructor(o) {
  210. // TODO: do we have any other stuff we want to check
  211. default:
  212. return &documentFragment{&BasicNode{o}}
  213. }
  214. }
  215. func wrapNode(o *js.Object) Node {
  216. if o == nil || o == js.Undefined {
  217. return nil
  218. }
  219. switch elementConstructor(o) {
  220. // TODO all the non-element cases
  221. case js.Global.Get("Text"):
  222. return &Text{&BasicNode{o}}
  223. default:
  224. return wrapElement(o)
  225. }
  226. }
  227. func wrapElement(o *js.Object) Element {
  228. if o == nil || o == js.Undefined {
  229. return nil
  230. }
  231. switch elementConstructor(o) {
  232. // TODO all the non-HTML cases
  233. default:
  234. return wrapHTMLElement(o)
  235. }
  236. }
  237. func wrapHTMLElement(o *js.Object) HTMLElement {
  238. if o == nil || o == js.Undefined {
  239. return nil
  240. }
  241. el := &BasicHTMLElement{&BasicElement{&BasicNode{o}}}
  242. c := elementConstructor(o)
  243. switch c {
  244. case js.Global.Get("HTMLAnchorElement"):
  245. return &HTMLAnchorElement{BasicHTMLElement: el, URLUtils: &URLUtils{Object: o}}
  246. case js.Global.Get("HTMLAppletElement"):
  247. return &HTMLAppletElement{BasicHTMLElement: el}
  248. case js.Global.Get("HTMLAreaElement"):
  249. return &HTMLAreaElement{BasicHTMLElement: el, URLUtils: &URLUtils{Object: o}}
  250. case js.Global.Get("HTMLAudioElement"):
  251. return &HTMLAudioElement{HTMLMediaElement: &HTMLMediaElement{BasicHTMLElement: el}}
  252. case js.Global.Get("HTMLBaseElement"):
  253. return &HTMLBaseElement{BasicHTMLElement: el}
  254. case js.Global.Get("HTMLBodyElement"):
  255. return &HTMLBodyElement{BasicHTMLElement: el}
  256. case js.Global.Get("HTMLBRElement"):
  257. return &HTMLBRElement{BasicHTMLElement: el}
  258. case js.Global.Get("HTMLButtonElement"):
  259. return &HTMLButtonElement{BasicHTMLElement: el}
  260. case js.Global.Get("HTMLCanvasElement"):
  261. return &HTMLCanvasElement{BasicHTMLElement: el}
  262. case js.Global.Get("HTMLDataElement"):
  263. return &HTMLDataElement{BasicHTMLElement: el}
  264. case js.Global.Get("HTMLDataListElement"):
  265. return &HTMLDataListElement{BasicHTMLElement: el}
  266. case js.Global.Get("HTMLDetailsElement"):
  267. return &HTMLDetailsElement{BasicHTMLElement: el}
  268. case js.Global.Get("HTMLDirectoryElement"):
  269. return &HTMLDirectoryElement{BasicHTMLElement: el}
  270. case js.Global.Get("HTMLDivElement"):
  271. return &HTMLDivElement{BasicHTMLElement: el}
  272. case js.Global.Get("HTMLDListElement"):
  273. return &HTMLDListElement{BasicHTMLElement: el}
  274. case js.Global.Get("HTMLEmbedElement"):
  275. return &HTMLEmbedElement{BasicHTMLElement: el}
  276. case js.Global.Get("HTMLFieldSetElement"):
  277. return &HTMLFieldSetElement{BasicHTMLElement: el}
  278. case js.Global.Get("HTMLFontElement"):
  279. return &HTMLFontElement{BasicHTMLElement: el}
  280. case js.Global.Get("HTMLFormElement"):
  281. return &HTMLFormElement{BasicHTMLElement: el}
  282. case js.Global.Get("HTMLFrameElement"):
  283. return &HTMLFrameElement{BasicHTMLElement: el}
  284. case js.Global.Get("HTMLFrameSetElement"):
  285. return &HTMLFrameSetElement{BasicHTMLElement: el}
  286. case js.Global.Get("HTMLHeadElement"):
  287. return &HTMLHeadElement{BasicHTMLElement: el}
  288. case js.Global.Get("HTMLHeadingElement"):
  289. return &HTMLHeadingElement{BasicHTMLElement: el}
  290. case js.Global.Get("HTMLHtmlElement"):
  291. return &HTMLHtmlElement{BasicHTMLElement: el}
  292. case js.Global.Get("HTMLHRElement"):
  293. return &HTMLHRElement{BasicHTMLElement: el}
  294. case js.Global.Get("HTMLIFrameElement"):
  295. return &HTMLIFrameElement{BasicHTMLElement: el}
  296. case js.Global.Get("HTMLImageElement"):
  297. return &HTMLImageElement{BasicHTMLElement: el}
  298. case js.Global.Get("HTMLInputElement"):
  299. return &HTMLInputElement{BasicHTMLElement: el}
  300. case js.Global.Get("HTMLKeygenElement"):
  301. return &HTMLKeygenElement{BasicHTMLElement: el}
  302. case js.Global.Get("HTMLLabelElement"):
  303. return &HTMLLabelElement{BasicHTMLElement: el}
  304. case js.Global.Get("HTMLLegendElement"):
  305. return &HTMLLegendElement{BasicHTMLElement: el}
  306. case js.Global.Get("HTMLLIElement"):
  307. return &HTMLLIElement{BasicHTMLElement: el}
  308. case js.Global.Get("HTMLLinkElement"):
  309. return &HTMLLinkElement{BasicHTMLElement: el}
  310. case js.Global.Get("HTMLMapElement"):
  311. return &HTMLMapElement{BasicHTMLElement: el}
  312. case js.Global.Get("HTMLMediaElement"):
  313. return &HTMLMediaElement{BasicHTMLElement: el}
  314. case js.Global.Get("HTMLMenuElement"):
  315. return &HTMLMenuElement{BasicHTMLElement: el}
  316. case js.Global.Get("HTMLMetaElement"):
  317. return &HTMLMetaElement{BasicHTMLElement: el}
  318. case js.Global.Get("HTMLMeterElement"):
  319. return &HTMLMeterElement{BasicHTMLElement: el}
  320. case js.Global.Get("HTMLModElement"):
  321. return &HTMLModElement{BasicHTMLElement: el}
  322. case js.Global.Get("HTMLObjectElement"):
  323. return &HTMLObjectElement{BasicHTMLElement: el}
  324. case js.Global.Get("HTMLOListElement"):
  325. return &HTMLOListElement{BasicHTMLElement: el}
  326. case js.Global.Get("HTMLOptGroupElement"):
  327. return &HTMLOptGroupElement{BasicHTMLElement: el}
  328. case js.Global.Get("HTMLOptionElement"):
  329. return &HTMLOptionElement{BasicHTMLElement: el}
  330. case js.Global.Get("HTMLOutputElement"):
  331. return &HTMLOutputElement{BasicHTMLElement: el}
  332. case js.Global.Get("HTMLParagraphElement"):
  333. return &HTMLParagraphElement{BasicHTMLElement: el}
  334. case js.Global.Get("HTMLParamElement"):
  335. return &HTMLParamElement{BasicHTMLElement: el}
  336. case js.Global.Get("HTMLPreElement"):
  337. return &HTMLPreElement{BasicHTMLElement: el}
  338. case js.Global.Get("HTMLProgressElement"):
  339. return &HTMLProgressElement{BasicHTMLElement: el}
  340. case js.Global.Get("HTMLQuoteElement"):
  341. return &HTMLQuoteElement{BasicHTMLElement: el}
  342. case js.Global.Get("HTMLScriptElement"):
  343. return &HTMLScriptElement{BasicHTMLElement: el}
  344. case js.Global.Get("HTMLSelectElement"):
  345. return &HTMLSelectElement{BasicHTMLElement: el}
  346. case js.Global.Get("HTMLSourceElement"):
  347. return &HTMLSourceElement{BasicHTMLElement: el}
  348. case js.Global.Get("HTMLSpanElement"):
  349. return &HTMLSpanElement{BasicHTMLElement: el}
  350. case js.Global.Get("HTMLStyleElement"):
  351. return &HTMLStyleElement{BasicHTMLElement: el}
  352. case js.Global.Get("HTMLTableElement"):
  353. return &HTMLTableElement{BasicHTMLElement: el}
  354. case js.Global.Get("HTMLTableCaptionElement"):
  355. return &HTMLTableCaptionElement{BasicHTMLElement: el}
  356. case js.Global.Get("HTMLTableCellElement"):
  357. return &HTMLTableCellElement{BasicHTMLElement: el}
  358. case js.Global.Get("HTMLTableDataCellElement"):
  359. return &HTMLTableDataCellElement{BasicHTMLElement: el}
  360. case js.Global.Get("HTMLTableHeaderCellElement"):
  361. return &HTMLTableHeaderCellElement{BasicHTMLElement: el}
  362. case js.Global.Get("HTMLTableColElement"):
  363. return &HTMLTableColElement{BasicHTMLElement: el}
  364. case js.Global.Get("HTMLTableRowElement"):
  365. return &HTMLTableRowElement{BasicHTMLElement: el}
  366. case js.Global.Get("HTMLTableSectionElement"):
  367. return &HTMLTableSectionElement{BasicHTMLElement: el}
  368. case js.Global.Get("HTMLTemplateElement"):
  369. return &HTMLTemplateElement{BasicHTMLElement: el}
  370. case js.Global.Get("HTMLTextAreaElement"):
  371. return &HTMLTextAreaElement{BasicHTMLElement: el}
  372. case js.Global.Get("HTMLTimeElement"):
  373. return &HTMLTimeElement{BasicHTMLElement: el}
  374. case js.Global.Get("HTMLTitleElement"):
  375. return &HTMLTitleElement{BasicHTMLElement: el}
  376. case js.Global.Get("HTMLTrackElement"):
  377. return &HTMLTrackElement{BasicHTMLElement: el}
  378. case js.Global.Get("HTMLUListElement"):
  379. return &HTMLUListElement{BasicHTMLElement: el}
  380. case js.Global.Get("HTMLUnknownElement"):
  381. return &HTMLUnknownElement{BasicHTMLElement: el}
  382. case js.Global.Get("HTMLVideoElement"):
  383. return &HTMLVideoElement{HTMLMediaElement: &HTMLMediaElement{BasicHTMLElement: el}}
  384. case js.Global.Get("HTMLElement"):
  385. return el
  386. default:
  387. return el
  388. }
  389. }
  390. func getForm(o *js.Object) *HTMLFormElement {
  391. form := wrapHTMLElement(o.Get("form"))
  392. if form == nil {
  393. return nil
  394. }
  395. return form.(*HTMLFormElement)
  396. }
  397. func getLabels(o *js.Object) []*HTMLLabelElement {
  398. labels := nodeListToElements(o.Get("labels"))
  399. out := make([]*HTMLLabelElement, len(labels))
  400. for i, label := range labels {
  401. out[i] = label.(*HTMLLabelElement)
  402. }
  403. return out
  404. }
  405. func getOptions(o *js.Object, attr string) []*HTMLOptionElement {
  406. options := nodeListToElements(o.Get(attr))
  407. out := make([]*HTMLOptionElement, len(options))
  408. for i, option := range options {
  409. out[i] = option.(*HTMLOptionElement)
  410. }
  411. return out
  412. }
  413. func GetWindow() Window {
  414. return &window{js.Global}
  415. }
  416. type TokenList struct {
  417. dtl *js.Object // the underlying DOMTokenList
  418. o *js.Object // the object to which the DOMTokenList belongs
  419. sa string // the name of the corresponding string attribute, empty if there isn't one
  420. Length int `js:"length"`
  421. }
  422. func (tl *TokenList) Item(idx int) string {
  423. o := tl.dtl.Call("item", idx)
  424. return toString(o)
  425. }
  426. func (tl *TokenList) Contains(token string) bool {
  427. return tl.dtl.Call("contains", token).Bool()
  428. }
  429. func (tl *TokenList) Add(token string) {
  430. tl.dtl.Call("add", token)
  431. }
  432. func (tl *TokenList) Remove(token string) {
  433. tl.dtl.Call("remove", token)
  434. }
  435. func (tl *TokenList) Toggle(token string) {
  436. tl.dtl.Call("toggle", token)
  437. }
  438. func (tl *TokenList) String() string {
  439. if tl.sa != "" {
  440. return tl.o.Get(tl.sa).String()
  441. }
  442. if tl.dtl.Get("constructor") == js.Global.Get("DOMSettableTokenList") {
  443. return tl.dtl.Get("value").String()
  444. }
  445. // We could manually construct the string, but I am not aware of
  446. // any case where we have neither a string attribute nor
  447. // DOMSettableTokenList.
  448. return ""
  449. }
  450. func (tl *TokenList) Slice() []string {
  451. var out []string
  452. length := tl.dtl.Get("length").Int()
  453. for i := 0; i < length; i++ {
  454. out = append(out, tl.dtl.Call("item", i).String())
  455. }
  456. return out
  457. }
  458. // SetString sets the TokenList's value to the space-separated list of
  459. // tokens in s.
  460. func (tl *TokenList) SetString(s string) {
  461. if tl.sa != "" {
  462. tl.o.Set(tl.sa, s)
  463. return
  464. }
  465. if tl.dtl.Get("constructor") == js.Global.Get("DOMSettableTokenList") {
  466. tl.dtl.Set("value", s)
  467. return
  468. }
  469. // This shouldn't be possible
  470. panic("no way to SetString on this TokenList")
  471. }
  472. // Set sets the TokenList's value to the list of tokens in s.
  473. //
  474. // Individual tokens in s shouldn't countain spaces.
  475. func (tl *TokenList) Set(s []string) {
  476. tl.SetString(strings.Join(s, " "))
  477. }
  478. type Document interface {
  479. Node
  480. ParentNode
  481. Async() bool
  482. SetAsync(bool)
  483. Doctype() DocumentType
  484. DocumentElement() Element
  485. DocumentURI() string
  486. Implementation() DOMImplementation
  487. LastStyleSheetSet() string
  488. PreferredStyleSheetSet() string // TODO correct type?
  489. SelectedStyleSheetSet() string // TODO correct type?
  490. StyleSheets() []StyleSheet // TODO s/StyleSheet/Stylesheet/ ?
  491. StyleSheetSets() []StyleSheet // TODO correct type?
  492. AdoptNode(node Node) Node
  493. ImportNode(node Node, deep bool) Node
  494. CreateElement(name string) Element
  495. CreateElementNS(namespace, name string) Element
  496. CreateTextNode(s string) *Text
  497. ElementFromPoint(x, y int) Element
  498. EnableStyleSheetsForSet(name string)
  499. GetElementsByClassName(name string) []Element
  500. GetElementsByTagName(name string) []Element
  501. GetElementsByTagNameNS(ns, name string) []Element
  502. GetElementByID(id string) Element
  503. QuerySelector(sel string) Element
  504. QuerySelectorAll(sel string) []Element
  505. CreateDocumentFragment() DocumentFragment
  506. }
  507. type DocumentFragment interface {
  508. Node
  509. ParentNode
  510. QuerySelector(sel string) Element
  511. QuerySelectorAll(sel string) []Element
  512. GetElementByID(id string) Element
  513. }
  514. type HTMLDocument interface {
  515. Document
  516. ActiveElement() HTMLElement
  517. Body() HTMLElement
  518. Cookie() string
  519. SetCookie(string)
  520. DefaultView() Window
  521. DesignMode() bool
  522. SetDesignMode(bool)
  523. Domain() string
  524. SetDomain(string)
  525. Forms() []*HTMLFormElement
  526. Head() *HTMLHeadElement
  527. Images() []*HTMLImageElement
  528. LastModified() time.Time
  529. Links() []HTMLElement
  530. Location() *Location
  531. Plugins() []*HTMLEmbedElement
  532. ReadyState() string
  533. Referrer() string
  534. Scripts() []*HTMLScriptElement
  535. Title() string
  536. SetTitle(string)
  537. URL() string
  538. // TODO HTMLDocument methods
  539. }
  540. type documentFragment struct {
  541. *BasicNode
  542. }
  543. func (d documentFragment) GetElementByID(id string) Element {
  544. return wrapElement(d.Call("getElementById", id))
  545. }
  546. func (d documentFragment) QuerySelector(sel string) Element {
  547. return (&BasicElement{&BasicNode{d.Object}}).QuerySelector(sel)
  548. }
  549. func (d documentFragment) QuerySelectorAll(sel string) []Element {
  550. return (&BasicElement{&BasicNode{d.Object}}).QuerySelectorAll(sel)
  551. }
  552. type document struct {
  553. *BasicNode
  554. }
  555. type htmlDocument struct {
  556. *document
  557. }
  558. func (d *htmlDocument) ActiveElement() HTMLElement {
  559. return wrapHTMLElement(d.Get("activeElement"))
  560. }
  561. func (d *htmlDocument) Body() HTMLElement {
  562. return wrapHTMLElement(d.Get("body"))
  563. }
  564. func (d *htmlDocument) Cookie() string {
  565. return d.Get("cookie").String()
  566. }
  567. func (d *htmlDocument) SetCookie(s string) {
  568. d.Set("cookie", s)
  569. }
  570. func (d *htmlDocument) DefaultView() Window {
  571. return &window{d.Get("defaultView")}
  572. }
  573. func (d *htmlDocument) DesignMode() bool {
  574. s := d.Get("designMode").String()
  575. return s != "off"
  576. }
  577. func (d *htmlDocument) SetDesignMode(b bool) {
  578. s := "off"
  579. if b {
  580. s = "on"
  581. }
  582. d.Set("designMode", s)
  583. }
  584. func (d *htmlDocument) Domain() string {
  585. return d.Get("domain").String()
  586. }
  587. func (d *htmlDocument) SetDomain(s string) {
  588. d.Set("domain", s)
  589. }
  590. func (d *htmlDocument) Forms() []*HTMLFormElement {
  591. var els []*HTMLFormElement
  592. forms := d.Get("forms")
  593. length := forms.Get("length").Int()
  594. for i := 0; i < length; i++ {
  595. els = append(els, wrapHTMLElement(forms.Call("item", i)).(*HTMLFormElement))
  596. }
  597. return els
  598. }
  599. func (d *htmlDocument) Head() *HTMLHeadElement {
  600. head := wrapElement(d.Get("head"))
  601. if head == nil {
  602. return nil
  603. }
  604. return head.(*HTMLHeadElement)
  605. }
  606. func (d *htmlDocument) Images() []*HTMLImageElement {
  607. var els []*HTMLImageElement
  608. images := d.Get("images")
  609. length := images.Get("length").Int()
  610. for i := 0; i < length; i++ {
  611. els = append(els, wrapHTMLElement(images.Call("item", i)).(*HTMLImageElement))
  612. }
  613. return els
  614. }
  615. func (d *htmlDocument) LastModified() time.Time {
  616. return d.Get("lastModified").Interface().(time.Time)
  617. }
  618. func (d *htmlDocument) Links() []HTMLElement {
  619. var els []HTMLElement
  620. links := d.Get("links")
  621. length := links.Get("length").Int()
  622. for i := 0; i < length; i++ {
  623. els = append(els, wrapHTMLElement(links.Call("item", i)))
  624. }
  625. return els
  626. }
  627. func (d *htmlDocument) Location() *Location {
  628. o := d.Get("location")
  629. return &Location{Object: o, URLUtils: &URLUtils{Object: o}}
  630. }
  631. func (d *htmlDocument) Plugins() []*HTMLEmbedElement {
  632. var els []*HTMLEmbedElement
  633. forms := d.Get("plugins")
  634. length := forms.Get("length").Int()
  635. for i := 0; i < length; i++ {
  636. els = append(els, wrapHTMLElement(forms.Call("item", i)).(*HTMLEmbedElement))
  637. }
  638. return els
  639. }
  640. func (d *htmlDocument) ReadyState() string {
  641. return d.Get("readyState").String()
  642. }
  643. func (d *htmlDocument) Referrer() string {
  644. return d.Get("referrer").String()
  645. }
  646. func (d *htmlDocument) Scripts() []*HTMLScriptElement {
  647. var els []*HTMLScriptElement
  648. forms := d.Get("scripts")
  649. length := forms.Get("length").Int()
  650. for i := 0; i < length; i++ {
  651. els = append(els, wrapHTMLElement(forms.Call("item", i)).(*HTMLScriptElement))
  652. }
  653. return els
  654. }
  655. func (d *htmlDocument) Title() string {
  656. return d.Get("title").String()
  657. }
  658. func (d *htmlDocument) SetTitle(s string) {
  659. d.Set("title", s)
  660. }
  661. func (d *htmlDocument) URL() string {
  662. return d.Get("URL").String()
  663. }
  664. func (d document) Async() bool {
  665. return d.Get("async").Bool()
  666. }
  667. func (d document) SetAsync(b bool) {
  668. d.Set("async", b)
  669. }
  670. func (d document) Doctype() DocumentType {
  671. // FIXME implement
  672. panic("not implemented")
  673. }
  674. func (d document) DocumentElement() Element {
  675. return wrapElement(d.Get("documentElement"))
  676. }
  677. func (d document) DocumentURI() string {
  678. return d.Get("documentURI").String()
  679. }
  680. func (d document) Implementation() DOMImplementation {
  681. // FIXME implement
  682. panic("not implemented")
  683. }
  684. func (d document) LastStyleSheetSet() string {
  685. return d.Get("lastStyleSheetSet").String()
  686. }
  687. func (d document) PreferredStyleSheetSet() string {
  688. return d.Get("preferredStyleSheetSet").String()
  689. }
  690. func (d document) SelectedStyleSheetSet() string {
  691. return d.Get("selectedStyleSheetSet").String()
  692. }
  693. func (d document) StyleSheets() []StyleSheet {
  694. // FIXME implement
  695. panic("not implemented")
  696. }
  697. func (d document) StyleSheetSets() []StyleSheet {
  698. // FIXME implement
  699. panic("not implemented")
  700. }
  701. func (d document) AdoptNode(node Node) Node {
  702. return wrapNode(d.Call("adoptNode", node.Underlying()))
  703. }
  704. func (d document) ImportNode(node Node, deep bool) Node {
  705. return wrapNode(d.Call("importNode", node.Underlying(), deep))
  706. }
  707. func (d document) CreateDocumentFragment() DocumentFragment {
  708. return wrapDocumentFragment(d.Call("createDocumentFragment"))
  709. }
  710. func (d document) CreateElement(name string) Element {
  711. return wrapElement(d.Call("createElement", name))
  712. }
  713. func (d document) CreateElementNS(ns string, name string) Element {
  714. return wrapElement(d.Call("createElementNS", ns, name))
  715. }
  716. func (d document) CreateTextNode(s string) *Text {
  717. return wrapNode(d.Call("createTextNode", s)).(*Text)
  718. }
  719. func (d document) ElementFromPoint(x, y int) Element {
  720. return wrapElement(d.Call("elementFromPoint", x, y))
  721. }
  722. func (d document) EnableStyleSheetsForSet(name string) {
  723. d.Call("enableStyleSheetsForSet", name)
  724. }
  725. func (d document) GetElementsByClassName(name string) []Element {
  726. return (&BasicElement{&BasicNode{d.Object}}).GetElementsByClassName(name)
  727. }
  728. func (d document) GetElementsByTagName(name string) []Element {
  729. return (&BasicElement{&BasicNode{d.Object}}).GetElementsByTagName(name)
  730. }
  731. func (d document) GetElementsByTagNameNS(ns, name string) []Element {
  732. return (&BasicElement{&BasicNode{d.Object}}).GetElementsByTagNameNS(ns, name)
  733. }
  734. func (d document) GetElementByID(id string) Element {
  735. return wrapElement(d.Call("getElementById", id))
  736. }
  737. func (d document) QuerySelector(sel string) Element {
  738. return (&BasicElement{&BasicNode{d.Object}}).QuerySelector(sel)
  739. }
  740. func (d document) QuerySelectorAll(sel string) []Element {
  741. return (&BasicElement{&BasicNode{d.Object}}).QuerySelectorAll(sel)
  742. }
  743. type URLUtils struct {
  744. *js.Object
  745. Href string `js:"href"`
  746. Protocol string `js:"protocol"`
  747. Host string `js:"host"`
  748. Hostname string `js:"hostname"`
  749. Port string `js:"port"`
  750. Pathname string `js:"pathname"`
  751. Search string `js:"search"`
  752. Hash string `js:"hash"`
  753. Username string `js:"username"`
  754. Password string `js:"password"`
  755. Origin string `js:"origin"`
  756. }
  757. // TODO Location methods
  758. type Location struct {
  759. *js.Object
  760. *URLUtils
  761. }
  762. type HTMLElement interface {
  763. Element
  764. GlobalEventHandlers
  765. AccessKey() string
  766. Dataset() map[string]string
  767. SetAccessKey(string)
  768. AccessKeyLabel() string
  769. SetAccessKeyLabel(string)
  770. ContentEditable() string
  771. SetContentEditable(string)
  772. IsContentEditable() bool
  773. Dir() string
  774. SetDir(string)
  775. Draggable() bool
  776. SetDraggable(bool)
  777. Lang() string
  778. SetLang(string)
  779. OffsetHeight() float64
  780. OffsetLeft() float64
  781. OffsetParent() HTMLElement
  782. OffsetTop() float64
  783. OffsetWidth() float64
  784. Style() *CSSStyleDeclaration
  785. Title() string
  786. SetTitle(string)
  787. Blur()
  788. Click()
  789. Focus()
  790. }
  791. type SVGElement interface {
  792. Element
  793. // TODO
  794. }
  795. type GlobalEventHandlers interface{}
  796. type Window interface {
  797. EventTarget
  798. Console() *Console
  799. Document() Document
  800. FrameElement() Element
  801. Location() *Location
  802. Name() string
  803. SetName(string)
  804. InnerHeight() int
  805. InnerWidth() int
  806. Length() int
  807. Opener() Window
  808. OuterHeight() int
  809. OuterWidth() int
  810. ScrollX() int
  811. ScrollY() int
  812. Parent() Window
  813. ScreenX() int
  814. ScreenY() int
  815. ScrollMaxX() int
  816. ScrollMaxY() int
  817. Top() Window
  818. History() History
  819. Navigator() Navigator
  820. Screen() *Screen
  821. Alert(string)
  822. Back()
  823. Blur()
  824. CancelAnimationFrame(int)
  825. ClearInterval(int)
  826. ClearTimeout(int)
  827. Close()
  828. Confirm(string) bool
  829. Focus()
  830. Forward()
  831. GetComputedStyle(el Element, pseudoElt string) *CSSStyleDeclaration
  832. GetSelection() Selection
  833. Home()
  834. MoveBy(dx, dy int)
  835. MoveTo(x, y int)
  836. Open(url, name, features string) Window
  837. OpenDialog(url, name, features string, args []interface{}) Window
  838. PostMessage(message string, target string, transfer []interface{})
  839. Print()
  840. Prompt(prompt string, initial string) string
  841. RequestAnimationFrame(callback func(time.Duration)) int
  842. ResizeBy(dw, dh int)
  843. ResizeTo(w, h int)
  844. Scroll(x, y int)
  845. ScrollBy(dx, dy int)
  846. ScrollByLines(int)
  847. ScrollTo(x, y int)
  848. SetCursor(name string)
  849. SetInterval(fn func(), delay int) int
  850. SetTimeout(fn func(), delay int) int
  851. Stop()
  852. // TODO constructors
  853. }
  854. type window struct {
  855. // TODO EventTarget
  856. *js.Object
  857. }
  858. func (w *window) Console() *Console {
  859. return &Console{w.Get("console")}
  860. }
  861. func (w *window) Document() Document {
  862. return wrapDocument(w.Get("document"))
  863. }
  864. func (w *window) FrameElement() Element {
  865. return wrapElement(w.Get("frameElement"))
  866. }
  867. func (w *window) Location() *Location {
  868. o := w.Get("location")
  869. return &Location{Object: o, URLUtils: &URLUtils{Object: o}}
  870. }
  871. func (w *window) Name() string {
  872. return w.Get("name").String()
  873. }
  874. func (w *window) SetName(s string) {
  875. w.Set("name", s)
  876. }
  877. func (w *window) InnerHeight() int {
  878. return w.Get("innerHeight").Int()
  879. }
  880. func (w *window) InnerWidth() int {
  881. return w.Get("innerWidth").Int()
  882. }
  883. func (w *window) Length() int {
  884. return w.Get("length").Int()
  885. }
  886. func (w *window) Opener() Window {
  887. return &window{w.Get("opener")}
  888. }
  889. func (w *window) OuterHeight() int {
  890. return w.Get("outerHeight").Int()
  891. }
  892. func (w *window) OuterWidth() int {
  893. return w.Get("outerWidth").Int()
  894. }
  895. func (w *window) ScrollX() int {
  896. return w.Get("scrollX").Int()
  897. }
  898. func (w *window) ScrollY() int {
  899. return w.Get("scrollY").Int()
  900. }
  901. func (w *window) Parent() Window {
  902. return &window{w.Get("parent")}
  903. }
  904. func (w *window) ScreenX() int {
  905. return w.Get("screenX").Int()
  906. }
  907. func (w *window) ScreenY() int {
  908. return w.Get("screenY").Int()
  909. }
  910. func (w *window) ScrollMaxX() int {
  911. return w.Get("scrollMaxX").Int()
  912. }
  913. func (w *window) ScrollMaxY() int {
  914. return w.Get("scrollMaxY").Int()
  915. }
  916. func (w *window) Top() Window {
  917. return &window{w.Get("top")}
  918. }
  919. func (w *window) History() History {
  920. // FIXME implement
  921. return nil
  922. }
  923. func (w *window) Navigator() Navigator {
  924. // FIXME implement
  925. panic("not implemented")
  926. }
  927. func (w *window) Screen() *Screen {
  928. return &Screen{Object: w.Get("screen")}
  929. }
  930. func (w *window) Alert(msg string) {
  931. w.Call("alert", msg)
  932. }
  933. func (w *window) Back() {
  934. w.Call("back")
  935. }
  936. func (w *window) Blur() {
  937. w.Call("blur")
  938. }
  939. func (w *window) ClearInterval(id int) {
  940. w.Call("clearInterval", id)
  941. }
  942. func (w *window) ClearTimeout(id int) {
  943. w.Call("clearTimeout", id)
  944. }
  945. func (w *window) Close() {
  946. w.Call("close")
  947. }
  948. func (w *window) Confirm(prompt string) bool {
  949. return w.Call("confirm", prompt).Bool()
  950. }
  951. func (w *window) Focus() {
  952. w.Call("focus")
  953. }
  954. func (w *window) Forward() {
  955. w.Call("forward")
  956. }
  957. // GetComputedStyle returns the values of all CSS properties of an
  958. // element after applying the active stylesheets. pseudoElt specifies
  959. // the pseudo-element to match. For normal elements, it must be the
  960. // empty string.
  961. func (w *window) GetComputedStyle(el Element, pseudoElt string) *CSSStyleDeclaration {
  962. var optArg interface{}
  963. if pseudoElt != "" {
  964. optArg = pseudoElt
  965. }
  966. return &CSSStyleDeclaration{w.Call("getComputedStyle", el.Underlying(), optArg)}
  967. }
  968. func (w *window) GetSelection() Selection {
  969. // FIXME implement
  970. panic("not implemented")
  971. }
  972. func (w *window) Home() {
  973. w.Call("home")
  974. }
  975. func (w *window) MoveBy(dx, dy int) {
  976. w.Call("moveBy", dx, dy)
  977. }
  978. func (w *window) MoveTo(x, y int) {
  979. w.Call("moveTo", x, y)
  980. }
  981. func (w *window) Open(url, name, features string) Window {
  982. return &window{w.Call("open", url, name, features)}
  983. }
  984. func (w *window) OpenDialog(url, name, features string, args []interface{}) Window {
  985. return &window{w.Call("openDialog", url, name, features, args)}
  986. }
  987. func (w *window) PostMessage(message string, target string, transfer []interface{}) {
  988. w.Call("postMessage", message, target, transfer)
  989. }
  990. func (w *window) Print() {
  991. w.Call("print")
  992. }
  993. func (w *window) Prompt(prompt string, initial string) string {
  994. return w.Call("prompt", prompt, initial).String()
  995. }
  996. func (w *window) ResizeBy(dw, dh int) {
  997. w.Call("resizeBy", dw, dh)
  998. }
  999. func (w *window) ResizeTo(width, height int) {
  1000. w.Call("resizeTo", width, height)
  1001. }
  1002. func (w *window) Scroll(x, y int) {
  1003. w.Call("scroll", x, y)
  1004. }
  1005. func (w *window) ScrollBy(dx, dy int) {
  1006. w.Call("scrollBy", dx, dy)
  1007. }
  1008. func (w *window) ScrollByLines(i int) {
  1009. w.Call("scrollByLines", i)
  1010. }
  1011. func (w *window) ScrollTo(x, y int) {
  1012. w.Call("scrollTo", x, y)
  1013. }
  1014. func (w *window) SetCursor(name string) {
  1015. w.Call("setCursor", name)
  1016. }
  1017. func (w *window) SetInterval(fn func(), delay int) int {
  1018. return w.Call("setInterval", fn, delay).Int()
  1019. }
  1020. func (w *window) SetTimeout(fn func(), delay int) int {
  1021. return w.Call("setTimeout", fn, delay).Int()
  1022. }
  1023. func (w *window) Stop() {
  1024. w.Call("stop")
  1025. }
  1026. // TODO reuse util.EventTarget
  1027. func (w *window) AddEventListener(typ string, useCapture bool, listener func(Event)) func(o *js.Object) {
  1028. wrapper := func(o *js.Object) { listener(wrapEvent(o)) }
  1029. w.Call("addEventListener", typ, wrapper, useCapture)
  1030. return wrapper
  1031. }
  1032. func (w *window) RemoveEventListener(typ string, useCapture bool, listener func(*js.Object)) {
  1033. w.Call("removeEventListener", typ, listener, useCapture)
  1034. }
  1035. func (w *window) DispatchEvent(event Event) bool {
  1036. return w.Call("dispatchEvent", event).Bool()
  1037. }
  1038. func wrapDOMHighResTimeStamp(o *js.Object) time.Duration {
  1039. return time.Duration(o.Float() * float64(time.Millisecond))
  1040. }
  1041. func (w *window) RequestAnimationFrame(callback func(time.Duration)) int {
  1042. wrapper := func(o *js.Object) { callback(wrapDOMHighResTimeStamp(o)) }
  1043. return w.Call("requestAnimationFrame", wrapper).Int()
  1044. }
  1045. func (w *window) CancelAnimationFrame(requestID int) {
  1046. w.Call("cancelAnimationFrame", requestID)
  1047. }
  1048. // TODO all the other window methods
  1049. type Selection interface {
  1050. // TODO
  1051. }
  1052. type Screen struct {
  1053. *js.Object
  1054. AvailTop int `js:"availTop"`
  1055. AvailLeft int `js:"availLeft"`
  1056. AvailHeight int `js:"availHeight"`
  1057. AvailWidth int `js:"availWidth"`
  1058. ColorDepth int `js:"colorDepth"`
  1059. Height int `js:"height"`
  1060. Left int `js:"left"`
  1061. PixelDepth int `js:"pixelDepth"`
  1062. Top int `js:"top"`
  1063. Width int `js:"width"`
  1064. }
  1065. func (s *Screen) Orientation() *ScreenOrientation { return &ScreenOrientation{s.Get("orientation")} }
  1066. type ScreenOrientation struct{ *js.Object }
  1067. func (so *ScreenOrientation) AddEventListener(typ string, useCapture bool, listener func(Event)) func(o *js.Object) {
  1068. wrapper := func(o *js.Object) { listener(wrapEvent(o)) }
  1069. so.Call("addEventListener", typ, wrapper, useCapture)
  1070. return wrapper
  1071. }
  1072. func (so *ScreenOrientation) RemoveEventListener(typ string, useCapture bool, listener func(*js.Object)) {
  1073. so.Call("removeEventListener", typ, listener, useCapture)
  1074. }
  1075. func (so *ScreenOrientation) DispatchEvent(event Event) bool {
  1076. return so.Call("dispatchEvent", event).Bool()
  1077. }
  1078. type Navigator interface {
  1079. NavigatorID
  1080. NavigatorLanguage
  1081. NavigatorOnLine
  1082. NavigatorGeolocation
  1083. // NavigatorPlugins
  1084. // NetworkInformation
  1085. CookieEnabled() bool
  1086. DoNotTrack() string
  1087. RegisterProtocolHandler(protocol, uri, title string)
  1088. }
  1089. type NavigatorID interface {
  1090. AppName() string
  1091. AppVersion() string
  1092. Platform() string
  1093. Product() string
  1094. UserAgent() string
  1095. }
  1096. type NavigatorLanguage interface {
  1097. Language() string
  1098. }
  1099. type NavigatorOnLine interface {
  1100. Online() bool
  1101. }
  1102. type NavigatorGeolocation interface {
  1103. Geolocation() Geolocation
  1104. }
  1105. type Geolocation interface {
  1106. // TODO wrap PositionOptions into something that uses the JS
  1107. // object
  1108. CurrentPosition(success func(Position), err func(PositionError), opts PositionOptions) Position
  1109. WatchPosition(success func(Position), err func(PositionError), opts PositionOptions) int
  1110. ClearWatch(int)
  1111. }
  1112. type PositionError struct {
  1113. *js.Object
  1114. Code int `js:"code"`
  1115. }
  1116. func (err *PositionError) Error() string {
  1117. return err.Call("message").String()
  1118. }
  1119. type PositionOptions struct {
  1120. EnableHighAccuracy bool
  1121. Timeout time.Duration
  1122. MaximumAge time.Duration
  1123. }
  1124. type Position struct {
  1125. Coords *Coordinates
  1126. Timestamp time.Time
  1127. }
  1128. type Coordinates struct {
  1129. *js.Object
  1130. Latitude float64 `js:"latitude"`
  1131. Longitude float64 `js:"longitude"`
  1132. Altitude float64 `js:"altitude"`
  1133. Accuracy float64 `js:"accuracy"`
  1134. AltitudeAccuracy float64 `js:"altitudeAccuracy"`
  1135. Heading float64 `js:"heading"`
  1136. Speed float64 `js:"speed"`
  1137. }
  1138. type History interface {
  1139. Length() int
  1140. State() interface{}
  1141. Back()
  1142. Forward()
  1143. Go(offset int)
  1144. PushState(state interface{}, title string, url string)
  1145. ReplaceState(state interface{}, title string, url string)
  1146. }
  1147. type Console struct {
  1148. *js.Object
  1149. // TODO will replace the js/console package
  1150. }
  1151. type SVGDocument interface{}
  1152. type DocumentType interface{}
  1153. type DOMImplementation interface{}
  1154. type StyleSheet interface{}
  1155. type CSSStyleSheet interface{}
  1156. type Node interface {
  1157. EventTarget
  1158. Underlying() *js.Object
  1159. BaseURI() string
  1160. ChildNodes() []Node
  1161. FirstChild() Node
  1162. LastChild() Node
  1163. NextSibling() Node
  1164. NodeName() string
  1165. NodeType() int
  1166. NodeValue() string
  1167. SetNodeValue(string)
  1168. OwnerDocument() Document
  1169. ParentNode() Node
  1170. ParentElement() Element
  1171. PreviousSibling() Node
  1172. TextContent() string
  1173. SetTextContent(string)
  1174. AppendChild(Node)
  1175. CloneNode(deep bool) Node
  1176. CompareDocumentPosition(Node) int
  1177. Contains(Node) bool
  1178. HasChildNodes() bool
  1179. InsertBefore(which Node, before Node)
  1180. IsDefaultNamespace(string) bool
  1181. IsEqualNode(Node) bool
  1182. LookupPrefix() string
  1183. LookupNamespaceURI(string) string
  1184. Normalize()
  1185. RemoveChild(Node)
  1186. ReplaceChild(newChild, oldChild Node)
  1187. }
  1188. // Type BasicNode implements the Node interface and is embedded by
  1189. // concrete node types and element types.
  1190. type BasicNode struct {
  1191. *js.Object
  1192. }
  1193. func (n *BasicNode) Underlying() *js.Object {
  1194. return n.Object
  1195. }
  1196. func (n *BasicNode) AddEventListener(typ string, useCapture bool, listener func(Event)) func(*js.Object) {
  1197. wrapper := func(o *js.Object) { listener(wrapEvent(o)) }
  1198. n.Call("addEventListener", typ, wrapper, useCapture)
  1199. return wrapper
  1200. }
  1201. func (n *BasicNode) RemoveEventListener(typ string, useCapture bool, listener func(*js.Object)) {
  1202. n.Call("removeEventListener", typ, listener, useCapture)
  1203. }
  1204. func (n *BasicNode) DispatchEvent(event Event) bool {
  1205. return n.Call("dispatchEvent", event).Bool()
  1206. }
  1207. func (n *BasicNode) BaseURI() string {
  1208. return n.Get("baseURI").String()
  1209. }
  1210. func (n *BasicNode) ChildNodes() []Node {
  1211. return nodeListToNodes(n.Get("childNodes"))
  1212. }
  1213. func (n *BasicNode) FirstChild() Node {
  1214. return wrapNode(n.Get("firstChild"))
  1215. }
  1216. func (n *BasicNode) LastChild() Node {
  1217. return wrapNode(n.Get("lastChild"))
  1218. }
  1219. func (n *BasicNode) NextSibling() Node {
  1220. return wrapNode(n.Get("nextSibling"))
  1221. }
  1222. func (n *BasicNode) NodeName() string {
  1223. return n.Get("nodeName").String()
  1224. }
  1225. func (n *BasicNode) NodeType() int {
  1226. return n.Get("nodeType").Int()
  1227. }
  1228. func (n *BasicNode) NodeValue() string {
  1229. return toString(n.Get("nodeValue"))
  1230. }
  1231. func (n *BasicNode) SetNodeValue(s string) {
  1232. n.Set("nodeValue", s)
  1233. }
  1234. func (n *BasicNode) OwnerDocument() Document {
  1235. // FIXME implement
  1236. panic("not implemented")
  1237. }
  1238. func (n *BasicNode) ParentNode() Node {
  1239. return wrapNode(n.Get("parentNode"))
  1240. }
  1241. func (n *BasicNode) ParentElement() Element {
  1242. return wrapElement(n.Get("parentElement"))
  1243. }
  1244. func (n *BasicNode) PreviousSibling() Node {
  1245. return wrapNode(n.Get("previousSibling"))
  1246. }
  1247. func (n *BasicNode) TextContent() string {
  1248. return toString(n.Get("textContent"))
  1249. }
  1250. func (n *BasicNode) SetTextContent(s string) {
  1251. n.Set("textContent", s)
  1252. }
  1253. func (n *BasicNode) AppendChild(newchild Node) {
  1254. n.Call("appendChild", newchild.Underlying())
  1255. }
  1256. func (n *BasicNode) CloneNode(deep bool) Node {
  1257. return wrapNode(n.Call("cloneNode", deep))
  1258. }
  1259. const (
  1260. DocumentPositionDisconnected = 1
  1261. DocumentPositionPreceding = 2
  1262. DocumentPositionFollowing = 4
  1263. DocumentPositionContains = 8
  1264. DocumentPositionContainedBy = 16
  1265. DocumentPositionImplementationSpecific = 32
  1266. )
  1267. func (n *BasicNode) CompareDocumentPosition(other Node) int {
  1268. return n.Call("compareDocumentPosition", other.Underlying()).Int()
  1269. }
  1270. func (n *BasicNode) Contains(other Node) bool {
  1271. return n.Call("contains", other.Underlying()).Bool()
  1272. }
  1273. func (n *BasicNode) HasChildNodes() bool {
  1274. return n.Call("hasChildNodes").Bool()
  1275. }
  1276. func (n *BasicNode) InsertBefore(which Node, before Node) {
  1277. var o interface{}
  1278. if before != nil {
  1279. o = before.Underlying()
  1280. }
  1281. n.Call("insertBefore", which.Underlying(), o)
  1282. }
  1283. func (n *BasicNode) IsDefaultNamespace(s string) bool {
  1284. return n.Call("isDefaultNamespace", s).Bool()
  1285. }
  1286. func (n *BasicNode) IsEqualNode(other Node) bool {
  1287. return n.Call("isEqualNode", other.Underlying()).Bool()
  1288. }
  1289. func (n *BasicNode) LookupPrefix() string {
  1290. return n.Call("lookupPrefix").String()
  1291. }
  1292. func (n *BasicNode) LookupNamespaceURI(s string) string {
  1293. return toString(n.Call("lookupNamespaceURI", s))
  1294. }
  1295. func (n *BasicNode) Normalize() {
  1296. n.Call("normalize")
  1297. }
  1298. func (n *BasicNode) RemoveChild(other Node) {
  1299. n.Call("removeChild", other.Underlying())
  1300. }
  1301. func (n *BasicNode) ReplaceChild(newChild, oldChild Node) {
  1302. n.Call("replaceChild", newChild.Underlying(), oldChild.Underlying())
  1303. }
  1304. type Element interface {
  1305. Node
  1306. ParentNode
  1307. ChildNode
  1308. Attributes() map[string]string
  1309. Class() *TokenList
  1310. Closest(string) Element
  1311. ID() string
  1312. SetID(string)
  1313. TagName() string
  1314. GetAttribute(string) string // TODO can attributes only be strings?
  1315. GetAttributeNS(ns string, name string) string // can attributes only be strings?
  1316. GetBoundingClientRect() ClientRect
  1317. GetElementsByClassName(string) []Element
  1318. GetElementsByTagName(string) []Element
  1319. GetElementsByTagNameNS(ns string, name string) []Element
  1320. HasAttribute(string) bool
  1321. HasAttributeNS(ns string, name string) bool
  1322. Matches(string) bool
  1323. QuerySelector(string) Element
  1324. QuerySelectorAll(string) []Element
  1325. Remove()
  1326. RemoveAttribute(string)
  1327. RemoveAttributeNS(ns string, name string)
  1328. SetAttribute(name string, value string)
  1329. SetAttributeNS(ns string, name string, value string)
  1330. InnerHTML() string
  1331. SetInnerHTML(string)
  1332. OuterHTML() string
  1333. SetOuterHTML(string)
  1334. }
  1335. type ClientRect struct {
  1336. *js.Object
  1337. Height float64 `js:"height"`
  1338. Width float64 `js:"width"`
  1339. Left float64 `js:"left"`
  1340. Right float64 `js:"right"`
  1341. Top float64 `js:"top"`
  1342. Bottom float64 `js:"bottom"`
  1343. }
  1344. type ParentNode interface {
  1345. // No properties/methods that aren't experimental
  1346. }
  1347. type ChildNode interface {
  1348. PreviousElementSibling() Element
  1349. NextElementSibling() Element
  1350. }
  1351. // Type BasicHTMLElement implements the HTMLElement interface and is
  1352. // embedded by concrete HTML element types.
  1353. type BasicHTMLElement struct {
  1354. *BasicElement
  1355. // TODO globalEventHandlers
  1356. }
  1357. func (e *BasicHTMLElement) AccessKey() string {
  1358. return e.Get("accessKey").String()
  1359. }
  1360. func (e *BasicHTMLElement) Dataset() map[string]string {
  1361. o := e.Get("dataset")
  1362. data := map[string]string{}
  1363. keys := js.Keys(o)
  1364. for _, key := range keys {
  1365. data[key] = o.Get(key).String()
  1366. }
  1367. return data
  1368. }
  1369. func (e *BasicHTMLElement) SetAccessKey(s string) {
  1370. e.Set("accessKey", s)
  1371. }
  1372. func (e *BasicHTMLElement) AccessKeyLabel() string {
  1373. return e.Get("accessKeyLabel").String()
  1374. }
  1375. func (e *BasicHTMLElement) SetAccessKeyLabel(s string) {
  1376. e.Set("accessKeyLabel", s)
  1377. }
  1378. func (e *BasicHTMLElement) ContentEditable() string {
  1379. return e.Get("contentEditable").String()
  1380. }
  1381. func (e *BasicHTMLElement) SetContentEditable(s string) {
  1382. e.Set("contentEditable", s)
  1383. }
  1384. func (e *BasicHTMLElement) IsContentEditable() bool {
  1385. return e.Get("isContentEditable").Bool()
  1386. }
  1387. func (e *BasicHTMLElement) Dir() string {
  1388. return e.Get("dir").String()
  1389. }
  1390. func (e *BasicHTMLElement) SetDir(s string) {
  1391. e.Set("dir", s)
  1392. }
  1393. func (e *BasicHTMLElement) Draggable() bool {
  1394. return e.Get("draggable").Bool()
  1395. }
  1396. func (e *BasicHTMLElement) SetDraggable(b bool) {
  1397. e.Set("draggable", b)
  1398. }
  1399. func (e *BasicHTMLElement) Lang() string {
  1400. return e.Get("lang").String()
  1401. }
  1402. func (e *BasicHTMLElement) SetLang(s string) {
  1403. e.Set("lang", s)
  1404. }
  1405. func (e *BasicHTMLElement) OffsetHeight() float64 {
  1406. return e.Get("offsetHeight").Float()
  1407. }
  1408. func (e *BasicHTMLElement) OffsetLeft() float64 {
  1409. return e.Get("offsetLeft").Float()
  1410. }
  1411. func (e *BasicHTMLElement) OffsetParent() HTMLElement {
  1412. return wrapHTMLElement(e.Get("offsetParent"))
  1413. }
  1414. func (e *BasicHTMLElement) OffsetTop() float64 {
  1415. return e.Get("offsetTop").Float()
  1416. }
  1417. func (e *BasicHTMLElement) OffsetWidth() float64 {
  1418. return e.Get("offsetWidth").Float()
  1419. }
  1420. func (e *BasicHTMLElement) Style() *CSSStyleDeclaration {
  1421. return &CSSStyleDeclaration{e.Get("style")}
  1422. }
  1423. func (e *BasicHTMLElement) TabIndex() int {
  1424. return e.Get("tabIndex").Int()
  1425. }
  1426. func (e *BasicHTMLElement) SetTabIndex(i int) {
  1427. e.Set("tabIndex", i)
  1428. }
  1429. func (e *BasicHTMLElement) Title() string {
  1430. return e.Get("title").String()
  1431. }
  1432. func (e *BasicHTMLElement) SetTitle(s string) {
  1433. e.Set("title", s)
  1434. }
  1435. func (e *BasicHTMLElement) Blur() {
  1436. e.Call("blur")
  1437. }
  1438. func (e *BasicHTMLElement) Click() {
  1439. e.Call("click")
  1440. }
  1441. func (e *BasicHTMLElement) Focus() {
  1442. e.Call("focus")
  1443. }
  1444. // Type BasicElement implements the Element interface and is embedded
  1445. // by concrete element types and HTML element types.
  1446. type BasicElement struct {
  1447. *BasicNode
  1448. }
  1449. func (e *BasicElement) Attributes() map[string]string {
  1450. o := e.Get("attributes")
  1451. attrs := map[string]string{}
  1452. length := o.Get("length").Int()
  1453. for i := 0; i < length; i++ {
  1454. item := o.Call("item", i)
  1455. attrs[item.Get("name").String()] = item.Get("value").String()
  1456. }
  1457. return attrs
  1458. }
  1459. func (e *BasicElement) GetBoundingClientRect() ClientRect {
  1460. obj := e.Call("getBoundingClientRect")
  1461. return ClientRect{Object: obj}
  1462. }
  1463. func (e *BasicElement) PreviousElementSibling() Element {
  1464. return wrapElement(e.Get("previousElementSibling"))
  1465. }
  1466. func (e *BasicElement) NextElementSibling() Element {
  1467. return wrapElement(e.Get("nextElementSibling"))
  1468. }
  1469. func (e *BasicElement) Class() *TokenList {
  1470. return &TokenList{dtl: e.Get("classList"), o: e.Object, sa: "className"}
  1471. }
  1472. // SetClass sets the element's className attribute to s. Consider
  1473. // using the Class method instead.
  1474. func (e *BasicElement) SetClass(s string) {
  1475. e.Set("className", s)
  1476. }
  1477. func (e *BasicElement) Closest(s string) Element {
  1478. return wrapElement(e.Call("closest", s))
  1479. }
  1480. func (e *BasicElement) ID() string {
  1481. return e.Get("id").String()
  1482. }
  1483. func (e *BasicElement) SetID(s string) {
  1484. e.Set("id", s)
  1485. }
  1486. func (e *BasicElement) TagName() string {
  1487. return e.Get("tagName").String()
  1488. }
  1489. func (e *BasicElement) GetAttribute(name string) string {
  1490. return toString(e.Call("getAttribute", name))
  1491. }
  1492. func (e *BasicElement) GetAttributeNS(ns string, name string) string {
  1493. return toString(e.Call("getAttributeNS", ns, name))
  1494. }
  1495. func (e *BasicElement) GetElementsByClassName(s string) []Element {
  1496. return nodeListToElements(e.Call("getElementsByClassName", s))
  1497. }
  1498. func (e *BasicElement) GetElementsByTagName(s string) []Element {
  1499. return nodeListToElements(e.Call("getElementsByTagName", s))
  1500. }
  1501. func (e *BasicElement) GetElementsByTagNameNS(ns string, name string) []Element {
  1502. return nodeListToElements(e.Call("getElementsByTagNameNS", ns, name))
  1503. }
  1504. func (e *BasicElement) HasAttribute(s string) bool {
  1505. return e.Call("hasAttribute", s).Bool()
  1506. }
  1507. func (e *BasicElement) HasAttributeNS(ns string, name string) bool {
  1508. return e.Call("hasAttributeNS", ns, name).Bool()
  1509. }
  1510. func (e *BasicElement) Matches(s string) bool {
  1511. return e.Call("matches", s).Bool()
  1512. }
  1513. func (e *BasicElement) QuerySelector(s string) Element {
  1514. return wrapElement(e.Call("querySelector", s))
  1515. }
  1516. func (e *BasicElement) QuerySelectorAll(s string) []Element {
  1517. return nodeListToElements(e.Call("querySelectorAll", s))
  1518. }
  1519. func (e *BasicElement) Remove() {
  1520. e.Call("remove")
  1521. }
  1522. func (e *BasicElement) RemoveAttribute(s string) {
  1523. e.Call("removeAttribute", s)
  1524. }
  1525. func (e *BasicElement) RemoveAttributeNS(ns string, name string) {
  1526. e.Call("removeAttributeNS", ns, name)
  1527. }
  1528. func (e *BasicElement) SetAttribute(name string, value string) {
  1529. e.Call("setAttribute", name, value)
  1530. }
  1531. func (e *BasicElement) SetAttributeNS(ns string, name string, value string) {
  1532. e.Call("setAttributeNS", ns, name, value)
  1533. }
  1534. func (e *BasicElement) InnerHTML() string {
  1535. return e.Get("innerHTML").String()
  1536. }
  1537. func (e *BasicElement) SetInnerHTML(s string) {
  1538. e.Set("innerHTML", s)
  1539. }
  1540. func (e *BasicElement) OuterHTML() string {
  1541. return e.Get("outerHTML").String()
  1542. }
  1543. func (e *BasicElement) SetOuterHTML(s string) {
  1544. e.Set("outerHTML", s)
  1545. }
  1546. type HTMLAnchorElement struct {
  1547. *BasicHTMLElement
  1548. *URLUtils
  1549. HrefLang string `js:"hreflang"`
  1550. Media string `js:"media"`
  1551. TabIndex int `js:"tabIndex"`
  1552. Target string `js:"target"`
  1553. Text string `js:"text"`
  1554. Type string `js:"type"`
  1555. }
  1556. func (e *HTMLAnchorElement) Rel() *TokenList {
  1557. return &TokenList{dtl: e.Get("relList"), o: e.Object, sa: "rel"}
  1558. }
  1559. type HTMLAppletElement struct {
  1560. *BasicHTMLElement
  1561. Alt string `js:"alt"`
  1562. Coords string `js:"coords"`
  1563. HrefLang string `js:"hreflang"`
  1564. Media string `js:"media"`
  1565. Search string `js:"search"`
  1566. Shape string `js:"shape"`
  1567. TabIndex int `js:"tabIndex"`
  1568. Target string `js:"target"`
  1569. Type string `js:"type"`
  1570. }
  1571. func (e *HTMLAppletElement) Rel() *TokenList {
  1572. return &TokenList{dtl: e.Get("relList"), o: e.Object, sa: "rel"}
  1573. }
  1574. type HTMLAreaElement struct {
  1575. *BasicHTMLElement
  1576. *URLUtils
  1577. Alt string `js:"alt"`
  1578. Coords string `js:"coords"`
  1579. HrefLang string `js:"hreflang"`
  1580. Media string `js:"media"`
  1581. Search string `js:"search"`
  1582. Shape string `js:"shape"`
  1583. TabIndex int `js:"tabIndex"`
  1584. Target string `js:"target"`
  1585. Type string `js:"type"`
  1586. }
  1587. func (e *HTMLAreaElement) Rel() *TokenList {
  1588. return &TokenList{dtl: e.Get("relList"), o: e.Object, sa: "rel"}
  1589. }
  1590. type HTMLAudioElement struct{ *HTMLMediaElement }
  1591. type HTMLBRElement struct{ *BasicHTMLElement }
  1592. type HTMLBaseElement struct{ *BasicHTMLElement }
  1593. func (e *HTMLBaseElement) Href() string {
  1594. return e.Get("href").String()
  1595. }
  1596. func (e *HTMLBaseElement) Target() string {
  1597. return e.Get("target").String()
  1598. }
  1599. type HTMLBodyElement struct{ *BasicHTMLElement }
  1600. type HTMLButtonElement struct {
  1601. *BasicHTMLElement
  1602. AutoFocus bool `js:"autofocus"`
  1603. Disabled bool `js:"disabled"`
  1604. FormAction string `js:"formAction"`
  1605. FormEncType string `js:"formEncType"`
  1606. FormMethod string `js:"formMethod"`
  1607. FormNoValidate bool `js:"formNoValidate"`
  1608. FormTarget string `js:"formTarget"`
  1609. Name string `js:"name"`
  1610. TabIndex int `js:"tabIndex"`
  1611. Type string `js:"type"`
  1612. ValidationMessage string `js:"validationMessage"`
  1613. Value string `js:"value"`
  1614. WillValidate bool `js:"willValidate"`
  1615. }
  1616. func (e *HTMLButtonElement) Form() *HTMLFormElement {
  1617. return getForm(e.Object)
  1618. }
  1619. func (e *HTMLButtonElement) Labels() []*HTMLLabelElement {
  1620. return getLabels(e.Object)
  1621. }
  1622. func (e *HTMLButtonElement) Validity() *ValidityState {
  1623. // TODO replace with a field once GopherJS supports that
  1624. return &ValidityState{Object: e.Get("validity")}
  1625. }
  1626. func (e *HTMLButtonElement) CheckValidity() bool {
  1627. return e.Call("checkValidity").Bool()
  1628. }
  1629. func (e *HTMLButtonElement) SetCustomValidity(s string) {
  1630. e.Call("setCustomValidity", s)
  1631. }
  1632. type HTMLCanvasElement struct {
  1633. *BasicHTMLElement
  1634. Height int `js:"height"`
  1635. Width int `js:"width"`
  1636. }
  1637. type CanvasRenderingContext2D struct {
  1638. *js.Object
  1639. // Colors, Styles, and Shadows
  1640. FillStyle string `js:"fillStyle"`
  1641. StrokeStyle string `js:"strokeStyle"`
  1642. ShadowColor string `js:"shadowColor"`
  1643. ShadowBlur int `js:"shadowBlur"`
  1644. ShadowOffsetX int `js:"shadowOffsetX"`
  1645. ShadowOffsetY int `js:"shadowOffsetY"`
  1646. // Line Styles
  1647. LineCap string `js:"lineCap"`
  1648. LineJoin string `js:"lineJoin"`
  1649. LineWidth int `js:"lineWidth"`
  1650. MiterLimit int `js:"miterLimit"`
  1651. // Text
  1652. Font string `js:"font"`
  1653. TextAlign string `js:"textAlign"`
  1654. TextBaseline string `js:"textBaseline"`
  1655. // Compositing
  1656. GlobalAlpha float64 `js:"globalAlpha"`
  1657. GlobalCompositeOperation string `js:"globalCompositeOperation"`
  1658. }
  1659. type ImageData struct {
  1660. *js.Object
  1661. Width int `js:"width"`
  1662. Height int `js:"height"`
  1663. Data *js.Object `js:"data"`
  1664. }
  1665. func (m *ImageData) ColorModel() color.Model { return color.NRGBAModel }
  1666. func (m *ImageData) Bounds() image.Rectangle {
  1667. return image.Rect(0, 0, m.Width, m.Height)
  1668. }
  1669. func (m *ImageData) At(x, y int) color.Color {
  1670. return m.NRGBAAt(x, y)
  1671. }
  1672. func (m *ImageData) NRGBAAt(x, y int) color.NRGBA {
  1673. if x < 0 || x >= m.Width ||
  1674. y < 0 || y >= m.Height {
  1675. return color.NRGBA{}
  1676. }
  1677. i := (y*m.Width + x) * 4
  1678. return color.NRGBA{
  1679. R: uint8(m.Data.Index(i + 0).Int()),
  1680. G: uint8(m.Data.Index(i + 1).Int()),
  1681. B: uint8(m.Data.Index(i + 2).Int()),
  1682. A: uint8(m.Data.Index(i + 3).Int()),
  1683. }
  1684. }
  1685. func (m *ImageData) Set(x, y int, c color.Color) {
  1686. if x < 0 || x >= m.Width ||
  1687. y < 0 || y >= m.Height {
  1688. return
  1689. }
  1690. c1 := color.NRGBAModel.Convert(c).(color.NRGBA)
  1691. i := (y*m.Width + x) * 4
  1692. m.Data.SetIndex(i+0, c1.R)
  1693. m.Data.SetIndex(i+1, c1.G)
  1694. m.Data.SetIndex(i+2, c1.B)
  1695. m.Data.SetIndex(i+3, c1.A)
  1696. }
  1697. func (m *ImageData) SetNRGBA(x, y int, c color.NRGBA) {
  1698. if x < 0 || x >= m.Width ||
  1699. y < 0 || y >= m.Height {
  1700. return
  1701. }
  1702. i := (y*m.Width + x) * 4
  1703. m.Data.SetIndex(i+0, c.R)
  1704. m.Data.SetIndex(i+1, c.G)
  1705. m.Data.SetIndex(i+2, c.B)
  1706. m.Data.SetIndex(i+3, c.A)
  1707. }
  1708. // CanvasGradient represents an opaque object describing a gradient.
  1709. // It is returned by the methods CanvasRenderingContext2D.CreateLinearGradient
  1710. // or CanvasRenderingContext2D.CreateRadialGradient.
  1711. //
  1712. // Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient.
  1713. type CanvasGradient struct {
  1714. *js.Object
  1715. }
  1716. // AddColorStop adds a new stop, defined by an offset and a color, to the gradient.
  1717. // It panics with *js.Error if the offset is not between 0 and 1, or if the color
  1718. // can't be parsed as a CSS <color>.
  1719. //
  1720. // Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient/addColorStop.
  1721. func (cg *CanvasGradient) AddColorStop(offset float64, color string) {
  1722. cg.Call("addColorStop", offset, color)
  1723. }
  1724. // CanvasPattern represents an opaque object describing a pattern.
  1725. // It is based on an image, a canvas or a video, created by the
  1726. // CanvasRenderingContext2D.CreatePattern method.
  1727. //
  1728. // Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasPattern.
  1729. type CanvasPattern struct {
  1730. *js.Object
  1731. }
  1732. type TextMetrics struct {
  1733. *js.Object
  1734. Width float64 `js:"width"`
  1735. ActualBoundingBoxLeft float64 `js:"actualBoundingBoxLeft"`
  1736. ActualBoundingBoxRight float64 `js:"actualBoundingBoxRight"`
  1737. FontBoundingBoxAscent float64 `js:"fontBoundingBoxAscent"`
  1738. FontBoundingBoxDescent float64 `js:"fontBoundingBoxDescent"`
  1739. ActualBoundingBoxAscent float64 `js:"actualBoundingBoxAscent"`
  1740. ActualBoundingBoxDescent float64 `js:"actualBoundingBoxDescent"`
  1741. EmHeightAscent float64 `js:"emHeightAscent"`
  1742. EmHeightDescent float64 `js:"emHeightDescent"`
  1743. HangingBaseline float64 `js:"hangingBaseline"`
  1744. AlphabeticBaseline float64 `js:"alphabeticBaseline"`
  1745. IdeographicBaseline float64 `js:"ideographicBaseline"`
  1746. }
  1747. // Creating canvas 2d context
  1748. func (e *HTMLCanvasElement) GetContext2d() *CanvasRenderingContext2D {
  1749. ctx := e.GetContext("2d")
  1750. return &CanvasRenderingContext2D{Object: ctx}
  1751. }
  1752. func (e *HTMLCanvasElement) GetContext(param string) *js.Object {
  1753. return e.Call("getContext", param)
  1754. }
  1755. // Drawing Rectangles
  1756. func (ctx *CanvasRenderingContext2D) ClearRect(x, y, width, height float64) {
  1757. ctx.Call("clearRect", x, y, width, height)
  1758. }
  1759. func (ctx *CanvasRenderingContext2D) FillRect(x, y, width, height float64) {
  1760. ctx.Call("fillRect", x, y, width, height)
  1761. }
  1762. func (ctx *CanvasRenderingContext2D) StrokeRect(x, y, width, height float64) {
  1763. ctx.Call("strokeRect", x, y, width, height)
  1764. }
  1765. // Drawing Text
  1766. // FillText fills a given text at the given (x, y) position.
  1767. // If the optional maxWidth parameter is not -1,
  1768. // the text will be scaled to fit that width.
  1769. func (ctx *CanvasRenderingContext2D) FillText(text string, x, y, maxWidth float64) {
  1770. if maxWidth == -1 {
  1771. ctx.Call("fillText", text, x, y)
  1772. return
  1773. }
  1774. ctx.Call("fillText", text, x, y, maxWidth)
  1775. }
  1776. // StrokeText strokes a given text at the given (x, y) position.
  1777. // If the optional maxWidth parameter is not -1,
  1778. // the text will be scaled to fit that width.
  1779. func (ctx *CanvasRenderingContext2D) StrokeText(text string, x, y, maxWidth float64) {
  1780. if maxWidth == -1 {
  1781. ctx.Call("strokeText", text, x, y)
  1782. return
  1783. }
  1784. ctx.Call("strokeText", text, x, y, maxWidth)
  1785. }
  1786. func (ctx *CanvasRenderingContext2D) MeasureText(text string) *TextMetrics {
  1787. textMetrics := ctx.Call("measureText", text)
  1788. return &TextMetrics{Object: textMetrics}
  1789. }
  1790. // Line styles
  1791. func (ctx *CanvasRenderingContext2D) GetLineDash() []float64 {
  1792. var dashes []float64
  1793. for _, dash := range ctx.Call("getLineDash").Interface().([]interface{}) {
  1794. dashes = append(dashes, dash.(float64))
  1795. }
  1796. return dashes
  1797. }
  1798. func (ctx *CanvasRenderingContext2D) SetLineDash(dashes []float64) {
  1799. ctx.Call("setLineDash", dashes)
  1800. }
  1801. // Gradients and patterns
  1802. // CreateLinearGradient creates a linear gradient along the line given
  1803. // by the coordinates represented by the parameters.
  1804. //
  1805. // Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient.
  1806. func (ctx *CanvasRenderingContext2D) CreateLinearGradient(x0, y0, x1, y1 float64) *CanvasGradient {
  1807. return &CanvasGradient{Object: ctx.Call("createLinearGradient", x0, y0, x1, y1)}
  1808. }
  1809. // CreateRadialGradient creates a radial gradient given by the coordinates of the two circles
  1810. // represented by the parameters.
  1811. //
  1812. // Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createRadialGradient.
  1813. func (ctx *CanvasRenderingContext2D) CreateRadialGradient(x0, y0, r0, x1, y1, r1 float64) *CanvasGradient {
  1814. return &CanvasGradient{Object: ctx.Call("createRadialGradient", x0, y0, r0, x1, y1, r1)}
  1815. }
  1816. // CreatePattern creates a pattern using the specified image (a CanvasImageSource).
  1817. // It repeats the source in the directions specified by the repetition argument.
  1818. //
  1819. // Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createPattern.
  1820. func (ctx *CanvasRenderingContext2D) CreatePattern(image Element, repetition string) *CanvasPattern {
  1821. return &CanvasPattern{Object: ctx.Call("createPattern", image, repetition)}
  1822. }
  1823. // Paths
  1824. func (ctx *CanvasRenderingContext2D) BeginPath() {
  1825. ctx.Call("beginPath")
  1826. }
  1827. func (ctx *CanvasRenderingContext2D) ClosePath() {
  1828. ctx.Call("closePath")
  1829. }
  1830. func (ctx *CanvasRenderingContext2D) MoveTo(x, y float64) {
  1831. ctx.Call("moveTo", x, y)
  1832. }
  1833. func (ctx *CanvasRenderingContext2D) LineTo(x, y float64) {
  1834. ctx.Call("lineTo", x, y)
  1835. }
  1836. func (ctx *CanvasRenderingContext2D) BezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y float64) {
  1837. ctx.Call("bezierCurveTo", cp1x, cp1y, cp2x, cp2y, x, y)
  1838. }
  1839. func (ctx *CanvasRenderingContext2D) QuadraticCurveTo(cpx, cpy, x, y float64) {
  1840. ctx.Call("quadraticCurveTo", cpx, cpy, x, y)
  1841. }
  1842. func (ctx *CanvasRenderingContext2D) Arc(x, y, r, sAngle, eAngle float64, counterclockwise bool) {
  1843. ctx.Call("arc", x, y, r, sAngle, eAngle, counterclockwise)
  1844. }
  1845. func (ctx *CanvasRenderingContext2D) ArcTo(x1, y1, x2, y2, r float64) {
  1846. ctx.Call("arcTo", x1, y1, x2, y2, r)
  1847. }
  1848. func (ctx *CanvasRenderingContext2D) Ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle float64, anticlockwise bool) {
  1849. ctx.Call("ellipse", x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)
  1850. }
  1851. func (ctx *CanvasRenderingContext2D) Rect(x, y, width, height float64) {
  1852. ctx.Call("rect", x, y, width, height)
  1853. }
  1854. // Drawing paths
  1855. func (ctx *CanvasRenderingContext2D) Fill() {
  1856. ctx.Call("fill")
  1857. }
  1858. func (ctx *CanvasRenderingContext2D) Stroke() {
  1859. ctx.Call("stroke")
  1860. }
  1861. func (ctx *CanvasRenderingContext2D) DrawFocusIfNeeded(element HTMLElement, path *js.Object) {
  1862. ctx.Call("drawFocusIfNeeded", element, path)
  1863. }
  1864. func (ctx *CanvasRenderingContext2D) ScrollPathIntoView(path *js.Object) {
  1865. ctx.Call("scrollPathIntoView", path)
  1866. }
  1867. func (ctx *CanvasRenderingContext2D) Clip() {
  1868. ctx.Call("clip")
  1869. }
  1870. func (ctx *CanvasRenderingContext2D) IsPointInPath(x, y float64) bool {
  1871. return ctx.Call("isPointInPath", x, y).Bool()
  1872. }
  1873. func (ctx *CanvasRenderingContext2D) IsPointInStroke(path *js.Object, x, y float64) bool {
  1874. return ctx.Call("isPointInStroke", path, x, y).Bool()
  1875. }
  1876. // Transformations
  1877. func (ctx *CanvasRenderingContext2D) Rotate(angle float64) {
  1878. ctx.Call("rotate", angle)
  1879. }
  1880. func (ctx *CanvasRenderingContext2D) Scale(scaleWidth, scaleHeight float64) {
  1881. ctx.Call("scale", scaleWidth, scaleHeight)
  1882. }
  1883. func (ctx *CanvasRenderingContext2D) Translate(x, y float64) {
  1884. ctx.Call("translate", x, y)
  1885. }
  1886. func (ctx *CanvasRenderingContext2D) Transform(a, b, c, d, e, f float64) {
  1887. ctx.Call("transform", a, b, c, d, e, f)
  1888. }
  1889. func (ctx *CanvasRenderingContext2D) SetTransform(a, b, c, d, e, f float64) {
  1890. ctx.Call("setTransform", a, b, c, d, e, f)
  1891. }
  1892. func (ctx *CanvasRenderingContext2D) ResetTransform() {
  1893. ctx.Call("resetTransform")
  1894. }
  1895. // Drawing images
  1896. func (ctx *CanvasRenderingContext2D) DrawImage(image Element, dx, dy float64) {
  1897. ctx.Call("drawImage", image, dx, dy)
  1898. }
  1899. func (ctx *CanvasRenderingContext2D) DrawImageWithDst(image Element, dx, dy, dWidth, dHeight float64) {
  1900. ctx.Call("drawImage", image, dx, dy, dWidth, dHeight)
  1901. }
  1902. func (ctx *CanvasRenderingContext2D) DrawImageWithSrcAndDst(image Element, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight float64) {
  1903. ctx.Call("drawImage", image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
  1904. }
  1905. // Pixel manipulation
  1906. func (ctx *CanvasRenderingContext2D) CreateImageData(width, height int) *ImageData {
  1907. return &ImageData{Object: ctx.Call("createImageData", width, height)}
  1908. }
  1909. func (ctx *CanvasRenderingContext2D) GetImageData(sx, sy, sw, sh int) *ImageData {
  1910. return &ImageData{Object: ctx.Call("getImageData", sx, sy, sw, sh)}
  1911. }
  1912. func (ctx *CanvasRenderingContext2D) PutImageData(imageData *ImageData, dx, dy float64) {
  1913. ctx.Call("putImageData", imageData, dx, dy)
  1914. }
  1915. func (ctx *CanvasRenderingContext2D) PutImageDataDirty(imageData *ImageData, dx, dy float64, dirtyX, dirtyY, dirtyWidth, dirtyHeight int) {
  1916. ctx.Call("putImageData", imageData, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight)
  1917. }
  1918. // State
  1919. func (ctx *CanvasRenderingContext2D) Save() {
  1920. ctx.Call("save")
  1921. }
  1922. func (ctx *CanvasRenderingContext2D) Restore() {
  1923. ctx.Call("restore")
  1924. }
  1925. // TODO Hit regions:
  1926. // addHitRegion
  1927. // removeHitRegion
  1928. // clearHitRegions
  1929. type HTMLDListElement struct{ *BasicHTMLElement }
  1930. type HTMLDataElement struct {
  1931. *BasicHTMLElement
  1932. Value string `js:"value"`
  1933. }
  1934. type HTMLDataListElement struct{ *BasicHTMLElement }
  1935. func (e *HTMLDataListElement) Options() []*HTMLOptionElement {
  1936. return getOptions(e.Object, "options")
  1937. }
  1938. type HTMLDetailsElement struct {
  1939. *BasicHTMLElement
  1940. Open bool `js:"open"`
  1941. }
  1942. type HTMLDirectoryElement struct{ *BasicHTMLElement }
  1943. type HTMLDivElement struct{ *BasicHTMLElement }
  1944. type HTMLEmbedElement struct {
  1945. *BasicHTMLElement
  1946. Src string `js:"src"`
  1947. Type string `js:"type"`
  1948. Width string `js:"width"`
  1949. }
  1950. type HTMLFieldSetElement struct {
  1951. *BasicHTMLElement
  1952. Disabled bool `js:"disabled"`
  1953. Name string `js:"name"`
  1954. Type string `js:"type"`
  1955. ValidationMessage string `js:"validationMessage"`
  1956. WillValidate bool `js:"willValidate"`
  1957. }
  1958. func (e *HTMLFieldSetElement) Elements() []HTMLElement {
  1959. return nodeListToHTMLElements(e.Get("elements"))
  1960. }
  1961. func (e *HTMLFieldSetElement) Form() *HTMLFormElement {
  1962. return getForm(e.Object)
  1963. }
  1964. func (e *HTMLFieldSetElement) Validity() *ValidityState {
  1965. // TODO replace with a field once GopherJS supports that
  1966. return &ValidityState{Object: e.Get("validity")}
  1967. }
  1968. func (e *HTMLFieldSetElement) CheckValidity() bool {
  1969. return e.Call("checkValidity").Bool()
  1970. }
  1971. func (e *HTMLFieldSetElement) SetCustomValidity(s string) {
  1972. e.Call("setCustomValidity", s)
  1973. }
  1974. type HTMLFontElement struct{ *BasicHTMLElement }
  1975. type HTMLFormElement struct {
  1976. *BasicHTMLElement
  1977. AcceptCharset string `js:"acceptCharset"`
  1978. Action string `js:"action"`
  1979. Autocomplete string `js:"autocomplete"`
  1980. Encoding string `js:"encoding"`
  1981. Enctype string `js:"enctype"`
  1982. Length int `js:"length"`
  1983. Method string `js:"method"`
  1984. Name string `js:"name"`
  1985. NoValidate bool `js:"noValidate"`
  1986. Target string `js:"target"`
  1987. }
  1988. func (e *HTMLFormElement) Elements() []HTMLElement {
  1989. return nodeListToHTMLElements(e.Get("elements"))
  1990. }
  1991. func (e *HTMLFormElement) CheckValidity() bool {
  1992. return e.Call("checkValidity").Bool()
  1993. }
  1994. func (e *HTMLFormElement) Submit() {
  1995. e.Call("submit")
  1996. }
  1997. func (e *HTMLFormElement) Reset() {
  1998. e.Call("reset")
  1999. }
  2000. func (e *HTMLFormElement) Item(index int) HTMLElement {
  2001. return wrapHTMLElement(e.Call("item", index))
  2002. }
  2003. func (e *HTMLFormElement) NamedItem(name string) HTMLElement {
  2004. return wrapHTMLElement(e.Call("namedItem", name))
  2005. }
  2006. type HTMLFrameElement struct{ *BasicHTMLElement }
  2007. type HTMLFrameSetElement struct{ *BasicHTMLElement }
  2008. type HTMLHRElement struct{ *BasicHTMLElement }
  2009. type HTMLHeadElement struct{ *BasicHTMLElement }
  2010. type HTMLHeadingElement struct{ *BasicHTMLElement }
  2011. type HTMLHtmlElement struct{ *BasicHTMLElement }
  2012. type HTMLIFrameElement struct {
  2013. *BasicHTMLElement
  2014. Width string `js:"width"`
  2015. Height string `js:"height"`
  2016. Name string `js:"name"`
  2017. Src string `js:"src"`
  2018. SrcDoc string `js:"srcdoc"`
  2019. Seamless bool `js:"seamless"`
  2020. // TODO sandbox attribute
  2021. }
  2022. func (e *HTMLIFrameElement) ContentDocument() Document {
  2023. return wrapDocument(e.Get("contentDocument"))
  2024. }
  2025. func (e *HTMLIFrameElement) ContentWindow() Window {
  2026. return &window{e.Get("contentWindow")}
  2027. }
  2028. type HTMLImageElement struct {
  2029. *BasicHTMLElement
  2030. Complete bool `js:"complete"`
  2031. CrossOrigin string `js:"crossOrigin"`
  2032. Height int `js:"height"`
  2033. IsMap bool `js:"isMap"`
  2034. NaturalHeight int `js:"naturalHeight"`
  2035. NaturalWidth int `js:"naturalWidth"`
  2036. Src string `js:"src"`
  2037. UseMap string `js:"useMap"`
  2038. Width int `js:"width"`
  2039. // TODO constructor
  2040. }
  2041. type HTMLInputElement struct {
  2042. *BasicHTMLElement
  2043. Accept string `js:"accept"`
  2044. Alt string `js:"alt"`
  2045. Autocomplete string `js:"autocomplete"`
  2046. Autofocus bool `js:"autofocus"`
  2047. Checked bool `js:"checked"`
  2048. DefaultChecked bool `js:"defaultChecked"`
  2049. DefaultValue string `js:"defaultValue"`
  2050. DirName string `js:"dirName"`
  2051. Disabled bool `js:"disabled"`
  2052. FormAction string `js:"formAction"`
  2053. FormEncType string `js:"formEncType"`
  2054. FormMethod string `js:"formMethod"`
  2055. FormNoValidate bool `js:"formNoValidate"`
  2056. FormTarget string `js:"formTarget"`
  2057. Height string `js:"height"`
  2058. Indeterminate bool `js:"indeterminate"`
  2059. Max string `js:"max"`
  2060. MaxLength int `js:"maxLength"`
  2061. Min string `js:"min"`
  2062. Multiple bool `js:"multiple"`
  2063. Name string `js:"name"`
  2064. Pattern string `js:"pattern"`
  2065. Placeholder string `js:"placeholder"`
  2066. ReadOnly bool `js:"readOnly"`
  2067. Required bool `js:"required"`
  2068. SelectionDirection string `js:"selectionDirection"`
  2069. SelectionEnd int `js:"selectionEnd"`
  2070. SelectionStart int `js:"selectionStart"`
  2071. Size int `js:"size"`
  2072. Src string `js:"src"`
  2073. Step string `js:"step"`
  2074. TabIndex int `js:"tabIndex"`
  2075. Type string `js:"type"`
  2076. ValidationMessage string `js:"validationMessage"`
  2077. Value string `js:"value"`
  2078. ValueAsDate time.Time `js:"valueAsDate"`
  2079. ValueAsNumber float64 `js:"valueAsNumber"`
  2080. Width string `js:"width"`
  2081. WillValidate bool `js:"willValidate"`
  2082. }
  2083. // File represents files as can be obtained from file choosers or drag
  2084. // and drop. The dom package does not define any methods on File nor
  2085. // does it provide access to the blob or a way to read it.
  2086. type File struct {
  2087. *js.Object
  2088. }
  2089. func (e *HTMLInputElement) Files() []*File {
  2090. files := e.Get("files")
  2091. out := make([]*File, files.Get("length").Int())
  2092. for i := range out {
  2093. out[i] = &File{files.Call("item", i)}
  2094. }
  2095. return out
  2096. }
  2097. func (e *HTMLInputElement) List() *HTMLDataListElement {
  2098. list := wrapHTMLElement(e.Get("list"))
  2099. if list == nil {
  2100. return nil
  2101. }
  2102. return list.(*HTMLDataListElement)
  2103. }
  2104. func (e *HTMLInputElement) Labels() []*HTMLLabelElement {
  2105. return getLabels(e.Object)
  2106. }
  2107. func (e *HTMLInputElement) Form() *HTMLFormElement {
  2108. return getForm(e.Object)
  2109. }
  2110. func (e *HTMLInputElement) Validity() *ValidityState {
  2111. // TODO replace with a field once GopherJS supports that
  2112. return &ValidityState{Object: e.Get("validity")}
  2113. }
  2114. func (e *HTMLInputElement) CheckValidity() bool {
  2115. return e.Call("checkValidity").Bool()
  2116. }
  2117. func (e *HTMLInputElement) SetCustomValidity(s string) {
  2118. e.Call("setCustomValidity", s)
  2119. }
  2120. func (e *HTMLInputElement) Select() {
  2121. e.Call("select")
  2122. }
  2123. func (e *HTMLInputElement) SetSelectionRange(start, end int, direction string) {
  2124. e.Call("setSelectionRange", start, end, direction)
  2125. }
  2126. func (e *HTMLInputElement) StepDown(n int) error {
  2127. return callRecover(e.Object, "stepDown", n)
  2128. }
  2129. func (e *HTMLInputElement) StepUp(n int) error {
  2130. return callRecover(e.Object, "stepUp", n)
  2131. }
  2132. type HTMLKeygenElement struct {
  2133. *BasicHTMLElement
  2134. Autofocus bool `js:"autofocus"`
  2135. Challenge string `js:"challenge"`
  2136. Disabled bool `js:"disabled"`
  2137. Keytype string `js:"keytype"`
  2138. Name string `js:"name"`
  2139. Type string `js:"type"`
  2140. ValidationMessage string `js:"validationMessage"`
  2141. WillValidate bool `js:"willValidate"`
  2142. }
  2143. func (e *HTMLKeygenElement) Form() *HTMLFormElement {
  2144. return getForm(e.Object)
  2145. }
  2146. func (e *HTMLKeygenElement) Labels() []*HTMLLabelElement {
  2147. return getLabels(e.Object)
  2148. }
  2149. func (e *HTMLKeygenElement) Validity() *ValidityState {
  2150. // TODO replace with a field once GopherJS supports that
  2151. return &ValidityState{Object: e.Get("validity")}
  2152. }
  2153. func (e *HTMLKeygenElement) CheckValidity() bool {
  2154. return e.Call("checkValidity").Bool()
  2155. }
  2156. func (e *HTMLKeygenElement) SetCustomValidity(s string) {
  2157. e.Call("setCustomValidity", s)
  2158. }
  2159. type HTMLLIElement struct {
  2160. *BasicHTMLElement
  2161. Value int `js:"value"`
  2162. }
  2163. type HTMLLabelElement struct {
  2164. *BasicHTMLElement
  2165. For string `js:"htmlFor"`
  2166. }
  2167. func (e *HTMLLabelElement) Control() HTMLElement {
  2168. return wrapHTMLElement(e.Get("control"))
  2169. }
  2170. func (e *HTMLLabelElement) Form() *HTMLFormElement {
  2171. return getForm(e.Object)
  2172. }
  2173. type HTMLLegendElement struct{ *BasicHTMLElement }
  2174. func (e *HTMLLegendElement) Form() *HTMLFormElement {
  2175. return getForm(e.Object)
  2176. }
  2177. type HTMLLinkElement struct {
  2178. *BasicHTMLElement
  2179. Disabled bool `js:"disabled"`
  2180. Href string `js:"href"`
  2181. HrefLang string `js:"hrefLang"`
  2182. Media string `js:"media"`
  2183. Type string `js:"type"`
  2184. }
  2185. func (e *HTMLLinkElement) Rel() *TokenList {
  2186. return &TokenList{dtl: e.Get("relList"), o: e.Object, sa: "rel"}
  2187. }
  2188. func (e *HTMLLinkElement) Sizes() *TokenList {
  2189. return &TokenList{dtl: e.Get("sizes"), o: e.Object}
  2190. }
  2191. func (e *HTMLLinkElement) Sheet() StyleSheet {
  2192. // FIXME implement
  2193. panic("not implemented")
  2194. }
  2195. type HTMLMapElement struct {
  2196. *BasicHTMLElement
  2197. Name string `js:"name"`
  2198. }
  2199. func (e *HTMLMapElement) Areas() []*HTMLAreaElement {
  2200. areas := nodeListToElements(e.Get("areas"))
  2201. out := make([]*HTMLAreaElement, len(areas))
  2202. for i, area := range areas {
  2203. out[i] = area.(*HTMLAreaElement)
  2204. }
  2205. return out
  2206. }
  2207. func (e *HTMLMapElement) Images() []HTMLElement {
  2208. return nodeListToHTMLElements(e.Get("areas"))
  2209. }
  2210. type HTMLMediaElement struct {
  2211. *BasicHTMLElement
  2212. Paused bool `js:"paused"`
  2213. }
  2214. func (e *HTMLMediaElement) Play() {
  2215. e.Call("play")
  2216. }
  2217. func (e *HTMLMediaElement) Pause() {
  2218. e.Call("pause")
  2219. }
  2220. type HTMLMenuElement struct{ *BasicHTMLElement }
  2221. type HTMLMetaElement struct {
  2222. *BasicHTMLElement
  2223. Content string `js:"content"`
  2224. HTTPEquiv string `js:"httpEquiv"`
  2225. Name string `js:"name"`
  2226. }
  2227. type HTMLMeterElement struct {
  2228. *BasicHTMLElement
  2229. High float64 `js:"high"`
  2230. Low float64 `js:"low"`
  2231. Max float64 `js:"max"`
  2232. Min float64 `js:"min"`
  2233. Optimum float64 `js:"optimum"`
  2234. }
  2235. func (e HTMLMeterElement) Labels() []*HTMLLabelElement {
  2236. return getLabels(e.Object)
  2237. }
  2238. type HTMLModElement struct {
  2239. *BasicHTMLElement
  2240. Cite string `js:"cite"`
  2241. DateTime string `js:"dateTime"`
  2242. }
  2243. type HTMLOListElement struct {
  2244. *BasicHTMLElement
  2245. Reversed bool `js:"reversed"`
  2246. Start int `js:"start"`
  2247. Type string `js:"type"`
  2248. }
  2249. type HTMLObjectElement struct {
  2250. *BasicHTMLElement
  2251. Data string `js:"data"`
  2252. Height string `js:"height"`
  2253. Name string `js:"name"`
  2254. TabIndex int `js:"tabIndex"`
  2255. Type string `js:"type"`
  2256. TypeMustMatch bool `js:"typeMustMatch"`
  2257. UseMap string `js:"useMap"`
  2258. ValidationMessage string `js:"validationMessage"`
  2259. With string `js:"with"`
  2260. WillValidate bool `js:"willValidate"`
  2261. }
  2262. func (e *HTMLObjectElement) Form() *HTMLFormElement {
  2263. return getForm(e.Object)
  2264. }
  2265. func (e *HTMLObjectElement) ContentDocument() Document {
  2266. return wrapDocument(e.Get("contentDocument"))
  2267. }
  2268. func (e *HTMLObjectElement) ContentWindow() Window {
  2269. return &window{e.Get("contentWindow")}
  2270. }
  2271. func (e *HTMLObjectElement) Validity() *ValidityState {
  2272. // TODO replace with a field once GopherJS supports that
  2273. return &ValidityState{Object: e.Get("validity")}
  2274. }
  2275. func (e *HTMLObjectElement) CheckValidity() bool {
  2276. return e.Call("checkValidity").Bool()
  2277. }
  2278. func (e *HTMLObjectElement) SetCustomValidity(s string) {
  2279. e.Call("setCustomValidity", s)
  2280. }
  2281. type HTMLOptGroupElement struct {
  2282. *BasicHTMLElement
  2283. Disabled bool `js:"disabled"`
  2284. Label string `js:"label"`
  2285. }
  2286. type HTMLOptionElement struct {
  2287. *BasicHTMLElement
  2288. DefaultSelected bool `js:"defaultSelected"`
  2289. Disabled bool `js:"disabled"`
  2290. Index int `js:"index"`
  2291. Label string `js:"label"`
  2292. Selected bool `js:"selected"`
  2293. Text string `js:"text"`
  2294. Value string `js:"value"`
  2295. }
  2296. func (e *HTMLOptionElement) Form() *HTMLFormElement {
  2297. return getForm(e.Object)
  2298. }
  2299. type HTMLOutputElement struct {
  2300. *BasicHTMLElement
  2301. DefaultValue string `js:"defaultValue"`
  2302. Name string `js:"name"`
  2303. Type string `js:"type"`
  2304. ValidationMessage string `js:"validationMessage"`
  2305. Value string `js:"value"`
  2306. WillValidate bool `js:"willValidate"`
  2307. }
  2308. func (e *HTMLOutputElement) Form() *HTMLFormElement {
  2309. return getForm(e.Object)
  2310. }
  2311. func (e *HTMLOutputElement) Labels() []*HTMLLabelElement {
  2312. return getLabels(e.Object)
  2313. }
  2314. func (e *HTMLOutputElement) Validity() *ValidityState {
  2315. // TODO replace with a field once GopherJS supports that
  2316. return &ValidityState{Object: e.Get("validity")}
  2317. }
  2318. func (e *HTMLOutputElement) For() *TokenList {
  2319. return &TokenList{dtl: e.Get("htmlFor"), o: e.Object}
  2320. }
  2321. func (e *HTMLOutputElement) CheckValidity() bool {
  2322. return e.Call("checkValidity").Bool()
  2323. }
  2324. func (e *HTMLOutputElement) SetCustomValidity(s string) {
  2325. e.Call("setCustomValidity", s)
  2326. }
  2327. type HTMLParagraphElement struct{ *BasicHTMLElement }
  2328. type HTMLParamElement struct {
  2329. *BasicHTMLElement
  2330. Name string `js:"name"`
  2331. Value string `js:"value"`
  2332. }
  2333. type HTMLPreElement struct{ *BasicHTMLElement }
  2334. type HTMLProgressElement struct {
  2335. *BasicHTMLElement
  2336. Max float64 `js:"max"`
  2337. Position float64 `js:"position"`
  2338. Value float64 `js:"value"`
  2339. }
  2340. func (e HTMLProgressElement) Labels() []*HTMLLabelElement {
  2341. return getLabels(e.Object)
  2342. }
  2343. type HTMLQuoteElement struct {
  2344. *BasicHTMLElement
  2345. Cite string `js:"cite"`
  2346. }
  2347. type HTMLScriptElement struct {
  2348. *BasicHTMLElement
  2349. Type string `js:"type"`
  2350. Src string `js:"src"`
  2351. Charset string `js:"charset"`
  2352. Async bool `js:"async"`
  2353. Defer bool `js:"defer"`
  2354. Text string `js:"text"`
  2355. }
  2356. type HTMLSelectElement struct {
  2357. *BasicHTMLElement
  2358. Autofocus bool `js:"autofocus"`
  2359. Disabled bool `js:"disabled"`
  2360. Length int `js:"length"`
  2361. Multiple bool `js:"multiple"`
  2362. Name string `js:"name"`
  2363. Required bool `js:"required"`
  2364. SelectedIndex int `js:"selectedIndex"`
  2365. Size int `js:"size"`
  2366. Type string `js:"type"`
  2367. ValidationMessage string `js:"validationMessage"`
  2368. Value string `js:"value"`
  2369. WillValidate bool `js:"willValidate"`
  2370. }
  2371. func (e *HTMLSelectElement) Labels() []*HTMLLabelElement {
  2372. return getLabels(e.Object)
  2373. }
  2374. func (e *HTMLSelectElement) Form() *HTMLFormElement {
  2375. return getForm(e.Object)
  2376. }
  2377. func (e *HTMLSelectElement) Options() []*HTMLOptionElement {
  2378. return getOptions(e.Object, "options")
  2379. }
  2380. func (e *HTMLSelectElement) SelectedOptions() []*HTMLOptionElement {
  2381. return getOptions(e.Object, "selectedOptions")
  2382. }
  2383. func (e *HTMLSelectElement) Item(index int) *HTMLOptionElement {
  2384. el := wrapHTMLElement(e.Call("item", index))
  2385. if el == nil {
  2386. return nil
  2387. }
  2388. return el.(*HTMLOptionElement)
  2389. }
  2390. func (e *HTMLSelectElement) NamedItem(name string) *HTMLOptionElement {
  2391. el := wrapHTMLElement(e.Call("namedItem", name))
  2392. if el == nil {
  2393. return nil
  2394. }
  2395. return el.(*HTMLOptionElement)
  2396. }
  2397. // TODO(dominikh): Not implementing Add or Remove for now. For one,
  2398. // Add with "before" behaves weird when dealing with optgroups. Also,
  2399. // there's already InsertBefore and RemoveChild which can be used
  2400. // instead.
  2401. func (e *HTMLSelectElement) Validity() *ValidityState {
  2402. return &ValidityState{Object: e.Get("validity")}
  2403. }
  2404. func (e *HTMLSelectElement) CheckValidity() bool {
  2405. return e.Call("checkValidity").Bool()
  2406. }
  2407. func (e *HTMLSelectElement) SetCustomValidity(s string) {
  2408. e.Call("setCustomValidity", s)
  2409. }
  2410. type HTMLSourceElement struct {
  2411. *BasicHTMLElement
  2412. Media string `js:"media"`
  2413. Src string `js:"src"`
  2414. Type string `js:"type"`
  2415. }
  2416. type HTMLSpanElement struct{ *BasicHTMLElement }
  2417. type HTMLStyleElement struct{ *BasicHTMLElement }
  2418. type HTMLTableCaptionElement struct{ *BasicHTMLElement }
  2419. type HTMLTableCellElement struct {
  2420. *BasicHTMLElement
  2421. ColSpan int `js:"colSpan"`
  2422. RowSpan int `js:"rowSpan"`
  2423. CellIndex int `js:"cellIndex"`
  2424. // TODO headers
  2425. }
  2426. type HTMLTableColElement struct {
  2427. *BasicHTMLElement
  2428. Span int `js:"span"`
  2429. }
  2430. type HTMLTableDataCellElement struct{ *BasicHTMLElement }
  2431. type HTMLTableElement struct{ *BasicHTMLElement }
  2432. type HTMLTableHeaderCellElement struct {
  2433. *BasicHTMLElement
  2434. Abbr string `js:"abbr"`
  2435. Scope string `js:"scope"`
  2436. }
  2437. type HTMLTableRowElement struct {
  2438. *BasicHTMLElement
  2439. RowIndex int `js:"rowIndex"`
  2440. SectionRowIndex int `js:"sectionRowIndex"`
  2441. }
  2442. func (e *HTMLTableRowElement) Cells() []*HTMLTableCellElement {
  2443. cells := nodeListToElements(e.Get("cells"))
  2444. out := make([]*HTMLTableCellElement, len(cells))
  2445. for i, cell := range cells {
  2446. out[i] = cell.(*HTMLTableCellElement)
  2447. }
  2448. return out
  2449. }
  2450. func (e *HTMLTableRowElement) InsertCell(index int) *HTMLTableCellElement {
  2451. return wrapHTMLElement(e.Call("insertCell", index)).(*HTMLTableCellElement)
  2452. }
  2453. func (e *HTMLTableRowElement) DeleteCell(index int) {
  2454. // FIXME exception handling/check that index is in bounds
  2455. e.Call("deleteCell", index)
  2456. }
  2457. type HTMLTableSectionElement struct{ *BasicHTMLElement }
  2458. func (e *HTMLTableSectionElement) Rows() []*HTMLTableRowElement {
  2459. rows := nodeListToElements(e.Get("rows"))
  2460. out := make([]*HTMLTableRowElement, len(rows))
  2461. for i, row := range rows {
  2462. out[i] = row.(*HTMLTableRowElement)
  2463. }
  2464. return out
  2465. }
  2466. func (e *HTMLTableSectionElement) DeleteRow(index int) {
  2467. // FIXME exception handling/check that index is in bounds
  2468. e.Call("deleteRow", index)
  2469. }
  2470. func (e *HTMLTableSectionElement) InsertRow(index int) *HTMLTableRowElement {
  2471. return wrapHTMLElement(e.Call("insertRow", index)).(*HTMLTableRowElement)
  2472. }
  2473. type HTMLTemplateElement struct{ *BasicHTMLElement }
  2474. func (e *HTMLTemplateElement) Content() DocumentFragment {
  2475. return wrapDocumentFragment(e.Get("content"))
  2476. }
  2477. type HTMLTextAreaElement struct {
  2478. *BasicHTMLElement
  2479. Autocomplete string `js:"autocomplete"`
  2480. Autofocus bool `js:"autofocus"`
  2481. Cols int `js:"cols"`
  2482. DefaultValue string `js:"defaultValue"`
  2483. DirName string `js:"dirName"`
  2484. Disabled bool `js:"disabled"`
  2485. MaxLength int `js:"maxLength"`
  2486. Name string `js:"name"`
  2487. Placeholder string `js:"placeholder"`
  2488. ReadOnly bool `js:"readOnly"`
  2489. Required bool `js:"required"`
  2490. Rows int `js:"rows"`
  2491. SelectionDirection string `js:"selectionDirection"`
  2492. SelectionStart int `js:"selectionStart"`
  2493. SelectionEnd int `js:"selectionEnd"`
  2494. TabIndex int `js:"tabIndex"`
  2495. TextLength int `js:"textLength"`
  2496. Type string `js:"type"`
  2497. ValidationMessage string `js:"validationMessage"`
  2498. Value string `js:"value"`
  2499. WillValidate bool `js:"willValidate"`
  2500. Wrap string `js:"wrap"`
  2501. }
  2502. func (e *HTMLTextAreaElement) Form() *HTMLFormElement {
  2503. return getForm(e.Object)
  2504. }
  2505. func (e *HTMLTextAreaElement) Labels() []*HTMLLabelElement {
  2506. return getLabels(e.Object)
  2507. }
  2508. func (e *HTMLTextAreaElement) Validity() *ValidityState {
  2509. // TODO replace with a field once GopherJS supports that
  2510. return &ValidityState{Object: e.Get("validity")}
  2511. }
  2512. func (e *HTMLTextAreaElement) CheckValidity() bool {
  2513. return e.Call("checkValidity").Bool()
  2514. }
  2515. func (e *HTMLTextAreaElement) SetCustomValidity(s string) {
  2516. e.Call("setCustomValidity", s)
  2517. }
  2518. func (e *HTMLTextAreaElement) Select() {
  2519. e.Call("select")
  2520. }
  2521. func (e *HTMLTextAreaElement) SetSelectionRange(start, end int, direction string) {
  2522. e.Call("setSelectionRange", start, end, direction)
  2523. }
  2524. type HTMLTimeElement struct {
  2525. *BasicHTMLElement
  2526. DateTime string `js:"dateTime"`
  2527. }
  2528. type HTMLTitleElement struct {
  2529. *BasicHTMLElement
  2530. Text string `js:"text"`
  2531. }
  2532. // TextTrack represents text track data for <track> elements. It does
  2533. // not currently provide any methods or attributes and it hasn't been
  2534. // decided yet whether they will be added to this package or a
  2535. // separate package.
  2536. type TextTrack struct{ *js.Object }
  2537. type HTMLTrackElement struct {
  2538. *BasicHTMLElement
  2539. Kind string `js:"kind"`
  2540. Src string `js:"src"`
  2541. Srclang string `js:"srclang"`
  2542. Label string `js:"label"`
  2543. Default bool `js:"default"`
  2544. ReadyState int `js:"readyState"`
  2545. }
  2546. func (e *HTMLTrackElement) Track() *TextTrack {
  2547. return &TextTrack{e.Get("track")}
  2548. }
  2549. type HTMLUListElement struct{ *BasicHTMLElement }
  2550. type HTMLUnknownElement struct{ *BasicHTMLElement }
  2551. type HTMLVideoElement struct{ *HTMLMediaElement }
  2552. type ValidityState struct {
  2553. *js.Object
  2554. CustomError bool `js:"customError"`
  2555. PatternMismatch bool `js:"patternMismatch"`
  2556. RangeOverflow bool `js:"rangeOverflow"`
  2557. RangeUnderflow bool `js:"rangeUnderflow"`
  2558. StepMismatch bool `js:"stepMismatch"`
  2559. TooLong bool `js:"tooLong"`
  2560. TypeMismatch bool `js:"typeMismatch"`
  2561. Valid bool `js:"valid"`
  2562. ValueMissing bool `js:"valueMissing"`
  2563. }
  2564. type CSSStyleDeclaration struct{ *js.Object }
  2565. func (css *CSSStyleDeclaration) ToMap() map[string]string {
  2566. m := make(map[string]string)
  2567. N := css.Get("length").Int()
  2568. for i := 0; i < N; i++ {
  2569. name := css.Call("item", i).String()
  2570. value := css.Call("getPropertyValue", name).String()
  2571. m[name] = value
  2572. }
  2573. return m
  2574. }
  2575. func (css *CSSStyleDeclaration) RemoveProperty(name string) {
  2576. css.Call("removeProperty", name)
  2577. }
  2578. func (css *CSSStyleDeclaration) GetPropertyValue(name string) string {
  2579. return toString(css.Call("getPropertyValue", name))
  2580. }
  2581. func (css *CSSStyleDeclaration) GetPropertyPriority(name string) string {
  2582. return toString(css.Call("getPropertyPriority", name))
  2583. }
  2584. func (css *CSSStyleDeclaration) SetProperty(name, value, priority string) {
  2585. css.Call("setProperty", name, value, priority)
  2586. }
  2587. func (css *CSSStyleDeclaration) Index(idx int) string {
  2588. return css.Call("index", idx).String()
  2589. }
  2590. func (css *CSSStyleDeclaration) Length() int {
  2591. return css.Get("length").Int()
  2592. }
  2593. type Text struct {
  2594. *BasicNode
  2595. }
  2596. // DataTransfer holds the data that is being
  2597. // dragged during a drag and drop operation.
  2598. type DataTransfer struct{ *js.Object }
  2599. func (dt *DataTransfer) GetData(format string) string {
  2600. return dt.Call("getData", format).String()
  2601. }