Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileLoader: text honors mimetype's charset as encoding #23292

Merged
merged 9 commits into from
Jan 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/loaders/FileLoader.js
Original file line number Diff line number Diff line change
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