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

Commit

Permalink
Tests: Added tests for link highlight removal during the downcast con…
Browse files Browse the repository at this point in the history
…version.
  • Loading branch information
oleq committed Apr 4, 2018
1 parent c0122d7 commit 5c6d15b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 13 deletions.
23 changes: 12 additions & 11 deletions src/linkediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,22 @@ export default class LinkEditing extends Plugin {
}
} );

// Removing the class.
editor.conversion.for( 'editingDowncast' ).add( dispatcher => {
// Make sure the highlight is remove on every possible event.
dispatcher.on( 'insert', removeHighlight, { priority: 'highest' } );
dispatcher.on( 'attribute', removeHighlight, { priority: 'highest' } );
dispatcher.on( 'remove', removeHighlight, { priority: 'highest' } );
dispatcher.on( 'attribute', removeHighlight, { priority: 'highest' } );
dispatcher.on( 'selection', removeHighlight, { priority: 'highest' } );
} );

// Removing the class.
function removeHighlight() {
view.change( writer => {
for ( const item of highlightedLinks.values() ) {
writer.removeClass( HIGHLIGHT_CLASSES, item );
highlightedLinks.delete( item );
}
} );
}
function removeHighlight() {
view.change( writer => {
for ( const item of highlightedLinks.values() ) {
writer.removeClass( HIGHLIGHT_CLASSES, item );
highlightedLinks.delete( item );
}
} );
}
} );
}
}
99 changes: 97 additions & 2 deletions tests/linkediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import UnlinkCommand from '../src/unlinkcommand';
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
import { isLinkElement } from '../src/utils';
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';

import { downcastAttributeToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
import {
downcastMarkerToHighlight,
downcastAttributeToElement
} from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';

/* global document */

Expand Down Expand Up @@ -232,5 +235,97 @@ describe( 'LinkEditing', () => {
'<p>foo <a class="ck ck-link_selected" href="url">l{}ink</a> baz</p>'
);
} );

describe( 'downcast conversion integration', () => {
it( 'works for the #insert event', () => {
setModelData( model,
'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
);

model.change( writer => {
writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
} );

expect( getViewData( view ) ).to.equal(
'<p>foo <a class="ck ck-link_selected" href="url">liFOO{}nk</a> baz</p>'
);
} );

it( 'works for the #remove event', () => {
setModelData( model,
'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
);

model.change( writer => {
writer.remove( ModelRange.createFromParentsAndOffsets(
model.document.getRoot().getChild( 0 ), 0,
model.document.getRoot().getChild( 0 ), 5 )
);
} );

expect( getViewData( view ) ).to.equal(
'<p><a class="ck ck-link_selected" href="url">i{}nk</a> baz</p>'
);
} );

it( 'works for the #attribute event', () => {
setModelData( model,
'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
);

model.change( writer => {
writer.setAttribute( 'linkHref', 'new-url', new ModelRange(
model.document.selection.getFirstPosition().getShiftedBy( -1 ),
model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
);
} );

expect( getViewData( view ) ).to.equal(
'<p>foo <a href="url">l</a><a class="ck ck-link_selected" href="new-url">i{}n</a><a href="url">k</a> baz</p>'
);
} );

it( 'works for the #selection event', () => {
setModelData( model,
'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
);

model.change( writer => {
writer.setSelection( new ModelRange(
model.document.selection.getFirstPosition().getShiftedBy( -1 ),
model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
);
} );

expect( getViewData( view ) ).to.equal(
'<p>foo <a class="ck ck-link_selected" href="url">l{in}k</a> baz</p>'
);
} );

it( 'works for the #addMarker and #removeMarker events', () => {
downcastMarkerToHighlight( { model: 'fooMarker', view: {} } )( editor.editing.downcastDispatcher );

setModelData( model,
'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
);

model.change( writer => {
writer.setMarker( 'fooMarker', ModelRange.createFromParentsAndOffsets(
model.document.getRoot().getChild( 0 ), 0,
model.document.getRoot().getChild( 0 ), 5 )
);
} );

expect( getViewData( view ) ).to.equal(
'<p><span>foo </span><a class="ck ck-link_selected" href="url"><span>l</span>i{}nk</a> baz</p>'
);

model.change( writer => writer.removeMarker( 'fooMarker' ) );

expect( getViewData( view ) ).to.equal(
'<p>foo <a class="ck ck-link_selected" href="url">li{}nk</a> baz</p>'
);
} );
} );
} );
} );

0 comments on commit 5c6d15b

Please sign in to comment.