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:
commit
1804838324
|
|
@ -50,12 +50,14 @@ class HtmlToText {
|
||||||
.firstMatch(text);
|
.firstMatch(text);
|
||||||
if (match == null) {
|
if (match == null) {
|
||||||
text = HtmlUnescape().convert(text);
|
text = HtmlUnescape().convert(text);
|
||||||
|
if (text.isNotEmpty) {
|
||||||
if (text[0] != '\n') {
|
if (text[0] != '\n') {
|
||||||
text = '\n$text';
|
text = '\n$text';
|
||||||
}
|
}
|
||||||
if (text[text.length - 1] != '\n') {
|
if (text[text.length - 1] != '\n') {
|
||||||
text += '\n';
|
text += '\n';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
// remove <code> opening tag
|
// remove <code> opening tag
|
||||||
|
|
@ -64,12 +66,14 @@ class HtmlToText {
|
||||||
text = text.replaceAll(
|
text = text.replaceAll(
|
||||||
RegExp(r'</code>$', multiLine: false, caseSensitive: false), '');
|
RegExp(r'</code>$', multiLine: false, caseSensitive: false), '');
|
||||||
text = HtmlUnescape().convert(text);
|
text = HtmlUnescape().convert(text);
|
||||||
|
if (text.isNotEmpty) {
|
||||||
if (text[0] != '\n') {
|
if (text[0] != '\n') {
|
||||||
text = '\n$text';
|
text = '\n$text';
|
||||||
}
|
}
|
||||||
if (text[text.length - 1] != '\n') {
|
if (text[text.length - 1] != '\n') {
|
||||||
text += '\n';
|
text += '\n';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
final language =
|
final language =
|
||||||
RegExp(r'language-(\w+)', multiLine: false, caseSensitive: false)
|
RegExp(r'language-(\w+)', multiLine: false, caseSensitive: false)
|
||||||
.firstMatch(match.group(1)!);
|
.firstMatch(match.group(1)!);
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ import 'package:test/test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('htmlToText', () {
|
group('htmlToText', () {
|
||||||
test('stuff', () async {
|
|
||||||
final testMap = <String, String>{
|
final testMap = <String, String>{
|
||||||
'': '',
|
'': '',
|
||||||
'hello world\nthis is a test': 'hello world\nthis is a test',
|
'hello world\nthis is a test': 'hello world\nthis is a test',
|
||||||
|
|
@ -91,10 +90,12 @@ void main() {
|
||||||
'<span>fox</span>': 'fox',
|
'<span>fox</span>': 'fox',
|
||||||
'<p>fox</p>\n<p>floof</p>': 'fox\n\nfloof',
|
'<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',
|
'<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) {
|
for (final entry in testMap.entries) {
|
||||||
|
test(entry.key, () async {
|
||||||
expect(HtmlToText.convert(entry.key), entry.value);
|
expect(HtmlToText.convert(entry.key), entry.value);
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue