Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Changed: Prevent removing attribute element from empty block element …
Browse files Browse the repository at this point in the history
…with stored selection attribute.
  • Loading branch information
scofalik committed Oct 4, 2017
1 parent 8192ac4 commit 3b797c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/conversion/model-selection-to-view-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
};
}

Expand Down Expand Up @@ -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 );
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/view/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 3b797c6

Please sign in to comment.