/** * @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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter'; import { getData } from '../../../src/dev-utils/model'; // NOTE: // dev utils' setData() loses white spaces so don't use it for tests here!!! // https://github.com/ckeditor/ckeditor5-engine/issues/1428 describe( 'DomConverter – whitespace handling – integration', () => { let editor; // See https://github.com/ckeditor/ckeditor5-engine/issues/822. describe( 'normalizing whitespaces around block boundaries (#822)', () => { beforeEach( () => { return VirtualTestEditor .create( { plugins: [ Paragraph ] } ) .then( newEditor => { editor = newEditor; editor.model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } ); editor.conversion.attributeToElement( { model: 'bold', view: 'b' } ); } ); } ); afterEach( () => { return editor.destroy(); } ); it( 'new line at the end of the content is ignored', () => { editor.setData( '

foo

\n' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo' ); expect( editor.getData() ).to.equal( '

foo

' ); } ); it( 'whitespaces at the end of the content are ignored', () => { editor.setData( '

foo

\n\r\n \t' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo' ); expect( editor.getData() ).to.equal( '

foo

' ); } ); it( 'nbsp at the end of the content is not ignored', () => { editor.setData( '

foo 

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo ' ); expect( editor.getData() ).to.equal( '

foo 

' ); } ); it( 'new line at the beginning of the content is ignored', () => { editor.setData( '\n

foo

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo' ); expect( editor.getData() ).to.equal( '

foo

' ); } ); it( 'whitespaces at the beginning of the content are ignored', () => { editor.setData( '\n\n \t

foo

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo' ); expect( editor.getData() ).to.equal( '

foo

' ); } ); it( 'nbsp at the beginning of the content is not ignored', () => { editor.setData( '

 foo

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( ' foo' ); expect( editor.getData() ).to.equal( '

 foo

' ); } ); it( 'new line between blocks is ignored', () => { editor.setData( '

foo

\n

bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foobar' ); expect( editor.getData() ).to.equal( '

foo

bar

' ); } ); it( 'whitespaces between blocks are ignored', () => { editor.setData( '

foo

\n\n \t

bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foobar' ); expect( editor.getData() ).to.equal( '

foo

bar

' ); } ); // Controversial result. See https://github.com/ckeditor/ckeditor5-engine/issues/987. it( 'nbsp between blocks is not ignored (between paragraphs)', () => { editor.setData( '

foo

 

bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo bar' ); expect( editor.getData() ).to.equal( '

foo

 

bar

' ); } ); it( 'nbsp between blocks is not ignored (different blocks)', () => { editor.model.schema.register( 'block', { inheritAllFrom: '$block' } ); editor.conversion.elementToElement( { model: 'block', view: 'block' } ); editor.setData( 'foo 

bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo' + ' ' + 'bar' ); expect( editor.getData() ) .to.equal( 'foo' + '

 

' + '

bar

' ); } ); it( 'new lines inside blocks are ignored', () => { editor.setData( '

\nfoo\n

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo' ); expect( editor.getData() ).to.equal( '

foo

' ); } ); it( 'whitespaces inside blocks are ignored', () => { editor.setData( '

\n\n \tfoo\n\n \t

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo' ); expect( editor.getData() ).to.equal( '

foo

' ); } ); it( 'nbsp inside blocks are not ignored', () => { editor.setData( '

 foo 

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( ' foo ' ); expect( editor.getData() ).to.equal( '

 foo 

' ); } ); it( 'single nbsp inside blocks are ignored', () => { editor.setData( '

 

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( '' ); expect( editor.getData() ).to.equal( '' ); // trimmed } ); it( 'all whitespaces together are ignored', () => { editor.setData( '\n

foo\n\r\n \t

\n

bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foobar' ); expect( editor.getData() ).to.equal( '

foo

bar

' ); } ); it( 'nbsp between inline elements is not ignored', () => { editor.setData( '

foo bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( '<$text bold="true">foo\u00A0<$text bold="true">bar' ); expect( editor.getData() ).to.equal( '

foo bar

' ); } ); it( 'nbsp before inline element is not ignored', () => { editor.setData( '

 bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( ' <$text bold="true">bar' ); expect( editor.getData() ).to.equal( '

 bar

' ); } ); it( 'nbsp after inline element is not ignored', () => { editor.setData( '

bar 

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( '<$text bold="true">bar ' ); expect( editor.getData() ).to.equal( '

bar 

' ); } ); } ); // https://github.com/ckeditor/ckeditor5/issues/1024 describe( 'whitespaces around
s', () => { beforeEach( () => { return VirtualTestEditor .create( { plugins: [ Paragraph, ShiftEnter ] } ) .then( newEditor => { editor = newEditor; } ); } ); afterEach( () => { return editor.destroy(); } ); it( 'single spaces around
#1', () => { editor.setData( '

foo 
 bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo\u00A0 bar' ); expect( editor.getData() ).to.equal( '

foo 
 bar

' ); } ); it( 'single spaces around
#2', () => { editor.setData( '

foo 
bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo\u00A0bar' ); expect( editor.getData() ).to.equal( '

foo 
bar

' ); } ); it( 'two spaces before a
', () => { editor.setData( '

foo  
bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo bar' ); expect( editor.getData() ).to.equal( '

foo  
bar

' ); } ); it( 'two nbsps before a
', () => { editor.setData( '

foo  
bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo\u00a0 bar' ); expect( editor.getData() ).to.equal( '

foo  
bar

' ); } ); it( 'single space after a
', () => { editor.setData( '

foo
 bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo bar' ); expect( editor.getData() ).to.equal( '

foo
 bar

' ); } ); it( 'single space after a
(normalization)', () => { editor.setData( '

foo
bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foobar' ); expect( editor.getData() ).to.equal( '

foo
bar

' ); } ); it( 'two spaces after a
', () => { editor.setData( '

foo
  bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo bar' ); expect( editor.getData() ).to.equal( '

foo
  bar

' ); } ); it( 'two spaces after a
(normalization)', () => { editor.setData( '

foo
 bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo bar' ); expect( editor.getData() ).to.equal( '

foo
 bar

' ); } ); it( 'two spaces after a
(normalization to a model nbsp)', () => { editor.setData( '

foo
  bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo \u00a0bar' ); expect( editor.getData() ).to.equal( '

foo
  bar

' ); } ); // https://github.com/ckeditor/ckeditor5-engine/issues/1429 // it( 'space between
s', () => { // editor.setData( '

foo
 
bar

' ); // expect( getData( editor.model, { withoutSelection: true } ) ) // .to.equal( 'foo bar' ); // expect( editor.getData() ).to.equal( '

foo
 
bar

' ); // } ); it( 'space between
s (normalization)', () => { editor.setData( '

foo

bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foobar' ); expect( editor.getData() ).to.equal( '

foo

bar

' ); } ); it( 'two spaces between
s', () => { editor.setData( '

foo
  
bar

' ); expect( getData( editor.model, { withoutSelection: true } ) ) .to.equal( 'foo bar' ); expect( editor.getData() ).to.equal( '

foo
  
bar

' ); } ); } ); } );