| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- /**
- * @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,
- '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
- );
- expect( model.document.selection ).to.have.attribute( 'linkHref' );
- expect( getViewData( view ) ).to.equal(
- '<p>foo <a class="ck-link_selected" href="url">b{}ar</a> baz</p>'
- );
- } );
- it( 'should work whenever selection has linkHref attribute - link start', () => {
- setModelData( model,
- '<paragraph>foo {}<$text linkHref="url">bar</$text> baz</paragraph>'
- );
- expect( model.document.selection ).to.not.have.attribute( 'linkHref' );
- model.change( writer => {
- writer.setSelectionAttribute( 'linkHref', 'url' );
- } );
- expect( model.document.selection ).to.have.attribute( 'linkHref' );
- expect( getViewData( view ) ).to.equal(
- '<p>foo <a class="ck-link_selected" href="url">{}bar</a> baz</p>'
- );
- } );
- it( 'should work whenever selection has linkHref attribute - link end', () => {
- setModelData( model,
- '<paragraph>foo <$text linkHref="url">bar</$text>{} baz</paragraph>'
- );
- expect( model.document.selection ).to.have.attribute( 'linkHref' );
- expect( getViewData( view ) ).to.equal(
- '<p>foo <a class="ck-link_selected" href="url">bar{}</a> baz</p>'
- );
- } );
- it( 'should render highlight correctly after splitting the link', () => {
- setModelData( model,
- '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
- );
- model.change( writer => {
- const splitPos = model.document.selection.getFirstRange().start;
- writer.split( splitPos );
- writer.setSelection( splitPos.parent.nextSibling, 0 );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
- '<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
- );
- expect( model.document.selection ).to.have.attribute( 'linkHref' );
- expect( getViewData( view ) ).to.equal(
- '<p>foo <a href="url">li</a></p>' +
- '<p><a class="ck-link_selected" href="url">{}nk</a> baz</p>'
- );
- } );
- it( 'should remove classes when selection is moved out from the link', () => {
- setModelData( model,
- '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
- );
- expect( getViewData( view ) ).to.equal(
- '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
- );
- model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
- expect( getViewData( view ) ).to.equal(
- '<p>{}foo <a href="url">link</a> baz</p>'
- );
- } );
- it( 'should work correctly when selection is moved inside link', () => {
- setModelData( model,
- '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
- );
- expect( getViewData( view ) ).to.equal(
- '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
- );
- model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 5 ) );
- expect( getViewData( view ) ).to.equal(
- '<p>foo <a class="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-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( writer.createRange(
- writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
- writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
- ) );
- } );
- expect( getViewData( view ) ).to.equal(
- '<p><a class="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', writer.createRange(
- 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-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( writer.createRange(
- model.document.selection.getFirstPosition().getShiftedBy( -1 ),
- model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
- );
- } );
- expect( getViewData( view ) ).to.equal(
- '<p>foo <a class="ck-link_selected" href="url">l{in}k</a> baz</p>'
- );
- } );
- it( 'works for the addMarker and removeMarker events', () => {
- editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
- setModelData( model,
- '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
- );
- model.change( writer => {
- const range = writer.createRange(
- writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
- writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
- );
- writer.addMarker( 'fooMarker', { range, usingOperation: true } );
- } );
- expect( getViewData( view ) ).to.equal(
- '<p><span>foo </span><a class="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-link_selected" href="url">li{}nk</a> baz</p>'
- );
- } );
- } );
- } );
- } );
|