Skip to content

Commit

Permalink
Update isDataOnlyNewLines and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Sep 20, 2023
1 parent 711a625 commit 19bd909
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
11 changes: 5 additions & 6 deletions packages/fleather/lib/src/widgets/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/fleather/lib/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
10 changes: 10 additions & 0 deletions packages/fleather/test/util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

0 comments on commit 19bd909

Please sign in to comment.