Skip to content

Commit

Permalink
FileLoader: text honors mimetype's charset as encoding (#23292)
Browse files Browse the repository at this point in the history
* honor encoding

* npm run lint-fix

* cmp idx

* lint

* textual encoding

* record state

* new api `.setResponseType`

* rollback mmdloader

* sniff encoding for 'text' kind
  • Loading branch information
ycw committed Jan 24, 2022
1 parent d9a9066 commit 55d4f24
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/loaders/FileLoader.js
Expand Up @@ -69,6 +69,10 @@ class FileLoader extends Loader {
// An abort controller could be added within a future PR
} );

// record states ( avoid data race )
const mimeType = this.mimeType;
const responseType = this.responseType;

// start the fetch
fetch( req )
.then( response => {
Expand Down Expand Up @@ -147,7 +151,7 @@ class FileLoader extends Loader {
} )
.then( response => {

switch ( this.responseType ) {
switch ( responseType ) {

case 'arraybuffer':

Expand All @@ -163,7 +167,7 @@ class FileLoader extends Loader {
.then( text => {

const parser = new DOMParser();
return parser.parseFromString( text, this.mimeType );
return parser.parseFromString( text, mimeType );

} );

Expand All @@ -173,7 +177,20 @@ class FileLoader extends Loader {

default:

return response.text();
if ( mimeType === undefined ) {

return response.text();

} else {

// sniff encoding
const re = /charset="?([^;"\s]*)"?/i;
const exec = re.exec( mimeType );
const label = exec && exec[ 1 ] ? exec[ 1 ].toLowerCase() : undefined;
const decoder = new TextDecoder( label );
return response.arrayBuffer().then( ab => decoder.decode( ab ) );

}

}

Expand Down

0 comments on commit 55d4f24

Please sign in to comment.