import 'package:core/presentation/utils/html_transformer/dom/image_transformers.dart'; import 'package:core/utils/html/html_utils.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { const imageTransformer = ImageTransformer(); group('findImageUrlFromStyleTag test', () { test('Test findImageUrlFromStyleTag with valid input', () { const style = 'background-image: url(\'example.com/image.jpg\');'; final result = imageTransformer.findImageUrlFromStyleTag(style); expect(result!.value1, 'background-image: url(\'example.com/image.jpg\')'); expect(result.value2, 'example.com/image.jpg'); }); test('Test findImageUrlFromStyleTag with valid input and no quotation marks', () { const style = 'background-image: url(example.com/image.jpg);'; final result = imageTransformer.findImageUrlFromStyleTag(style); expect(result!.value1, 'background-image: url(example.com/image.jpg)'); expect(result.value2, 'example.com/image.jpg'); }); test('Test findImageUrlFromStyleTag with empty input', () { const style = ''; final result = imageTransformer.findImageUrlFromStyleTag(style); expect(result, null); }); test('Test findImageUrlFromStyleTag with invalid input', () { const style = 'background-image: invalid-url(\'example.com/image.jpg\');'; final result = imageTransformer.findImageUrlFromStyleTag(style); expect(result, null); }); }); group('HtmlUtils.unescapeHtml', () { test('should return original string when no HTML entities present', () { const input = 'This is a normal string'; expect(HtmlUtils.unescapeHtml(input), equals(input)); }); test('should unescape basic HTML entities', () { const input = '<div>Hello & Welcome</div>'; const expected = '
should be removed
Keep this
After '''; expect(HtmlUtils.extractPlainText(html), 'Keep this After'); }); test('removes multiple blockquotes at same level', () { const html = '''remove me
nested remove
Start
first
second
End
'''; expect(HtmlUtils.extractPlainText(html), 'Start End'); }); test('removes all html tags outside blockquotes', () { const html = 'Hello
remove
World
'''; expect(HtmlUtils.extractPlainText(html), 'Hello World'); }); test('returns empty string if only blockquote present', () { const html = ''; expect(HtmlUtils.extractPlainText(html), ''); }); test('case insensitive blockquote tag', () { const html = '''everything removed
Remove me
Keep me
'''; expect(HtmlUtils.extractPlainText(html), 'Keep me'); }); test('handles text without any html', () { const html = 'Just plain text already'; expect(HtmlUtils.extractPlainText(html), 'Just plain text already'); }); test('decodes HTML entities correctly', () { const html = '''A & B
5 < 10 > 3
Hello World
'''; expect(HtmlUtils.extractPlainText(html), 'A & B 5 < 10 > 3 Hello World'); }); test('handles double-encoded html entities with full decode', () { const html = '''remove ©
Before
<div>Hello</div>After
'''; expect(HtmlUtils.extractPlainText(html), 'Before Hello After'); }); test('handles emoji correctly', () { const html = '''Hello 🌍🚀
😅 should be removed
Done ✅
'''; expect(HtmlUtils.extractPlainText(html), 'Hello 🌍🚀 Done ✅'); }); test('handles Cyrillic text', () { const html = '''Привет мир
Удалить это
Отчёт завершён
'''; expect(HtmlUtils.extractPlainText(html), 'Привет мир Отчёт завершён'); }); test('handles Japanese text', () { const html = '''こんにちは 世界
これは削除される
完了しました
'''; expect(HtmlUtils.extractPlainText(html), 'こんにちは 世界 完了しました'); }); test('handles Chinese text', () { const html = '''你好,世界
这部分要删除
完成
'''; expect(HtmlUtils.extractPlainText(html), '你好,世界 完成'); }); test('returns empty string when input is empty', () { expect(HtmlUtils.extractPlainText(''), ''); }); test('handles html without closing tags', () { const html = 'Hello World'; expect(HtmlUtils.extractPlainText(html), 'Hello World'); }); test('ignores numeric-like tags (e.g. <123>) as text', () { const html = '
value is <123> not a tag
'; // <123> is not a valid HTML tag, so it should be kept expect(HtmlUtils.extractPlainText(html), 'value is <123> not a tag'); }); test('keeps unknown HTML entities as-is', () { const html = 'custom &unknown; entity
'; // if the entity cannot be decoded, keep it as is expect(HtmlUtils.extractPlainText(html), 'custom &unknown; entity'); }); test('removes nested blockquote completely', () { const html = '''Intro
nested
deep
Outro
'''; expect(HtmlUtils.extractPlainText(html), 'Intro Outro'); }); test('preserves spacing when multiple tags removed', () { const html = '<b>Bold</b>
'; // Because is decoded into a real tag and removed, only Bold remains expect(HtmlUtils.extractPlainText(html), 'Bold'); }); test('keeps unknown tags as plain text', () { // Unknown HTML tags should be keep as html text, // not stripped or removed by the extractor. const html = 'Intro
Level1Level2
Level3Level4
Outro
'''; expect(HtmlUtils.extractPlainText(html), 'Intro Outro'); }); test('removes adjacent blockquotes and preserves surrounding text', () { const html = '''Q1
Q2
Q3
Start
On Nov 10 ...Quoted text
End
'''; expect(HtmlUtils.extractPlainText(html), 'Start End'); }); test('keeps content when removeQuotes=false', () { const html = '''A
B inside quote
C
'''; expect( HtmlUtils.extractPlainText(html, removeQuotes: false), 'A B inside quote C', ); }); test('keeps content of encoded blockquotes since they are not real tags', () { const html = '''Before
<blockquote>Inner from encoded quote</blockquote>After
'''; expect(HtmlUtils.extractPlainText(html), 'Before Inner from encoded quote After'); }); test('handles multiple encoded tags that become real tags then stripped', () { const html = ''' <div>A</div><span>B</span> '''; // After decoding twice:Keep
Me
'''; expect(HtmlUtils.extractPlainText(html), 'Keep Me'); }); test('keeps style content when removeStyle=false', () { const html = '''Top
Bottom
'''; // When removeStyle=false, style content remains after tag stripping. expect( HtmlUtils.extractPlainText(html, removeStyle: false), 'Top Bottom', ); }); test('keeps script content when removeScript=false', () { const html = '''Hello
World
'''; expect( HtmlUtils.extractPlainText(html, removeScript: false), 'Hello World', ); }); test('collapses multipleXin chào
Bonjour (quoted)
こんにちは
'''; expect(HtmlUtils.extractPlainText(html), 'Xin chào こんにちは'); }); test('handles messy whitespace around quotes and tags', () { const html = '''B '''; expect(HtmlUtils.extractPlainText(html), 'A B'); }); test('ignores disclaimers when wrapped inside blockquotes', () { const html = '''should be gone
Keep
Ce message et les éventuels documents joints...
Text
'''; expect(HtmlUtils.extractPlainText(html), 'Keep Text'); }); test('keeps disclaimers that are not wrapped in blockquotes', () { const html = '''Top
Bottom
'''; expect(HtmlUtils.extractPlainText(html), 'Top Ce message et les éventuels documents joints... Bottom'); }); test('removes malformed nested blockquotes completely', () { const html = '''Intro
Q1Q2Q3
Outro
'''; expect(HtmlUtils.extractPlainText(html), 'Intro Outro'); }); test('leaves unknown entities untouched while decoding known ones', () { const html = '''© 2025 &unknown; & <tag>
'''; // © → ©, & → &, <tag> →', () { const input = '
https://google.com
Go to https://flutter.dev and https://dart.dev now
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Go to https://flutter.dev and https://dart.dev now
', ); }); test('keeps non-link text unchanged', () { const input = 'Hello world!
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect(result.trim(), 'Hello world!
'); }); test('handles mixed content with existing and new links', () { const input = '''Visit https://google.com or Flutter
Visit https://google.com or Flutter
\nCheck https://google.com, it is great!
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Check https://google.com, it is great!
', ); }); test('handles link with query parameters', () { const input = 'Go to https://example.com/search?q=test
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Go to https://example.com/search?q=test
', ); }); test('wraps link inside span', () { const input = 'See https://flutter.dev/docs for details'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'See https://flutter.dev/docs for details', ); }); test('handles multiple paragraphs with links', () { const input = '''Visit https://google.com
Then go to https://flutter.dev
Visit https://google.com
\nThen go to https://flutter.dev
\nhttps://google.com is wrapped
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'https://google.com is wrapped
', ); }); test('handles link with trailing parenthesis correctly', () { const input = '(https://example.com)
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), '', ); }); test('handles link followed by exclamation mark', () { const input = 'Wow https://amazing.dev!
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Wow https://amazing.dev!
', ); }); test('handles link followed by question mark', () { const input = 'What is https://example.com?
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'What is https://example.com?
', ); }); test('handles http (non-https) link', () { const input = 'Visit http://example.org for info
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Visit http://example.org for info
', ); }); test('handles multiple text segments and links mixed', () { const input = 'Before https://link1.com middle https://link2.com end
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Before https://link1.com middle https://link2.com end
', ); }); test('handles sentence with link between words', () { const input = 'Check this https://docs.flutter.dev today
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Check this https://docs.flutter.dev today
', ); }); test('handles link at beginning of text', () { const input = 'https://start.com is the first link
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'https://start.com is the first link
', ); }); test('handles link at end of text', () { const input = 'Go to end https://end.com
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Go to end https://end.com
', ); }); test('handles text containing only the link', () { const input = 'https://onlylink.com
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), '', ); }); test('wraps link starting with www', () { const input = 'Visit www.google.com for info
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Visit www.google.com for info
', ); }); test('handles link followed by colon', () { const input = 'URL: https://flutter.dev:
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'URL: https://flutter.dev:
', ); }); test('handles parentheses around link', () { const input = '(https://parenthesis.com)
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), '', ); }); test('handles link inside nested span', () { const input = 'Check https://nested.com inside span
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Check https://nested.com inside span
', ); }); test('handles multiple links across multiple tags', () { const input = 'First https://one.com
Second https://two.com
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'First https://one.com
Second https://two.com
', ); }); test('handles link with port number', () { const input = 'Visit http://localhost:8080/home
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Visit http://localhost:8080/home
', ); }); test('handles unicode domain', () { const input = 'Try https://例子.测试
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Try https://例子.测试
', ); }); test('handles link with query params & escapes correctly', () { const input = 'Search https://google.com?q=flutter&lang=en
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'Search https://google.com?q=flutter&lang=en
', ); }); test('does not wrap text inside existing ', () { const input = 'https://flutter.dev'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), 'https://flutter.dev', ); }); test('handles empty html safely', () { const input = ''; final result = HtmlUtils.wrapPlainTextLinks(input); expect(result, ''); }); test('handles text without link', () { const input = 'No link here
'; final result = HtmlUtils.wrapPlainTextLinks(input); expect(result.trim(), 'No link here
'); }); test('handles invalid HTML gracefully', () { const input = 'Some broken
Flutter'), true);
});
test('does not alter src or href attributes', () {
const input = '''
''';
final result = HtmlUtils.wrapPlainTextLinks(input).trim();
expect(
result,
'',
);
});
test('does NOT wrap URLs inside special tags (img, video, script, link, iframe)', () {
const input = '''
Check www.google.com
should be wrapped expect( result.contains('www.google.com'), true, reason: 'The text www.google.com inside
should be wrapped into a link',
);
// There must NOT be any tag inside these special tags
final forbiddenTags = ['img', 'video', 'script', 'iframe', 'link'];
for (final tag in forbiddenTags) {
final pattern = RegExp('<$tag[^>]*>.*?<\\/\\s*$tag>', dotAll: true);
final matches = pattern.allMatches(result);
for (final match in matches) {
final segment = match.group(0)!;
expect(
segment.contains(' tag inside <$tag>',
);
}
}
// There must NOT be any tag inside href/src attributes
final attributePattern = RegExp(r'(src|href)="[^"]* tag inside href or src attributes',
);
});
test('Protocol variations: http/https/www', () {
const html = 'A https://example.com B http://foo.bar C www.site.com';
final result = HtmlUtils.wrapPlainTextLinks(html);
expect(result, contains('https://example.com'));
expect(result, contains('http://foo.bar'));
expect(result, contains('www.site.com'));
});
test('Protocol variations: ftp, mailto, file', () {
const html =
'ftp://files.example.com mailto:me@example.com file:///Users/me/readme.txt';
final out = HtmlUtils.wrapPlainTextLinks(html);
expect(
out,
contains('ftp://files.example.com'),
);
expect(
out,
contains('mailto:me@example.com'),
);
expect(
out,
contains(
'file:///Users/me/readme.txt',
),
);
});
test('Fragment + query: keep the same href, do not double https://', () {
const html =
'View https://example.com/page?query=1#section and https://a.b/c#frag';
final out = HtmlUtils.wrapPlainTextLinks(html);
expect(
out,
contains(
'https://example.com/page?query=1#section',
),
);
expect(
out,
contains('https://a.b/c#frag'),
);
});
test('Do not wrap in and ', () {
const html = 'https://example.com
www.site.com';
final out = HtmlUtils.wrapPlainTextLinks(html);
expect(out, contains('https://example.com
'));
expect(out, contains('www.site.com'));
expect(out, isNot(contains(''));
});
test('IP addresses: http + www.*', () {
const html = 'http://192.168.1.1 www.127.0.0.1';
final out = HtmlUtils.wrapPlainTextLinks(html);
expect(out, contains('http://192.168.1.1'));
expect(out, contains('www.127.0.0.1'));
});
test('URLs in blockquote/list/table are wrapped', () {
const html = '''
https://example.com
''';
final out = HtmlUtils.wrapPlainTextLinks(html).replaceAll('\n', '');
expect(
out,
contains(
'https://abc.com https://example.com
',
),
);
expect(
out,
contains('https://abc.com '),
);
});
test('Malformed URLs: no wrap', () {
const html = 'Wrong: http//example and www..com';
final out = HtmlUtils.wrapPlainTextLinks(html);
expect(out, isNot(contains('www.example1.comwww.example2.com',
),
);
expect(out.indexOf(' (skip parent tag)', () {
const html =
'https://example.com và www.site.com';
final out = HtmlUtils.wrapPlainTextLinks(html);
expect(
out,
contains(
'https://example.com',
),
);
expect(
out,
contains('www.site.com'),
);
});
});
}