zefyr

delete_rules_test.dart 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (c) 2018, the Zefyr project authors. Please see the AUTHORS file
  2. // for details. All rights reserved. Use of this source code is governed by a
  3. // BSD-style license that can be found in the LICENSE file.
  4. import 'package:test/test.dart';
  5. import 'package:quill_delta/quill_delta.dart';
  6. import 'package:notus/notus.dart';
  7. final ul = NotusAttribute.ul.toJson();
  8. final bold = NotusAttribute.bold.toJson();
  9. void main() {
  10. group('$PreserveLineStyleOnMergeRule', () {
  11. final rule = PreserveLineStyleOnMergeRule();
  12. test('preserves block style', () {
  13. final ul = NotusAttribute.ul.toJson();
  14. final doc = Delta()
  15. ..insert('Title\nOne')
  16. ..insert('\n', ul)
  17. ..insert('Two\n');
  18. final actual = rule.apply(doc, 9, 1);
  19. final expected = Delta()
  20. ..retain(9)
  21. ..delete(1)
  22. ..retain(3)
  23. ..retain(1, ul);
  24. expect(actual, expected);
  25. });
  26. test('resets block style', () {
  27. final unsetUl = NotusAttribute.block.unset.toJson();
  28. final doc = Delta()
  29. ..insert('Title\nOne')
  30. ..insert('\n', NotusAttribute.ul.toJson())
  31. ..insert('Two\n');
  32. final actual = rule.apply(doc, 5, 1);
  33. final expected = Delta()
  34. ..retain(5)
  35. ..delete(1)
  36. ..retain(3)
  37. ..retain(1, unsetUl);
  38. expect(actual, expected);
  39. });
  40. });
  41. group('$CatchAllDeleteRule', () {
  42. final rule = CatchAllDeleteRule();
  43. test('applies change as-is', () {
  44. final doc = Delta()..insert('Document\n');
  45. final actual = rule.apply(doc, 3, 5);
  46. final expected = Delta()
  47. ..retain(3)
  48. ..delete(5);
  49. expect(actual, expected);
  50. });
  51. });
  52. group('$EnsureEmbedLineRule', () {
  53. final rule = EnsureEmbedLineRule();
  54. test('ensures line-break before embed', () {
  55. final hr = NotusAttribute.embed.horizontalRule;
  56. final doc = Delta()
  57. ..insert('Document\n')
  58. ..insert(kZeroWidthSpace, hr.toJson())
  59. ..insert('\n');
  60. final actual = rule.apply(doc, 8, 1);
  61. final expected = Delta()..retain(8);
  62. expect(actual, expected);
  63. });
  64. test('ensures line-break after embed', () {
  65. final hr = NotusAttribute.embed.horizontalRule;
  66. final doc = Delta()
  67. ..insert('Document\n')
  68. ..insert(kZeroWidthSpace, hr.toJson())
  69. ..insert('\n');
  70. final actual = rule.apply(doc, 10, 1);
  71. final expected = Delta()..retain(11);
  72. expect(actual, expected);
  73. });
  74. test('still deletes everything between embeds', () {
  75. final hr = NotusAttribute.embed.horizontalRule;
  76. final doc = Delta()
  77. ..insert('Document\n')
  78. ..insert(kZeroWidthSpace, hr.toJson())
  79. ..insert('\nSome text\n')
  80. ..insert(kZeroWidthSpace, hr.toJson())
  81. ..insert('\n');
  82. final actual = rule.apply(doc, 10, 11);
  83. final expected = Delta()
  84. ..retain(11)
  85. ..delete(9);
  86. expect(actual, expected);
  87. });
  88. test('allows deleting empty line after embed', () {
  89. final hr = NotusAttribute.embed.horizontalRule;
  90. final doc = Delta()
  91. ..insert('Document\n')
  92. ..insert(kZeroWidthSpace, hr.toJson())
  93. ..insert('\n')
  94. ..insert('\n', NotusAttribute.block.bulletList.toJson())
  95. ..insert('Text')
  96. ..insert('\n');
  97. final actual = rule.apply(doc, 10, 1);
  98. final expected = Delta()
  99. ..retain(11)
  100. ..delete(1);
  101. expect(actual, expected);
  102. });
  103. test('allows deleting empty line(s) before embed', () {
  104. final hr = NotusAttribute.embed.horizontalRule;
  105. final doc = Delta()
  106. ..insert('Document\n')
  107. ..insert('\n')
  108. ..insert('\n')
  109. ..insert(kZeroWidthSpace, hr.toJson())
  110. ..insert('\n')
  111. ..insert('Text')
  112. ..insert('\n');
  113. final actual = rule.apply(doc, 11, 1);
  114. expect(actual, isNull);
  115. });
  116. });
  117. }