2021-06-11 01:52:01 +10:00
|
|
|
import { convertHtmlToTree, processTextForEmoji, getAttrs } from 'src/services/html_converter/html_tree_converter.service.js'
|
2021-06-07 10:14:48 +10:00
|
|
|
|
|
|
|
describe('MiniHtmlConverter', () => {
|
2021-06-11 01:52:01 +10:00
|
|
|
describe('convertHtmlToTree', () => {
|
2021-06-07 10:14:48 +10:00
|
|
|
it('converts html into a tree structure', () => {
|
2021-06-07 23:15:32 +10:00
|
|
|
const input = '1 <p>2</p> <b>3<img src="a">4</b>5'
|
2021-06-11 01:52:01 +10:00
|
|
|
expect(convertHtmlToTree(input)).to.eql([
|
2021-06-07 10:14:48 +10:00
|
|
|
'1 ',
|
|
|
|
[
|
|
|
|
'<p>',
|
|
|
|
['2'],
|
|
|
|
'</p>'
|
|
|
|
],
|
|
|
|
' ',
|
|
|
|
[
|
|
|
|
'<b>',
|
|
|
|
[
|
|
|
|
'3',
|
|
|
|
['<img src="a">'],
|
|
|
|
'4'
|
|
|
|
],
|
|
|
|
'</b>'
|
|
|
|
],
|
|
|
|
'5'
|
|
|
|
])
|
|
|
|
})
|
|
|
|
it('converts html to tree while preserving tag formatting', () => {
|
2021-06-07 23:15:32 +10:00
|
|
|
const input = '1 <p >2</p><b >3<img src="a">4</b>5'
|
2021-06-11 01:52:01 +10:00
|
|
|
expect(convertHtmlToTree(input)).to.eql([
|
2021-06-07 10:14:48 +10:00
|
|
|
'1 ',
|
|
|
|
[
|
|
|
|
'<p >',
|
|
|
|
['2'],
|
|
|
|
'</p>'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<b >',
|
|
|
|
[
|
|
|
|
'3',
|
|
|
|
['<img src="a">'],
|
|
|
|
'4'
|
|
|
|
],
|
|
|
|
'</b>'
|
|
|
|
],
|
|
|
|
'5'
|
|
|
|
])
|
|
|
|
})
|
|
|
|
it('converts semi-broken html', () => {
|
2021-06-07 23:15:32 +10:00
|
|
|
const input = '1 <br> 2 <p> 42'
|
2021-06-11 01:52:01 +10:00
|
|
|
expect(convertHtmlToTree(input)).to.eql([
|
2021-06-07 10:14:48 +10:00
|
|
|
'1 ',
|
|
|
|
['<br>'],
|
|
|
|
' 2 ',
|
|
|
|
[
|
|
|
|
'<p>',
|
|
|
|
[' 42']
|
|
|
|
]
|
|
|
|
])
|
|
|
|
})
|
2021-06-07 19:38:27 +10:00
|
|
|
it('realistic case 1', () => {
|
2021-06-07 23:15:32 +10:00
|
|
|
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>'
|
2021-06-11 01:52:01 +10:00
|
|
|
expect(convertHtmlToTree(input)).to.eql([
|
2021-06-07 10:14:48 +10:00
|
|
|
[
|
|
|
|
'<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>'
|
|
|
|
]
|
|
|
|
])
|
|
|
|
})
|
2021-06-07 19:38:27 +10:00
|
|
|
it('realistic case 2', () => {
|
|
|
|
const inputOutput = 'Country improv: give me a city<br/>Audience: Memphis<br/>Improv troupe: come on, a better one<br/>Audience: el paso'
|
2021-06-11 01:52:01 +10:00
|
|
|
expect(convertHtmlToTree(inputOutput)).to.eql([
|
2021-06-07 19:38:27 +10:00
|
|
|
'Country improv: give me a city',
|
|
|
|
[
|
|
|
|
'<br/>'
|
|
|
|
],
|
|
|
|
'Audience: Memphis',
|
|
|
|
[
|
|
|
|
'<br/>'
|
|
|
|
],
|
|
|
|
'Improv troupe: come on, a better one',
|
|
|
|
[
|
|
|
|
'<br/>'
|
|
|
|
],
|
|
|
|
'Audience: el paso'
|
|
|
|
])
|
|
|
|
})
|
2021-06-07 10:14:48 +10:00
|
|
|
})
|
2021-06-07 23:15:32 +10:00
|
|
|
|
2021-06-07 10:14:48 +10:00
|
|
|
describe('processTextForEmoji', () => {
|
|
|
|
it('processes all emoji in text', () => {
|
2021-06-07 23:15:32 +10:00
|
|
|
const input = 'Hello from finland! :lol: We have best water! :lmao:'
|
2021-06-07 10:14:48 +10:00
|
|
|
const emojis = [
|
|
|
|
{ shortcode: 'lol', src: 'LOL' },
|
|
|
|
{ shortcode: 'lmao', src: 'LMAO' }
|
|
|
|
]
|
|
|
|
const processor = ({ shortcode, src }) => ({ shortcode, src })
|
2021-06-07 23:15:32 +10:00
|
|
|
expect(processTextForEmoji(input, emojis, processor)).to.eql([
|
2021-06-07 10:14:48 +10:00
|
|
|
'Hello from finland! ',
|
|
|
|
{ shortcode: 'lol', src: 'LOL' },
|
|
|
|
' We have best water! ',
|
|
|
|
{ shortcode: 'lmao', src: 'LMAO' }
|
|
|
|
])
|
|
|
|
})
|
2021-06-07 19:38:27 +10:00
|
|
|
it('leaves text as is', () => {
|
2021-06-07 23:15:32 +10:00
|
|
|
const input = 'Number one: that\'s terror'
|
2021-06-07 19:38:27 +10:00
|
|
|
const emojis = []
|
|
|
|
const processor = ({ shortcode, src }) => ({ shortcode, src })
|
2021-06-07 23:15:32 +10:00
|
|
|
expect(processTextForEmoji(input, emojis, processor)).to.eql([
|
2021-06-07 19:38:27 +10:00
|
|
|
'Number one: that\'s terror'
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
2021-06-07 23:15:32 +10:00
|
|
|
|
|
|
|
describe('getAttrs', () => {
|
|
|
|
it('extracts arguments from tag', () => {
|
2021-06-07 23:31:39 +10:00
|
|
|
const input = '<img src="boop" cool ebin=\'true\'>'
|
2021-06-07 23:15:32 +10:00
|
|
|
const output = { src: 'boop', cool: true, ebin: 'true' }
|
|
|
|
|
|
|
|
expect(getAttrs(input)).to.eql(output)
|
|
|
|
})
|
2021-06-07 10:14:48 +10:00
|
|
|
})
|
|
|
|
})
|