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

feat: add option to disable character detection #342

Merged
merged 2 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ func (o disableTextConversionOption) apply(p *Parser) {
func DisableTextConversion(disableTextConversion bool) Option {
return disableTextConversionOption(disableTextConversion)
}

type disableCharacterDetectionOption bool

func (o disableCharacterDetectionOption) apply(p *Parser) {
p.disableCharacterDetection = bool(o)
}

// DisableCharacterDetection sets the disableCharacterDetection option. When true, the parser will use the
// defined character set if it is defined in the message part.
func DisableCharacterDetection(disableCharacterDetection bool) Option {
return disableCharacterDetectionOption(disableCharacterDetection)
}
1 change: 1 addition & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Parser struct {
customParseMediaType CustomParseMediaType
stripMediaTypeInvalidCharacters bool
disableTextConversion bool
disableCharacterDetection bool
}

// defaultParser is a Parser with default configuration.
Expand Down
3 changes: 2 additions & 1 deletion part.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ func (p *Part) convertFromDetectedCharset(r io.Reader, readPartErrorPolicy ReadP
// Restore r.
r = bytes.NewReader(buf)

if cs == nil || cs.Confidence < minCharsetConfidence || len(bytes.Runes(buf)) < minCharsetRuneLength {
if (p.parser.disableCharacterDetection && p.Charset != "") ||
(cs == nil || cs.Confidence < minCharsetConfidence || len(bytes.Runes(buf)) < minCharsetRuneLength) {
// Low confidence or not enough characters, use declared character set.
return p.convertFromStatedCharset(r), nil
}
Expand Down
25 changes: 25 additions & 0 deletions part_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,3 +1318,28 @@ func TestCtypeInvalidCharacters(t *testing.T) {

test.ComparePart(t, p, wantp)
}

func TestDisableCharacterDetectionPart(t *testing.T) {
var wantp *enmime.Part

// chardet considers this test file to be ISO-8859-1.
r := test.OpenTestData("parts", "chardet-detection.raw")
parser := enmime.NewParser(enmime.DisableCharacterDetection(true))
p, err := parser.ReadParts(r)

// Examine root
if err != nil {
t.Fatalf("Unexpected parse error: %+v", err)
}
if p == nil {
t.Fatal("Root node should not be nil")
}

wantp = &enmime.Part{
ContentType: "text/plain",
PartID: "0",
Charset: "utf-8",
}

test.ComparePart(t, p, wantp)
}
8 changes: 8 additions & 0 deletions testdata/parts/chardet-detection.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Loggen Sie sich ein, um die Einladung zu akzeptieren oder geben Sie den fol=
gen1233

Nachricht:
=C3=B6o=C3=B6o
Loading