TF-1961 Refactor html transformer

(cherry picked from commit 0272e45226ddb12827b7ef4820b00632758c353a)
This commit is contained in:
dab246
2023-08-16 12:02:34 +07:00
committed by Dat Vu
parent 289d5bbbe2
commit 1af33dd20d
14 changed files with 134 additions and 130 deletions
@@ -1,5 +1,5 @@
import 'package:core/core.dart';
import 'package:core/data/network/dio_client.dart';
import 'package:html/dom.dart';
/// Transforms the HTML DOM.
@@ -10,13 +10,11 @@ abstract class DomTransformer {
/// Uses the `DOM` [document] to transform the `document`.
///
/// All changes will be visible to subsequent transformers.
Future<void> process(
Document document,
{
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient,
}
);
Future<void> process({
required Document document,
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient,
});
/// Adds a HEAD element if necessary
void ensureDocumentHeadIsAvailable(Document document) {
@@ -6,8 +6,8 @@ class AddTargetBlankInTagATransformer extends DomTransformer {
const AddTargetBlankInTagATransformer();
@override
Future<void> process(
Document document, {
Future<void> process({
required Document document,
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient,
}) async {
@@ -1,22 +1,19 @@
import 'package:core/data/network/dio_client.dart';
import 'package:core/presentation/utils/html_transformer/base/dom_transformer.dart';
import 'package:core/presentation/utils/html_transformer/html_template.dart';
import 'package:html/dom.dart';
import '../html_template.dart';
class AddTooltipLinkTransformer extends DomTransformer {
const AddTooltipLinkTransformer();
@override
Future<void> process(
Document document,
{
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}
) async {
Future<void> process({
required Document document,
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}) async {
final linkElements = document.querySelectorAll('a[href^="http"]');
await Future.wait(linkElements.map((linkElement) async {
_addToolTipWhenHoverLink(linkElement);
@@ -8,13 +8,11 @@ class BlockCodeTransformer extends DomTransformer {
const BlockCodeTransformer();
@override
Future<void> process(
Document document,
{
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}
) async {
Future<void> process({
required Document document,
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}) async {
final codeElements = document.getElementsByTagName('pre');
await Future.wait(codeElements.map((element) async {
element.attributes['style'] = '''
@@ -8,13 +8,11 @@ class BlockQuotedTransformer extends DomTransformer {
const BlockQuotedTransformer();
@override
Future<void> process(
Document document,
{
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}
) async {
Future<void> process({
required Document document,
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}) async {
final quotedElements = document.getElementsByTagName('blockquote');
await Future.wait(quotedElements.map((quotedElement) async {
quotedElement.attributes['style'] = '''
@@ -5,7 +5,6 @@ import 'package:core/data/network/dio_client.dart';
import 'package:core/data/utils/compress_file_utils.dart';
import 'package:core/presentation/extensions/html_extension.dart';
import 'package:core/presentation/utils/html_transformer/base/dom_transformer.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
@@ -16,16 +15,12 @@ class ImageTransformer extends DomTransformer {
const ImageTransformer();
@override
Future<void> process(
Document document,
{
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}
) async {
final compressFileUtils = CompressFileUtils();
final imageElements = document.querySelectorAll('img[src^="cid:"]');
log('ImageTransformer::process(): imageElements: ${imageElements.length}');
Future<void> process({
required Document document,
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}) async {
final imageElements = document.querySelectorAll('img');
await Future.wait(imageElements.map((imageElement) async {
final exStyle = imageElement.attributes['style'];
if (exStyle != null) {
@@ -34,24 +29,42 @@ class ImageTransformer extends DomTransformer {
imageElement.attributes['style'] = 'display: inline;max-width: 100%;';
}
final src = imageElement.attributes['src'];
if (src != null) {
log('ImageTransformer::process(): src: $src');
final cid = src.replaceFirst('cid:', '').trim();
final urlDownloadCid = mapUrlDownloadCID?[cid];
log('ImageTransformer::process(): urlDownloadCid: $urlDownloadCid');
if (urlDownloadCid?.isNotEmpty == true && dioClient != null) {
final imgBase64Uri = await loadAsyncNetworkImageToBase64(
dioClient,
compressFileUtils,
urlDownloadCid!);
if (imgBase64Uri.isNotEmpty) {
imageElement.attributes['src'] = imgBase64Uri;
}
}
if (src == null) return;
if (src.startsWith('cid:') && dioClient != null && mapUrlDownloadCID != null) {
final imageBase64 = await _convertCidToBase64Image(
dioClient: dioClient,
mapUrlDownloadCID: mapUrlDownloadCID,
imageSource: src
);
imageElement.attributes['src'] = imageBase64 ?? src;
}
}));
}
Future<String?> _convertCidToBase64Image({
required DioClient dioClient,
required Map<String, String> mapUrlDownloadCID,
required String imageSource
}) async {
final cid = imageSource.replaceFirst('cid:', '').trim();
final urlDownloadCid = mapUrlDownloadCID[cid];
if (urlDownloadCid == null || urlDownloadCid.isEmpty) return null;
final compressFileUtils = CompressFileUtils();
final imgBase64Uri = await loadAsyncNetworkImageToBase64(
dioClient,
compressFileUtils,
urlDownloadCid
);
if (imgBase64Uri.isEmpty) return null;
return imgBase64Uri;
}
Future<String> loadAsyncNetworkImageToBase64(
DioClient dioClient,
CompressFileUtils compressFileUtils,
@@ -8,13 +8,11 @@ class RemoveScriptTransformer extends DomTransformer {
const RemoveScriptTransformer();
@override
Future<void> process(
Document document,
{
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}
) async {
Future<void> process({
required Document document,
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}) async {
final scriptElements = document.getElementsByTagName('script');
await Future.wait(scriptElements.map((scriptElement) async {
scriptElement.remove();
@@ -8,13 +8,11 @@ class SignatureTransformer extends DomTransformer {
const SignatureTransformer();
@override
Future<void> process(
Document document,
{
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}
) async {
Future<void> process({
required Document document,
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}) async {
final signatureElements = document.querySelectorAll('div.tmail-signature');
await Future.wait(signatureElements.map((element) async {
element.attributes['class'] = 'tmail-signature-blocked';
@@ -3,34 +3,33 @@ import 'package:core/presentation/utils/html_transformer/message_content_transfo
class HtmlTransform {
final String _contentHtml;
Map<String, String>? mapUrlDownloadCID;
DioClient? dioClient;
final DioClient _dioClient;
HtmlTransform(
this._contentHtml,
{
this.mapUrlDownloadCID,
this.dioClient,
}
);
HtmlTransform(this._dioClient);
/// Transforms this message to HTML code.
Future<String> transformToHtml({TransformConfiguration? transformConfiguration}) async {
Future<String> transformToHtml({
required String contentHtml,
Map<String, String>? mapUrlDownloadCID,
TransformConfiguration? transformConfiguration,
}) async {
transformConfiguration ??= TransformConfiguration.create();
final transformer = MessageContentTransformer(transformConfiguration);
final transformer = MessageContentTransformer(transformConfiguration, _dioClient);
final document = await transformer.toDocument(
_contentHtml,
mapUrlDownloadCID: mapUrlDownloadCID,
dioClient: dioClient);
message: contentHtml,
mapUrlDownloadCID: mapUrlDownloadCID
);
return document.outerHtml;
}
/// Transforms this message to Text Plain.
String transformToTextPlain({TransformConfiguration? transformConfiguration}) {
String transformToTextPlain({
required String content,
TransformConfiguration? transformConfiguration
}) {
transformConfiguration ??= TransformConfiguration.create();
final transformer = MessageContentTransformer(transformConfiguration);
final message = transformer.toMessage(_contentHtml);
final transformer = MessageContentTransformer(transformConfiguration, _dioClient);
final message = transformer.toMessage(content);
return message;
}
}
@@ -1,4 +1,5 @@
import 'package:core/core.dart';
import 'package:core/data/network/dio_client.dart';
import 'package:core/presentation/utils/html_transformer/transform_configuration.dart';
import 'package:html/dom.dart';
import 'package:html/parser.dart' show parse;
@@ -6,38 +7,35 @@ import 'package:html/parser.dart' show parse;
class MessageContentTransformer {
/// The configuration used for the transformation
final TransformConfiguration configuration;
final DioClient dioClient;
MessageContentTransformer(this.configuration);
MessageContentTransformer(this.configuration, this.dioClient);
Future<void> _transformDocument(
Document document,
{
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}
) async {
Future<void> _transformDocument({
required Document document,
Map<String, String>? mapUrlDownloadCID
}) async {
await Future.wait([
if (configuration.domTransformers.isNotEmpty)
...configuration.domTransformers.map((domTransformer) async =>
domTransformer.process(
document,
mapUrlDownloadCID: mapUrlDownloadCID,
dioClient: dioClient))
document: document,
mapUrlDownloadCID: mapUrlDownloadCID,
dioClient: dioClient
)
)
]);
}
Future<Document> toDocument(
String message,
{
Map<String, String>? mapUrlDownloadCID,
DioClient? dioClient
}
) async {
Future<Document> toDocument({
required String message,
Map<String, String>? mapUrlDownloadCID
}) async {
final document = parse(message);
await _transformDocument(
document,
mapUrlDownloadCID: mapUrlDownloadCID,
dioClient: dioClient);
document: document,
mapUrlDownloadCID: mapUrlDownloadCID,
);
return document;
}