html_line_converter.spec.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { convertHtmlToLines } from 'src/services/html_converter/html_line_converter.service.js'
  2. const greentextHandle = new Set(['p', 'div'])
  3. const mapOnlyText = (processor) => (input) => {
  4. if (input.text && input.level.every(l => greentextHandle.has(l))) {
  5. return processor(input.text)
  6. } else if (input.text) {
  7. return input.text
  8. } else {
  9. return input
  10. }
  11. }
  12. describe('html_line_converter', () => {
  13. describe('with processor that keeps original line should not make any changes to HTML when', () => {
  14. const processorKeep = (line) => line
  15. it('fed with regular HTML with newlines', () => {
  16. const inputOutput = '1<br/>2<p class="lol">3 4</p> 5 \n 6 <p > 7 <br> 8 </p> <br>\n<br/>'
  17. const result = convertHtmlToLines(inputOutput)
  18. const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
  19. expect(comparableResult).to.eql(inputOutput)
  20. })
  21. it('fed with possibly broken HTML with invalid tags/composition', () => {
  22. const inputOutput = '<feeee dwdwddddddw> <i>ayy<b>lm</i>ao</b> </section>'
  23. const result = convertHtmlToLines(inputOutput)
  24. const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
  25. expect(comparableResult).to.eql(inputOutput)
  26. })
  27. it('fed with very broken HTML with broken composition', () => {
  28. const inputOutput = '</p> lmao what </div> whats going on <div> wha <p>'
  29. const result = convertHtmlToLines(inputOutput)
  30. const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
  31. expect(comparableResult).to.eql(inputOutput)
  32. })
  33. it('fed with sorta valid HTML but tags aren\'t closed', () => {
  34. const inputOutput = 'just leaving a <div> hanging'
  35. const result = convertHtmlToLines(inputOutput)
  36. const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
  37. expect(comparableResult).to.eql(inputOutput)
  38. })
  39. it('fed with not really HTML at this point... tags that aren\'t finished', () => {
  40. const inputOutput = 'do you expect me to finish this <div class='
  41. const result = convertHtmlToLines(inputOutput)
  42. const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
  43. expect(comparableResult).to.eql(inputOutput)
  44. })
  45. it('fed with dubiously valid HTML (p within p and also div inside p)', () => {
  46. const inputOutput = 'look ma <p> p \nwithin <p> p! </p> and a <br/><div>div!</div></p>'
  47. const result = convertHtmlToLines(inputOutput)
  48. const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
  49. expect(comparableResult).to.eql(inputOutput)
  50. })
  51. it('fed with maybe valid HTML? self-closing divs and ps', () => {
  52. const inputOutput = 'a <div class="what"/> what now <p aria-label="wtf"/> ?'
  53. const result = convertHtmlToLines(inputOutput)
  54. const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
  55. expect(comparableResult).to.eql(inputOutput)
  56. })
  57. it('fed with valid XHTML containing a CDATA', () => {
  58. const inputOutput = 'Yes, it is me, <![CDATA[DIO]]>'
  59. const result = convertHtmlToLines(inputOutput)
  60. const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
  61. expect(comparableResult).to.eql(inputOutput)
  62. })
  63. it('fed with some recognized but not handled elements', () => {
  64. const inputOutput = 'testing images\n\n<img src="benis.png">'
  65. const result = convertHtmlToLines(inputOutput)
  66. const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
  67. expect(comparableResult).to.eql(inputOutput)
  68. })
  69. })
  70. describe('with processor that replaces lines with word "_" should match expected line when', () => {
  71. const processorReplace = (line) => '_'
  72. it('fed with regular HTML with newlines', () => {
  73. const input = '1<br/>2<p class="lol">3 4</p> 5 \n 6 <p > 7 <br> 8 </p> <br>\n<br/>'
  74. const output = '_<br/>_<p class="lol">_</p>_\n_<p >_<br>_</p> <br>\n<br/>'
  75. const result = convertHtmlToLines(input)
  76. const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
  77. expect(comparableResult).to.eql(output)
  78. })
  79. it('fed with possibly broken HTML with invalid tags/composition', () => {
  80. const input = '<feeee dwdwddddddw> <i>ayy<b>lm</i>ao</b> </section>'
  81. const output = '_'
  82. const result = convertHtmlToLines(input)
  83. const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
  84. expect(comparableResult).to.eql(output)
  85. })
  86. it('fed with very broken HTML with broken composition', () => {
  87. const input = '</p> lmao what </div> whats going on <div> wha <p>'
  88. const output = '_<div>_<p>'
  89. const result = convertHtmlToLines(input)
  90. const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
  91. expect(comparableResult).to.eql(output)
  92. })
  93. it('fed with sorta valid HTML but tags aren\'t closed', () => {
  94. const input = 'just leaving a <div> hanging'
  95. const output = '_<div>_'
  96. const result = convertHtmlToLines(input)
  97. const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
  98. expect(comparableResult).to.eql(output)
  99. })
  100. it('fed with not really HTML at this point... tags that aren\'t finished', () => {
  101. const input = 'do you expect me to finish this <div class='
  102. const output = '_'
  103. const result = convertHtmlToLines(input)
  104. const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
  105. expect(comparableResult).to.eql(output)
  106. })
  107. it('fed with dubiously valid HTML (p within p and also div inside p)', () => {
  108. const input = 'look ma <p> p \nwithin <p> p! </p> and a <br/><div>div!</div></p>'
  109. const output = '_<p>_\n_<p>_</p>_<br/><div>_</div></p>'
  110. const result = convertHtmlToLines(input)
  111. const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
  112. expect(comparableResult).to.eql(output)
  113. })
  114. it('fed with maybe valid HTML? (XHTML) self-closing divs and ps', () => {
  115. const input = 'a <div class="what"/> what now <p aria-label="wtf"/> ?'
  116. const output = '_<div class="what"/>_<p aria-label="wtf"/>_'
  117. const result = convertHtmlToLines(input)
  118. const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
  119. expect(comparableResult).to.eql(output)
  120. })
  121. it('fed with valid XHTML containing a CDATA', () => {
  122. const input = 'Yes, it is me, <![CDATA[DIO]]>'
  123. const output = '_'
  124. const result = convertHtmlToLines(input)
  125. const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
  126. expect(comparableResult).to.eql(output)
  127. })
  128. it('Testing handling ignored blocks', () => {
  129. const input = `
  130. <pre><code>&gt; rei = &quot;0&quot;
  131. &#39;0&#39;
  132. &gt; rei == 0
  133. true
  134. &gt; rei == null
  135. false</code></pre><blockquote>That, christian-like JS diagram but it’s evangelion instead.</blockquote>
  136. `
  137. const result = convertHtmlToLines(input)
  138. const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
  139. expect(comparableResult).to.eql(input)
  140. })
  141. it('Testing handling ignored blocks 2', () => {
  142. const input = `
  143. <blockquote>An SSL error has happened.</blockquote><p>Shakespeare</p>
  144. `
  145. const output = `
  146. <blockquote>An SSL error has happened.</blockquote><p>_</p>
  147. `
  148. const result = convertHtmlToLines(input)
  149. const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
  150. expect(comparableResult).to.eql(output)
  151. })
  152. })
  153. })