filters.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. describe('Filters', function() {
  2. let $filter;
  3. // Ignoring page reload request
  4. beforeAll(() => window.onbeforeunload = () => null);
  5. beforeEach(function() {
  6. // Load 3ema.filters module
  7. module('3ema.filters');
  8. // Inject the $filter function
  9. inject(function(_$filter_) {
  10. $filter = _$filter_;
  11. });
  12. });
  13. function testPatterns(filterName, cases) {
  14. const filter = $filter(filterName);
  15. for (let testcase of cases) {
  16. const input = testcase[0];
  17. const expected = testcase[1];
  18. expect(filter(input)).toEqual(expected);
  19. };
  20. };
  21. describe('markify', function() {
  22. this.testPatterns = (cases) => testPatterns('markify', cases);
  23. it('detects bold text', () => {
  24. this.testPatterns([
  25. ['*bold text (not italic)*',
  26. '<span class="text-bold">bold text (not italic)</span>'],
  27. ]);
  28. });
  29. it('detects italic text', () => {
  30. this.testPatterns([
  31. ['This text is not italic.',
  32. 'This text is not italic.'],
  33. ['_This text is italic._',
  34. '<span class="text-italic">This text is italic.</span>'],
  35. ['This text is _partially_ italic',
  36. 'This text is <span class="text-italic">partially</span> italic'],
  37. ['This text has _two_ _italic_ bits',
  38. 'This text has <span class="text-italic">two</span> <span class="text-italic">italic</span> bits'],
  39. ]);
  40. });
  41. it('detects strikethrough text', () => {
  42. this.testPatterns([
  43. ['so ~strikethrough~', 'so <span class="text-strike">strikethrough</span>'],
  44. ]);
  45. });
  46. it('detects mixed markup', () => {
  47. this.testPatterns([
  48. ['*bold text with _italic_ *',
  49. '<span class="text-bold">bold text with <span class="text-italic">italic</span> </span>'],
  50. ['*part bold,* _part italic_',
  51. '<span class="text-bold">part bold,</span> <span class="text-italic">part italic</span>'],
  52. ['_italic text with *bold* _',
  53. '<span class="text-italic">italic text with <span class="text-bold">bold</span> </span>'],
  54. ]);
  55. });
  56. it('is only applied on word boundaries', () => {
  57. this.testPatterns([
  58. ['so not_really_italic',
  59. 'so not_really_italic'],
  60. ['invalid*bold*stuff',
  61. 'invalid*bold*stuff'],
  62. ['no~strike~through',
  63. 'no~strike~through'],
  64. ['*bold_but_no~strike~through*',
  65. '<span class="text-bold">bold_but_no~strike~through</span>'],
  66. ]);
  67. });
  68. it('does not break URLs', () => {
  69. this.testPatterns([
  70. ['https://en.wikipedia.org/wiki/Java_class_file *nice*',
  71. 'https://en.wikipedia.org/wiki/Java_class_file <span class="text-bold">nice</span>'],
  72. ['<a href="https://threema.ch/>_Threema_</a>',
  73. '<a href="https://threema.ch/><span class="text-italic">Threema</span></a>'],
  74. ]);
  75. });
  76. it('ignores invalid markup', () => {
  77. this.testPatterns([
  78. ['*invalid markup (do not parse)_', '*invalid markup (do not parse)_'],
  79. ['random *asterisk', 'random *asterisk'],
  80. ]);
  81. });
  82. it('ignores markup with \\n (newline)', () => {
  83. this.testPatterns([
  84. ['*First line\n and a new one. (do not parse)*', '*First line\n and a new one. (do not parse)*'],
  85. ['*\nbegins with linebreak. (do not parse)*', '*\nbegins with linebreak. (do not parse)*'],
  86. ['*Just some text. But it ends with newline (do not parse)\n*', '*Just some text. But it ends with newline (do not parse)\n*'],
  87. ]);
  88. });
  89. });
  90. describe('escapeHtml', function() {
  91. this.testPatterns = (cases) => testPatterns('escapeHtml', cases);
  92. it('escapes html tags', () => {
  93. this.testPatterns([
  94. ['<h1>heading</h1>', '&lt;h1&gt;heading&lt;/h1&gt;'],
  95. ['<b>< script >foo&ndash;</b>< script>', '&lt;b&gt;&lt; script &gt;foo&amp;ndash;&lt;/b&gt;&lt; script&gt;'],
  96. ['<a href="/">a</a>', '&lt;a href=&quot;/&quot;&gt;a&lt;/a&gt;'],
  97. ]);
  98. });
  99. });
  100. });