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 = '
Hello & Welcome
'; expect(HtmlUtils.unescapeHtml(input), equals(expected)); }); test('should unescape numeric decimal HTML entities', () { const input = 'ABC'; const expected = 'ABC'; expect(HtmlUtils.unescapeHtml(input), equals(expected)); }); test('should unescape numeric hexadecimal HTML entities', () { const input = 'ABC'; const expected = 'ABC'; expect(HtmlUtils.unescapeHtml(input), equals(expected)); }); test('should unescape special character entities', () { const input = '"Hello' © ® ™'; const expected = '"Hello\' © ® ™'; expect(HtmlUtils.unescapeHtml(input), equals(expected)); }); test('should handle empty string', () { const input = ''; expect(HtmlUtils.unescapeHtml(input), isEmpty); }); test('should handle string with only HTML entity', () { const input = '&'; const expected = '&'; expect(HtmlUtils.unescapeHtml(input), equals(expected)); }); test('should leave invalid HTML entities unchanged', () { const input = 'This & is not an entity &invalid; &123'; const expected = 'This & is not an entity &invalid; &123'; expect(HtmlUtils.unescapeHtml(input), equals(expected)); }); test('should handle Unicode characters', () { const input = '<こんにちは>'; // <こんにちは> const expected = '<こんにちは>'; expect(HtmlUtils.unescapeHtml(input), equals(expected)); }); test('should handle complex mixed entities', () { const input = '<script>alert("Hello");</script>&@@'; const expected = '&@@'; expect(HtmlUtils.unescapeHtml(input), equals(expected)); }); }); group('HtmlUtils.removeWhitespace', () { test('should remove carriage returns (\\r)', () { expect(HtmlUtils.removeWhitespace('Hello\rWorld'), equals('HelloWorld')); expect(HtmlUtils.removeWhitespace('\r\r\r'), equals('')); expect(HtmlUtils.removeWhitespace('a\rb\rc'), equals('abc')); }); test('should remove newlines (\\n)', () { expect(HtmlUtils.removeWhitespace('Hello\nWorld'), equals('HelloWorld')); expect(HtmlUtils.removeWhitespace('\n\n\n'), equals('')); expect(HtmlUtils.removeWhitespace('a\nb\nc'), equals('abc')); }); test('should remove tabs (\\t)', () { expect(HtmlUtils.removeWhitespace('Hello\tWorld'), equals('HelloWorld')); expect(HtmlUtils.removeWhitespace('\t\t\t'), equals('')); expect(HtmlUtils.removeWhitespace('a\tb\tc'), equals('abc')); }); test('should remove all whitespace characters when combined', () { expect(HtmlUtils.removeWhitespace('Hello\r\n\tWorld'), equals('HelloWorld')); expect(HtmlUtils.removeWhitespace('a\rb\nc\td'), equals('abcd')); expect(HtmlUtils.removeWhitespace('\r\n\t\r\n\t'), equals('')); }); test('should return empty string for empty input', () { expect(HtmlUtils.removeWhitespace(''), equals('')); }); test('should leave string unchanged when no whitespace present', () { expect(HtmlUtils.removeWhitespace('HelloWorld'), equals('HelloWorld')); expect(HtmlUtils.removeWhitespace('12345'), equals('12345')); expect(HtmlUtils.removeWhitespace('!@#\$%^'), equals('!@#\$%^')); }); test('should preserve spaces if not configured to remove them', () { expect(HtmlUtils.removeWhitespace('Hello World'), equals('Hello World')); expect(HtmlUtils.removeWhitespace('a b c'), equals('a b c')); }); test('should handle mixed content with various whitespace', () { expect( HtmlUtils.removeWhitespace('Text\rwith\nnew\tlines\r\nand\ttabs'), equals('Textwithnewlinesandtabs'), ); }); }); group('HtmlUtils.extractPlainText', () { test('removes blockquote and keeps other text', () { const html = '''
Hello world

should be removed

Final
'''; expect(HtmlUtils.extractPlainText(html), 'Hello world Final'); }); test('removes nested blockquotes', () { const html = '''

Keep this

remove me

nested remove

After '''; expect(HtmlUtils.extractPlainText(html), 'Keep this After'); }); test('removes multiple blockquotes at same level', () { const html = '''

Start

first

second

End

'''; expect(HtmlUtils.extractPlainText(html), 'Start End'); }); test('removes all html tags outside blockquotes', () { const html = '
Hello bold italic
'; expect(HtmlUtils.extractPlainText(html), 'Hello bold italic'); }); test('normalizes whitespaces', () { const html = '''

Hello

remove

World

'''; expect(HtmlUtils.extractPlainText(html), 'Hello World'); }); test('returns empty string if only blockquote present', () { const html = '

everything removed

'; expect(HtmlUtils.extractPlainText(html), ''); }); test('case insensitive blockquote tag', () { const html = '''

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

remove ©

'''; expect(HtmlUtils.extractPlainText(html), 'A & B 5 < 10 > 3 Hello World'); }); test('handles double-encoded html entities with full decode', () { const html = '''

Before

&lt;div&gt;Hello&lt;/div&gt;

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 = '
Hello
World'; expect(HtmlUtils.extractPlainText(html), 'Hello World'); }); test('decodes multiple levels of entities', () { // &lt; = < → < const html = '

&lt;b&gt;Bold&lt;/b&gt;

'; // 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 = 'dab'; expect( HtmlUtils.extractPlainText(html), 'dab', ); }); test('removes deeply nested blockquotes with mixed content', () { const html = '''

Intro

Level1

Level2

Level3
Level4

Outro

'''; expect(HtmlUtils.extractPlainText(html), 'Intro Outro'); }); test('removes adjacent blockquotes and preserves surrounding text', () { const html = '''
Top

Q1

Q2

Q3

Bottom
'''; expect(HtmlUtils.extractPlainText(html), 'Top Bottom'); }); test('removes blockquote with attributes and cite', () { const html = '''

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

&lt;blockquote&gt;Inner from encoded quote&lt;/blockquote&gt;

After

'''; expect(HtmlUtils.extractPlainText(html), 'Before Inner from encoded quote After'); }); test('handles multiple encoded tags that become real tags then stripped', () { const html = ''' &lt;div&gt;A&lt;/div&gt;&lt;span&gt;B&lt;/span&gt; '''; // After decoding twice:
A
B → "A B" expect(HtmlUtils.extractPlainText(html), 'A B'); }); test('removes style and script tags when enabled', () { const html = '''

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 multiple
tags into a single space', () { const html = ''' Hello


World '''; expect(HtmlUtils.extractPlainText(html), 'Hello World'); }); test('handles mixed languages inside and outside quotes', () { const html = '''

Xin chào

Bonjour (quoted)

こんにちは

'''; expect(HtmlUtils.extractPlainText(html), 'Xin chào こんにちは'); }); test('handles messy whitespace around quotes and tags', () { const html = '''
A

should be gone

B '''; expect(HtmlUtils.extractPlainText(html), 'A B'); }); test('ignores disclaimers when wrapped inside blockquotes', () { const html = '''

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

Ce message et les éventuels documents joints...

Bottom

'''; expect(HtmlUtils.extractPlainText(html), 'Top Ce message et les éventuels documents joints... Bottom'); }); test('removes malformed nested blockquotes completely', () { const html = '''

Intro

Q1
Q2
Q3

Outro

'''; expect(HtmlUtils.extractPlainText(html), 'Intro Outro'); }); test('leaves unknown entities untouched while decoding known ones', () { const html = '''

© 2025 &unknown; & <tag>

'''; // © → ©, & → &, <tag> → , unknown entities stay as-is expect(HtmlUtils.extractPlainText(html), '© 2025 &unknown; & '); }); test('keeps unknown tags as html text even when adjacent', () { const html = 'ab'; expect(HtmlUtils.extractPlainText(html), 'ab'); }); test('handles encoded unknown tags that become html text after decoding', () { const html = '&lt;weird&gt;X&lt;/weird&gt;Y'; expect(HtmlUtils.extractPlainText(html), 'XY'); }); }); group('HtmlUtils.wrapPlainTextLinks', () { test('wraps plain text link inside

', () { const input = '

https://google.com

'; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), '', ); }); test('keeps existing tags unchanged', () { const input = ""; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), "", ); }); test('wraps multiple links in text', () { const input = '

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

'''; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), '', ); }); test('returns input if body is null', () { const input = ''; final result = HtmlUtils.wrapPlainTextLinks(input); expect(result, input); }); test('handles link with trailing punctuation', () { const input = '

Check 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

'''; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.trim(), '
\n

Visit https://google.com

\n

Then go to https://flutter.dev

\n
', ); }); test('ignores links already wrapped in ', () { const input = '

https://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(), '

(https://example.com)

', ); }); 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(), '

https://onlylink.com

', ); }); 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(), '

(https://parenthesis.com)

', ); }); 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

www.test.com'; final result = HtmlUtils.wrapPlainTextLinks(input); expect(result.contains(''), true); }); test('ignores URLs inside img/video/link/script attributes', () { const input = ''' '''; final result = HtmlUtils.wrapPlainTextLinks(input); expect( result.contains('www.google.com'), true, reason: 'Text link www.google.com should be wrapped', ); expect(result.contains('Flutter'), true); }); test('does not alter src or href attributes', () { const input = '''

Visit www.google.com

example Flutter
'''; final result = HtmlUtils.wrapPlainTextLinks(input).trim(); expect( result, '
\n

Visit www.google.com

\n ' 'example\n ' '\n ' 'Flutter\n
', ); }); test('does NOT wrap URLs inside special tags (img, video, script, link, iframe)', () { const input = '''

Check www.google.com

Sample image with link google.com
'''; final result = HtmlUtils.wrapPlainTextLinks(input); // The URL in

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
  • www.site.com
https://abc.com
'''; final out = HtmlUtils.wrapPlainTextLinks(html).replaceAll('\n', ''); expect( out, contains( '
https://example.com
', ), ); expect( out, contains('
  • www.site.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'), ); }); }); }