From 85ce98bb46befa9b2276a7d77800a860963ddd9f Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Mon, 27 Jul 2020 09:36:41 +0200 Subject: [PATCH 1/2] Selection will not inherit attribues if spotted an inline element while searching for a node from which attributes could be copied. --- .../src/model/documentselection.js | 4 +-- .../tests/model/documentselection.js | 26 +++++++++++++++++++ packages/ckeditor5-enter/package.json | 1 + .../tests/shiftenter-integration.js | 25 +++++++++++++++--- 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/packages/ckeditor5-engine/src/model/documentselection.js b/packages/ckeditor5-engine/src/model/documentselection.js index 90902ed6a91..d0a258d9cb7 100644 --- a/packages/ckeditor5-engine/src/model/documentselection.js +++ b/packages/ckeditor5-engine/src/model/documentselection.js @@ -1095,7 +1095,7 @@ class LiveSelection extends Selection { if ( !this.isGravityOverridden && !attrs ) { let node = nodeBefore; - while ( node && !attrs ) { + while ( node && !schema.isInline( node ) && !attrs ) { node = node.previousSibling; attrs = getAttrsIfCharacter( node ); } @@ -1105,7 +1105,7 @@ class LiveSelection extends Selection { if ( !attrs ) { let node = nodeAfter; - while ( node && !attrs ) { + while ( node && !schema.isInline( node ) && !attrs ) { node = node.nextSibling; attrs = getAttrsIfCharacter( node ); } diff --git a/packages/ckeditor5-engine/tests/model/documentselection.js b/packages/ckeditor5-engine/tests/model/documentselection.js index c7c89d348e1..b21608c7d11 100644 --- a/packages/ckeditor5-engine/tests/model/documentselection.js +++ b/packages/ckeditor5-engine/tests/model/documentselection.js @@ -1218,6 +1218,32 @@ describe( 'DocumentSelection', () => { expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' ); } ); } ); + + // #7459 + describe( 'ignores inline elements while reading surrounding attributes', () => { + beforeEach( () => { + model.schema.register( 'softBreak', { + allowWhere: '$text', + isInline: true + } ); + } ); + + it( 'should not inherit attributes from a node before an inline element', () => { + setData( model, '

<$text bold="true">Caption for the image.[]

' ); + + expect( selection.hasAttribute( 'bold' ) ).to.equal( false ); + } ); + + it( 'should not inherit attributes from a node after an inline element (override gravity)', () => { + setData( model, '

[]<$text bold="true">Caption for the image.

' ); + + const overrideGravityUid = selection._overrideGravity(); + + expect( selection.hasAttribute( 'bold' ) ).to.equal( false ); + + selection._restoreGravity( overrideGravityUid ); + } ); + } ); } ); describe( '_overrideGravity()', () => { diff --git a/packages/ckeditor5-enter/package.json b/packages/ckeditor5-enter/package.json index 2691d5fcd27..d8de3350f06 100644 --- a/packages/ckeditor5-enter/package.json +++ b/packages/ckeditor5-enter/package.json @@ -18,6 +18,7 @@ "@ckeditor/ckeditor5-basic-styles": "^20.0.0", "@ckeditor/ckeditor5-editor-classic": "^20.0.0", "@ckeditor/ckeditor5-heading": "^20.0.0", + "@ckeditor/ckeditor5-link": "^20.0.0", "@ckeditor/ckeditor5-paragraph": "^20.0.0", "@ckeditor/ckeditor5-typing": "^20.0.0", "@ckeditor/ckeditor5-undo": "^20.0.0" diff --git a/packages/ckeditor5-enter/tests/shiftenter-integration.js b/packages/ckeditor5-enter/tests/shiftenter-integration.js index 3d9e6f0f998..b03be91b493 100644 --- a/packages/ckeditor5-enter/tests/shiftenter-integration.js +++ b/packages/ckeditor5-enter/tests/shiftenter-integration.js @@ -6,8 +6,10 @@ /* global document */ import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'; - import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; +import LinkEditing from '@ckeditor/ckeditor5-link/src/linkediting'; +import Delete from '@ckeditor/ckeditor5-typing/src/delete'; +import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting'; import ShiftEnter from '../src/shiftenter'; import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; @@ -24,7 +26,7 @@ describe( 'ShiftEnter integration', () => { document.body.appendChild( div ); - return ClassicEditor.create( div, { plugins: [ Paragraph, ShiftEnter ] } ) + return ClassicEditor.create( div, { plugins: [ Paragraph, ShiftEnter, LinkEditing, Delete, BoldEditing ] } ) .then( newEditor => { editor = newEditor; @@ -46,9 +48,26 @@ describe( 'ShiftEnter integration', () => { it( 'BLOCK_FILLER should be inserted after
in the paragraph', () => { setModelData( model, '[]' ); - editor.commands.get( 'shiftEnter' ).execute(); + editor.execute( 'shiftEnter' ); expect( editor.getData( { trim: 'none' } ) ).to.equal( '


 

' ); expect( editor.ui.view.editable.element.innerHTML ).to.equal( '



' ); } ); + + it( 'should not inherit text attributes before the "softBreak" element', () => { + setModelData( model, + '' + + '<$text linkHref="foo" bold="true">Bolded link' + + '' + + 'F[]' + + '' + ); + + editor.execute( 'delete' ); + + const selection = model.document.selection; + + expect( selection.hasAttribute( 'linkHref' ) ).to.equal( false ); + expect( selection.hasAttribute( 'bold' ) ).to.equal( false ); + } ); } ); From a078d2505e25b78dd19878b3ebb8aa49f39f0001 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Mon, 27 Jul 2020 09:39:35 +0200 Subject: [PATCH 2/2] Changed text node value. --- packages/ckeditor5-engine/tests/model/documentselection.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/ckeditor5-engine/tests/model/documentselection.js b/packages/ckeditor5-engine/tests/model/documentselection.js index b21608c7d11..3718eadb277 100644 --- a/packages/ckeditor5-engine/tests/model/documentselection.js +++ b/packages/ckeditor5-engine/tests/model/documentselection.js @@ -1229,13 +1229,13 @@ describe( 'DocumentSelection', () => { } ); it( 'should not inherit attributes from a node before an inline element', () => { - setData( model, '

<$text bold="true">Caption for the image.[]

' ); + setData( model, '

<$text bold="true">Foo Bar.[]

' ); expect( selection.hasAttribute( 'bold' ) ).to.equal( false ); } ); it( 'should not inherit attributes from a node after an inline element (override gravity)', () => { - setData( model, '

[]<$text bold="true">Caption for the image.

' ); + setData( model, '

[]<$text bold="true">Foo Bar.

' ); const overrideGravityUid = selection._overrideGravity();