From ff87aeed7f642fd14010aca84f9bcee666ae343d Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Thu, 9 May 2024 11:16:52 -0700 Subject: [PATCH] rename option, add tests, always warn Signed-off-by: James Hillyerd --- envelope.go | 15 +++++++++------ envelope_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ options.go | 14 +++++++------- parser.go | 2 +- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/envelope.go b/envelope.go index f0e7c78..bfade4f 100644 --- a/envelope.go +++ b/envelope.go @@ -196,16 +196,19 @@ func (p Parser) EnvelopeFromPart(root *Part) (*Envelope, error) { } // Down-convert HTML to text if necessary - if e.Text == "" && e.HTML != "" && !p.skipHTML2TextDownConversion { + if e.Text == "" && e.HTML != "" { // We always warn when this happens e.Root.addWarning( ErrorPlainTextFromHTML, "Message did not contain a text/plain part") - var err error - if e.Text, err = html2text.FromString(e.HTML); err != nil { - e.Text = "" // Down-conversion shouldn't fail - p := e.Root.BreadthMatchFirst(matchHTMLBodyPart) - p.addError(ErrorPlainTextFromHTML, "Failed to downconvert HTML: %v", err) + + if !p.disableTextConversion { + var err error + if e.Text, err = html2text.FromString(e.HTML); err != nil { + e.Text = "" // Down-conversion shouldn't fail + p := e.Root.BreadthMatchFirst(matchHTMLBodyPart) + p.addError(ErrorPlainTextFromHTML, "Failed to downconvert HTML: %v", err) + } } } diff --git a/envelope_test.go b/envelope_test.go index bc19759..bc351e6 100644 --- a/envelope_test.go +++ b/envelope_test.go @@ -516,6 +516,51 @@ func TestParseHTMLOnlyInline(t *testing.T) { } } +func TestParseHTMLOnlyInlineTextDisabled(t *testing.T) { + msg := test.OpenTestData("mail", "html-only-inline.raw") + + parser := enmime.NewParser(enmime.DisableTextConversion(true)) + e, err := parser.ReadEnvelope(msg) + if err != nil { + t.Fatal("Failed to parse MIME:", err) + } + + if len(e.Errors) == 1 { + want := enmime.ErrorPlainTextFromHTML + got := e.Errors[0].Name + if got != want { + t.Errorf("e.Errors[0] got: %v, want: %v", got, want) + } + } else { + t.Errorf("len(e.Errors) got: %v, want: 1", len(e.Errors)) + } + + if e.Text != "" { + t.Errorf("Downconverted Text was %q, should be empty", e.Text) + } + + want := ">Test of HTML section<" + if !strings.Contains(e.HTML, want) { + t.Errorf("HTML: %q should contain %q", e.HTML, want) + } + + if len(e.Inlines) != 1 { + t.Error("Should one inline, got:", len(e.Inlines)) + } + if len(e.Attachments) > 0 { + t.Fatal("Should have no attachments, got:", len(e.Attachments)) + } + + want = "favicon.png" + got := e.Inlines[0].FileName + if got != want { + t.Error("FileName got:", got, "want:", want) + } + if !bytes.HasPrefix(e.Inlines[0].Content, []byte{0x89, 'P', 'N', 'G'}) { + t.Error("Inline should have correct content") + } +} + func TestParseInlineMultipart(t *testing.T) { msg := test.OpenTestData("mail", "inlinemultipart.raw") e, err := enmime.ReadEnvelope(msg) diff --git a/options.go b/options.go index 61f6123..b7e6fd2 100644 --- a/options.go +++ b/options.go @@ -89,14 +89,14 @@ func StripMediaTypeInvalidCharacters(stripMediaTypeInvalidCharacters bool) Optio return stripMediaTypeInvalidCharactersOption(stripMediaTypeInvalidCharacters) } -type skipHTML2TextDownConversionOption bool +type disableTextConversionOption bool -func (o skipHTML2TextDownConversionOption) apply(p *Parser) { - p.skipHTML2TextDownConversion = bool(o) +func (o disableTextConversionOption) apply(p *Parser) { + p.disableTextConversion = bool(o) } -// SkipHTML2TextDownConversion sets skipHTML2TextDownConversion option. If true, there will be no automatic down conversion -// of HTML 2 Text. -func SkipHTML2TextDownConversion(skipHTML2TextDownConversion bool) Option { - return skipHTML2TextDownConversionOption(skipHTML2TextDownConversion) +// DisableTextConversion sets the disableTextConversion option. When true, there will be no +// automated down conversion of HTML to text when a plain/text body is missing. +func DisableTextConversion(disableTextConversion bool) Option { + return disableTextConversionOption(disableTextConversion) } diff --git a/parser.go b/parser.go index 2fd01cd..2ca6939 100644 --- a/parser.go +++ b/parser.go @@ -26,7 +26,7 @@ type Parser struct { rawContent bool customParseMediaType CustomParseMediaType stripMediaTypeInvalidCharacters bool - skipHTML2TextDownConversion bool + disableTextConversion bool } // defaultParser is a Parser with default configuration.