| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: conversion */
- import ModelDocument from '/ckeditor5/engine/model/document.js';
- import ModelElement from '/ckeditor5/engine/model/element.js';
- import ModelRange from '/ckeditor5/engine/model/range.js';
- import ModelPosition from '/ckeditor5/engine/model/position.js';
- import ViewDocument from '/ckeditor5/engine/view/document.js';
- import ViewContainerElement from '/ckeditor5/engine/view/containerelement.js';
- import ViewAttributeElement from '/ckeditor5/engine/view/attributeelement.js';
- import { mergeAt } from '/ckeditor5/engine/view/writer.js';
- import Mapper from '/ckeditor5/engine/conversion/mapper.js';
- import ModelConversionDispatcher from '/ckeditor5/engine/conversion/modelconversiondispatcher.js';
- import {
- convertRangeSelection,
- convertCollapsedSelection,
- convertSelectionAttribute,
- clearAttributes
- } from '/ckeditor5/engine/conversion/model-selection-to-view-converters.js';
- import {
- insertElement,
- insertText,
- wrap
- } from '/ckeditor5/engine/conversion/model-to-view-converters.js';
- import { stringify as stringifyView } from '/tests/engine/_utils/view.js';
- import { setData as setModelData } from '/tests/engine/_utils/model.js';
- let dispatcher, mapper;
- let modelDoc, modelRoot, modelSelection;
- let viewDoc, viewRoot, viewSelection;
- beforeEach( () => {
- modelDoc = new ModelDocument();
- modelRoot = modelDoc.createRoot();
- modelSelection = modelDoc.selection;
- modelDoc.schema.allow( { name: '$text', inside: '$root' } );
- viewDoc = new ViewDocument();
- viewRoot = viewDoc.createRoot( 'div' );
- viewSelection = viewDoc.selection;
- mapper = new Mapper();
- mapper.bindElements( modelRoot, viewRoot );
- dispatcher = new ModelConversionDispatcher( { mapper, viewSelection } );
- dispatcher.on( 'insert:$text', insertText() );
- dispatcher.on( 'addAttribute:bold', wrap( new ViewAttributeElement( 'strong' ) ) );
- // Default selection converters.
- dispatcher.on( 'selection', clearAttributes(), 'low' );
- dispatcher.on( 'selection', convertRangeSelection(), 'low' );
- dispatcher.on( 'selection', convertCollapsedSelection(), 'low' );
- } );
- describe( 'default converters', () => {
- beforeEach( () => {
- // Selection converters for selection attributes.
- dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'strong' ) ) );
- dispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
- } );
- describe( 'range selection', () => {
- it( 'in same container', () => {
- test(
- [ 1, 4 ],
- 'foobar',
- 'f{oob}ar'
- );
- } );
- it( 'in same container with unicode characters', () => {
- test(
- [ 2, 6 ],
- 'நிலைக்கு',
- 'நி{லைக்}கு'
- );
- } );
- it( 'in same container, over attribute', () => {
- test(
- [ 1, 5 ],
- 'fo<$text bold="true">ob</$text>ar',
- 'f{o<strong>ob</strong>a}r'
- );
- } );
- it( 'in same container, next to attribute', () => {
- test(
- [ 1, 2 ],
- 'fo<$text bold="true">ob</$text>ar',
- 'f{o}<strong>ob</strong>ar'
- );
- } );
- it( 'in same attribute', () => {
- test(
- [ 2, 4 ],
- 'f<$text bold="true">ooba</$text>r',
- 'f<strong>o{ob}a</strong>r'
- );
- } );
- it( 'in same attribute, selection same as attribute', () => {
- test(
- [ 2, 4 ],
- 'fo<$text bold="true">ob</$text>ar',
- 'fo{<strong>ob</strong>}ar'
- );
- } );
- it( 'starts in text node, ends in attribute #1', () => {
- test(
- [ 1, 3 ],
- 'fo<$text bold="true">ob</$text>ar',
- 'f{o<strong>o}b</strong>ar'
- );
- } );
- it( 'starts in text node, ends in attribute #2', () => {
- test(
- [ 1, 4 ],
- 'fo<$text bold="true">ob</$text>ar',
- 'f{o<strong>ob</strong>}ar'
- );
- } );
- it( 'starts in attribute, ends in text node', () => {
- test(
- [ 3, 5 ],
- 'fo<$text bold="true">ob</$text>ar',
- 'fo<strong>o{b</strong>a}r'
- );
- } );
- it( 'consumes consumable values properly', () => {
- // Add callback that will fire before default ones.
- // This should prevent default callback doing anything.
- dispatcher.on( 'selection', ( evt, data, consumable ) => {
- expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
- }, 'high' );
- // Similar test case as the first in this suite.
- test(
- [ 1, 4 ],
- 'foobar',
- 'foobar' // No selection in view.
- );
- } );
- it( 'should convert backward selection', () => {
- test(
- [ 1, 3, 'backward' ],
- 'foobar',
- 'f{oo}bar'
- );
- expect( viewSelection.focus.offset ).to.equal( 1 );
- } );
- } );
- describe( 'collapsed selection', () => {
- it( 'in container', () => {
- test(
- [ 1, 1 ],
- 'foobar',
- 'f{}oobar'
- );
- } );
- it( 'in attribute', () => {
- test(
- [ 3, 3 ],
- 'f<$text bold="true">ooba</$text>r',
- 'f<strong>oo{}ba</strong>r'
- );
- } );
- it( 'in container with extra attributes', () => {
- test(
- [ 1, 1 ],
- 'foobar',
- 'f<em>[]</em>oobar',
- { italic: true }
- );
- } );
- it( 'in attribute with extra attributes', () => {
- test(
- [ 3, 3 ],
- 'f<$text bold="true">ooba</$text>r',
- 'f<strong>oo</strong><em><strong>[]</strong></em><strong>ba</strong>r',
- { italic: true }
- );
- } );
- it( 'consumes consumable values properly', () => {
- // Add callbacks that will fire before default ones.
- // This should prevent default callbacks doing anything.
- dispatcher.on( 'selection', ( evt, data, consumable ) => {
- expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
- }, 'high' );
- dispatcher.on( 'selectionAttribute:bold', ( evt, data, consumable ) => {
- expect( consumable.consume( data.selection, 'selectionAttribute:bold' ) ).to.be.true;
- }, 'high' );
- // Similar test case as above
- test(
- [ 3, 3 ],
- 'f<$text bold="true">ooba</$text>r',
- 'f<strong>ooba</strong>r' // No selection in view.
- );
- } );
- } );
- } );
- describe( 'clean-up', () => {
- describe( 'convertRangeSelection', () => {
- it( 'should remove all ranges before adding new range', () => {
- test(
- [ 0, 2 ],
- 'foobar',
- '{fo}obar'
- );
- test(
- [ 3, 5 ],
- 'foobar',
- 'foo{ba}r'
- );
- expect( viewSelection.rangeCount ).to.equal( 1 );
- } );
- } );
- describe( 'convertCollapsedSelection', () => {
- it( 'should remove all ranges before adding new range', () => {
- test(
- [ 2, 2 ],
- 'foobar',
- 'fo{}obar'
- );
- test(
- [ 3, 3 ],
- 'foobar',
- 'foo{}bar'
- );
- expect( viewSelection.rangeCount ).to.equal( 1 );
- } );
- } );
- describe( 'clearAttributes', () => {
- it( 'should remove all ranges before adding new range', () => {
- dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
- dispatcher.on( 'addAttribute:style', wrap( new ViewAttributeElement( 'b' ) ) );
- test(
- [ 3, 3 ],
- 'foobar',
- 'foo<b>[]</b>bar',
- { bold: 'true' }
- );
- const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
- modelDoc.selection.setRanges( [ modelRange ] );
- dispatcher.convertSelection( modelDoc.selection );
- expect( viewSelection.rangeCount ).to.equal( 1 );
- const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
- expect( viewString ).to.equal( '<div>f{}oobar</div>' );
- } );
- it( 'should do nothing if the attribute element had been already removed', () => {
- dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
- dispatcher.on( 'addAttribute:style', wrap( new ViewAttributeElement( 'b' ) ) );
- test(
- [ 3, 3 ],
- 'foobar',
- 'foo<b>[]</b>bar',
- { bold: 'true' }
- );
- // Remove <b></b> manually.
- mergeAt( viewSelection.getFirstPosition() );
- const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
- modelDoc.selection.setRanges( [ modelRange ] );
- dispatcher.convertSelection( modelDoc.selection );
- expect( viewSelection.rangeCount ).to.equal( 1 );
- const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
- expect( viewString ).to.equal( '<div>f{}oobar</div>' );
- } );
- } );
- } );
- describe( 'using element creator for attributes conversion', () => {
- beforeEach( () => {
- function themeElementCreator( themeValue ) {
- if ( themeValue == 'important' ) {
- return new ViewAttributeElement( 'strong', { style: 'text-transform:uppercase;' } );
- } else if ( themeValue == 'gold' ) {
- return new ViewAttributeElement( 'span', { style: 'color:yellow;' } );
- }
- }
- dispatcher.on( 'selectionAttribute:theme', convertSelectionAttribute( themeElementCreator ) );
- dispatcher.on( 'addAttribute:theme', wrap( themeElementCreator ) );
- dispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
- } );
- describe( 'range selection', () => {
- it( 'in same container, over attribute', () => {
- test(
- [ 1, 5 ],
- 'fo<$text theme="gold">ob</$text>ar',
- 'f{o<span style="color:yellow;">ob</span>a}r'
- );
- } );
- it( 'in same attribute', () => {
- test(
- [ 2, 4 ],
- 'f<$text theme="gold">ooba</$text>r',
- 'f<span style="color:yellow;">o{ob}a</span>r'
- );
- } );
- it( 'in same attribute, selection same as attribute', () => {
- test(
- [ 2, 4 ],
- 'fo<$text theme="important">ob</$text>ar',
- 'fo{<strong style="text-transform:uppercase;">ob</strong>}ar'
- );
- } );
- it( 'starts in attribute, ends in text node', () => {
- test(
- [ 3, 5 ],
- 'fo<$text theme="important">ob</$text>ar',
- 'fo<strong style="text-transform:uppercase;">o{b</strong>a}r'
- );
- } );
- } );
- describe( 'collapsed selection', () => {
- it( 'in attribute', () => {
- test(
- [ 3, 3 ],
- 'f<$text theme="gold">ooba</$text>r',
- 'f<span style="color:yellow;">oo{}ba</span>r'
- );
- } );
- it( 'in container with theme attribute', () => {
- test(
- [ 1, 1 ],
- 'foobar',
- 'f<strong style="text-transform:uppercase;">[]</strong>oobar',
- { theme: 'important' }
- );
- } );
- it( 'in theme attribute with extra attributes #1', () => {
- test(
- [ 3, 3 ],
- 'f<$text theme="gold">ooba</$text>r',
- 'f<span style="color:yellow;">oo</span>' +
- '<em><span style="color:yellow;">[]</span></em>' +
- '<span style="color:yellow;">ba</span>r',
- { italic: true }
- );
- } );
- it( 'in theme attribute with extra attributes #2', () => {
- // In contrary to test above, we don't have strong + span on the selection.
- // This is because strong and span are both created by the same attribute.
- // Since style="important" overwrites style="gold" on selection, we have only strong element.
- // In example above, selection has both style and italic attribute.
- test(
- [ 3, 3 ],
- 'f<$text theme="gold">ooba</$text>r',
- 'f<span style="color:yellow;">oo</span>' +
- '<strong style="text-transform:uppercase;">[]</strong>' +
- '<span style="color:yellow;">ba</span>r',
- { theme: 'important' }
- );
- } );
- } );
- } );
- describe( 'table cell selection converter', () => {
- beforeEach( () => {
- modelDoc.schema.registerItem( 'table', '$block' );
- modelDoc.schema.registerItem( 'tr', '$block' );
- modelDoc.schema.registerItem( 'td', '$block' );
- // "Universal" converter to convert table structure.
- const tableConverter = insertElement( ( data ) => new ViewContainerElement( data.item.name ) );
- dispatcher.on( 'insert:table', tableConverter );
- dispatcher.on( 'insert:tr', tableConverter );
- dispatcher.on( 'insert:td', tableConverter );
- // Special converter for table cells.
- dispatcher.on( 'selection', ( evt, data, consumable, conversionApi ) => {
- const selection = data.selection;
- if ( !consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
- return;
- }
- for ( let range of selection.getRanges() ) {
- const node = range.start.nodeAfter;
- if ( node == range.end.nodeBefore && node instanceof ModelElement && node.name == 'td' ) {
- consumable.consume( selection, 'selection' );
- let viewNode = conversionApi.mapper.toViewElement( node );
- viewNode.addClass( 'selected' );
- }
- }
- } );
- } );
- it( 'should not be used to convert selection that is not on table cell', () => {
- test(
- [ 1, 5 ],
- 'f{o<$text bold="true">ob</$text>a}r',
- 'f{o<strong>ob</strong>a}r'
- );
- } );
- it( 'should add a class to the selected table cell', () => {
- test(
- // table tr#0 |td#0, table tr#0 td#0|
- [ [ 0, 0, 0 ], [ 0, 0, 1 ] ],
- '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
- '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
- );
- } );
- it( 'should not be used if selection contains more than just a table cell', () => {
- test(
- // table tr td#1, table tr#2
- [ [ 0, 0, 0, 1 ], [ 0, 0, 2 ] ],
- '<table><tr><td>foo</td><td>bar</td></tr></table>',
- '<table><tr><td>f{oo</td><td>bar</td>]</tr></table>'
- );
- } );
- } );
- // Tests if the selection got correctly converted.
- // Because `setData` might use selection converters itself to set the selection, we can't use it
- // to set the selection (because then we would test converters using converters).
- // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
- // that are offsets or paths of selection positions in root element.
- function test( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
- // Parse passed `modelInput` string and set it as current model.
- setModelData( modelDoc, modelInput );
- // Manually set selection ranges using passed `selectionPaths`.
- const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
- const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
- const startPos = new ModelPosition( modelRoot, startPath );
- const endPos = new ModelPosition( modelRoot, endPath );
- const isBackward = selectionPaths[ 2 ] === 'backward';
- modelSelection.setRanges( [ new ModelRange( startPos, endPos ) ], isBackward );
- // Updated selection attributes according to model.
- modelSelection._updateAttributes();
- // And add or remove passed attributes.
- for ( let key in selectionAttributes ) {
- let value = selectionAttributes[ key ];
- if ( value ) {
- modelSelection.setAttribute( key, value );
- } else {
- modelSelection.removeAttribute( key );
- }
- }
- // Remove view children since we do not want to convert deletion.
- viewRoot.removeChildren( 0, viewRoot.childCount );
- // Convert model to view.
- dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
- dispatcher.convertSelection( modelSelection );
- // Stringify view and check if it is same as expected.
- expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
- }
|