From 3b797c6a9e810a83563d556b3e804a9357d6d294 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Wed, 4 Oct 2017 13:11:21 +0200 Subject: [PATCH] Changed: Prevent removing attribute element from empty block element with stored selection attribute. --- .../model-selection-to-view-converters.js | 18 +++++++++++++----- src/view/writer.js | 10 ++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/conversion/model-selection-to-view-converters.js b/src/conversion/model-selection-to-view-converters.js index 71e4d5023..529e766b7 100644 --- a/src/conversion/model-selection-to-view-converters.js +++ b/src/conversion/model-selection-to-view-converters.js @@ -83,11 +83,14 @@ export function convertCollapsedSelection() { } const modelPosition = selection.getFirstPosition(); - const viewPosition = conversionApi.mapper.toViewPosition( modelPosition ); - const brokenPosition = viewWriter.breakAttributes( viewPosition ); + let viewPosition = conversionApi.mapper.toViewPosition( modelPosition ); + + if ( !modelPosition.parent.isEmpty ) { + viewPosition = viewWriter.breakAttributes( viewPosition ); + } conversionApi.viewSelection.removeAllRanges(); - conversionApi.viewSelection.addRange( new ViewRange( brokenPosition, brokenPosition ) ); + conversionApi.viewSelection.addRange( new ViewRange( viewPosition, viewPosition ) ); }; } @@ -257,9 +260,14 @@ export function clearAttributes() { for ( const range of conversionApi.viewSelection.getRanges() ) { // Not collapsed selection should not have artifacts. if ( range.isCollapsed ) { + const viewPosition = range.start; + const modelPosition = conversionApi.mapper.toModelPosition( viewPosition ); + // Position might be in the node removed by the view writer. - if ( range.end.parent.document ) { - viewWriter.mergeAttributes( range.start ); + if ( viewPosition.parent.document && modelPosition ) { + if ( !modelPosition.parent.isEmpty ) { + viewWriter.mergeAttributes( viewPosition ); + } } } } diff --git a/src/view/writer.js b/src/view/writer.js index ffbd49b07..2d2d3072f 100644 --- a/src/view/writer.js +++ b/src/view/writer.js @@ -540,6 +540,16 @@ export function wrapPosition( position, attribute ) { return movePositionToTextNode( Position.createFromPosition( position ) ); } + // Put position inside node, at the end of it, if previous node is similar. + if ( position.nodeBefore && position.nodeBefore.isSimilar( attribute ) ) { + return movePositionToTextNode( Position.createAt( position.nodeBefore, 'end' ) ); + } + + // Put position inside node, at the beginning of it, if next node is similar. + if ( position.nodeAfter && position.nodeAfter.isSimilar( attribute ) ) { + return movePositionToTextNode( Position.createAt( position.nodeAfter, 0 ) ); + } + // When position is inside text node - break it and place new position between two text nodes. if ( position.parent.is( 'text' ) ) { position = breakTextNode( position );