0
# W3C DOM and Browser APIs
1
2
Comprehensive type-safe bindings for W3C web standards including DOM manipulation, HTTP requests, file handling, media APIs, WebGL, and other browser APIs. This module provides 599+ external declarations covering the complete browser API surface.
3
4
## Capabilities
5
6
### Core DOM Interfaces
7
8
Fundamental DOM interfaces for document and element manipulation.
9
10
```kotlin { .api }
11
/**
12
* Document interface representing the HTML document
13
*/
14
external interface Document : Node, DocumentOrShadowRoot, GlobalEventHandlers {
15
val documentElement: Element?
16
val body: HTMLElement?
17
val head: HTMLHeadElement?
18
val title: String
19
20
fun createElement(localName: String): Element
21
fun createTextNode(data: String): Text
22
fun getElementById(elementId: String): Element?
23
fun getElementsByTagName(qualifiedName: String): HTMLCollection
24
fun getElementsByClassName(classNames: String): HTMLCollection
25
fun querySelector(selectors: String): Element?
26
fun querySelectorAll(selectors: String): NodeList
27
}
28
29
/**
30
* Base element interface
31
*/
32
external interface Element : Node, DocumentOrShadowRoot, GlobalEventHandlers {
33
val tagName: String
34
val id: String
35
val className: String
36
val classList: DOMTokenList
37
38
fun getAttribute(qualifiedName: String): String?
39
fun setAttribute(qualifiedName: String, value: String)
40
fun removeAttribute(qualifiedName: String)
41
fun hasAttribute(qualifiedName: String): Boolean
42
43
fun querySelector(selectors: String): Element?
44
fun querySelectorAll(selectors: String): NodeList
45
fun getElementsByTagName(qualifiedName: String): HTMLCollection
46
fun getElementsByClassName(classNames: String): HTMLCollection
47
}
48
49
/**
50
* Base node interface for DOM tree
51
*/
52
external interface Node : EventTarget {
53
val nodeType: Short
54
val nodeName: String
55
val nodeValue: String?
56
val textContent: String?
57
val parentNode: Node?
58
val childNodes: NodeList
59
val firstChild: Node?
60
val lastChild: Node?
61
val nextSibling: Node?
62
val previousSibling: Node?
63
64
fun appendChild(node: Node): Node
65
fun insertBefore(node: Node, child: Node?): Node
66
fun removeChild(child: Node): Node
67
fun replaceChild(node: Node, child: Node): Node
68
fun cloneNode(deep: Boolean): Node
69
}
70
```
71
72
### HTML Elements
73
74
Type-safe interfaces for all HTML elements with their specific properties.
75
76
```kotlin { .api }
77
/**
78
* Base HTML element interface
79
*/
80
external interface HTMLElement : Element, GlobalEventHandlers {
81
var title: String
82
var lang: String
83
var dir: String
84
var hidden: Boolean
85
val style: CSSStyleDeclaration
86
val offsetParent: Element?
87
val offsetTop: Int
88
val offsetLeft: Int
89
val offsetWidth: Int
90
val offsetHeight: Int
91
92
fun click()
93
fun focus()
94
fun blur()
95
}
96
97
/**
98
* HTML input element
99
*/
100
external interface HTMLInputElement : HTMLElement {
101
var type: String
102
var value: String
103
var placeholder: String
104
var disabled: Boolean
105
var readonly: Boolean
106
var required: Boolean
107
var checked: Boolean
108
var name: String
109
var form: HTMLFormElement?
110
111
fun select()
112
fun focus()
113
fun blur()
114
fun checkValidity(): Boolean
115
}
116
117
/**
118
* HTML form element
119
*/
120
external interface HTMLFormElement : HTMLElement {
121
val elements: HTMLFormControlsCollection
122
val length: Int
123
var action: String
124
var method: String
125
var target: String
126
var enctype: String
127
128
fun submit()
129
fun reset()
130
fun checkValidity(): Boolean
131
}
132
133
/**
134
* HTML div element
135
*/
136
external interface HTMLDivElement : HTMLElement
137
138
/**
139
* HTML span element
140
*/
141
external interface HTMLSpanElement : HTMLElement
142
143
/**
144
* HTML anchor element
145
*/
146
external interface HTMLAnchorElement : HTMLElement {
147
var href: String
148
var target: String
149
var download: String
150
var rel: String
151
val hostname: String
152
val pathname: String
153
val search: String
154
val hash: String
155
}
156
157
/**
158
* HTML image element
159
*/
160
external interface HTMLImageElement : HTMLElement {
161
var src: String
162
var alt: String
163
var width: Int
164
var height: Int
165
var crossOrigin: String?
166
val naturalWidth: Int
167
val naturalHeight: Int
168
val complete: Boolean
169
}
170
```
171
172
### Event System
173
174
Comprehensive event handling system for DOM interactions.
175
176
```kotlin { .api }
177
/**
178
* Base event interface
179
*/
180
external interface Event {
181
val type: String
182
val target: EventTarget?
183
val currentTarget: EventTarget?
184
val eventPhase: Short
185
val bubbles: Boolean
186
val cancelable: Boolean
187
val defaultPrevented: Boolean
188
val timeStamp: Number
189
190
fun preventDefault()
191
fun stopPropagation()
192
fun stopImmediatePropagation()
193
}
194
195
/**
196
* Event target interface for event handling
197
*/
198
external interface EventTarget {
199
fun addEventListener(type: String, listener: EventListener?)
200
fun addEventListener(type: String, listener: EventListener?, options: AddEventListenerOptions?)
201
fun removeEventListener(type: String, listener: EventListener?)
202
fun removeEventListener(type: String, listener: EventListener?, options: EventListenerOptions?)
203
fun dispatchEvent(event: Event): Boolean
204
}
205
206
/**
207
* Event listener interface
208
*/
209
external interface EventListener {
210
fun handleEvent(event: Event)
211
}
212
213
/**
214
* Mouse event interface
215
*/
216
external interface MouseEvent : UIEvent {
217
val button: Short
218
val buttons: Short
219
val clientX: Int
220
val clientY: Int
221
val screenX: Int
222
val screenY: Int
223
val ctrlKey: Boolean
224
val shiftKey: Boolean
225
val altKey: Boolean
226
val metaKey: Boolean
227
}
228
229
/**
230
* Keyboard event interface
231
*/
232
external interface KeyboardEvent : UIEvent {
233
val key: String
234
val code: String
235
val location: Int
236
val ctrlKey: Boolean
237
val shiftKey: Boolean
238
val altKey: Boolean
239
val metaKey: Boolean
240
val repeat: Boolean
241
}
242
```
243
244
### HTTP and Network APIs
245
246
Modern HTTP client and traditional XMLHttpRequest support.
247
248
```kotlin { .api }
249
/**
250
* Modern fetch API for HTTP requests
251
*/
252
external fun fetch(input: RequestInfo, init: RequestInit? = definedExternally): Promise<Response>
253
254
/**
255
* HTTP request representation
256
*/
257
external interface Request {
258
val method: String
259
val url: String
260
val headers: Headers
261
val body: dynamic
262
val mode: RequestMode
263
val credentials: RequestCredentials
264
265
fun text(): Promise<String>
266
fun json(): Promise<dynamic>
267
fun blob(): Promise<Blob>
268
fun arrayBuffer(): Promise<ArrayBuffer>
269
}
270
271
/**
272
* HTTP response representation
273
*/
274
external interface Response {
275
val ok: Boolean
276
val status: Short
277
val statusText: String
278
val headers: Headers
279
val body: ReadableStream?
280
val bodyUsed: Boolean
281
282
fun text(): Promise<String>
283
fun json(): Promise<dynamic>
284
fun blob(): Promise<Blob>
285
fun arrayBuffer(): Promise<ArrayBuffer>
286
fun clone(): Response
287
}
288
289
/**
290
* HTTP headers manipulation
291
*/
292
external interface Headers {
293
fun append(name: String, value: String)
294
fun delete(name: String)
295
fun get(name: String): String?
296
fun has(name: String): Boolean
297
fun set(name: String, value: String)
298
}
299
300
/**
301
* Traditional XMLHttpRequest
302
*/
303
external interface XMLHttpRequest : EventTarget {
304
var onload: ((Event) -> Unit)?
305
var onerror: ((Event) -> Unit)?
306
var onreadystatechange: ((Event) -> Unit)?
307
val readyState: Short
308
val response: dynamic
309
val responseText: String
310
val responseType: XMLHttpRequestResponseType
311
val status: Short
312
val statusText: String
313
314
fun open(method: String, url: String)
315
fun open(method: String, url: String, async: Boolean)
316
fun send()
317
fun send(body: dynamic)
318
fun setRequestHeader(name: String, value: String)
319
fun getResponseHeader(name: String): String?
320
fun getAllResponseHeaders(): String
321
fun abort()
322
}
323
```
324
325
### File API
326
327
File system access and binary data handling.
328
329
```kotlin { .api }
330
/**
331
* Binary large object representation
332
*/
333
external interface Blob {
334
val size: Number
335
val type: String
336
337
fun slice(): Blob
338
fun slice(start: Number): Blob
339
fun slice(start: Number, end: Number): Blob
340
fun slice(start: Number, end: Number, contentType: String): Blob
341
fun stream(): ReadableStream
342
fun text(): Promise<String>
343
fun arrayBuffer(): Promise<ArrayBuffer>
344
}
345
346
/**
347
* File system file representation
348
*/
349
external interface File : Blob {
350
val name: String
351
val lastModified: Number
352
}
353
354
/**
355
* Collection of files (from input[type=file])
356
*/
357
external interface FileList {
358
val length: Int
359
fun item(index: Int): File?
360
operator fun get(index: Int): File?
361
}
362
363
/**
364
* Asynchronous file reading
365
*/
366
external interface FileReader : EventTarget {
367
val readyState: Short
368
val result: dynamic
369
val error: dynamic
370
var onload: ((Event) -> Unit)?
371
var onerror: ((Event) -> Unit)?
372
var onloadend: ((Event) -> Unit)?
373
374
fun readAsArrayBuffer(file: Blob)
375
fun readAsDataURL(file: Blob)
376
fun readAsText(file: Blob)
377
fun readAsText(file: Blob, encoding: String)
378
fun abort()
379
}
380
381
/**
382
* Form data encoding
383
*/
384
external interface FormData {
385
fun append(name: String, value: String)
386
fun append(name: String, value: Blob)
387
fun append(name: String, value: Blob, filename: String)
388
fun delete(name: String)
389
fun get(name: String): dynamic
390
fun getAll(name: String): Array<dynamic>
391
fun has(name: String): Boolean
392
fun set(name: String, value: String)
393
fun set(name: String, value: Blob)
394
fun set(name: String, value: Blob, filename: String)
395
}
396
```
397
398
### Media APIs
399
400
Media capture, playback, and streaming capabilities.
401
402
```kotlin { .api }
403
/**
404
* Media stream representation
405
*/
406
external interface MediaStream : EventTarget {
407
val id: String
408
val active: Boolean
409
410
fun getAudioTracks(): Array<MediaStreamTrack>
411
fun getVideoTracks(): Array<MediaStreamTrack>
412
fun getTracks(): Array<MediaStreamTrack>
413
fun addTrack(track: MediaStreamTrack)
414
fun removeTrack(track: MediaStreamTrack)
415
fun clone(): MediaStream
416
}
417
418
/**
419
* Individual media track
420
*/
421
external interface MediaStreamTrack : EventTarget {
422
val id: String
423
val kind: String
424
val label: String
425
val enabled: Boolean
426
val muted: Boolean
427
val readyState: MediaStreamTrackState
428
429
fun clone(): MediaStreamTrack
430
fun stop()
431
fun getCapabilities(): MediaTrackCapabilities
432
fun getConstraints(): MediaTrackConstraints
433
}
434
435
/**
436
* Media device access
437
*/
438
external interface MediaDevices : EventTarget {
439
fun getUserMedia(constraints: MediaStreamConstraints): Promise<MediaStream>
440
fun getDisplayMedia(constraints: DisplayMediaStreamConstraints): Promise<MediaStream>
441
fun enumerateDevices(): Promise<Array<MediaDeviceInfo>>
442
}
443
444
/**
445
* HTML video element
446
*/
447
external interface HTMLVideoElement : HTMLMediaElement {
448
var width: Int
449
var height: Int
450
val videoWidth: Int
451
val videoHeight: Int
452
var poster: String
453
}
454
455
/**
456
* HTML audio element
457
*/
458
external interface HTMLAudioElement : HTMLMediaElement
459
```
460
461
### WebGL APIs
462
463
Complete WebGL 1.0 API for 3D graphics programming.
464
465
```kotlin { .api }
466
/**
467
* WebGL rendering context
468
*/
469
external interface WebGLRenderingContext {
470
// Context constants
471
val DEPTH_BUFFER_BIT: Int
472
val STENCIL_BUFFER_BIT: Int
473
val COLOR_BUFFER_BIT: Int
474
val TRIANGLES: Int
475
val TRIANGLE_STRIP: Int
476
val TRIANGLE_FAN: Int
477
478
// Buffer operations
479
fun createBuffer(): WebGLBuffer?
480
fun bindBuffer(target: Int, buffer: WebGLBuffer?)
481
fun bufferData(target: Int, size: Int, usage: Int)
482
fun bufferData(target: Int, data: ArrayBufferView, usage: Int)
483
fun deleteBuffer(buffer: WebGLBuffer?)
484
485
// Shader operations
486
fun createShader(type: Int): WebGLShader?
487
fun shaderSource(shader: WebGLShader, source: String)
488
fun compileShader(shader: WebGLShader)
489
fun deleteShader(shader: WebGLShader?)
490
491
// Program operations
492
fun createProgram(): WebGLProgram?
493
fun attachShader(program: WebGLProgram, shader: WebGLShader)
494
fun linkProgram(program: WebGLProgram)
495
fun useProgram(program: WebGLProgram?)
496
fun deleteProgram(program: WebGLProgram?)
497
498
// Drawing operations
499
fun clear(mask: Int)
500
fun clearColor(red: Float, green: Float, blue: Float, alpha: Float)
501
fun drawArrays(mode: Int, first: Int, count: Int)
502
fun drawElements(mode: Int, count: Int, type: Int, offset: Int)
503
504
// Viewport and scissor
505
fun viewport(x: Int, y: Int, width: Int, height: Int)
506
fun scissor(x: Int, y: Int, width: Int, height: Int)
507
}
508
509
/**
510
* WebGL buffer object
511
*/
512
external interface WebGLBuffer
513
514
/**
515
* WebGL shader object
516
*/
517
external interface WebGLShader
518
519
/**
520
* WebGL program object
521
*/
522
external interface WebGLProgram
523
524
/**
525
* WebGL texture object
526
*/
527
external interface WebGLTexture
528
529
/**
530
* Typed array for 32-bit floats
531
*/
532
external interface Float32Array : ArrayBufferView {
533
val length: Int
534
operator fun get(index: Int): Float
535
operator fun set(index: Int, value: Float)
536
}
537
538
/**
539
* Typed array for unsigned 8-bit integers
540
*/
541
external interface Uint8Array : ArrayBufferView {
542
val length: Int
543
operator fun get(index: Int): Byte
544
operator fun set(index: Int, value: Byte)
545
}
546
```
547
548
### Service Workers and Cache API
549
550
Progressive Web App capabilities with service worker and caching support.
551
552
```kotlin { .api }
553
/**
554
* Service worker representation
555
*/
556
external interface ServiceWorker : EventTarget {
557
val scriptURL: String
558
val state: ServiceWorkerState
559
560
fun postMessage(message: dynamic)
561
}
562
563
/**
564
* Service worker registration
565
*/
566
external interface ServiceWorkerRegistration : EventTarget {
567
val scope: String
568
val installing: ServiceWorker?
569
val waiting: ServiceWorker?
570
val active: ServiceWorker?
571
572
fun update(): Promise<Unit>
573
fun unregister(): Promise<Boolean>
574
}
575
576
/**
577
* Cache storage interface
578
*/
579
external interface CacheStorage {
580
fun open(cacheName: String): Promise<Cache>
581
fun has(cacheName: String): Promise<Boolean>
582
fun delete(cacheName: String): Promise<Boolean>
583
fun keys(): Promise<Array<String>>
584
}
585
586
/**
587
* Individual cache interface
588
*/
589
external interface Cache {
590
fun match(request: RequestInfo): Promise<Response?>
591
fun matchAll(request: RequestInfo): Promise<Array<Response>>
592
fun add(request: RequestInfo): Promise<Unit>
593
fun addAll(requests: Array<RequestInfo>): Promise<Unit>
594
fun put(request: RequestInfo, response: Response): Promise<Unit>
595
fun delete(request: RequestInfo): Promise<Boolean>
596
fun keys(): Promise<Array<Request>>
597
}
598
```
599
600
## Types
601
602
```kotlin { .api }
603
// Core DOM types
604
external interface Document : Node
605
external interface Element : Node
606
external interface Node : EventTarget
607
external interface EventTarget
608
609
// HTML element types
610
external interface HTMLElement : Element
611
external interface HTMLInputElement : HTMLElement
612
external interface HTMLFormElement : HTMLElement
613
external interface HTMLDivElement : HTMLElement
614
external interface HTMLAnchorElement : HTMLElement
615
external interface HTMLImageElement : HTMLElement
616
617
// Event types
618
external interface Event
619
external interface MouseEvent : UIEvent
620
external interface KeyboardEvent : UIEvent
621
external interface EventListener
622
623
// Network types
624
external interface Request
625
external interface Response
626
external interface Headers
627
external interface XMLHttpRequest : EventTarget
628
629
// File types
630
external interface Blob
631
external interface File : Blob
632
external interface FileList
633
external interface FileReader : EventTarget
634
external interface FormData
635
636
// Media types
637
external interface MediaStream : EventTarget
638
external interface MediaStreamTrack : EventTarget
639
external interface MediaDevices : EventTarget
640
external interface HTMLVideoElement : HTMLMediaElement
641
external interface HTMLAudioElement : HTMLMediaElement
642
643
// WebGL types
644
external interface WebGLRenderingContext
645
external interface WebGLBuffer
646
external interface WebGLShader
647
external interface WebGLProgram
648
external interface WebGLTexture
649
external interface Float32Array : ArrayBufferView
650
external interface Uint8Array : ArrayBufferView
651
652
// Service Worker types
653
external interface ServiceWorker : EventTarget
654
external interface ServiceWorkerRegistration : EventTarget
655
external interface Cache
656
external interface CacheStorage
657
658
// Utility types
659
external interface ArrayBuffer
660
external interface ArrayBufferView
661
external interface ReadableStream
662
external interface DOMTokenList
663
external interface HTMLCollection
664
external interface NodeList
665
```