fix(utf8): strip only one leading BOM on decode - #415
Open
spokodev wants to merge 1 commit into
Open
Conversation
TextDecoder (ignoreBOM: false) already strips a leading BOM, and the StripBOM wrapper strips one too, so decoding UTF-8 dropped a second leading U+FEFF that is part of the content. Let the wrapper be the sole BOM stripper (matches utf-16/utf-32, which strip exactly one).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Decoding UTF-8 strips two leading BOMs instead of one, dropping a U+FEFF that belongs to the content.
For UTF-8 the internal
TextDecoderis created withignoreBOM: false, so it already removes one leading BOM. The decoder is then also wrapped by iconv-lite'sStripBOM(added for every bomAware codec unlessstripBOM: false), which removes a leading U+FEFF as well. When the byte stream isBOMfollowed by content that begins with U+FEFF, both strip and the content's U+FEFF is lost.Repro:
utf-16le/utf-16be/utf-32*all strip exactly one BOM here; onlyutf-8strips two.Fix: let
StripBOMbe the sole BOM stripper by settingignoreBOM: trueon the internal TextDecoder. The wrapper is present whenever a BOM should be removed (and absent forstripBOM: false, where the BOM should be kept), so a single strip is correct in every case. ThestripBOM: falseandstripBOMcallback paths are unchanged.Oracle: the WHATWG Encoding Standard /
TextDecoder("utf-8")removes exactly one leading BOM.Adds a regression test.