/** * @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, '
' + '' ); expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal( 'Hello\nworld\nfoo\nbar' ); } ); it( 'extracts plain text from tables', () => { setModelData( model, 'Hello ' + 'world ' + 'foo ' + 'bar ' + '
' + '444 ' + '555 ' + '
' + '' + ' ' + '' + ' 666 ' + ' 7<$text bold="true">7$text>7 ' + ' ' + '' + ' 888 ' + ' 999