2021-06-11 01:52:01 +10:00
|
|
|
|
import { convertHtmlToLines } from 'src/services/html_converter/html_line_converter.service.js'
|
|
|
|
|
|
2021-06-13 20:29:26 +10:00
|
|
|
|
const greentextHandle = new Set(['p', 'div'])
|
|
|
|
|
const mapOnlyText = (processor) => (input) => {
|
|
|
|
|
if (input.text && input.level.every(l => greentextHandle.has(l))) {
|
|
|
|
|
return processor(input.text)
|
|
|
|
|
} else if (input.text) {
|
|
|
|
|
return input.text
|
|
|
|
|
} else {
|
|
|
|
|
return input
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 17:30:08 +10:00
|
|
|
|
describe('html_line_converter', () => {
|
2019-11-15 07:40:20 +11:00
|
|
|
|
describe('with processor that keeps original line should not make any changes to HTML when', () => {
|
|
|
|
|
const processorKeep = (line) => line
|
|
|
|
|
it('fed with regular HTML with newlines', () => {
|
|
|
|
|
const inputOutput = '1<br/>2<p class="lol">3 4</p> 5 \n 6 <p > 7 <br> 8 </p> <br>\n<br/>'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(inputOutput)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(inputOutput)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with possibly broken HTML with invalid tags/composition', () => {
|
|
|
|
|
const inputOutput = '<feeee dwdwddddddw> <i>ayy<b>lm</i>ao</b> </section>'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(inputOutput)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(inputOutput)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with very broken HTML with broken composition', () => {
|
|
|
|
|
const inputOutput = '</p> lmao what </div> whats going on <div> wha <p>'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(inputOutput)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(inputOutput)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with sorta valid HTML but tags aren\'t closed', () => {
|
|
|
|
|
const inputOutput = 'just leaving a <div> hanging'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(inputOutput)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(inputOutput)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with not really HTML at this point... tags that aren\'t finished', () => {
|
|
|
|
|
const inputOutput = 'do you expect me to finish this <div class='
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(inputOutput)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(inputOutput)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with dubiously valid HTML (p within p and also div inside p)', () => {
|
|
|
|
|
const inputOutput = 'look ma <p> p \nwithin <p> p! </p> and a <br/><div>div!</div></p>'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(inputOutput)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(inputOutput)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with maybe valid HTML? self-closing divs and ps', () => {
|
|
|
|
|
const inputOutput = 'a <div class="what"/> what now <p aria-label="wtf"/> ?'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(inputOutput)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(inputOutput)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with valid XHTML containing a CDATA', () => {
|
|
|
|
|
const inputOutput = 'Yes, it is me, <![CDATA[DIO]]>'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(inputOutput)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(inputOutput)
|
2021-06-19 04:29:47 +10:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with some recognized but not handled elements', () => {
|
|
|
|
|
const inputOutput = 'testing images\n\n<img src="benis.png">'
|
|
|
|
|
const result = convertHtmlToLines(inputOutput)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorKeep)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(inputOutput)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
describe('with processor that replaces lines with word "_" should match expected line when', () => {
|
|
|
|
|
const processorReplace = (line) => '_'
|
|
|
|
|
it('fed with regular HTML with newlines', () => {
|
|
|
|
|
const input = '1<br/>2<p class="lol">3 4</p> 5 \n 6 <p > 7 <br> 8 </p> <br>\n<br/>'
|
|
|
|
|
const output = '_<br/>_<p class="lol">_</p>_\n_<p >_<br>_</p> <br>\n<br/>'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(input)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(output)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with possibly broken HTML with invalid tags/composition', () => {
|
|
|
|
|
const input = '<feeee dwdwddddddw> <i>ayy<b>lm</i>ao</b> </section>'
|
|
|
|
|
const output = '_'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(input)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(output)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with very broken HTML with broken composition', () => {
|
|
|
|
|
const input = '</p> lmao what </div> whats going on <div> wha <p>'
|
2021-06-13 20:29:26 +10:00
|
|
|
|
const output = '_<div>_<p>'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(input)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(output)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with sorta valid HTML but tags aren\'t closed', () => {
|
|
|
|
|
const input = 'just leaving a <div> hanging'
|
|
|
|
|
const output = '_<div>_'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(input)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(output)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with not really HTML at this point... tags that aren\'t finished', () => {
|
|
|
|
|
const input = 'do you expect me to finish this <div class='
|
|
|
|
|
const output = '_'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(input)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(output)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with dubiously valid HTML (p within p and also div inside p)', () => {
|
|
|
|
|
const input = 'look ma <p> p \nwithin <p> p! </p> and a <br/><div>div!</div></p>'
|
|
|
|
|
const output = '_<p>_\n_<p>_</p>_<br/><div>_</div></p>'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(input)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(output)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
2021-06-13 20:29:26 +10:00
|
|
|
|
it('fed with maybe valid HTML? (XHTML) self-closing divs and ps', () => {
|
2019-11-15 07:40:20 +11:00
|
|
|
|
const input = 'a <div class="what"/> what now <p aria-label="wtf"/> ?'
|
|
|
|
|
const output = '_<div class="what"/>_<p aria-label="wtf"/>_'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(input)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(output)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fed with valid XHTML containing a CDATA', () => {
|
|
|
|
|
const input = 'Yes, it is me, <![CDATA[DIO]]>'
|
|
|
|
|
const output = '_'
|
2021-06-11 01:52:01 +10:00
|
|
|
|
const result = convertHtmlToLines(input)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(output)
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
2021-06-11 18:50:05 +10:00
|
|
|
|
|
|
|
|
|
it('Testing handling ignored blocks', () => {
|
|
|
|
|
const input = `
|
|
|
|
|
<pre><code>> rei = "0"
|
|
|
|
|
'0'
|
|
|
|
|
> rei == 0
|
|
|
|
|
true
|
|
|
|
|
> rei == null
|
|
|
|
|
false</code></pre><blockquote>That, christian-like JS diagram but it’s evangelion instead.</blockquote>
|
|
|
|
|
`
|
|
|
|
|
const result = convertHtmlToLines(input)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(input)
|
|
|
|
|
})
|
|
|
|
|
it('Testing handling ignored blocks 2', () => {
|
|
|
|
|
const input = `
|
|
|
|
|
<blockquote>An SSL error has happened.</blockquote><p>Shakespeare</p>
|
|
|
|
|
`
|
|
|
|
|
const output = `
|
|
|
|
|
<blockquote>An SSL error has happened.</blockquote><p>_</p>
|
|
|
|
|
`
|
|
|
|
|
const result = convertHtmlToLines(input)
|
|
|
|
|
const comparableResult = result.map(mapOnlyText(processorReplace)).join('')
|
|
|
|
|
expect(comparableResult).to.eql(output)
|
|
|
|
|
})
|
2019-11-15 07:40:20 +11:00
|
|
|
|
})
|
|
|
|
|
})
|