zefyr

insert_rules_test.dart 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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('$CatchAllInsertRule', () {
  11. final rule = CatchAllInsertRule();
  12. test('applies change as-is', () {
  13. final doc = Delta()..insert('Document\n');
  14. final actual = rule.apply(doc, 8, '!');
  15. final expected = Delta()
  16. ..retain(8)
  17. ..insert('!');
  18. expect(actual, expected);
  19. });
  20. });
  21. group('$PreserveLineStyleOnSplitRule', () {
  22. final rule = PreserveLineStyleOnSplitRule();
  23. test('skips at the beginning of a document', () {
  24. final doc = Delta()..insert('One\n');
  25. final actual = rule.apply(doc, 0, '\n');
  26. expect(actual, isNull);
  27. });
  28. test('applies in a block', () {
  29. final doc = Delta()
  30. ..insert('One and two')
  31. ..insert('\n', ul)
  32. ..insert('Three')
  33. ..insert('\n', ul);
  34. final actual = rule.apply(doc, 8, '\n');
  35. final expected = Delta()
  36. ..retain(8)
  37. ..insert('\n', ul);
  38. expect(actual, isNotNull);
  39. expect(actual, expected);
  40. });
  41. });
  42. group('$ResetLineFormatOnNewLineRule', () {
  43. final rule = const ResetLineFormatOnNewLineRule();
  44. test('applies when line-break is inserted at the end of line', () {
  45. final doc = Delta()
  46. ..insert('Hello world')
  47. ..insert('\n', NotusAttribute.h1.toJson());
  48. final actual = rule.apply(doc, 11, '\n');
  49. expect(actual, isNotNull);
  50. final expected = Delta()
  51. ..retain(11)
  52. ..insert('\n', NotusAttribute.h1.toJson())
  53. ..retain(1, NotusAttribute.heading.unset.toJson());
  54. expect(actual, expected);
  55. });
  56. test('applies without style reset if not needed', () {
  57. final doc = Delta()..insert('Hello world\n');
  58. final actual = rule.apply(doc, 11, '\n');
  59. expect(actual, isNotNull);
  60. final expected = Delta()
  61. ..retain(11)
  62. ..insert('\n');
  63. expect(actual, expected);
  64. });
  65. test('applies at the beginning of a document', () {
  66. final doc = Delta()..insert('\n', NotusAttribute.h1.toJson());
  67. final actual = rule.apply(doc, 0, '\n');
  68. expect(actual, isNotNull);
  69. final expected = Delta()
  70. ..insert('\n', NotusAttribute.h1.toJson())
  71. ..retain(1, NotusAttribute.heading.unset.toJson());
  72. expect(actual, expected);
  73. });
  74. test('applies and keeps block style', () {
  75. final style = NotusAttribute.ul.toJson();
  76. style.addAll(NotusAttribute.h1.toJson());
  77. final doc = Delta()..insert('Hello world')..insert('\n', style);
  78. final actual = rule.apply(doc, 11, '\n');
  79. expect(actual, isNotNull);
  80. final expected = Delta()
  81. ..retain(11)
  82. ..insert('\n', style)
  83. ..retain(1, NotusAttribute.heading.unset.toJson());
  84. expect(actual, expected);
  85. });
  86. test('applies to a line in the middle of a document', () {
  87. final doc = Delta()
  88. ..insert('Hello \nworld!\nMore lines here.')
  89. ..insert('\n', NotusAttribute.h2.toJson());
  90. final actual = rule.apply(doc, 30, '\n');
  91. expect(actual, isNotNull);
  92. final expected = Delta()
  93. ..retain(30)
  94. ..insert('\n', NotusAttribute.h2.toJson())
  95. ..retain(1, NotusAttribute.heading.unset.toJson());
  96. expect(actual, expected);
  97. });
  98. });
  99. group('$AutoExitBlockRule', () {
  100. final rule = AutoExitBlockRule();
  101. test('applies when line-break is inserted on empty line in a block', () {
  102. final ul = NotusAttribute.ul.toJson();
  103. final doc = Delta()
  104. ..insert('Item 1')
  105. ..insert('\n', ul)
  106. ..insert('Item 2')
  107. ..insert('\n\n', ul);
  108. final actual = rule.apply(doc, 14, '\n');
  109. expect(actual, isNotNull);
  110. final expected = Delta()
  111. ..retain(14)
  112. ..retain(1, NotusAttribute.block.unset.toJson());
  113. expect(actual, expected);
  114. });
  115. test('applies only on empty line', () {
  116. final ul = NotusAttribute.ul.toJson();
  117. final doc = Delta()..insert('Item 1')..insert('\n', ul);
  118. final actual = rule.apply(doc, 6, '\n');
  119. expect(actual, isNull);
  120. });
  121. test('applies at the beginning of a document', () {
  122. final ul = NotusAttribute.ul.toJson();
  123. final doc = Delta()..insert('\n', ul);
  124. final actual = rule.apply(doc, 0, '\n');
  125. expect(actual, isNotNull);
  126. final expected = Delta()..retain(1, NotusAttribute.block.unset.toJson());
  127. expect(actual, expected);
  128. });
  129. test('ignores non-empty line at the beginning of a document', () {
  130. final ul = NotusAttribute.ul.toJson();
  131. final doc = Delta()..insert('Text\n', ul);
  132. final actual = rule.apply(doc, 0, '\n');
  133. expect(actual, isNull);
  134. });
  135. });
  136. group('$PreserveInlineStylesRule', () {
  137. final rule = PreserveInlineStylesRule();
  138. test('apply', () {
  139. final doc = Delta()
  140. ..insert('Doc with ')
  141. ..insert('bold', bold)
  142. ..insert(' text');
  143. final actual = rule.apply(doc, 13, 'er');
  144. final expected = Delta()
  145. ..retain(13)
  146. ..insert('er', bold);
  147. expect(expected, actual);
  148. });
  149. test('apply at the beginning of a document', () {
  150. final doc = Delta()..insert('Doc with ');
  151. final actual = rule.apply(doc, 0, 'A ');
  152. expect(actual, isNull);
  153. });
  154. });
  155. group('$AutoFormatLinksRule', () {
  156. final rule = AutoFormatLinksRule();
  157. final link = NotusAttribute.link.fromString('https://example.com').toJson();
  158. test('apply simple', () {
  159. final doc = Delta()..insert('Doc with link https://example.com');
  160. final actual = rule.apply(doc, 33, ' ');
  161. final expected = Delta()
  162. ..retain(14)
  163. ..retain(19, link)
  164. ..insert(' ');
  165. expect(expected, actual);
  166. });
  167. test('applies only to insert of single space', () {
  168. final doc = Delta()..insert('Doc with link https://example.com');
  169. final actual = rule.apply(doc, 33, '/');
  170. expect(actual, isNull);
  171. });
  172. test('applies for links at the beginning of line', () {
  173. final doc = Delta()..insert('Doc with link\nhttps://example.com');
  174. final actual = rule.apply(doc, 33, ' ');
  175. final expected = Delta()
  176. ..retain(14)
  177. ..retain(19, link)
  178. ..insert(' ');
  179. expect(expected, actual);
  180. });
  181. test('ignores if already formatted as link', () {
  182. final doc = Delta()
  183. ..insert('Doc with link\n')
  184. ..insert('https://example.com', link);
  185. final actual = rule.apply(doc, 33, ' ');
  186. expect(actual, isNull);
  187. });
  188. });
  189. group('$PreserveBlockStyleOnPasteRule', () {
  190. final rule = PreserveBlockStyleOnPasteRule();
  191. test('applies in a block', () {
  192. final doc = Delta()
  193. ..insert('One and two')
  194. ..insert('\n', ul)
  195. ..insert('Three')
  196. ..insert('\n', ul);
  197. final actual = rule.apply(doc, 8, 'also \n');
  198. final expected = Delta()
  199. ..retain(8)
  200. ..insert('also ')
  201. ..insert('\n', ul);
  202. expect(actual, isNotNull);
  203. expect(actual, expected);
  204. });
  205. });
  206. }