| 'use strict' |
|
|
| const { |
| staticPropertyDescriptors, |
| readOperation, |
| fireAProgressEvent |
| } = require('./util') |
| const { |
| kState, |
| kError, |
| kResult, |
| kEvents, |
| kAborted |
| } = require('./symbols') |
| const { webidl } = require('../fetch/webidl') |
| const { kEnumerableProperty } = require('../core/util') |
|
|
| class FileReader extends EventTarget { |
| constructor () { |
| super() |
|
|
| this[kState] = 'empty' |
| this[kResult] = null |
| this[kError] = null |
| this[kEvents] = { |
| loadend: null, |
| error: null, |
| abort: null, |
| load: null, |
| progress: null, |
| loadstart: null |
| } |
| } |
|
|
| |
| |
| |
| |
| readAsArrayBuffer (blob) { |
| webidl.brandCheck(this, FileReader) |
|
|
| webidl.argumentLengthCheck(arguments, 1, { header: 'FileReader.readAsArrayBuffer' }) |
|
|
| blob = webidl.converters.Blob(blob, { strict: false }) |
|
|
| |
| |
| readOperation(this, blob, 'ArrayBuffer') |
| } |
|
|
| |
| |
| |
| |
| readAsBinaryString (blob) { |
| webidl.brandCheck(this, FileReader) |
|
|
| webidl.argumentLengthCheck(arguments, 1, { header: 'FileReader.readAsBinaryString' }) |
|
|
| blob = webidl.converters.Blob(blob, { strict: false }) |
|
|
| |
| |
| readOperation(this, blob, 'BinaryString') |
| } |
|
|
| |
| |
| |
| |
| |
| readAsText (blob, encoding = undefined) { |
| webidl.brandCheck(this, FileReader) |
|
|
| webidl.argumentLengthCheck(arguments, 1, { header: 'FileReader.readAsText' }) |
|
|
| blob = webidl.converters.Blob(blob, { strict: false }) |
|
|
| if (encoding !== undefined) { |
| encoding = webidl.converters.DOMString(encoding) |
| } |
|
|
| |
| |
| readOperation(this, blob, 'Text', encoding) |
| } |
|
|
| |
| |
| |
| |
| readAsDataURL (blob) { |
| webidl.brandCheck(this, FileReader) |
|
|
| webidl.argumentLengthCheck(arguments, 1, { header: 'FileReader.readAsDataURL' }) |
|
|
| blob = webidl.converters.Blob(blob, { strict: false }) |
|
|
| |
| |
| readOperation(this, blob, 'DataURL') |
| } |
|
|
| |
| |
| |
| abort () { |
| |
| |
| |
| if (this[kState] === 'empty' || this[kState] === 'done') { |
| this[kResult] = null |
| return |
| } |
|
|
| |
| |
| if (this[kState] === 'loading') { |
| this[kState] = 'done' |
| this[kResult] = null |
| } |
|
|
| |
| |
| |
| this[kAborted] = true |
|
|
| |
| |
|
|
| |
| fireAProgressEvent('abort', this) |
|
|
| |
| |
| if (this[kState] !== 'loading') { |
| fireAProgressEvent('loadend', this) |
| } |
| } |
|
|
| |
| |
| |
| get readyState () { |
| webidl.brandCheck(this, FileReader) |
|
|
| switch (this[kState]) { |
| case 'empty': return this.EMPTY |
| case 'loading': return this.LOADING |
| case 'done': return this.DONE |
| } |
| } |
|
|
| |
| |
| |
| get result () { |
| webidl.brandCheck(this, FileReader) |
|
|
| |
| |
| return this[kResult] |
| } |
|
|
| |
| |
| |
| get error () { |
| webidl.brandCheck(this, FileReader) |
|
|
| |
| |
| return this[kError] |
| } |
|
|
| get onloadend () { |
| webidl.brandCheck(this, FileReader) |
|
|
| return this[kEvents].loadend |
| } |
|
|
| set onloadend (fn) { |
| webidl.brandCheck(this, FileReader) |
|
|
| if (this[kEvents].loadend) { |
| this.removeEventListener('loadend', this[kEvents].loadend) |
| } |
|
|
| if (typeof fn === 'function') { |
| this[kEvents].loadend = fn |
| this.addEventListener('loadend', fn) |
| } else { |
| this[kEvents].loadend = null |
| } |
| } |
|
|
| get onerror () { |
| webidl.brandCheck(this, FileReader) |
|
|
| return this[kEvents].error |
| } |
|
|
| set onerror (fn) { |
| webidl.brandCheck(this, FileReader) |
|
|
| if (this[kEvents].error) { |
| this.removeEventListener('error', this[kEvents].error) |
| } |
|
|
| if (typeof fn === 'function') { |
| this[kEvents].error = fn |
| this.addEventListener('error', fn) |
| } else { |
| this[kEvents].error = null |
| } |
| } |
|
|
| get onloadstart () { |
| webidl.brandCheck(this, FileReader) |
|
|
| return this[kEvents].loadstart |
| } |
|
|
| set onloadstart (fn) { |
| webidl.brandCheck(this, FileReader) |
|
|
| if (this[kEvents].loadstart) { |
| this.removeEventListener('loadstart', this[kEvents].loadstart) |
| } |
|
|
| if (typeof fn === 'function') { |
| this[kEvents].loadstart = fn |
| this.addEventListener('loadstart', fn) |
| } else { |
| this[kEvents].loadstart = null |
| } |
| } |
|
|
| get onprogress () { |
| webidl.brandCheck(this, FileReader) |
|
|
| return this[kEvents].progress |
| } |
|
|
| set onprogress (fn) { |
| webidl.brandCheck(this, FileReader) |
|
|
| if (this[kEvents].progress) { |
| this.removeEventListener('progress', this[kEvents].progress) |
| } |
|
|
| if (typeof fn === 'function') { |
| this[kEvents].progress = fn |
| this.addEventListener('progress', fn) |
| } else { |
| this[kEvents].progress = null |
| } |
| } |
|
|
| get onload () { |
| webidl.brandCheck(this, FileReader) |
|
|
| return this[kEvents].load |
| } |
|
|
| set onload (fn) { |
| webidl.brandCheck(this, FileReader) |
|
|
| if (this[kEvents].load) { |
| this.removeEventListener('load', this[kEvents].load) |
| } |
|
|
| if (typeof fn === 'function') { |
| this[kEvents].load = fn |
| this.addEventListener('load', fn) |
| } else { |
| this[kEvents].load = null |
| } |
| } |
|
|
| get onabort () { |
| webidl.brandCheck(this, FileReader) |
|
|
| return this[kEvents].abort |
| } |
|
|
| set onabort (fn) { |
| webidl.brandCheck(this, FileReader) |
|
|
| if (this[kEvents].abort) { |
| this.removeEventListener('abort', this[kEvents].abort) |
| } |
|
|
| if (typeof fn === 'function') { |
| this[kEvents].abort = fn |
| this.addEventListener('abort', fn) |
| } else { |
| this[kEvents].abort = null |
| } |
| } |
| } |
|
|
| |
| FileReader.EMPTY = FileReader.prototype.EMPTY = 0 |
| |
| FileReader.LOADING = FileReader.prototype.LOADING = 1 |
| |
| FileReader.DONE = FileReader.prototype.DONE = 2 |
|
|
| Object.defineProperties(FileReader.prototype, { |
| EMPTY: staticPropertyDescriptors, |
| LOADING: staticPropertyDescriptors, |
| DONE: staticPropertyDescriptors, |
| readAsArrayBuffer: kEnumerableProperty, |
| readAsBinaryString: kEnumerableProperty, |
| readAsText: kEnumerableProperty, |
| readAsDataURL: kEnumerableProperty, |
| abort: kEnumerableProperty, |
| readyState: kEnumerableProperty, |
| result: kEnumerableProperty, |
| error: kEnumerableProperty, |
| onloadstart: kEnumerableProperty, |
| onprogress: kEnumerableProperty, |
| onload: kEnumerableProperty, |
| onabort: kEnumerableProperty, |
| onerror: kEnumerableProperty, |
| onloadend: kEnumerableProperty, |
| [Symbol.toStringTag]: { |
| value: 'FileReader', |
| writable: false, |
| enumerable: false, |
| configurable: true |
| } |
| }) |
|
|
| Object.defineProperties(FileReader, { |
| EMPTY: staticPropertyDescriptors, |
| LOADING: staticPropertyDescriptors, |
| DONE: staticPropertyDescriptors |
| }) |
|
|
| module.exports = { |
| FileReader |
| } |
|
|