From 66bf8e6ace796337ea0c4eec6a2a783db89578f5 Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Tue, 16 Nov 2021 08:18:07 +0100 Subject: [PATCH] fix: HtmlToText crashes with an empty code block --- lib/src/utils/html_to_text.dart | 24 ++--- test/html_to_text_test.dart | 149 ++++++++++++++++---------------- 2 files changed, 89 insertions(+), 84 deletions(-) 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': 'a test case', - 'List of cute animals:\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```', - 'before
code
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:': ':wave:', - 'fox
floof': 'fox\nfloof', - '
fox
floof': '> fox\nfloof', - '

fox

floof': '> fox\nfloof', - '

fox

floof

': '> fox\nfloof', - 'a
fox
floof': 'a\n> fox\nfloof', - '
fox
floof
fluff': - '> > fox\n> floof\nfluff', - '
  • hey
    • a
    • b
  • foxies
': - '● hey\n β—‹ a\n β—‹ b\n● foxies', - '
  1. a
  2. b
': '1. a\n2. b', - '
  1. a
  2. b
': '42. a\n43. b', - '
  1. a
    1. aa
    2. bb
  2. b
': - '1. a\n 1. aa\n 2. bb\n2. b', - '
  1. a
    • aa
    • bb
  2. b
': - '1. a\n β—‹ aa\n β—‹ bb\n2. b', - '
  • a
    1. aa
    2. bb
  • 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', - 'floof

fox

': 'floof\n# fox', - '

floof

fox

': 'floof\n# fox', - '

fox

': '## fox', - '

fox

': '### fox', - '

fox

': '#### fox', - '
fox
': '##### fox', - '
fox
': '###### fox', - 'fox': 'fox', - '

fox

\n

floof

': 'fox\n\nfloof', - 'beep

fox

\n

floof

': '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': '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```', + 'before
code
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:': ':wave:', + 'fox
floof': 'fox\nfloof', + '
fox
floof': '> fox\nfloof', + '

fox

floof': '> fox\nfloof', + '

fox

floof

': '> fox\nfloof', + 'a
fox
floof': 'a\n> fox\nfloof', + '
fox
floof
fluff': + '> > fox\n> floof\nfluff', + '
  • hey
    • a
    • b
  • foxies
': + '● hey\n β—‹ a\n β—‹ b\n● foxies', + '
  1. a
  2. b
': '1. a\n2. b', + '
  1. a
  2. b
': '42. a\n43. b', + '
  1. a
    1. aa
    2. bb
  2. b
': + '1. a\n 1. aa\n 2. bb\n2. b', + '
  1. a
    • aa
    • bb
  2. b
': + '1. a\n β—‹ aa\n β—‹ bb\n2. b', + '
  • a
    1. aa
    2. bb
  • 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', + 'floof

fox

': 'floof\n# fox', + '

floof

fox

': 'floof\n# fox', + '

fox

': '## fox', + '

fox

': '### fox', + '

fox

': '#### fox', + '
fox
': '##### fox', + '
fox
': '###### fox', + 'fox': 'fox', + '

fox

\n

floof

': 'fox\n\nfloof', + 'beep

fox

\n

floof

': 'fox\n\nfloof', + '
': '``````', + }; + for (final entry in testMap.entries) { + test(entry.key, () async { expect(HtmlToText.convert(entry.key), entry.value); - } - }); + }); + } }); }