-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks great! I didn't know about this feature of TextDecoder. I've added some comments above. Also I found these links useful for understanding the mimetype format:
https://stackoverflow.com/questions/63909392/content-type-multiple-parameters
https://www.w3.org/Protocols/rfc1341/4_Content-Type.html
Edit: Also do you have a file to recommend trying testing this with?
I updated fileloader // for text
loader.setResponseType( 'text', 'shift_jis' )
// for doc
loader.setResponseType( 'document', 'text/xml' )
// deprecated
loader.setResponseType( 'document' ).setMimeType( 'text/xml' ) I make this change because the old api is misleading, setmimetype is irrelevant to 'text', instead, mimetype was just used to config the domparser for 'document'.. you can see that mmdloader author also got confused: three.js/examples/jsm/loaders/MMDLoader.js Lines 308 to 324 in bd42765
( he attempted to set encoding 'shift_jis' for a 'text' respond using mimetype... ) summon @gkjohnson @Mugen87 |
@gkjohnson https://jsfiddle.net/saue56qm/ ( http/1.1 https://httpwg.org/specs/rfc7231.html#representation.metadata ) |
I'm not sure if fully follow the motivation behind the change and the code is a bit unclear to me with the difference in behavior between document and text in the function. Perhaps @mrdoob or @Mugen87 can chime in on the API change? I think I'd prefer to avoid them for this fix. |
If I'm right, mimetype has an effect to text type response according to the spec... |
I vote for a solution which does not change the API. |
3js had changed to use |
Ah, I think I understand what you meant now.
I, MMDLoader author, just haven't updated the code yet since FileLoader started to use fetch. |
Response kind should come w/ "pares method" (if needed), they should come in one go, e.g. loader.setResponseType( 'text', 'shift_jis' ) // jp text
loader.setResponseType( 'document', 'image/svg+xml' ) // xmldocument
loader.setResponseType( 'blob', 'video/mp4' ) // mp4 blob
loader.setResponseType( 'arraybuffer' )
loader.setResponseType( 'json' ) current api is misleading to me, consider: loader.setResponseType( 'arraybuffer' ).setMimeType( 'video/mp4' ) // blob? / ab ?
loader.setResponseType( 'text' ).setMimeType( 'image/svg+xml' ) // xmldocument? / plain text? |
By the way, do we really want mimeType and textEncoding configuration? IIRC, the reason why But now loader
.setResponseType('arraybuffer')
.load(url, buffer => {
const decoder = new TextDecoder(encodingType);
const text = decoder.decode(buffer);
});
or
loader
.setResponseType('text')
.load(url, text => {
const parser = new DOMParser();
const doc = parser.parseFromString(text, mimeType);
}); That means, regarding #23287, the problem can be fixed in And if I'm right, only using UTF-8 encoded resource is encouraged in JavaScript, so textEncoding should be rarely used. Actually it was suggested that we may be able to remove |
@takahirox nonutf8 text is so rare.. I agree that this should be handled by specific loaders in the future. for now, in this pr, i will only focus on sniffing encoding from mimetype, and make 'text' kind response honors the encoding. |
text
honors mimetype's charset as encoding
Code looks good to me but it would be good if @takahirox could verify since I'm unfamiliar with what exactly was breaking with MMDLoader and how to test it. |
@gkjohnson Thanks for review. The mmdloader broke due to the fact that current fileloader doesn't take encoding into account for @takahirox plesae help checking if vpds are parsed correctly, thanks. https://raw.githack.com/ycw/three.js/fileloader-sniff-encoding/examples/?q=mmd#webgl_loader_mmd_pose |
@ycw Yes, that example looks good, thanks. |
Thanks! |
Related issue: #23287