/** * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ import { modelElementToPlainText } from '../src/utils'; import Element from '@ckeditor/ckeditor5-engine/src/model/element'; import Text from '@ckeditor/ckeditor5-engine/src/model/text'; import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting'; import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting'; import LinkEditing from '@ckeditor/ckeditor5-link/src/linkediting'; import ListEditing from '@ckeditor/ckeditor5-list/src/listediting'; import TableEditing from '@ckeditor/ckeditor5-table/src/tableediting'; import Enter from '@ckeditor/ckeditor5-enter/src/enter'; import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter'; describe( 'utils', () => { describe( 'modelElementToPlainText()', () => { it( 'should extract only plain text', () => { const text1 = new Text( 'Foo' ); const text2 = new Text( 'Bar', { bold: true } ); const text3 = new Text( 'Baz', { bold: true, underline: true } ); const innerElement1 = new Element( 'paragraph', null, [ text1 ] ); const innerElement2 = new Element( 'paragraph', null, [ text2, text3 ] ); const mainElement = new Element( 'container', null, [ innerElement1, innerElement2 ] ); expect( modelElementToPlainText( mainElement ) ).to.equal( 'Foo\nBarBaz' ); } ); describe( 'complex structures', () => { let editor, model; beforeEach( () => { return VirtualTestEditor .create( { plugins: [ Enter, ShiftEnter, Paragraph, BoldEditing, LinkEditing, BlockQuoteEditing, ListEditing, TableEditing ] } ) .then( newEditor => { editor = newEditor; model = editor.model; } ); } ); afterEach( () => { editor.destroy(); } ); it( 'extracts plain text from blockqoutes', () => { setModelData( model, '
' + 'Hello' + 'world' + 'foo' + 'bar' + '
' ); expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal( 'Hello\nworld\nfoo\nbar' ); } ); it( 'extracts plain text from tables', () => { setModelData( model, '' + '' + '' + 'Foo' + '' + '' + 'Bar' + '' + '' + '' + '' + 'Baz' + '' + '' + 'Foo' + '' + '' + '
' ); expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal( 'Foo\nBar\nBaz\nFoo' ); } ); it( 'extracts plain text with soft break', () => { setModelData( model, 'Foobar' ); expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal( 'Foo\nbar' ); } ); it( 'extracts plain text with inline styles', () => { setModelData( model, 'F<$text bold="true">oo<$text href="url">Bar' ); expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal( 'FooBar' ); } ); it( 'extracts plain text from mixed structure', () => { setModelData( model, '' + '<$text bold="true">111<$text href="url" bold="true">222333' + '
' + '444555' + '' + '' + '666' + '7<$text bold="true">77' + '' + '' + '888' + '999' + '' + '
' + '
' + '' + '000' + '
' + '111' + '222' + '
' + '
' + '
' ); expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal( '111222333\n444\n555\n666\n777\n888\n999\n000\n111\n222' ); } ); } ); } ); } );