zefyr

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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:notus/notus.dart';
  5. import 'package:test/test.dart';
  6. import 'package:quill_delta/quill_delta.dart';
  7. final ulAttrs = new NotusStyle().merge(NotusAttribute.ul);
  8. final olAttrs = new NotusStyle().merge(NotusAttribute.ol);
  9. final h1Attrs = new NotusStyle().merge(NotusAttribute.h1);
  10. void main() {
  11. group('$BlockNode', () {
  12. ContainerNode root;
  13. setUp(() {
  14. root = new RootNode();
  15. });
  16. test('empty', () {
  17. BlockNode node = new BlockNode();
  18. expect(node, isEmpty);
  19. expect(node.length, 0);
  20. expect(node.style, new NotusStyle());
  21. });
  22. test('toString', () {
  23. LineNode line = new LineNode();
  24. line.add(new TextNode('London "Grammar"'));
  25. BlockNode block = new BlockNode();
  26. block.applyAttribute(NotusAttribute.ul);
  27. block.add(line);
  28. final expected = '§ {ul}\n └ ¶ ⟨London "Grammar"⟩ ⏎';
  29. expect('$block', expected);
  30. });
  31. test('unwrapLine from first block', () {
  32. root.insert(0, 'One\nTwo\nThree', null);
  33. root.retain(3, 1, ulAttrs);
  34. root.retain(7, 1, ulAttrs);
  35. root.retain(13, 1, ulAttrs);
  36. expect(root.childCount, 1);
  37. BlockNode block = root.first;
  38. LineNode line = block.children.elementAt(1);
  39. block.unwrapLine(line);
  40. expect(root.children, hasLength(3));
  41. expect(root.children.elementAt(0), const TypeMatcher<BlockNode>());
  42. expect(root.children.elementAt(1), line);
  43. expect(root.children.elementAt(2), block);
  44. });
  45. test('format first line as list', () {
  46. root.insert(0, 'Hello world', null);
  47. root.retain(11, 1, ulAttrs);
  48. expect(root.childCount, 1);
  49. BlockNode block = root.first;
  50. expect(block.style.get(NotusAttribute.block),
  51. NotusAttribute.ul);
  52. expect(block.childCount, 1);
  53. expect(block.first, const TypeMatcher<LineNode>());
  54. LineNode line = block.first;
  55. Delta delta = new Delta()
  56. ..insert('Hello world')
  57. ..insert('\n', ulAttrs.toJson());
  58. expect(line.toDelta(), delta);
  59. });
  60. test('format second line as list', () {
  61. root.insert(0, 'Hello world\nAb cd ef!', null);
  62. root.retain(21, 1, ulAttrs);
  63. expect(root.childCount, 2);
  64. BlockNode block = root.last;
  65. expect(block.style.get(NotusAttribute.block),
  66. NotusAttribute.ul);
  67. expect(block.childCount, 1);
  68. expect(block.first, const TypeMatcher<LineNode>());
  69. });
  70. test('format two sibling lines as list', () {
  71. root.insert(0, 'Hello world\nAb cd ef!', null);
  72. root.retain(11, 1, ulAttrs);
  73. root.retain(21, 1, ulAttrs);
  74. expect(root.childCount, 1);
  75. BlockNode block = root.first;
  76. expect(block.style.get(NotusAttribute.block),
  77. NotusAttribute.ul);
  78. expect(block.childCount, 2);
  79. expect(block.first, const TypeMatcher<LineNode>());
  80. expect(block.last, const TypeMatcher<LineNode>());
  81. });
  82. test('format to split first line from block', () {
  83. root.insert(
  84. 0, 'London Grammar Songs\nHey now\nStrong\nIf You Wait', null);
  85. root.retain(20, 1, h1Attrs);
  86. root.retain(28, 1, ulAttrs);
  87. root.retain(35, 1, ulAttrs);
  88. root.retain(47, 1, ulAttrs);
  89. expect(root.childCount, 2);
  90. root.retain(28, 1, olAttrs);
  91. expect(root.childCount, 3);
  92. final expected = new Delta()
  93. ..insert('London Grammar Songs')
  94. ..insert('\n', NotusAttribute.h1.toJson())
  95. ..insert('Hey now')
  96. ..insert('\n', NotusAttribute.ol.toJson())
  97. ..insert('Strong')
  98. ..insert('\n', ulAttrs.toJson())
  99. ..insert('If You Wait')
  100. ..insert('\n', ulAttrs.toJson());
  101. expect(root.toDelta(), expected);
  102. });
  103. test('format to split last line from block', () {
  104. root.insert(
  105. 0, 'London Grammar Songs\nHey now\nStrong\nIf You Wait', null);
  106. root.retain(20, 1, h1Attrs);
  107. root.retain(28, 1, ulAttrs);
  108. root.retain(35, 1, ulAttrs);
  109. root.retain(47, 1, ulAttrs);
  110. expect(root.childCount, 2);
  111. root.retain(47, 1, olAttrs);
  112. expect(root.childCount, 3);
  113. final expected = new Delta()
  114. ..insert('London Grammar Songs')
  115. ..insert('\n', NotusAttribute.h1.toJson())
  116. ..insert('Hey now')
  117. ..insert('\n', ulAttrs.toJson())
  118. ..insert('Strong')
  119. ..insert('\n', ulAttrs.toJson())
  120. ..insert('If You Wait')
  121. ..insert('\n', NotusAttribute.ol.toJson());
  122. expect(root.toDelta(), expected);
  123. });
  124. test('format to split middle line from block', () {
  125. root.insert(
  126. 0, 'London Grammar Songs\nHey now\nStrong\nIf You Wait', null);
  127. root.retain(20, 1, h1Attrs);
  128. root.retain(28, 1, ulAttrs);
  129. root.retain(35, 1, ulAttrs);
  130. root.retain(47, 1, ulAttrs);
  131. expect(root.childCount, 2);
  132. root.retain(35, 1, olAttrs);
  133. expect(root.childCount, 4);
  134. final expected = new Delta()
  135. ..insert('London Grammar Songs')
  136. ..insert('\n', NotusAttribute.h1.toJson())
  137. ..insert('Hey now')
  138. ..insert('\n', ulAttrs.toJson())
  139. ..insert('Strong')
  140. ..insert('\n', NotusAttribute.ol.toJson())
  141. ..insert('If You Wait')
  142. ..insert('\n', ulAttrs.toJson());
  143. expect(root.toDelta(), expected);
  144. });
  145. test('insert line-break at the begining of the document', () {
  146. root.insert(
  147. 0, 'London Grammar Songs\nHey now\nStrong\nIf You Wait', null);
  148. root.retain(20, 1, ulAttrs);
  149. root.retain(28, 1, ulAttrs);
  150. root.retain(35, 1, ulAttrs);
  151. root.retain(47, 1, ulAttrs);
  152. expect(root.childCount, 1);
  153. root.insert(0, '\n', null);
  154. expect(root.childCount, 2);
  155. });
  156. });
  157. }