/** * @copyright 2017 Christoph Wurst * * @author 2017 Christoph Wurst * * @license AGPL-3.0-or-later * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * */ import { detect, html, plain, toPlain } from '../../../util/text' describe('text', () => { describe('toPlain', () => { it('preserves breaks', () => { const source = html('line1
line2') const expected = plain('line1\nline2') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('removes leading line breaks', () => { const source = html('


hello world') const expected = plain('hello world') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('removes trailing line breaks', () => { const source = html('hello world


') const expected = plain('hello world') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('removes trailing spaces of each line', () => { const source = html('line1
line2
line3') const expected = plain('line1\nline2\nline3') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('breaks on divs', () => { const source = html('
one
two
') const actual = toPlain(source) expect(actual).toEqual(plain('one\ntwo')) }) it('merges spaces at the beginning of a line', () => { const source = html('
line1
') const expected = plain(' line1') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('produces a line break for each ending div element', () => { const source = html('
line1
line3
') const expected = plain('line1\n\nline3') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('converts blocks to text', () => { const source = html('
hello
') const expected = plain('hello') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('converts paragraph to text', () => { const source = html('

hello

') const expected = plain('hello') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('produces a single line break between paragraphs', () => { const source = html('

hello

world

') const expected = plain('hello\nworld') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('produces a single line break between a div and a paragraph', () => { const source = html('
hello

world

') const expected = plain('hello\nworld') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('produces a single line break after each block element', () => { const selectors = ['p', 'div', 'header', 'footer', 'form', 'article', 'aside', 'main', 'nav', 'section'] const source = html( selectors .map(tag => `<${tag}>foobar`) .join('') ) const expected = plain(selectors.map(tag => 'foobar').join('\n')) const actual = toPlain(source) expect(actual).toEqual(expected) }) it('produces exactly one line break for each closing block element', () => { const selectors = ['p', 'div', 'header', 'footer', 'form', 'article', 'aside', 'main', 'nav', 'section'] const source = html( selectors .map(tag => `<${tag}><${tag}>foobar`) .join('') ) const expected = plain(selectors.map(tag => 'foobar').join('\n\n')) const actual = toPlain(source) expect(actual).toEqual(expected) }) it('converts lists to text', () => { const source = html('
  • one
  • two
  • three
') const expected = plain(' * one\n * two\n * three') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('converts deeply nested elements to text', () => { const source = html( '' + '

Hello!

this is some random text

' + '' ) const expected = plain('Hello!\nthis is some random text') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('does not leak internal redirection URLs', () => { const source = html('domain.tld') const expected = plain('domain.tld') const actual = toPlain(source) expect(actual).toEqual(expected) }) it('preserves quotes', () => { const source = html( '
yes.

Am Montag, den 21.10.2019, 16:51 +0200 schrieb Christoph Wurst:
ok cool

Am Montag, den 21.10.2019, 16:51 +0200 schrieb Christoph Wurst:
Hello

this is some text

yes

cheers


' ) const expected = plain(`> yes. > > Am Montag, den 21.10.2019, 16:51 +0200 schrieb Christoph Wurst: > > ok cool > > > > Am Montag, den 21.10.2019, 16:51 +0200 schrieb Christoph Wurst: > > > Hello > > > > > > this is some text > > > > > > yes > > > > > > cheers > > > > > > > >`) const actual = toPlain(source) expect(actual).toEqual(expected) }) }) describe('detect', () => { it('detects plain text', () => { const text = 'hello world\nsecond line' const detected = detect(text) expect(detected).toEqual(plain(text)) }) it('detects html', () => { const text = '

hello world

second line

' const detected = detect(text) expect(detected).toEqual(html(text)) }) }) })