html_tree_converter.spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { convertHtmlToTree } from 'src/services/html_converter/html_tree_converter.service.js'
  2. describe('html_tree_converter', () => {
  3. describe('convertHtmlToTree', () => {
  4. it('converts html into a tree structure', () => {
  5. const input = '1 <p>2</p> <b>3<img src="a">4</b>5'
  6. expect(convertHtmlToTree(input)).to.eql([
  7. '1 ',
  8. [
  9. '<p>',
  10. ['2'],
  11. '</p>'
  12. ],
  13. ' ',
  14. [
  15. '<b>',
  16. [
  17. '3',
  18. ['<img src="a">'],
  19. '4'
  20. ],
  21. '</b>'
  22. ],
  23. '5'
  24. ])
  25. })
  26. it('converts html to tree while preserving tag formatting', () => {
  27. const input = '1 <p >2</p><b >3<img src="a">4</b>5'
  28. expect(convertHtmlToTree(input)).to.eql([
  29. '1 ',
  30. [
  31. '<p >',
  32. ['2'],
  33. '</p>'
  34. ],
  35. [
  36. '<b >',
  37. [
  38. '3',
  39. ['<img src="a">'],
  40. '4'
  41. ],
  42. '</b>'
  43. ],
  44. '5'
  45. ])
  46. })
  47. it('converts semi-broken html', () => {
  48. const input = '1 <br> 2 <p> 42'
  49. expect(convertHtmlToTree(input)).to.eql([
  50. '1 ',
  51. ['<br>'],
  52. ' 2 ',
  53. [
  54. '<p>',
  55. [' 42']
  56. ]
  57. ])
  58. })
  59. it('realistic case 1', () => {
  60. const input = '<p><span class="h-card"><a class="u-url mention" data-user="9wRC6T2ZZiKWJ0vUi8" href="https://cawfee.club/users/benis" rel="ugc">@<span>benis</span></a></span> <span class="h-card"><a class="u-url mention" data-user="194" href="https://shigusegubu.club/users/hj" rel="ugc">@<span>hj</span></a></span> nice</p>'
  61. expect(convertHtmlToTree(input)).to.eql([
  62. [
  63. '<p>',
  64. [
  65. [
  66. '<span class="h-card">',
  67. [
  68. [
  69. '<a class="u-url mention" data-user="9wRC6T2ZZiKWJ0vUi8" href="https://cawfee.club/users/benis" rel="ugc">',
  70. [
  71. '@',
  72. [
  73. '<span>',
  74. [
  75. 'benis'
  76. ],
  77. '</span>'
  78. ]
  79. ],
  80. '</a>'
  81. ]
  82. ],
  83. '</span>'
  84. ],
  85. ' ',
  86. [
  87. '<span class="h-card">',
  88. [
  89. [
  90. '<a class="u-url mention" data-user="194" href="https://shigusegubu.club/users/hj" rel="ugc">',
  91. [
  92. '@',
  93. [
  94. '<span>',
  95. [
  96. 'hj'
  97. ],
  98. '</span>'
  99. ]
  100. ],
  101. '</a>'
  102. ]
  103. ],
  104. '</span>'
  105. ],
  106. ' nice'
  107. ],
  108. '</p>'
  109. ]
  110. ])
  111. })
  112. it('realistic case 2', () => {
  113. const inputOutput = 'Country improv: give me a city<br/>Audience: Memphis<br/>Improv troupe: come on, a better one<br/>Audience: el paso'
  114. expect(convertHtmlToTree(inputOutput)).to.eql([
  115. 'Country improv: give me a city',
  116. [
  117. '<br/>'
  118. ],
  119. 'Audience: Memphis',
  120. [
  121. '<br/>'
  122. ],
  123. 'Improv troupe: come on, a better one',
  124. [
  125. '<br/>'
  126. ],
  127. 'Audience: el paso'
  128. ])
  129. })
  130. })
  131. })