@@ -70,3 +70,86 @@ func TestReplyInfoFromMessage_IncludeBody_DoesNotTreatHTMLAsPlain(t *testing.T)
7070 t .Fatalf ("expected BodyHTML to be set" )
7171 }
7272}
73+
74+ func TestApplyQuoteToBodiesDerivesPlainReplyFromHTML (t * testing.T ) {
75+ plain , html := applyQuoteToBodies (
76+ "" ,
77+ "<p>HTML <strong>reply</strong></p>" ,
78+ true ,
79+ & replyInfo {
80+ FromAddr : "sender@example.com" ,
81+ Date : "Mon, 1 Jan 2024 00:00:00 +0000" ,
82+ Body : "Original plain" ,
83+ BodyHTML : "<p>Original HTML</p>" ,
84+ },
85+ )
86+
87+ if ! strings .Contains (plain , "HTML reply" ) {
88+ t .Fatalf ("plain body omitted derived reply text: %q" , plain )
89+ }
90+ if ! strings .Contains (plain , "> Original plain" ) {
91+ t .Fatalf ("plain body omitted quoted original: %q" , plain )
92+ }
93+ if ! strings .Contains (html , "<p>HTML <strong>reply</strong></p>" ) || ! strings .Contains (html , "gmail_quote" ) {
94+ t .Fatalf ("HTML body missing reply or quote: %q" , html )
95+ }
96+ }
97+
98+ func TestApplyQuoteToBodiesOmitsNonVisibleHTMLFromPlainReply (t * testing.T ) {
99+ plain , _ := applyQuoteToBodies (
100+ "" ,
101+ `<!doctype html><html><head><title>Hidden title</title><style>.secret { color: red; }</style><script>alert("hidden")</script></head><body><p>Visible reply</p></body></html>` ,
102+ true ,
103+ & replyInfo {
104+ Body : "Original plain" ,
105+ BodyHTML : "<p>Original HTML</p>" ,
106+ },
107+ )
108+
109+ if ! strings .Contains (plain , "Visible reply" ) || ! strings .Contains (plain , "> Original plain" ) {
110+ t .Fatalf ("plain body missing visible reply or quote: %q" , plain )
111+ }
112+ for _ , hidden := range []string {"Hidden title" , ".secret" , `alert("hidden")` } {
113+ if strings .Contains (plain , hidden ) {
114+ t .Fatalf ("plain body included non-visible HTML %q: %q" , hidden , plain )
115+ }
116+ }
117+ }
118+
119+ func TestApplyQuoteToBodiesDerivesPlainQuoteFromHTMLOriginal (t * testing.T ) {
120+ plain , html := applyQuoteToBodies (
121+ "" ,
122+ "<p>HTML reply</p>" ,
123+ true ,
124+ & replyInfo {
125+ FromAddr : "sender@example.com" ,
126+ BodyHTML : "<p>HTML-only original</p>" ,
127+ },
128+ )
129+
130+ if ! strings .Contains (plain , "HTML reply" ) || ! strings .Contains (plain , "> HTML-only original" ) {
131+ t .Fatalf ("plain body missing derived reply or quote: %q" , plain )
132+ }
133+ if ! strings .Contains (html , "<p>HTML reply</p>" ) || ! strings .Contains (html , "HTML-only original" ) {
134+ t .Fatalf ("HTML body missing reply or quote: %q" , html )
135+ }
136+ }
137+
138+ func TestApplyQuoteToBodiesKeepsImageOnlyReplyHTMLOnly (t * testing.T ) {
139+ plain , html := applyQuoteToBodies (
140+ "" ,
141+ `<img src="cid:reply-image">` ,
142+ true ,
143+ & replyInfo {
144+ Body : "Original plain" ,
145+ BodyHTML : "<p>Original HTML</p>" ,
146+ },
147+ )
148+
149+ if strings .TrimSpace (plain ) != "" {
150+ t .Fatalf ("image-only HTML reply produced quote-only plain alternative: %q" , plain )
151+ }
152+ if ! strings .Contains (html , `cid:reply-image` ) || ! strings .Contains (html , "gmail_quote" ) {
153+ t .Fatalf ("HTML body missing reply image or quote: %q" , html )
154+ }
155+ }
0 commit comments