filters.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. describe('markify', function() {
  13. this.testPatterns = (cases) => {
  14. const filter = $filter('markify');
  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. it('detects bold text', () => {
  22. this.testPatterns([
  23. ['*bold text (not italic)*',
  24. '<span class="text-bold">bold text (not italic)</span>'],
  25. ]);
  26. });
  27. it('detects italic text', () => {
  28. this.testPatterns([
  29. ['This text is not italic.',
  30. 'This text is not italic.'],
  31. ['_This text is italic._',
  32. '<span class="text-italic">This text is italic.</span>'],
  33. ['This text is _partially_ italic',
  34. 'This text is <span class="text-italic">partially</span> italic'],
  35. ['This text has _two_ _italic_ bits',
  36. 'This text has <span class="text-italic">two</span> <span class="text-italic">italic</span> bits'],
  37. ]);
  38. });
  39. it('detects strikethrough text', () => {
  40. this.testPatterns([
  41. ['so ~strikethrough~', 'so <span class="text-strike">strikethrough</span>'],
  42. ]);
  43. });
  44. it('detects mixed markup', () => {
  45. this.testPatterns([
  46. ['*bold text with _italic_ *',
  47. '<span class="text-bold">bold text with <span class="text-italic">italic</span> </span>'],
  48. ['*part bold,* _part italic_',
  49. '<span class="text-bold">part bold,</span> <span class="text-italic">part italic</span>'],
  50. ['_italic text with *bold* _',
  51. '<span class="text-italic">italic text with <span class="text-bold">bold</span> </span>'],
  52. ]);
  53. });
  54. it('is only applied on word boundaries', () => {
  55. this.testPatterns([
  56. ['so not_really_italic',
  57. 'so not_really_italic'],
  58. ['invalid*bold*stuff',
  59. 'invalid*bold*stuff'],
  60. ['no~strike~through',
  61. 'no~strike~through'],
  62. ['*bold_but_no~strike~through*',
  63. '<span class="text-bold">bold_but_no~strike~through</span>'],
  64. ]);
  65. });
  66. it('does not break URLs', () => {
  67. this.testPatterns([
  68. ['https://en.wikipedia.org/wiki/Java_class_file *nice*',
  69. 'https://en.wikipedia.org/wiki/Java_class_file <span class="text-bold">nice</span>'],
  70. ['<a href="https://threema.ch/>_Threema_</a>',
  71. '<a href="https://threema.ch/><span class="text-italic">Threema</span></a>'],
  72. ]);
  73. });
  74. it('ignores invalid markup', () => {
  75. this.testPatterns([
  76. ['*invalid markup (do not parse)_', '*invalid markup (do not parse)_'],
  77. ['random *asterisk', 'random *asterisk'],
  78. ]);
  79. });
  80. });
  81. describe('htmlToAsciiMarkup', function() {
  82. this.testPatterns = (cases) => {
  83. const filter = $filter('htmlToAsciiMarkup');
  84. for (let testcase of cases) {
  85. const input = testcase[0];
  86. const expected = testcase[1];
  87. expect(filter(input)).toEqual(expected);
  88. };
  89. };
  90. it('converts bold text', () => {
  91. this.testPatterns([
  92. ['<b>bold</b>', '*bold*'],
  93. ['< b >bold</b>', '*bold*'],
  94. ['<B>bold</b>', '*bold*'],
  95. ['<b class="asdf">bold</b>', '*bold*'],
  96. ['<strong>bold</strong>', '*bold*'],
  97. ['<b><b>bold</b></b>', '**bold**'],
  98. ['<b><strong>bold</strong></b>', '**bold**'],
  99. ]);
  100. });
  101. it('converts italic text', () => {
  102. this.testPatterns([
  103. ['<i>italic</i>', '_italic_'],
  104. ['<i onclick="alert(1)">italic</i>', '_italic_'],
  105. ['<em>italic</em>', '_italic_'],
  106. ['<i><em>italic</em></i>', '__italic__'],
  107. ]);
  108. });
  109. it('converts strikethrough text', () => {
  110. this.testPatterns([
  111. ['<strike>strikethrough</strike>', '~strikethrough~'],
  112. ['<del>strikethrough</del>', '~strikethrough~'],
  113. ['<del href="/">strikethrough</del>', '~strikethrough~'],
  114. ['<s>strikethrough</s>', '~strikethrough~'],
  115. ['<strike><del><s>strikethrough</s></del></strike>', '~~~strikethrough~~~'],
  116. ]);
  117. });
  118. it('does not affect other tags', () => {
  119. this.testPatterns([
  120. ['<script>alert("pho soup time")</script>', '<script>alert("pho soup time")</script>'],
  121. ]);
  122. });
  123. it('combination of all', () => {
  124. this.testPatterns([
  125. ['<b><em><del>foo</del></em></b>', '*_~foo~_*'],
  126. ]);
  127. });
  128. });
  129. });