diff --git a/lib/src/utils/html_to_text.dart b/lib/src/utils/html_to_text.dart
index 2abbf0a4..aa4d0bf2 100644
--- a/lib/src/utils/html_to_text.dart
+++ b/lib/src/utils/html_to_text.dart
@@ -50,11 +50,13 @@ class HtmlToText {
.firstMatch(text);
if (match == null) {
text = HtmlUnescape().convert(text);
- if (text[0] != '\n') {
- text = '\n$text';
- }
- if (text[text.length - 1] != '\n') {
- text += '\n';
+ if (text.isNotEmpty) {
+ if (text[0] != '\n') {
+ text = '\n$text';
+ }
+ if (text[text.length - 1] != '\n') {
+ text += '\n';
+ }
}
return text;
}
@@ -64,11 +66,13 @@ class HtmlToText {
text = text.replaceAll(
RegExp(r'$', multiLine: false, caseSensitive: false), '');
text = HtmlUnescape().convert(text);
- if (text[0] != '\n') {
- text = '\n$text';
- }
- if (text[text.length - 1] != '\n') {
- text += '\n';
+ if (text.isNotEmpty) {
+ if (text[0] != '\n') {
+ text = '\n$text';
+ }
+ if (text[text.length - 1] != '\n') {
+ text += '\n';
+ }
}
final language =
RegExp(r'language-(\w+)', multiLine: false, caseSensitive: false)
diff --git a/test/html_to_text_test.dart b/test/html_to_text_test.dart
index fc29a6a3..9dde56e7 100644
--- a/test/html_to_text_test.dart
+++ b/test/html_to_text_test.dart
@@ -21,80 +21,81 @@ import 'package:test/test.dart';
void main() {
group('htmlToText', () {
- test('stuff', () async {
- final testMap = {
- '': '',
- 'hello world\nthis is a test': 'hello world\nthis is a test',
- 'That\'s not a test, this is a test':
- '*That\'s* not a test, **this** is a test',
- 'Visit our website (outdated)':
- 'Visit ~~πour website~~ (outdated)',
- '(cw spiders) spiders are pretty cool':
- '(cw spiders) βββββββββββββββββββββββ',
- 'spiders are pretty cool':
- '(cw spiders) βββββββββββββββββββββββ',
- '
': 'a test case',
- 'List of cute animals:\n\n- Kittens
\n- Puppies
\n- Snakes
(I think they\'re cute!) \n
\n(This list is incomplete, you can help by adding to it!)':
- 'List of cute animals:\nβ Kittens\nβ Puppies\nβ Snakes\n (I think they\'re cute!)\n(This list is incomplete, you can help by adding to it!)',
- 'fox': '*fox*',
- 'fox': '*fox*',
- 'fox': '**fox**',
- 'fox': '**fox**',
- 'fox': '__fox__',
- 'fox': '__fox__',
- 'fox': '~~fox~~',
- 'fox': '~~fox~~',
- 'fox': '~~fox~~',
- '>fox': '`>fox`',
- 'meep
': '```\nmeep\n```',
- 'meep\n
': '```\nmeep\n```',
- 'meep
':
- '```floof\nmeep\n```',
- 'beforecode
after': 'before\n```\ncode\n```\nafter',
- 'before
code
after
':
- 'before\n```\ncode\n```\nafter',
- 'fox
': 'fox',
- 'fox
floof
': 'fox\n\nfloof',
- 'website': 'πwebsite',
- 'fox': 'fox',
- 'fox': 'fox',
- '
': ':wave:',
- 'fox
floof': 'fox\nfloof',
- 'fox
floof': '> fox\nfloof',
- 'fox
floof': '> fox\nfloof',
- 'fox
floof
': '> fox\nfloof',
- 'afox
floof': 'a\n> fox\nfloof',
- 'fox
floof
fluff':
- '> > fox\n> floof\nfluff',
- '':
- 'β hey\n β a\n β b\nβ foxies',
- '- a
- b
': '1. a\n2. b',
- '- a
- b
': '42. a\n43. b',
- '- a
- aa
- bb
- b
':
- '1. a\n 1. aa\n 2. bb\n2. b',
- '- a
- b
':
- '1. a\n β aa\n β bb\n2. b',
- '':
- 'β a\n 1. aa\n 2. bb\nβ b',
- 'bunnyfox': 'fox',
- 'fox
floof': 'fox\n----------\nfloof',
- 'fox
floof
': 'fox\n----------\nfloof',
- 'fox
floof': '# fox\nfloof',
- 'fox
floof
': '# fox\nfloof',
- 'flooffox
': 'floof\n# fox',
- 'floof
fox
': 'floof\n# fox',
- 'fox
': '## fox',
- 'fox
': '### fox',
- 'fox
': '#### fox',
- 'fox
': '##### fox',
- 'fox
': '###### fox',
- 'fox': 'fox',
- 'fox
\nfloof
': 'fox\n\nfloof',
- 'beepfox
\nfloof
': 'fox\n\nfloof',
- };
- for (final entry in testMap.entries) {
+ final testMap = {
+ '': '',
+ 'hello world\nthis is a test': 'hello world\nthis is a test',
+ 'That\'s not a test, this is a test':
+ '*That\'s* not a test, **this** is a test',
+ 'Visit our website (outdated)':
+ 'Visit ~~πour website~~ (outdated)',
+ '(cw spiders) spiders are pretty cool':
+ '(cw spiders) βββββββββββββββββββββββ',
+ 'spiders are pretty cool':
+ '(cw spiders) βββββββββββββββββββββββ',
+ '
': 'a test case',
+ 'List of cute animals:\n\n- Kittens
\n- Puppies
\n- Snakes
(I think they\'re cute!) \n
\n(This list is incomplete, you can help by adding to it!)':
+ 'List of cute animals:\nβ Kittens\nβ Puppies\nβ Snakes\n (I think they\'re cute!)\n(This list is incomplete, you can help by adding to it!)',
+ 'fox': '*fox*',
+ 'fox': '*fox*',
+ 'fox': '**fox**',
+ 'fox': '**fox**',
+ 'fox': '__fox__',
+ 'fox': '__fox__',
+ 'fox': '~~fox~~',
+ 'fox': '~~fox~~',
+ 'fox': '~~fox~~',
+ '>fox': '`>fox`',
+ 'meep
': '```\nmeep\n```',
+ 'meep\n
': '```\nmeep\n```',
+ 'meep
':
+ '```floof\nmeep\n```',
+ 'beforecode
after': 'before\n```\ncode\n```\nafter',
+ 'before
code
after
':
+ 'before\n```\ncode\n```\nafter',
+ 'fox
': 'fox',
+ 'fox
floof
': 'fox\n\nfloof',
+ 'website': 'πwebsite',
+ 'fox': 'fox',
+ 'fox': 'fox',
+ '
': ':wave:',
+ 'fox
floof': 'fox\nfloof',
+ 'fox
floof': '> fox\nfloof',
+ 'fox
floof': '> fox\nfloof',
+ 'fox
floof
': '> fox\nfloof',
+ 'afox
floof': 'a\n> fox\nfloof',
+ 'fox
floof
fluff':
+ '> > fox\n> floof\nfluff',
+ '':
+ 'β hey\n β a\n β b\nβ foxies',
+ '- a
- b
': '1. a\n2. b',
+ '- a
- b
': '42. a\n43. b',
+ '- a
- aa
- bb
- b
':
+ '1. a\n 1. aa\n 2. bb\n2. b',
+ '- a
- b
':
+ '1. a\n β aa\n β bb\n2. b',
+ '':
+ 'β a\n 1. aa\n 2. bb\nβ b',
+ 'bunnyfox': 'fox',
+ 'fox
floof': 'fox\n----------\nfloof',
+ 'fox
floof
': 'fox\n----------\nfloof',
+ 'fox
floof': '# fox\nfloof',
+ 'fox
floof
': '# fox\nfloof',
+ 'flooffox
': 'floof\n# fox',
+ 'floof
fox
': 'floof\n# fox',
+ 'fox
': '## fox',
+ 'fox
': '### fox',
+ 'fox
': '#### fox',
+ 'fox
': '##### fox',
+ 'fox
': '###### fox',
+ 'fox': 'fox',
+ 'fox
\nfloof
': 'fox\n\nfloof',
+ 'beepfox
\nfloof
': 'fox\n\nfloof',
+ '
': '``````',
+ };
+ for (final entry in testMap.entries) {
+ test(entry.key, () async {
expect(HtmlToText.convert(entry.key), entry.value);
- }
- });
+ });
+ }
});
}