filters.js 9.2 KB

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