浏览代码

Merge pull request #7714 from ckeditor/i/7459

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.
Maciej 5 年之前
父节点
当前提交
1ddb955cc6

+ 2 - 2
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 );
 				}

+ 26 - 0
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, '<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()', () => {

+ 1 - 0
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"

+ 22 - 3
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 <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 );
+	} );
 } );