zefyr

insert_rules_test.dart 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 = new CatchAllInsertRule();
  12. test('applies change as-is', () {
  13. final doc = new Delta()..insert('Document\n');
  14. final actual = rule.apply(doc, 8, '!');
  15. final expected = new Delta()
  16. ..retain(8)
  17. ..insert('!');
  18. expect(actual, expected);
  19. });
  20. });
  21. group('$PreserveLineStyleOnSplitRule', () {
  22. final rule = new PreserveLineStyleOnSplitRule();
  23. test('skips at the beginning of a document', () {
  24. final doc = new 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 = new 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 = new 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 = new 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 = new 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 = new Delta()..insert('Hello world\n');
  58. final actual = rule.apply(doc, 11, '\n');
  59. expect(actual, isNotNull);
  60. final expected = new Delta()
  61. ..retain(11)
  62. ..insert('\n');
  63. expect(actual, expected);
  64. });
  65. test('applies at the beginning of a document', () {
  66. final doc = new Delta()..insert('\n', NotusAttribute.h1.toJson());
  67. final actual = rule.apply(doc, 0, '\n');
  68. expect(actual, isNotNull);
  69. final expected = new 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 = new Delta()..insert('Hello world')..insert('\n', style);
  78. final actual = rule.apply(doc, 11, '\n');
  79. expect(actual, isNotNull);
  80. final expected = new 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 = new 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 = new 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 = new AutoExitBlockRule();
  101. test('applies when line-break is inserted on empty line in a block', () {
  102. final ul = NotusAttribute.ul.toJson();
  103. final doc = new 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 = new 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 = new 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 = new Delta()..insert('\n', ul);
  124. final actual = rule.apply(doc, 0, '\n');
  125. expect(actual, isNotNull);
  126. final expected = new Delta()
  127. ..retain(1, NotusAttribute.block.unset.toJson());
  128. expect(actual, expected);
  129. });
  130. test('ignores non-empty line at the beginning of a document', () {
  131. final ul = NotusAttribute.ul.toJson();
  132. final doc = new Delta()..insert('Text\n', ul);
  133. final actual = rule.apply(doc, 0, '\n');
  134. expect(actual, isNull);
  135. });
  136. });
  137. group('$PreserveInlineStylesRule', () {
  138. final rule = new PreserveInlineStylesRule();
  139. test('apply', () {
  140. final doc = new Delta()
  141. ..insert('Doc with ')
  142. ..insert('bold', bold)
  143. ..insert(' text');
  144. final actual = rule.apply(doc, 13, 'er');
  145. final expected = new Delta()
  146. ..retain(13)
  147. ..insert('er', bold);
  148. expect(expected, actual);
  149. });
  150. test('apply at the beginning of a document', () {
  151. final doc = new Delta()..insert('Doc with ');
  152. final actual = rule.apply(doc, 0, 'A ');
  153. expect(actual, isNull);
  154. });
  155. });
  156. group('$AutoFormatLinksRule', () {
  157. final rule = new AutoFormatLinksRule();
  158. final link = NotusAttribute.link.fromString('https://example.com').toJson();
  159. test('apply simple', () {
  160. final doc = new Delta()..insert('Doc with link https://example.com');
  161. final actual = rule.apply(doc, 33, ' ');
  162. final expected = new Delta()
  163. ..retain(14)
  164. ..retain(19, link)
  165. ..insert(' ');
  166. expect(expected, actual);
  167. });
  168. test('applies only to insert of single space', () {
  169. final doc = new Delta()..insert('Doc with link https://example.com');
  170. final actual = rule.apply(doc, 33, '/');
  171. expect(actual, isNull);
  172. });
  173. test('applies for links at the beginning of line', () {
  174. final doc = new Delta()..insert('Doc with link\nhttps://example.com');
  175. final actual = rule.apply(doc, 33, ' ');
  176. final expected = new Delta()
  177. ..retain(14)
  178. ..retain(19, link)
  179. ..insert(' ');
  180. expect(expected, actual);
  181. });
  182. test('ignores if already formatted as link', () {
  183. final doc = new Delta()
  184. ..insert('Doc with link\n')
  185. ..insert('https://example.com', link);
  186. final actual = rule.apply(doc, 33, ' ');
  187. expect(actual, isNull);
  188. });
  189. });
  190. group('$PreserveBlockStyleOnPasteRule', () {
  191. final rule = new PreserveBlockStyleOnPasteRule();
  192. test('applies in a block', () {
  193. final doc = new Delta()
  194. ..insert('One and two')
  195. ..insert('\n', ul)
  196. ..insert('Three')
  197. ..insert('\n', ul);
  198. final actual = rule.apply(doc, 8, 'also \n');
  199. final expected = new Delta()
  200. ..retain(8)
  201. ..insert('also ')
  202. ..insert('\n', ul);
  203. expect(actual, isNotNull);
  204. expect(actual, expected);
  205. });
  206. });
  207. }