Skip to content

Commit

Permalink
Merge pull request #7714 from ckeditor/i/7459
Browse files Browse the repository at this point in the history
Fix (engine): Selection will not inherit attributes if spotted an inline element while searching for a node from which attributes could be copied. Closes #7459.

Tests (enter): Added an integration test that covers a bug with copying attributes from a node before an inline element. See #7459.
  • Loading branch information
jodator authored Jul 27, 2020
2 parents af4d33e + a078d25 commit 1ddb955
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/ckeditor5-engine/src/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand All @@ -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 );
}
Expand Down
26 changes: 26 additions & 0 deletions packages/ckeditor5-engine/tests/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<p><$text bold="true">Foo Bar.</$text><softBreak></softBreak>[]</p>' );

expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
} );

it( 'should not inherit attributes from a node after an inline element (override gravity)', () => {
setData( model, '<p>[]<softBreak></softBreak><$text bold="true">Foo Bar.</$text></p>' );

const overrideGravityUid = selection._overrideGravity();

expect( selection.hasAttribute( 'bold' ) ).to.equal( false );

selection._restoreGravity( overrideGravityUid );
} );
} );
} );

describe( '_overrideGravity()', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/ckeditor5-enter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 22 additions & 3 deletions packages/ckeditor5-enter/tests/shiftenter-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;

Expand All @@ -46,9 +48,26 @@ describe( 'ShiftEnter integration', () => {
it( 'BLOCK_FILLER should be inserted after <br> in the paragraph', () => {
setModelData( model, '<paragraph>[]</paragraph>' );

editor.commands.get( 'shiftEnter' ).execute();
editor.execute( 'shiftEnter' );

expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p><br>&nbsp;</p>' );
expect( editor.ui.view.editable.element.innerHTML ).to.equal( '<p><br><br data-cke-filler="true"></p>' );
} );

it( 'should not inherit text attributes before the "softBreak" element', () => {
setModelData( model,
'<paragraph>' +
'<$text linkHref="foo" bold="true">Bolded link</$text>' +
'<softBreak></softBreak>' +
'F[]' +
'</paragraph>'
);

editor.execute( 'delete' );

const selection = model.document.selection;

expect( selection.hasAttribute( 'linkHref' ) ).to.equal( false );
expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
} );
} );

0 comments on commit 1ddb955

Please sign in to comment.