TF-105 [BUG] Fix long line should be wrapped

This commit is contained in:
dab246
2021-10-12 16:51:30 +07:00
committed by Dat H. Pham
parent cb2c889fe2
commit dd5e8e4519
22 changed files with 243 additions and 286 deletions
@@ -1,24 +0,0 @@
import 'package:core/presentation/utils/html_transformer/base/dom_transformer.dart';
import 'package:core/presentation/utils/html_transformer/transform_configuration.dart';
import 'package:html/dom.dart';
class SetStyleBlockQuoteTransformer extends DomTransformer {
const SetStyleBlockQuoteTransformer();
@override
void process(Document document, String message, TransformConfiguration configuration) {
final quoteElements = document.getElementsByTagName('blockquote');
for (final quote in quoteElements) {
final style = quote.attributes['style'];
if (style == null) {
quote.attributes['style'] = '''
margin: 0px 8px 0px 8px;
padding: 8px 16px 8px 16px;
border-left:5px solid #eee;
''';
}
}
}
}
@@ -12,23 +12,12 @@ class ImageTransformer extends DomTransformer {
final imageElements = document.getElementsByTagName('img');
for (final imageElement in imageElements) {
final src = imageElement.attributes['src'];
if (src != null) {
if (src.startsWith('http')) {
if (configuration.blockExternalImages) {
imageElement.attributes.remove('src');
} else if (src.startsWith('http:')) {
// always at least enforce HTTPS images:
final url = src.substring('http:'.length);
imageElement.attributes['src'] = 'https:$url';
}
}
}
final style = imageElement.attributes['style'];
if (style == null) {
imageElement.attributes['style'] = 'display: inline;max-width: 100%;height: auto;';
} else {
imageElement.attributes['style'] = 'display: inline;max-width: 100%;height: auto;' + style;
if (src != null && src.startsWith('http:')) {
// always at least enforce HTTPS images:
final url = src.substring('http:'.length);
imageElement.attributes['src'] = 'https:$url';
}
imageElement.attributes['style'] = 'display: inline;max-width: 100%;height: auto;';
}
}
}
@@ -1,36 +0,0 @@
import 'package:core/presentation/utils/html_transformer/base/dom_transformer.dart';
import 'package:core/presentation/utils/html_transformer/transform_configuration.dart';
import 'package:html/dom.dart';
class SetStyleTableTransformer extends DomTransformer {
const SetStyleTableTransformer();
@override
void process(Document document, String message, TransformConfiguration configuration) {
final tableElements = document.getElementsByTagName('table');
for (final table in tableElements) {
final style = table.attributes['style'];
if (style == null) {
table.attributes['style'] = 'width: 100%;max-width: 100%;border:1px solid #f0f0f0;border-collapse: collapse;border-spacing: 2px;';
}
}
final tdElements = document.getElementsByTagName('td');
for (final tdTag in tdElements) {
final style = tdTag.attributes['style'];
if (style == null) {
tdTag.attributes['style'] = 'padding: 13px;margin: 0px;border:1px solid #f0f0f0;';
}
}
final thElements = document.getElementsByTagName('th');
for (final thTag in thElements) {
final style = thTag.attributes['style'];
if (style == null) {
thTag.attributes['style'] = 'padding: 13px;margin: 0px;border:1px solid #f0f0f0;';
}
}
}
}
@@ -4,39 +4,22 @@ import 'package:html/dom.dart';
class HtmlTransform {
final String _message;
final String _contentHtml;
HtmlTransform(this._message);
HtmlTransform(this._contentHtml);
/// Transforms this message to HTML code.
///
/// Set [blockExternalImages] to `true` in case external images should be blocked.
/// Optionally specify the [transformConfiguration] to control all aspects of the transformation - in that case other parameters are ignored.
String transformToHtml({
bool? blockExternalImages,
TransformConfiguration? transformConfiguration,
}) {
final document = transformToDocument(
blockExternalImages: blockExternalImages,
transformConfiguration: transformConfiguration,
);
/// Optionally specify the [transformConfiguration] to control all aspects of the transformation
/// - in that case other parameters are ignored.
String transformToHtml({TransformConfiguration? transformConfiguration}) {
final document = transformToDocument(transformConfiguration: transformConfiguration);
return document.outerHtml;
}
/// Transforms this message to Document.
///
/// Set [blockExternalImages] to `true` in case external images should be blocked.
/// Optionally specify the [transformConfiguration] to control all aspects of the transformation - in that case other parameters are ignored.
Document transformToDocument({
bool? blockExternalImages,
int? maxImageWidth,
TransformConfiguration? transformConfiguration,
}) {
transformConfiguration ??= TransformConfiguration.create(
blockExternalImages: blockExternalImages,
maxImageWidth: maxImageWidth,
);
Document transformToDocument({TransformConfiguration? transformConfiguration}) {
transformConfiguration ??= TransformConfiguration.create();
final transformer = MessageContentTransformer(transformConfiguration);
return transformer.toDocument(_message);
return transformer.toDocument(_contentHtml);
}
}
@@ -1,11 +1,9 @@
import 'package:core/presentation/utils/html_transformer/dom/blockquote_transformers.dart';
import 'package:core/presentation/utils/html_transformer/dom/image_transformers.dart';
import 'package:core/presentation/utils/html_transformer/dom/link_transformers.dart';
import 'package:core/presentation/utils/html_transformer/dom/meta_transformers.dart';
import 'package:core/presentation/utils/html_transformer/dom/script_transformers.dart';
import 'package:core/presentation/utils/html_transformer/base/dom_transformer.dart';
import 'package:core/presentation/utils/html_transformer/dom/table_transformers.dart';
import 'package:core/presentation/utils/html_transformer/text/convert_tags_text_transformer.dart';
import 'package:core/presentation/utils/html_transformer/text/linebreak_text_transformer.dart';
import 'package:core/presentation/utils/html_transformer/text/links_text_transformer.dart';
@@ -14,12 +12,6 @@ import 'package:core/presentation/utils/html_transformer/base/text_transformer.d
/// Contains the configuration for all transformations.
class TransformConfiguration {
/// Should external images be blocked?
final bool blockExternalImages;
/// The maximum width for embedded images. It make sense to limit this to reduce the generated HTML size.
final int? maxImageWidth;
/// The list of DOM transformers being used
final List<DomTransformer> domTransformers;
@@ -30,16 +22,12 @@ class TransformConfiguration {
///
/// Compare [create] to have an easier to use building function
const TransformConfiguration(
this.blockExternalImages,
this.maxImageWidth,
this.domTransformers,
this.textTransformers
);
/// Provides easy access to a standard configuration that does not block external images.
static const TransformConfiguration standardConfiguration = TransformConfiguration(
false,
standardMaxImageWidth,
standardDomTransformers,
standardTextTransformers
);
@@ -61,8 +49,6 @@ class TransformConfiguration {
: standardTextTransformers;
maxImageWidth ??= standardMaxImageWidth;
return TransformConfiguration(
blockExternalImages ?? false,
maxImageWidth,
domTransformers,
textTransformers
);
@@ -75,8 +61,6 @@ class TransformConfiguration {
RemoveScriptTransformer(),
ImageTransformer(),
EnsureRelationNoReferrerTransformer(),
SetStyleBlockQuoteTransformer(),
SetStyleTableTransformer(),
];
static const List<TextTransformer> standardTextTransformers = [