Skip to content

Commit a859cf3

Browse files
committed
add quoteToHtml, transformToDocument ext. methods
Also add transformToInnerHtml extension method.
1 parent d650bca commit a859cf3

File tree

1 file changed

+81
-7
lines changed

1 file changed

+81
-7
lines changed

lib/src/enough_mail_html_base.dart

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class TransformConfiguration {
2020
final String emptyMessageText;
2121

2222
/// The template for converting a plain text message into a HTML document.
23+
///
2324
/// Requires to have the string `{text}` into which the plain text message is pasted, e.g. `<p>{text}</p>`.
2425
final String plainTextHtmlTemplate;
2526

@@ -56,6 +57,7 @@ class TransformConfiguration {
5657
null);
5758

5859
/// Provides an easy optopn to customize a configuration.
60+
///
5961
/// Any specified [customDomTransformers] or [customTextTransformers] are being appended to the standard transformers.
6062
static TransformConfiguration create({
6163
bool blockExternalImages,
@@ -107,6 +109,7 @@ abstract class DomTransformer {
107109
const DomTransformer();
108110

109111
/// Uses the `DOM` [document] and specified [message] to transform the `document`.
112+
///
110113
/// All changes will be visible to subsequenc transformers.
111114
void process(Document document, MimeMessage message,
112115
TransformConfiguration configuration);
@@ -170,21 +173,92 @@ abstract class TextTransformer {
170173

171174
/// Extends the MimeMessage with the [transformToHtml] method.
172175
extension HtmlTransform on MimeMessage {
173-
/// Transforms this message to HTML code.
176+
/// Transforms this message to Document.
177+
///
174178
/// Set [blockExternalImages] to `true` in case external images should be blocked.
175179
/// Optionally specify the [maxImageWidth] to set the maximum width for embedded images.
176180
/// Optionally specify the [emptyMessageText] for messages that contain no other content.
177181
/// Optionally specify the [transformConfiguration] to control all aspects of the transformation - in that case other parameters are ignored.
178-
String transformToHtml(
179-
{bool blockExternalImages,
180-
int maxImageWidth,
181-
String emptyMessageText,
182-
TransformConfiguration transformConfiguration}) {
182+
Document transformToDocument({
183+
bool blockExternalImages,
184+
int maxImageWidth,
185+
String emptyMessageText,
186+
TransformConfiguration transformConfiguration,
187+
}) {
183188
transformConfiguration ??= TransformConfiguration.create(
184189
blockExternalImages: blockExternalImages,
185190
emptyMessageText: emptyMessageText,
186191
maxImageWidth: maxImageWidth);
187192
final transformer = MimeMessageTransformer(transformConfiguration);
188-
return transformer.toHtml(this);
193+
return transformer.toDocument(this);
194+
}
195+
196+
/// Transforms this message to HTML code.
197+
///
198+
/// Set [blockExternalImages] to `true` in case external images should be blocked.
199+
/// Optionally specify the [maxImageWidth] to set the maximum width for embedded images.
200+
/// Optionally specify the [emptyMessageText] for messages that contain no other content.
201+
/// Optionally specify the [transformConfiguration] to control all aspects of the transformation - in that case other parameters are ignored.
202+
String transformToHtml({
203+
bool blockExternalImages,
204+
int maxImageWidth,
205+
String emptyMessageText,
206+
TransformConfiguration transformConfiguration,
207+
}) {
208+
final document = transformToDocument(
209+
blockExternalImages: blockExternalImages,
210+
maxImageWidth: maxImageWidth,
211+
emptyMessageText: emptyMessageText,
212+
transformConfiguration: transformConfiguration);
213+
return document.outerHtml;
214+
}
215+
216+
/// Transforms this message to the innter BODY HTML code.
217+
///
218+
/// Set [blockExternalImages] to `true` in case external images should be blocked.
219+
/// Optionally specify the [maxImageWidth] to set the maximum width for embedded images.
220+
/// Optionally specify the [emptyMessageText] for messages that contain no other content.
221+
/// Optionally specify the [transformConfiguration] to control all aspects of the transformation - in that case other parameters are ignored.
222+
String transformToBodyInnerHtml({
223+
bool blockExternalImages,
224+
int maxImageWidth,
225+
String emptyMessageText,
226+
TransformConfiguration transformConfiguration,
227+
}) {
228+
final document = transformToDocument(
229+
blockExternalImages: blockExternalImages,
230+
maxImageWidth: maxImageWidth,
231+
emptyMessageText: emptyMessageText,
232+
transformConfiguration: transformConfiguration);
233+
return document.body.innerHtml;
234+
}
235+
236+
/// Quotes the body of this message for editing HTML.
237+
///
238+
/// Optionally specify the [quoteHeaderTemplate], defaults to `MailConventions.defaultReplyHeaderTemplate`, for forwarding you can use the `MailConventions.defaultForwardHeaderTemplate`.
239+
/// Set [blockExternalImages] to `true` in case external images should be blocked.
240+
/// Optionally specify the [maxImageWidth] to set the maximum width for embedded images.
241+
/// Optionally specify the [emptyMessageText] for messages that contain no other content.
242+
/// Optionally specify the [transformConfiguration] to control all aspects of the transformation - in that case other parameters are ignored.
243+
String quoteToHtml({
244+
String quoteHeaderTemplate,
245+
bool blockExternalImages,
246+
int maxImageWidth,
247+
String emptyMessageText,
248+
TransformConfiguration transformConfiguration,
249+
}) {
250+
quoteHeaderTemplate ??= MailConventions.defaultReplyHeaderTemplate;
251+
final quoteHeader = MessageBuilder.fillTemplate(quoteHeaderTemplate, this)
252+
.replaceAll('&', '&amp;')
253+
.replaceAll('>', '&gt;')
254+
.replaceAll('<', '&lt;')
255+
.replaceAll('"', '&quot;')
256+
.replaceAll('\r\n', '<br/>');
257+
final document = transformToDocument(
258+
blockExternalImages: blockExternalImages,
259+
maxImageWidth: maxImageWidth,
260+
emptyMessageText: emptyMessageText,
261+
transformConfiguration: transformConfiguration);
262+
return '<p><br/></p><blockquote>$quoteHeader<br/>${document.body.innerHtml}</blockquote>';
189263
}
190264
}

0 commit comments

Comments
 (0)