From 19bd90978b942f6b5aa61a407288af1df83a740a Mon Sep 17 00:00:00 2001 From: Amir Panahandeh Date: Wed, 20 Sep 2023 09:40:00 +0330 Subject: [PATCH] Update isDataOnlyNewLines and add tests --- packages/fleather/lib/src/widgets/controller.dart | 11 +++++------ packages/fleather/lib/util.dart | 4 ++-- packages/fleather/test/util_test.dart | 10 ++++++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/fleather/lib/src/widgets/controller.dart b/packages/fleather/lib/src/widgets/controller.dart index 55aa09f2..b603c6d1 100644 --- a/packages/fleather/lib/src/widgets/controller.dart +++ b/packages/fleather/lib/src/widgets/controller.dart @@ -78,12 +78,11 @@ class FleatherController extends ChangeNotifier { } // special case for AutoTextDirectionRule if (delta.length <= 3 && delta.last.isRetain) { - return delta.last.attributes - ?.containsKey(ParchmentAttribute.direction.key) == - true && - delta.last.attributes - ?.containsKey(ParchmentAttribute.alignment.key) == - true; + return delta.last.attributes != null && + delta.last.attributes! + .containsKey(ParchmentAttribute.direction.key) && + delta.last.attributes! + .containsKey(ParchmentAttribute.alignment.key); } } return false; diff --git a/packages/fleather/lib/util.dart b/packages/fleather/lib/util.dart index 7ba23a76..0c548a6a 100644 --- a/packages/fleather/lib/util.dart +++ b/packages/fleather/lib/util.dart @@ -55,6 +55,6 @@ TextDirection getDirectionOfNode(StyledNode node) { } bool isDataOnlyNewLines(Object data) { - if (data is! String) return false; - return data.replaceAll('\n', '').isEmpty; + if (data is! String || data.isEmpty) return false; + return RegExp('^(\n)+\$').hasMatch(data); } diff --git a/packages/fleather/test/util_test.dart b/packages/fleather/test/util_test.dart index e043ee74..e8c7108d 100644 --- a/packages/fleather/test/util_test.dart +++ b/packages/fleather/test/util_test.dart @@ -40,4 +40,14 @@ void main() { expect(result, 1); }); }); + + test('isDataOnlyNewLines', () { + expect(isDataOnlyNewLines(123), false); + expect(isDataOnlyNewLines(Object()), false); + expect(isDataOnlyNewLines(''), false); + expect(isDataOnlyNewLines('\nTest\nTest\n'), false); + expect(isDataOnlyNewLines('\n \n\n'), false); + expect(isDataOnlyNewLines('\n\t\n\n'), false); + expect(isDataOnlyNewLines('\n\n\n'), true); + }); }