Merge branch 'krille/fix-crash-in-htmltotext' into 'main'

fix: HtmlToText crashes with an empty code block

Closes #249

See merge request famedly/company/frontend/famedlysdk!897
This commit is contained in:
td 2021-11-16 07:27:09 +00:00
commit 1804838324
2 changed files with 89 additions and 84 deletions

View File

@ -50,12 +50,14 @@ class HtmlToText {
.firstMatch(text);
if (match == null) {
text = HtmlUnescape().convert(text);
if (text.isNotEmpty) {
if (text[0] != '\n') {
text = '\n$text';
}
if (text[text.length - 1] != '\n') {
text += '\n';
}
}
return text;
}
// remove <code> opening tag
@ -64,12 +66,14 @@ class HtmlToText {
text = text.replaceAll(
RegExp(r'</code>$', multiLine: false, caseSensitive: false), '');
text = HtmlUnescape().convert(text);
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)
.firstMatch(match.group(1)!);

View File

@ -21,7 +21,6 @@ import 'package:test/test.dart';
void main() {
group('htmlToText', () {
test('stuff', () async {
final testMap = <String, String>{
'': '',
'hello world\nthis is a test': 'hello world\nthis is a test',
@ -91,10 +90,12 @@ void main() {
'<span>fox</span>': 'fox',
'<p>fox</p>\n<p>floof</p>': 'fox\n\nfloof',
'<mx-reply>beep</mx-reply><p>fox</p>\n<p>floof</p>': 'fox\n\nfloof',
'<pre><code></code></pre>': '``````',
};
for (final entry in testMap.entries) {
test(entry.key, () async {
expect(HtmlToText.convert(entry.key), entry.value);
}
});
});
}
});
}