11cb0ef41Sopenharmony_ci'use strict' 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { 41cb0ef41Sopenharmony_ci staticPropertyDescriptors, 51cb0ef41Sopenharmony_ci readOperation, 61cb0ef41Sopenharmony_ci fireAProgressEvent 71cb0ef41Sopenharmony_ci} = require('./util') 81cb0ef41Sopenharmony_ciconst { 91cb0ef41Sopenharmony_ci kState, 101cb0ef41Sopenharmony_ci kError, 111cb0ef41Sopenharmony_ci kResult, 121cb0ef41Sopenharmony_ci kEvents, 131cb0ef41Sopenharmony_ci kAborted 141cb0ef41Sopenharmony_ci} = require('./symbols') 151cb0ef41Sopenharmony_ciconst { webidl } = require('../fetch/webidl') 161cb0ef41Sopenharmony_ciconst { kEnumerableProperty } = require('../core/util') 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciclass FileReader extends EventTarget { 191cb0ef41Sopenharmony_ci constructor () { 201cb0ef41Sopenharmony_ci super() 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci this[kState] = 'empty' 231cb0ef41Sopenharmony_ci this[kResult] = null 241cb0ef41Sopenharmony_ci this[kError] = null 251cb0ef41Sopenharmony_ci this[kEvents] = { 261cb0ef41Sopenharmony_ci loadend: null, 271cb0ef41Sopenharmony_ci error: null, 281cb0ef41Sopenharmony_ci abort: null, 291cb0ef41Sopenharmony_ci load: null, 301cb0ef41Sopenharmony_ci progress: null, 311cb0ef41Sopenharmony_ci loadstart: null 321cb0ef41Sopenharmony_ci } 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci /** 361cb0ef41Sopenharmony_ci * @see https://w3c.github.io/FileAPI/#dfn-readAsArrayBuffer 371cb0ef41Sopenharmony_ci * @param {import('buffer').Blob} blob 381cb0ef41Sopenharmony_ci */ 391cb0ef41Sopenharmony_ci readAsArrayBuffer (blob) { 401cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci webidl.argumentLengthCheck(arguments, 1, { header: 'FileReader.readAsArrayBuffer' }) 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci blob = webidl.converters.Blob(blob, { strict: false }) 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci // The readAsArrayBuffer(blob) method, when invoked, 471cb0ef41Sopenharmony_ci // must initiate a read operation for blob with ArrayBuffer. 481cb0ef41Sopenharmony_ci readOperation(this, blob, 'ArrayBuffer') 491cb0ef41Sopenharmony_ci } 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci /** 521cb0ef41Sopenharmony_ci * @see https://w3c.github.io/FileAPI/#readAsBinaryString 531cb0ef41Sopenharmony_ci * @param {import('buffer').Blob} blob 541cb0ef41Sopenharmony_ci */ 551cb0ef41Sopenharmony_ci readAsBinaryString (blob) { 561cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci webidl.argumentLengthCheck(arguments, 1, { header: 'FileReader.readAsBinaryString' }) 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci blob = webidl.converters.Blob(blob, { strict: false }) 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci // The readAsBinaryString(blob) method, when invoked, 631cb0ef41Sopenharmony_ci // must initiate a read operation for blob with BinaryString. 641cb0ef41Sopenharmony_ci readOperation(this, blob, 'BinaryString') 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci /** 681cb0ef41Sopenharmony_ci * @see https://w3c.github.io/FileAPI/#readAsDataText 691cb0ef41Sopenharmony_ci * @param {import('buffer').Blob} blob 701cb0ef41Sopenharmony_ci * @param {string?} encoding 711cb0ef41Sopenharmony_ci */ 721cb0ef41Sopenharmony_ci readAsText (blob, encoding = undefined) { 731cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci webidl.argumentLengthCheck(arguments, 1, { header: 'FileReader.readAsText' }) 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci blob = webidl.converters.Blob(blob, { strict: false }) 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci if (encoding !== undefined) { 801cb0ef41Sopenharmony_ci encoding = webidl.converters.DOMString(encoding) 811cb0ef41Sopenharmony_ci } 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci // The readAsText(blob, encoding) method, when invoked, 841cb0ef41Sopenharmony_ci // must initiate a read operation for blob with Text and encoding. 851cb0ef41Sopenharmony_ci readOperation(this, blob, 'Text', encoding) 861cb0ef41Sopenharmony_ci } 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci /** 891cb0ef41Sopenharmony_ci * @see https://w3c.github.io/FileAPI/#dfn-readAsDataURL 901cb0ef41Sopenharmony_ci * @param {import('buffer').Blob} blob 911cb0ef41Sopenharmony_ci */ 921cb0ef41Sopenharmony_ci readAsDataURL (blob) { 931cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci webidl.argumentLengthCheck(arguments, 1, { header: 'FileReader.readAsDataURL' }) 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci blob = webidl.converters.Blob(blob, { strict: false }) 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci // The readAsDataURL(blob) method, when invoked, must 1001cb0ef41Sopenharmony_ci // initiate a read operation for blob with DataURL. 1011cb0ef41Sopenharmony_ci readOperation(this, blob, 'DataURL') 1021cb0ef41Sopenharmony_ci } 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci /** 1051cb0ef41Sopenharmony_ci * @see https://w3c.github.io/FileAPI/#dfn-abort 1061cb0ef41Sopenharmony_ci */ 1071cb0ef41Sopenharmony_ci abort () { 1081cb0ef41Sopenharmony_ci // 1. If this's state is "empty" or if this's state is 1091cb0ef41Sopenharmony_ci // "done" set this's result to null and terminate 1101cb0ef41Sopenharmony_ci // this algorithm. 1111cb0ef41Sopenharmony_ci if (this[kState] === 'empty' || this[kState] === 'done') { 1121cb0ef41Sopenharmony_ci this[kResult] = null 1131cb0ef41Sopenharmony_ci return 1141cb0ef41Sopenharmony_ci } 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci // 2. If this's state is "loading" set this's state to 1171cb0ef41Sopenharmony_ci // "done" and set this's result to null. 1181cb0ef41Sopenharmony_ci if (this[kState] === 'loading') { 1191cb0ef41Sopenharmony_ci this[kState] = 'done' 1201cb0ef41Sopenharmony_ci this[kResult] = null 1211cb0ef41Sopenharmony_ci } 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci // 3. If there are any tasks from this on the file reading 1241cb0ef41Sopenharmony_ci // task source in an affiliated task queue, then remove 1251cb0ef41Sopenharmony_ci // those tasks from that task queue. 1261cb0ef41Sopenharmony_ci this[kAborted] = true 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci // 4. Terminate the algorithm for the read method being processed. 1291cb0ef41Sopenharmony_ci // TODO 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci // 5. Fire a progress event called abort at this. 1321cb0ef41Sopenharmony_ci fireAProgressEvent('abort', this) 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci // 6. If this's state is not "loading", fire a progress 1351cb0ef41Sopenharmony_ci // event called loadend at this. 1361cb0ef41Sopenharmony_ci if (this[kState] !== 'loading') { 1371cb0ef41Sopenharmony_ci fireAProgressEvent('loadend', this) 1381cb0ef41Sopenharmony_ci } 1391cb0ef41Sopenharmony_ci } 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci /** 1421cb0ef41Sopenharmony_ci * @see https://w3c.github.io/FileAPI/#dom-filereader-readystate 1431cb0ef41Sopenharmony_ci */ 1441cb0ef41Sopenharmony_ci get readyState () { 1451cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci switch (this[kState]) { 1481cb0ef41Sopenharmony_ci case 'empty': return this.EMPTY 1491cb0ef41Sopenharmony_ci case 'loading': return this.LOADING 1501cb0ef41Sopenharmony_ci case 'done': return this.DONE 1511cb0ef41Sopenharmony_ci } 1521cb0ef41Sopenharmony_ci } 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci /** 1551cb0ef41Sopenharmony_ci * @see https://w3c.github.io/FileAPI/#dom-filereader-result 1561cb0ef41Sopenharmony_ci */ 1571cb0ef41Sopenharmony_ci get result () { 1581cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci // The result attribute’s getter, when invoked, must return 1611cb0ef41Sopenharmony_ci // this's result. 1621cb0ef41Sopenharmony_ci return this[kResult] 1631cb0ef41Sopenharmony_ci } 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ci /** 1661cb0ef41Sopenharmony_ci * @see https://w3c.github.io/FileAPI/#dom-filereader-error 1671cb0ef41Sopenharmony_ci */ 1681cb0ef41Sopenharmony_ci get error () { 1691cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci // The error attribute’s getter, when invoked, must return 1721cb0ef41Sopenharmony_ci // this's error. 1731cb0ef41Sopenharmony_ci return this[kError] 1741cb0ef41Sopenharmony_ci } 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci get onloadend () { 1771cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci return this[kEvents].loadend 1801cb0ef41Sopenharmony_ci } 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci set onloadend (fn) { 1831cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci if (this[kEvents].loadend) { 1861cb0ef41Sopenharmony_ci this.removeEventListener('loadend', this[kEvents].loadend) 1871cb0ef41Sopenharmony_ci } 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci if (typeof fn === 'function') { 1901cb0ef41Sopenharmony_ci this[kEvents].loadend = fn 1911cb0ef41Sopenharmony_ci this.addEventListener('loadend', fn) 1921cb0ef41Sopenharmony_ci } else { 1931cb0ef41Sopenharmony_ci this[kEvents].loadend = null 1941cb0ef41Sopenharmony_ci } 1951cb0ef41Sopenharmony_ci } 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci get onerror () { 1981cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ci return this[kEvents].error 2011cb0ef41Sopenharmony_ci } 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci set onerror (fn) { 2041cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci if (this[kEvents].error) { 2071cb0ef41Sopenharmony_ci this.removeEventListener('error', this[kEvents].error) 2081cb0ef41Sopenharmony_ci } 2091cb0ef41Sopenharmony_ci 2101cb0ef41Sopenharmony_ci if (typeof fn === 'function') { 2111cb0ef41Sopenharmony_ci this[kEvents].error = fn 2121cb0ef41Sopenharmony_ci this.addEventListener('error', fn) 2131cb0ef41Sopenharmony_ci } else { 2141cb0ef41Sopenharmony_ci this[kEvents].error = null 2151cb0ef41Sopenharmony_ci } 2161cb0ef41Sopenharmony_ci } 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci get onloadstart () { 2191cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci return this[kEvents].loadstart 2221cb0ef41Sopenharmony_ci } 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ci set onloadstart (fn) { 2251cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci if (this[kEvents].loadstart) { 2281cb0ef41Sopenharmony_ci this.removeEventListener('loadstart', this[kEvents].loadstart) 2291cb0ef41Sopenharmony_ci } 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ci if (typeof fn === 'function') { 2321cb0ef41Sopenharmony_ci this[kEvents].loadstart = fn 2331cb0ef41Sopenharmony_ci this.addEventListener('loadstart', fn) 2341cb0ef41Sopenharmony_ci } else { 2351cb0ef41Sopenharmony_ci this[kEvents].loadstart = null 2361cb0ef41Sopenharmony_ci } 2371cb0ef41Sopenharmony_ci } 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci get onprogress () { 2401cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci return this[kEvents].progress 2431cb0ef41Sopenharmony_ci } 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ci set onprogress (fn) { 2461cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci if (this[kEvents].progress) { 2491cb0ef41Sopenharmony_ci this.removeEventListener('progress', this[kEvents].progress) 2501cb0ef41Sopenharmony_ci } 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci if (typeof fn === 'function') { 2531cb0ef41Sopenharmony_ci this[kEvents].progress = fn 2541cb0ef41Sopenharmony_ci this.addEventListener('progress', fn) 2551cb0ef41Sopenharmony_ci } else { 2561cb0ef41Sopenharmony_ci this[kEvents].progress = null 2571cb0ef41Sopenharmony_ci } 2581cb0ef41Sopenharmony_ci } 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci get onload () { 2611cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 2621cb0ef41Sopenharmony_ci 2631cb0ef41Sopenharmony_ci return this[kEvents].load 2641cb0ef41Sopenharmony_ci } 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ci set onload (fn) { 2671cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ci if (this[kEvents].load) { 2701cb0ef41Sopenharmony_ci this.removeEventListener('load', this[kEvents].load) 2711cb0ef41Sopenharmony_ci } 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci if (typeof fn === 'function') { 2741cb0ef41Sopenharmony_ci this[kEvents].load = fn 2751cb0ef41Sopenharmony_ci this.addEventListener('load', fn) 2761cb0ef41Sopenharmony_ci } else { 2771cb0ef41Sopenharmony_ci this[kEvents].load = null 2781cb0ef41Sopenharmony_ci } 2791cb0ef41Sopenharmony_ci } 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci get onabort () { 2821cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 2831cb0ef41Sopenharmony_ci 2841cb0ef41Sopenharmony_ci return this[kEvents].abort 2851cb0ef41Sopenharmony_ci } 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ci set onabort (fn) { 2881cb0ef41Sopenharmony_ci webidl.brandCheck(this, FileReader) 2891cb0ef41Sopenharmony_ci 2901cb0ef41Sopenharmony_ci if (this[kEvents].abort) { 2911cb0ef41Sopenharmony_ci this.removeEventListener('abort', this[kEvents].abort) 2921cb0ef41Sopenharmony_ci } 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ci if (typeof fn === 'function') { 2951cb0ef41Sopenharmony_ci this[kEvents].abort = fn 2961cb0ef41Sopenharmony_ci this.addEventListener('abort', fn) 2971cb0ef41Sopenharmony_ci } else { 2981cb0ef41Sopenharmony_ci this[kEvents].abort = null 2991cb0ef41Sopenharmony_ci } 3001cb0ef41Sopenharmony_ci } 3011cb0ef41Sopenharmony_ci} 3021cb0ef41Sopenharmony_ci 3031cb0ef41Sopenharmony_ci// https://w3c.github.io/FileAPI/#dom-filereader-empty 3041cb0ef41Sopenharmony_ciFileReader.EMPTY = FileReader.prototype.EMPTY = 0 3051cb0ef41Sopenharmony_ci// https://w3c.github.io/FileAPI/#dom-filereader-loading 3061cb0ef41Sopenharmony_ciFileReader.LOADING = FileReader.prototype.LOADING = 1 3071cb0ef41Sopenharmony_ci// https://w3c.github.io/FileAPI/#dom-filereader-done 3081cb0ef41Sopenharmony_ciFileReader.DONE = FileReader.prototype.DONE = 2 3091cb0ef41Sopenharmony_ci 3101cb0ef41Sopenharmony_ciObject.defineProperties(FileReader.prototype, { 3111cb0ef41Sopenharmony_ci EMPTY: staticPropertyDescriptors, 3121cb0ef41Sopenharmony_ci LOADING: staticPropertyDescriptors, 3131cb0ef41Sopenharmony_ci DONE: staticPropertyDescriptors, 3141cb0ef41Sopenharmony_ci readAsArrayBuffer: kEnumerableProperty, 3151cb0ef41Sopenharmony_ci readAsBinaryString: kEnumerableProperty, 3161cb0ef41Sopenharmony_ci readAsText: kEnumerableProperty, 3171cb0ef41Sopenharmony_ci readAsDataURL: kEnumerableProperty, 3181cb0ef41Sopenharmony_ci abort: kEnumerableProperty, 3191cb0ef41Sopenharmony_ci readyState: kEnumerableProperty, 3201cb0ef41Sopenharmony_ci result: kEnumerableProperty, 3211cb0ef41Sopenharmony_ci error: kEnumerableProperty, 3221cb0ef41Sopenharmony_ci onloadstart: kEnumerableProperty, 3231cb0ef41Sopenharmony_ci onprogress: kEnumerableProperty, 3241cb0ef41Sopenharmony_ci onload: kEnumerableProperty, 3251cb0ef41Sopenharmony_ci onabort: kEnumerableProperty, 3261cb0ef41Sopenharmony_ci onerror: kEnumerableProperty, 3271cb0ef41Sopenharmony_ci onloadend: kEnumerableProperty, 3281cb0ef41Sopenharmony_ci [Symbol.toStringTag]: { 3291cb0ef41Sopenharmony_ci value: 'FileReader', 3301cb0ef41Sopenharmony_ci writable: false, 3311cb0ef41Sopenharmony_ci enumerable: false, 3321cb0ef41Sopenharmony_ci configurable: true 3331cb0ef41Sopenharmony_ci } 3341cb0ef41Sopenharmony_ci}) 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ciObject.defineProperties(FileReader, { 3371cb0ef41Sopenharmony_ci EMPTY: staticPropertyDescriptors, 3381cb0ef41Sopenharmony_ci LOADING: staticPropertyDescriptors, 3391cb0ef41Sopenharmony_ci DONE: staticPropertyDescriptors 3401cb0ef41Sopenharmony_ci}) 3411cb0ef41Sopenharmony_ci 3421cb0ef41Sopenharmony_cimodule.exports = { 3431cb0ef41Sopenharmony_ci FileReader 3441cb0ef41Sopenharmony_ci} 345