LoaderUtils.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. class LoaderUtils {
  2. static decodeText( array ) {
  3. if ( typeof TextDecoder !== 'undefined' ) {
  4. return new TextDecoder().decode( array );
  5. }
  6. // Avoid the String.fromCharCode.apply(null, array) shortcut, which
  7. // throws a "maximum call stack size exceeded" error for large arrays.
  8. let s = '';
  9. for ( let i = 0, il = array.length; i < il; i ++ ) {
  10. // Implicitly assumes little-endian.
  11. s += String.fromCharCode( array[ i ] );
  12. }
  13. try {
  14. // merges multi-byte utf-8 characters.
  15. return decodeURIComponent( escape( s ) );
  16. } catch ( e ) { // see #16358
  17. return s;
  18. }
  19. }
  20. static extractUrlBase( url ) {
  21. const index = url.lastIndexOf( '/' );
  22. if ( index === - 1 ) return './';
  23. return url.slice( 0, index + 1 );
  24. }
  25. static resolveURL( url, path ) {
  26. // Invalid URL
  27. if ( typeof url !== 'string' || url === '' ) return '';
  28. // Host Relative URL
  29. if ( /^https?:\/\//i.test( path ) && /^\//.test( url ) ) {
  30. path = path.replace( /(^https?:\/\/[^\/]+).*/i, '$1' );
  31. }
  32. // Absolute URL http://,https://,//
  33. if ( /^(https?:)?\/\//i.test( url ) ) return url;
  34. // Data URI
  35. if ( /^data:.*,.*$/i.test( url ) ) return url;
  36. // Blob URL
  37. if ( /^blob:.*$/i.test( url ) ) return url;
  38. // Relative URL
  39. return path + url;
  40. }
  41. }
  42. export { LoaderUtils };
粤ICP备19079148号