filters.js 4.4 KB

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