filters.js 4.8 KB

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