filters.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. describe('Filters', function() {
  2. let $filter;
  3. // Ignoring page reload request
  4. beforeAll(() => window.onbeforeunload = () => null);
  5. let webClientServiceMock = {
  6. contacts: {
  7. get: function(id) {
  8. if (id === 'AAAAAAAA') {
  9. return {
  10. displayName: 'ContactA'
  11. }
  12. }
  13. else if (id === 'XXXXXXXX') {
  14. return {
  15. displayName: 'ContactX'
  16. }
  17. }
  18. else if (id === '*AAAAAAA') {
  19. return {
  20. displayName: 'GWContactA'
  21. }
  22. }
  23. return null;
  24. }
  25. }
  26. };
  27. let translationMock = {
  28. instant: function(label) {
  29. return label;
  30. }
  31. };
  32. beforeEach(function() {
  33. // Load 3ema.filters module
  34. module('3ema.filters');
  35. module(function ($provide) {
  36. $provide.value('WebClientService', webClientServiceMock);
  37. $provide.value('$translate', translationMock);
  38. });
  39. // Inject the $filter function
  40. inject(function(_$filter_) {
  41. $filter = _$filter_;
  42. });
  43. });
  44. function testPatterns(filterName, cases) {
  45. const filter = $filter(filterName);
  46. for (let testcase of cases) {
  47. const input = testcase[0];
  48. const expected = testcase[1];
  49. expect(filter(input)).toEqual(expected);
  50. };
  51. };
  52. describe('markify', function() {
  53. this.testPatterns = (cases) => testPatterns('markify', cases);
  54. it('detects bold text', () => {
  55. this.testPatterns([
  56. ['*bold text (not italic)*',
  57. '<span class="text-bold">bold text (not italic)</span>'],
  58. ]);
  59. });
  60. it('detects italic text', () => {
  61. this.testPatterns([
  62. ['This text is not italic.',
  63. 'This text is not italic.'],
  64. ['_This text is italic._',
  65. '<span class="text-italic">This text is italic.</span>'],
  66. ['This text is _partially_ italic',
  67. 'This text is <span class="text-italic">partially</span> italic'],
  68. ['This text has _two_ _italic_ bits',
  69. 'This text has <span class="text-italic">two</span> <span class="text-italic">italic</span> bits'],
  70. ]);
  71. });
  72. it('detects strikethrough text', () => {
  73. this.testPatterns([
  74. ['so ~strikethrough~', 'so <span class="text-strike">strikethrough</span>'],
  75. ]);
  76. });
  77. it('detects mixed markup', () => {
  78. this.testPatterns([
  79. ['*bold text with _italic_ *',
  80. '<span class="text-bold">bold text with <span class="text-italic">italic</span> </span>'],
  81. ['*part bold,* _part italic_',
  82. '<span class="text-bold">part bold,</span> <span class="text-italic">part italic</span>'],
  83. ['_italic text with *bold* _',
  84. '<span class="text-italic">italic text with <span class="text-bold">bold</span> </span>'],
  85. ]);
  86. });
  87. it('is only applied on word boundaries', () => {
  88. this.testPatterns([
  89. ['so not_really_italic',
  90. 'so not_really_italic'],
  91. ['invalid*bold*stuff',
  92. 'invalid*bold*stuff'],
  93. ['no~strike~through',
  94. 'no~strike~through'],
  95. ['*bold_but_no~strike~through*',
  96. '<span class="text-bold">bold_but_no~strike~through</span>'],
  97. ]);
  98. });
  99. it('does not break URLs', () => {
  100. this.testPatterns([
  101. ['https://en.wikipedia.org/wiki/Java_class_file *nice*',
  102. 'https://en.wikipedia.org/wiki/Java_class_file <span class="text-bold">nice</span>'],
  103. ['<a href="https://threema.ch/>_Threema_</a>',
  104. '<a href="https://threema.ch/><span class="text-italic">Threema</span></a>'],
  105. ]);
  106. });
  107. it('ignores invalid markup', () => {
  108. this.testPatterns([
  109. ['*invalid markup (do not parse)_', '*invalid markup (do not parse)_'],
  110. ['random *asterisk', 'random *asterisk'],
  111. ]);
  112. });
  113. it('ignores markup with \\n (newline)', () => {
  114. this.testPatterns([
  115. ['*First line\n and a new one. (do not parse)*', '*First line\n and a new one. (do not parse)*'],
  116. ['*\nbegins with linebreak. (do not parse)*', '*\nbegins with linebreak. (do not parse)*'],
  117. ['*Just some text. But it ends with newline (do not parse)\n*', '*Just some text. But it ends with newline (do not parse)\n*'],
  118. ]);
  119. });
  120. });
  121. describe('escapeHtml', function() {
  122. this.testPatterns = (cases) => testPatterns('escapeHtml', cases);
  123. it('escapes html tags', () => {
  124. this.testPatterns([
  125. ['<h1>heading</h1>', '&lt;h1&gt;heading&lt;/h1&gt;'],
  126. ['<b>< script >foo&ndash;</b>< script>', '&lt;b&gt;&lt; script &gt;foo&amp;ndash;&lt;/b&gt;&lt; script&gt;'],
  127. ['<a href="/">a</a>', '&lt;a href=&quot;/&quot;&gt;a&lt;/a&gt;'],
  128. ]);
  129. });
  130. });
  131. describe('mentionify', function() {
  132. this.testPatterns = (cases) => testPatterns('mentionify', cases);
  133. it('no mentions', () => {
  134. this.testPatterns([
  135. ['', ''],
  136. ['hello my friend', 'hello my friend'],
  137. ['@[AAAAAAA]', '@[AAAAAAA]'],
  138. ['this is not a valid @[AAAAAAA]', 'this is not a valid @[AAAAAAA]'],
  139. ['@[@@@@@@@]', '@[@@@@@@@]'],
  140. ['this is not a valid @[@@@@@@@]', 'this is not a valid @[@@@@@@@]'],
  141. ]);
  142. });
  143. it('mention - no contacts', () => {
  144. this.testPatterns([
  145. ['@[BBBBBBBB]', '@[BBBBBBBB]'],
  146. ['@[*BBBBBBB]', '@[*BBBBBBB]'],
  147. ]);
  148. });
  149. it('mention - contact', () => {
  150. this.testPatterns([
  151. ['@[AAAAAAAA]', '<span class="mention contact">ContactA</span>'],
  152. ['hello @[AAAAAAAA]. @[AAAAAAAA] you are my friend', 'hello <span class="mention contact">ContactA</span>. <span class="mention contact">ContactA</span> you are my friend'],
  153. ['@[AAAAAAAA] @[AAAAAAAA] @[AAAAAAAA]', '<span class="mention contact">ContactA</span> <span class="mention contact">ContactA</span> <span class="mention contact">ContactA</span>']
  154. ]);
  155. });
  156. it('mention - all', () => {
  157. this.testPatterns([
  158. ['@[@@@@@@@@]', '<span class="mention all">messenger.ALL</span>'],
  159. ['@[@@@@@@@@] your base are belong to us', '<span class="mention all">messenger.ALL</span> your base are belong to us'],
  160. ['@[@@@@@@@@] @[@@@@@@@@] @[@@@@@@@@]', '<span class="mention all">messenger.ALL</span> <span class="mention all">messenger.ALL</span> <span class="mention all">messenger.ALL</span>']
  161. ]);
  162. });
  163. it('mention - mixed', () => {
  164. this.testPatterns([
  165. ['@[@@@@@@@@] @[AAAAAAAA] @[BBBBBBBB]', '<span class="mention all">messenger.ALL</span> <span class="mention contact">ContactA</span> @[BBBBBBBB]'],
  166. ]);
  167. });
  168. });
  169. });