filters.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. describe('Filters', function() {
  2. let $filter;
  3. // Ignoring page reload request
  4. beforeAll(() => window.onbeforeunload = () => null);
  5. let webClientServiceMock = {
  6. me: {
  7. id: 'MEMEMEME',
  8. displayName: 'Er'
  9. },
  10. contacts: {
  11. get: function(id) {
  12. if (id === 'AAAAAAAA') {
  13. return {
  14. displayName: 'ContactA'
  15. }
  16. }
  17. else if (id === 'XXXXXXXX') {
  18. return {
  19. displayName: 'ContactX'
  20. }
  21. }
  22. else if (id === '*AAAAAAA') {
  23. return {
  24. displayName: 'GWContactA'
  25. }
  26. }
  27. else if (id === 'BAD0BAD1') {
  28. return {
  29. displayName: '<b>< script >foo&ndash;</b>< script>',
  30. }
  31. }
  32. return null;
  33. }
  34. }
  35. };
  36. let translationMock = {
  37. instant: function(label) {
  38. return label;
  39. }
  40. };
  41. beforeEach(function() {
  42. module('3ema.services');
  43. module('3ema.filters');
  44. module(function($provide) {
  45. $provide.value('WebClientService', webClientServiceMock);
  46. $provide.value('$translate', translationMock);
  47. $provide.constant('$state', null);
  48. });
  49. // Inject the $filter function
  50. inject(function(_$filter_) {
  51. $filter = _$filter_;
  52. });
  53. });
  54. function testPatterns(filterName, cases) {
  55. const filter = $filter(filterName);
  56. for (let testcase of cases) {
  57. const input = testcase[0];
  58. const expected = testcase[1];
  59. expect(filter(input)).toEqual(expected);
  60. };
  61. };
  62. describe('escapeHtml', function() {
  63. this.testPatterns = (cases) => testPatterns('escapeHtml', cases);
  64. it('escapes html tags', () => {
  65. this.testPatterns([
  66. ['<h1>heading</h1>', '&lt;h1&gt;heading&lt;/h1&gt;'],
  67. ['<b>< script >foo&ndash;</b>< script>', '&lt;b&gt;&lt; script &gt;foo&amp;ndash;&lt;/b&gt;&lt; script&gt;'],
  68. ['<a href="/">a</a>', '&lt;a href=&quot;/&quot;&gt;a&lt;/a&gt;'],
  69. ]);
  70. });
  71. });
  72. describe('mentionify', function() {
  73. this.testPatterns = (cases) => testPatterns('mentionify', cases);
  74. it('no mentions', () => {
  75. this.testPatterns([
  76. ['', ''],
  77. ['hello my friend', 'hello my friend'],
  78. ['@[AAAAAAA]', '@[AAAAAAA]'],
  79. ['this is not a valid @[AAAAAAA]', 'this is not a valid @[AAAAAAA]'],
  80. ['@[@@@@@@@]', '@[@@@@@@@]'],
  81. ['this is not a valid @[@@@@@@@]', 'this is not a valid @[@@@@@@@]'],
  82. ]);
  83. });
  84. it('mention - no contacts', () => {
  85. this.testPatterns([
  86. ['@[BBBBBBBB]', '@[BBBBBBBB]'],
  87. ['@[*BBBBBBB]', '@[*BBBBBBB]'],
  88. ]);
  89. });
  90. it('mention - contact', () => {
  91. this.testPatterns([
  92. ['@[AAAAAAAA]', '<span class="mention id AAAAAAAA" text="@[AAAAAAAA]">ContactA</span>'],
  93. ['hello @[AAAAAAAA]. @[AAAAAAAA] you are my friend', 'hello <span class="mention id AAAAAAAA" text="@[AAAAAAAA]">ContactA</span>. <span class="mention id AAAAAAAA" text="@[AAAAAAAA]">ContactA</span> you are my friend'],
  94. ['@[AAAAAAAA] @[AAAAAAAA] @[AAAAAAAA]', '<span class="mention id AAAAAAAA" text="@[AAAAAAAA]">ContactA</span> <span class="mention id AAAAAAAA" text="@[AAAAAAAA]">ContactA</span> <span class="mention id AAAAAAAA" text="@[AAAAAAAA]">ContactA</span>']
  95. ]);
  96. });
  97. it('mention - all', () => {
  98. this.testPatterns([
  99. ['@[@@@@@@@@]', '<span class="mention all" text="@[@@@@@@@@]">messenger.ALL</span>'],
  100. ['@[@@@@@@@@] your base are belong to us', '<span class="mention all" text="@[@@@@@@@@]">messenger.ALL</span> your base are belong to us'],
  101. ['@[@@@@@@@@] @[@@@@@@@@] @[@@@@@@@@]', '<span class="mention all" text="@[@@@@@@@@]">messenger.ALL</span> <span class="mention all" text="@[@@@@@@@@]">messenger.ALL</span> <span class="mention all" text="@[@@@@@@@@]">messenger.ALL</span>']
  102. ]);
  103. });
  104. it('mention - mixed', () => {
  105. this.testPatterns([
  106. ['@[@@@@@@@@] @[AAAAAAAA] @[BBBBBBBB]', '<span class="mention all" text="@[@@@@@@@@]">messenger.ALL</span> <span class="mention id AAAAAAAA" text="@[AAAAAAAA]">ContactA</span> @[BBBBBBBB]'],
  107. ]);
  108. });
  109. it('mention - me contact', () => {
  110. this.testPatterns([
  111. ['@[MEMEMEME]', '<span class="mention me" text="@[MEMEMEME]">Er</span>'],
  112. ['hello @[MEMEMEME]. @[MEMEMEME] you are my friend', 'hello <span class="mention me" text="@[MEMEMEME]">Er</span>. <span class="mention me" text="@[MEMEMEME]">Er</span> you are my friend'],
  113. ['@[MEMEMEME] @[MEMEMEME] @[MEMEMEME]', '<span class="mention me" text="@[MEMEMEME]">Er</span> <span class="mention me" text="@[MEMEMEME]">Er</span> <span class="mention me" text="@[MEMEMEME]">Er</span>']
  114. ]);
  115. });
  116. it('mention - escape html parameters', () => {
  117. this.testPatterns([
  118. ['@[BAD0BAD1]', '<span class="mention id BAD0BAD1" text="@[BAD0BAD1]">&lt;b&gt;&lt; script &gt;foo&amp;ndash;&lt;/b&gt;&lt; script&gt;</span>'],
  119. ]);
  120. });
  121. });
  122. describe('nlToBr', function() {
  123. this.testPatterns = (cases) => testPatterns('nlToBr', cases);
  124. it('converts newlines (enabled=true)', () => {
  125. const filter = $filter('nlToBr');
  126. expect(filter('abc \n def', true)).toEqual('abc <br> def');
  127. expect(filter('a\nb\nc\\n', true)).toEqual('a<br>b<br>c\\n');
  128. });
  129. it('does not converts newlines (enabled=false)', () => {
  130. const filter = $filter('nlToBr');
  131. expect(filter('abc\ndef', false)).toEqual('abc\ndef');
  132. });
  133. it('if enabled flag is not set, converts newlines', () => {
  134. const filter = $filter('nlToBr');
  135. expect(filter('abc\ndef')).toEqual('abc<br>def');
  136. });
  137. });
  138. describe('unixToTimestring', function() {
  139. this.testPatterns = (cases) => testPatterns('unixToTimestring', cases);
  140. it('shows only time for today', () => {
  141. const d1 = new Date(); d1.setHours(8); d1.setMinutes(7);
  142. const d2 = new Date(); d2.setHours(12); d2.setMinutes(14);
  143. const d3 = new Date(); d3.setHours(0); d3.setMinutes(0);
  144. this.testPatterns([
  145. [d1.getTime() / 1000, '08:07'],
  146. [d2.getTime() / 1000, '12:14'],
  147. [d3.getTime() / 1000, '00:00'],
  148. ]);
  149. });
  150. it('shows full date with forceFull flag', () => {
  151. const d = new Date();
  152. const formatted = $filter('unixToTimestring')(d.getTime() / 1000, true);
  153. expect(formatted.length > 10).toBe(true);
  154. expect(formatted).toContain(d.getFullYear().toString());
  155. });
  156. it('shows "yesterday" for yesterday', () => {
  157. const d1 = new Date();
  158. const ts = d1.getTime();
  159. const d2 = new Date(ts - 1000 * 60 * 60 * 24);
  160. d2.setHours(8); d2.setMinutes(7);
  161. this.testPatterns([
  162. [d2.getTime() / 1000, 'date.YESTERDAY, 08:07'],
  163. ]);
  164. });
  165. it('shows full datetime for other days', () => {
  166. jasmine.clock().install();
  167. jasmine.clock().mockDate(new Date(2018, 9, 9, 20, 42));
  168. const now = new Date();
  169. const d1 = new Date(2010, 1, 7, 18, 42);
  170. const d2 = new Date(now.getFullYear(), 4, 2, 23, 59);
  171. this.testPatterns([
  172. [d1.getTime() / 1000, '7. date.month_short.FEB 2010, 18:42'],
  173. [d2.getTime() / 1000, '2. date.month_short.MAY, 23:59'],
  174. ]);
  175. });
  176. });
  177. describe('linkify', function() {
  178. let process = (text) => {
  179. return $filter('linkify')(text)
  180. };
  181. it('links http urls', () => {
  182. expect(process('hello https://threema.ch/!'))
  183. .toEqual('hello <a href="https://threema.ch/" class="autolinked autolinked-url" target="_blank" rel="noopener noreferrer">https://threema.ch/</a>!');
  184. });
  185. it('links e-mails', () => {
  186. expect(process('hello info@threema.ch!'))
  187. .toEqual('hello <a href="mailto:info@threema.ch" class="autolinked autolinked-email" target="_blank" rel="noopener noreferrer">info@threema.ch</a>!');
  188. });
  189. it('does not link phone numbers', () => {
  190. const input = 'hello +41791234567';
  191. expect(process(input)).toEqual(input);
  192. });
  193. it('does not link mentions', () => {
  194. const input = 'hello @threemaapp';
  195. expect(process(input)).toEqual(input);
  196. });
  197. it('does not link hashtags', () => {
  198. const input = 'hello #threema';
  199. expect(process(input)).toEqual(input);
  200. });
  201. });
  202. });