|
8 | 8 | * See https://github.com/eligrey/Blob.js/blob/master/LICENSE.md |
9 | 9 | */ |
10 | 10 |
|
| 11 | +function array2base64 (input) { |
| 12 | + var byteToCharMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; |
| 13 | + |
| 14 | + var output = []; |
| 15 | + |
| 16 | + for (var i = 0; i < input.length; i += 3) { |
| 17 | + var byte1 = input[i]; |
| 18 | + var haveByte2 = i + 1 < input.length; |
| 19 | + var byte2 = haveByte2 ? input[i + 1] : 0; |
| 20 | + var haveByte3 = i + 2 < input.length; |
| 21 | + var byte3 = haveByte3 ? input[i + 2] : 0; |
| 22 | + |
| 23 | + var outByte1 = byte1 >> 2; |
| 24 | + var outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4); |
| 25 | + var outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6); |
| 26 | + var outByte4 = byte3 & 0x3F; |
| 27 | + |
| 28 | + if (!haveByte3) { |
| 29 | + outByte4 = 64; |
| 30 | + |
| 31 | + if (!haveByte2) { |
| 32 | + outByte3 = 64; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + output.push( |
| 37 | + byteToCharMap[outByte1], byteToCharMap[outByte2], |
| 38 | + byteToCharMap[outByte3], byteToCharMap[outByte4] |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + return output.join(""); |
| 43 | +} |
| 44 | + |
11 | 45 | (function(global) { |
12 | 46 | (function (factory) { |
13 | 47 | if (typeof define === "function" && define.amd) { |
|
263 | 297 | } |
264 | 298 | return view; |
265 | 299 | } |
266 | | - function array2base64 (input) { |
267 | | - var byteToCharMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; |
268 | | - |
269 | | - var output = []; |
270 | | - |
271 | | - for (var i = 0; i < input.length; i += 3) { |
272 | | - var byte1 = input[i]; |
273 | | - var haveByte2 = i + 1 < input.length; |
274 | | - var byte2 = haveByte2 ? input[i + 1] : 0; |
275 | | - var haveByte3 = i + 2 < input.length; |
276 | | - var byte3 = haveByte3 ? input[i + 2] : 0; |
277 | | - |
278 | | - var outByte1 = byte1 >> 2; |
279 | | - var outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4); |
280 | | - var outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6); |
281 | | - var outByte4 = byte3 & 0x3F; |
282 | | - |
283 | | - if (!haveByte3) { |
284 | | - outByte4 = 64; |
285 | | - |
286 | | - if (!haveByte2) { |
287 | | - outByte3 = 64; |
288 | | - } |
289 | | - } |
290 | | - |
291 | | - output.push( |
292 | | - byteToCharMap[outByte1], byteToCharMap[outByte2], |
293 | | - byteToCharMap[outByte3], byteToCharMap[outByte4] |
294 | | - ); |
295 | | - } |
296 | | - |
297 | | - return output.join(""); |
298 | | - } |
299 | 300 |
|
300 | 301 | var create = Object.create || function (a) { |
301 | 302 | function c () {} |
|
384 | 385 | this.type = this.type.toLowerCase(); |
385 | 386 | } |
386 | 387 | } |
| 388 | + Blob.isPolyfill = true; |
387 | 389 |
|
388 | 390 | Blob.prototype.arrayBuffer = function () { |
389 | 391 | return Promise.resolve(this._buffer.buffer || this._buffer); |
|
415 | 417 | return a; |
416 | 418 | } |
417 | 419 |
|
| 420 | + File.isPolyfill = true; |
418 | 421 | File.prototype = create(Blob.prototype); |
419 | 422 | File.prototype.constructor = File; |
420 | 423 |
|
|
430 | 433 | return "[object File]"; |
431 | 434 | }; |
432 | 435 |
|
433 | | - /********************************************************/ |
434 | | - /* FileReader constructor */ |
435 | | - /********************************************************/ |
436 | | - function FileReader () { |
437 | | - if (!(this instanceof FileReader)) { |
438 | | - throw new TypeError("Failed to construct 'FileReader': Please use the 'new' operator, this DOM object constructor cannot be called as a function."); |
439 | | - } |
440 | | - |
441 | | - var delegate = document.createDocumentFragment(); |
442 | | - this.addEventListener = delegate.addEventListener; |
443 | | - this.dispatchEvent = function (evt) { |
444 | | - var local = this["on" + evt.type]; |
445 | | - if (typeof local === "function") local(evt); |
446 | | - delegate.dispatchEvent(evt); |
447 | | - }; |
448 | | - this.removeEventListener = delegate.removeEventListener; |
449 | | - } |
450 | | - |
451 | | - function _read (fr, blob, kind) { |
452 | | - if (!(blob instanceof Blob)) { |
453 | | - throw new TypeError("Failed to execute '" + kind + "' on 'FileReader': parameter 1 is not of type 'Blob'."); |
454 | | - } |
455 | | - |
456 | | - fr.result = ""; |
457 | | - |
458 | | - setTimeout(function () { |
459 | | - this.readyState = FileReader.LOADING; |
460 | | - fr.dispatchEvent(new Event("load")); |
461 | | - fr.dispatchEvent(new Event("loadend")); |
462 | | - }); |
463 | | - } |
464 | | - |
465 | | - FileReader.EMPTY = 0; |
466 | | - FileReader.LOADING = 1; |
467 | | - FileReader.DONE = 2; |
468 | | - FileReader.prototype.error = null; |
469 | | - FileReader.prototype.onabort = null; |
470 | | - FileReader.prototype.onerror = null; |
471 | | - FileReader.prototype.onload = null; |
472 | | - FileReader.prototype.onloadend = null; |
473 | | - FileReader.prototype.onloadstart = null; |
474 | | - FileReader.prototype.onprogress = null; |
475 | | - |
476 | | - FileReader.prototype.readAsDataURL = function (blob) { |
477 | | - _read(this, blob, "readAsDataURL"); |
478 | | - this.result = "data:" + blob.type + ";base64," + array2base64(blob._buffer); |
479 | | - }; |
480 | | - |
481 | | - FileReader.prototype.readAsText = function (blob) { |
482 | | - _read(this, blob, "readAsText"); |
483 | | - this.result = textDecode(blob._buffer); |
484 | | - }; |
485 | | - |
486 | | - FileReader.prototype.readAsArrayBuffer = function (blob) { |
487 | | - _read(this, blob, "readAsText"); |
488 | | - // return ArrayBuffer when possible |
489 | | - this.result = (blob._buffer.buffer || blob._buffer).slice(); |
490 | | - }; |
491 | | - |
492 | | - FileReader.prototype.abort = function () {}; |
493 | | - |
494 | 436 | /********************************************************/ |
495 | 437 | /* URL */ |
496 | 438 | /********************************************************/ |
| 439 | + |
497 | 440 | URL.createObjectURL = function (blob) { |
498 | 441 | return blob instanceof Blob |
499 | 442 | ? "data:" + blob.type + ";base64," + array2base64(blob._buffer) |
500 | 443 | : createObjectURL.call(URL, blob); |
501 | 444 | }; |
| 445 | + URL.createObjectURL.isPolyfill = true; |
502 | 446 |
|
503 | 447 | URL.revokeObjectURL = function (url) { |
504 | 448 | revokeObjectURL && revokeObjectURL.call(URL, url); |
505 | 449 | }; |
| 450 | + URL.revokeObjectURL.isPolyfill = true; |
506 | 451 |
|
507 | 452 | /********************************************************/ |
508 | 453 | /* XHR */ |
|
521 | 466 |
|
522 | 467 | exports.Blob = Blob; |
523 | 468 | exports.File = File; |
524 | | - exports.FileReader = FileReader; |
525 | | - exports.URL = URL; |
526 | 469 | } |
527 | 470 |
|
528 | 471 | function fixFileAndXHR () { |
529 | | - var isIE = !!global.ActiveXObject || ( |
| 472 | + var isIE = !!global.ActiveXObject || (typeof document !== "undefined" && |
530 | 473 | "-ms-scroll-limit" in document.documentElement.style && |
531 | 474 | "-ms-ime-align" in document.documentElement.style |
532 | 475 | ); |
|
549 | 492 | try { |
550 | 493 | new File([], ""); |
551 | 494 | exports.File = global.File; |
552 | | - exports.FileReader = global.FileReader; |
553 | 495 | } catch (e) { |
554 | 496 | try { |
555 | 497 | exports.File = new Function("class File extends Blob {" + |
|
563 | 505 | "return new File([], \"\"), File" |
564 | 506 | )(); |
565 | 507 | } catch (e) { |
566 | | - exports.File = function (b, d, c) { |
| 508 | + exports.File = function File(b, d, c) { |
567 | 509 | var blob = new Blob(b, c); |
568 | 510 | var t = c && void 0 !== c.lastModified ? new Date(c.lastModified) : new Date(); |
569 | 511 |
|
|
581 | 523 | return blob; |
582 | 524 | }; |
583 | 525 | } |
| 526 | + exports.File.isPolyfill = true; |
584 | 527 | } |
585 | 528 | } |
586 | 529 |
|
|
594 | 537 | FakeBlobBuilder(); |
595 | 538 | } |
596 | 539 |
|
| 540 | + /********************************************************/ |
| 541 | + /* FileReader constructor */ |
| 542 | + /********************************************************/ |
| 543 | + function FileReader () { |
| 544 | + if (!(this instanceof FileReader)) { |
| 545 | + throw new TypeError("Failed to construct 'FileReader': Please use the 'new' operator, this DOM object constructor cannot be called as a function."); |
| 546 | + } |
| 547 | + |
| 548 | + var delegate = document.createDocumentFragment(); |
| 549 | + this.addEventListener = delegate.addEventListener; |
| 550 | + this.dispatchEvent = function (evt) { |
| 551 | + var local = this["on" + evt.type]; |
| 552 | + if (typeof local === "function") local(evt); |
| 553 | + delegate.dispatchEvent(evt); |
| 554 | + }; |
| 555 | + this.removeEventListener = delegate.removeEventListener; |
| 556 | + } |
| 557 | + |
| 558 | + function _read (fr, blob, kind) { |
| 559 | + if (!(blob instanceof exports.Blob)) { |
| 560 | + throw new TypeError("Failed to execute '" + kind + "' on 'FileReader': parameter 1 is not of type 'Blob'."); |
| 561 | + } |
| 562 | + |
| 563 | + fr.result = ""; |
| 564 | + |
| 565 | + setTimeout(function () { |
| 566 | + this.readyState = FileReader.LOADING; |
| 567 | + fr.dispatchEvent(new Event("load")); |
| 568 | + fr.dispatchEvent(new Event("loadend")); |
| 569 | + }); |
| 570 | + } |
| 571 | + |
| 572 | + FileReader.EMPTY = 0; |
| 573 | + FileReader.LOADING = 1; |
| 574 | + FileReader.DONE = 2; |
| 575 | + FileReader.prototype.error = null; |
| 576 | + FileReader.prototype.onabort = null; |
| 577 | + FileReader.prototype.onerror = null; |
| 578 | + FileReader.prototype.onload = null; |
| 579 | + FileReader.prototype.onloadend = null; |
| 580 | + FileReader.prototype.onloadstart = null; |
| 581 | + FileReader.prototype.onprogress = null; |
| 582 | + |
| 583 | + FileReader.prototype.readAsDataURL = function (blob) { |
| 584 | + _read(this, blob, "readAsDataURL"); |
| 585 | + this.result = "data:" + blob.type + ";base64," + array2base64(blob._buffer); |
| 586 | + }; |
| 587 | + |
| 588 | + FileReader.prototype.readAsText = function (blob) { |
| 589 | + _read(this, blob, "readAsText"); |
| 590 | + this.result = textDecode(blob._buffer); |
| 591 | + }; |
| 592 | + |
| 593 | + FileReader.prototype.readAsArrayBuffer = function (blob) { |
| 594 | + _read(this, blob, "readAsText"); |
| 595 | + // return ArrayBuffer when possible |
| 596 | + this.result = (blob._buffer.buffer || blob._buffer).slice(); |
| 597 | + }; |
| 598 | + |
| 599 | + FileReader.prototype.abort = function () {}; |
| 600 | + |
| 601 | + exports.FileReader = global.FileReader || FileReader; |
| 602 | + exports.URL = global.URL || URL; |
| 603 | + |
597 | 604 | if (strTag) { |
598 | 605 | if (!exports.File.prototype[strTag]) exports.File.prototype[strTag] = "File"; |
599 | 606 | if (!exports.Blob.prototype[strTag]) exports.Blob.prototype[strTag] = "Blob"; |
|
0 commit comments