logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

html_tree_converter.spec.js (3484B)


  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. })