Skip to content

Commit

Permalink
Add parser option to disable HTML to Text down conversion (#334)
Browse files Browse the repository at this point in the history
* Add parser option to skip HTML to Text down conversion
* option, add tests, always warn

---------

Signed-off-by: James Hillyerd <james@hillyerd.com>
Co-authored-by: tvdv <2144030+tvdv@users.noreply.github.com>
Co-authored-by: James Hillyerd <james@hillyerd.com>
  • Loading branch information
3 people committed May 9, 2024
1 parent 5139f90 commit 8f65d85
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
13 changes: 8 additions & 5 deletions envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,14 @@ func (p Parser) EnvelopeFromPart(root *Part) (*Envelope, error) {
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
12 changes: 12 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,15 @@ func (o stripMediaTypeInvalidCharactersOption) apply(p *Parser) {
func StripMediaTypeInvalidCharacters(stripMediaTypeInvalidCharacters bool) Option {
return stripMediaTypeInvalidCharactersOption(stripMediaTypeInvalidCharacters)
}

type disableTextConversionOption bool

func (o disableTextConversionOption) apply(p *Parser) {
p.disableTextConversion = bool(o)
}

// 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)
}
1 change: 1 addition & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Parser struct {
rawContent bool
customParseMediaType CustomParseMediaType
stripMediaTypeInvalidCharacters bool
disableTextConversion bool
}

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

0 comments on commit 8f65d85

Please sign in to comment.