Skip to content

Commit

Permalink
rename option, add tests, always warn
Browse files Browse the repository at this point in the history
Signed-off-by: James Hillyerd <james@hillyerd.com>
  • Loading branch information
jhillyerd committed May 9, 2024
1 parent 6578c9b commit ff87aee
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
15 changes: 9 additions & 6 deletions envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
45 changes: 45 additions & 0 deletions envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Parser struct {
rawContent bool
customParseMediaType CustomParseMediaType
stripMediaTypeInvalidCharacters bool
skipHTML2TextDownConversion bool
disableTextConversion bool
}

// defaultParser is a Parser with default configuration.
Expand Down

0 comments on commit ff87aee

Please sign in to comment.