/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import inlineHighlight from '../../src/utils/inlinehighlight';
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
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 '@ckeditor/ckeditor5-core/tests/_utils/assertions/attribute';
/* global document */
describe( 'inlineHighlight', () => {
let element, editor, model, view;
beforeEach( async () => {
element = document.createElement( 'div' );
document.body.appendChild( element );
editor = await ClassicTestEditor.create( element );
model = editor.model;
view = editor.editing.view;
model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );
model.schema.extend( '$text', { allowAttributes: 'linkHref' } );
editor.conversion.for( 'editingDowncast' )
.attributeToElement( { model: 'linkHref', view: ( href, { writer } ) => {
return writer.createAttributeElement( 'a', { href } );
} } );
// Setup highlight over selected link.
inlineHighlight( editor, 'linkHref', 'a', 'ck-link_selected' );
} );
afterEach( async () => {
element.remove();
await editor.destroy();
} );
describe( 'attribute highlighting', () => {
it( 'should convert the highlight to a proper view classes', () => {
setModelData( model,
'
foo b{}ar baz
' ); } ); it( 'should work whenever selection has linkHref attribute - link start', () => { setModelData( model, 'foo {}bar baz
' ); } ); it( 'should work whenever selection has linkHref attribute - link end', () => { setModelData( model, 'foo bar{} baz
' ); } ); it( 'should render highlight correctly after splitting the link', () => { setModelData( model, 'foo li
' + '{}nk baz
' ); } ); it( 'should remove classes when selection is moved out from the link', () => { setModelData( model, 'foo li{}nk baz
' ); model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) ); expect( getViewData( view ) ).to.equal( '{}foo link baz
' ); } ); it( 'should work correctly when selection is moved inside link', () => { setModelData( model, 'foo li{}nk baz
' ); model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 5 ) ); expect( getViewData( view ) ).to.equal( 'foo l{}ink baz
' ); } ); describe( 'downcast conversion integration', () => { it( 'works for the #insert event', () => { setModelData( model, 'foo liFOO{}nk baz
' ); } ); it( 'works for the #remove event', () => { setModelData( model, 'i{}nk baz
' ); } ); it( 'works for the #attribute event', () => { setModelData( model, 'foo l{in}k baz
' ); } ); it( 'works for the addMarker and removeMarker events', () => { editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } ); setModelData( model, 'foo li{}nk baz
' ); model.change( writer => writer.removeMarker( 'fooMarker' ) ); expect( getViewData( view ) ).to.equal( 'foo li{}nk baz
' ); } ); } ); } ); } );