| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import EditingController from '../../src/controller/editingcontroller';
- import Conversion from '../../src/conversion/conversion';
- import Model from '../../src/model/model';
- import ModelElement from '../../src/model/element';
- import ModelText from '../../src/model/text';
- import ViewElement from '../../src/view/element';
- import ViewAttributeElement from '../../src/view/attributeelement';
- import ViewContainerElement from '../../src/view/containerelement';
- import ViewUIElement from '../../src/view/uielement';
- import ViewText from '../../src/view/text';
- import log from '@ckeditor/ckeditor5-utils/src/log';
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- import DowncastHelpers, {
- insertElement, insertUIElement, changeAttribute, wrap, removeUIElement,
- highlightElement, highlightText, removeHighlight, createViewElementFromHighlightDescriptor
- } from '../../src/conversion/downcast-converters';
- import { stringify } from '../../src/dev-utils/view';
- describe( 'downcast-helpers', () => {
- let conversion, model, modelRoot, viewRoot, downcastHelpers;
- beforeEach( () => {
- model = new Model();
- const modelDoc = model.document;
- modelRoot = modelDoc.createRoot();
- const controller = new EditingController( model );
- // Set name of view root the same as dom root.
- // This is a mock of attaching view root to dom root.
- controller.view.document.getRoot()._name = 'div';
- viewRoot = controller.view.document.getRoot();
- downcastHelpers = new DowncastHelpers( controller.downcastDispatcher );
- conversion = new Conversion();
- conversion.register( 'downcast', downcastHelpers );
- } );
- describe( '_downcastElementToElement', () => {
- it( 'config.view is a string', () => {
- downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
- model.change( writer => {
- writer.insertElement( 'paragraph', modelRoot, 0 );
- } );
- expectResult( '<p></p>' );
- } );
- it( 'can be overwritten using converterPriority', () => {
- downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
- downcastHelpers.elementToElement( { model: 'paragraph', view: 'foo', converterPriority: 'high' } );
- model.change( writer => {
- writer.insertElement( 'paragraph', modelRoot, 0 );
- } );
- expectResult( '<foo></foo>' );
- } );
- it( 'config.view is a view element definition', () => {
- downcastHelpers.elementToElement( {
- model: 'fancyParagraph',
- view: {
- name: 'p',
- classes: 'fancy'
- }
- } );
- model.change( writer => {
- writer.insertElement( 'fancyParagraph', modelRoot, 0 );
- } );
- expectResult( '<p class="fancy"></p>' );
- } );
- it( 'config.view is a function', () => {
- downcastHelpers.elementToElement( {
- model: 'heading',
- view: ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'h' + modelElement.getAttribute( 'level' ) )
- } );
- model.change( writer => {
- writer.insertElement( 'heading', { level: 2 }, modelRoot, 0 );
- } );
- expectResult( '<h2></h2>' );
- } );
- } );
- describe( '_downcastAttributeToElement', () => {
- it( 'config.view is a string', () => {
- downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
- model.change( writer => {
- writer.insertText( 'foo', { bold: true }, modelRoot, 0 );
- } );
- expectResult( '<strong>foo</strong>' );
- } );
- it( 'can be overwritten using converterPriority', () => {
- downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
- downcastHelpers.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
- model.change( writer => {
- writer.insertText( 'foo', { bold: true }, modelRoot, 0 );
- } );
- expectResult( '<b>foo</b>' );
- } );
- it( 'config.view is a view element definition', () => {
- downcastHelpers.attributeToElement( {
- model: 'invert',
- view: {
- name: 'span',
- classes: [ 'font-light', 'bg-dark' ]
- }
- } );
- model.change( writer => {
- writer.insertText( 'foo', { invert: true }, modelRoot, 0 );
- } );
- expectResult( '<span class="bg-dark font-light">foo</span>' );
- expect( viewRoot.getChild( 0 ).priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
- } );
- it( 'config.view allows specifying the element\'s priority', () => {
- downcastHelpers.attributeToElement( {
- model: 'invert',
- view: {
- name: 'span',
- priority: 5
- }
- } );
- model.change( writer => {
- writer.insertText( 'foo', { invert: true }, modelRoot, 0 );
- } );
- expect( viewRoot.getChild( 0 ).priority ).to.equal( 5 );
- } );
- it( 'model attribute value is enum', () => {
- downcastHelpers.attributeToElement( {
- model: {
- key: 'fontSize',
- values: [ 'big', 'small' ]
- },
- view: {
- big: {
- name: 'span',
- styles: {
- 'font-size': '1.2em'
- }
- },
- small: {
- name: 'span',
- styles: {
- 'font-size': '0.8em'
- },
- priority: 5
- }
- }
- } );
- model.change( writer => {
- writer.insertText( 'foo', { fontSize: 'big' }, modelRoot, 0 );
- } );
- expect( viewRoot.getChild( 0 ).priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
- expectResult( '<span style="font-size:1.2em">foo</span>' );
- model.change( writer => {
- writer.setAttribute( 'fontSize', 'small', modelRoot.getChild( 0 ) );
- } );
- expectResult( '<span style="font-size:0.8em">foo</span>' );
- expect( viewRoot.getChild( 0 ).priority ).to.equal( 5 );
- model.change( writer => {
- writer.removeAttribute( 'fontSize', modelRoot.getChild( 0 ) );
- } );
- expectResult( 'foo' );
- } );
- it( 'config.view is a function', () => {
- downcastHelpers.attributeToElement( {
- model: 'bold',
- view: ( modelAttributeValue, viewWriter ) => {
- return viewWriter.createAttributeElement( 'span', { style: 'font-weight:' + modelAttributeValue } );
- }
- } );
- model.change( writer => {
- writer.insertText( 'foo', { bold: '500' }, modelRoot, 0 );
- } );
- expectResult( '<span style="font-weight:500">foo</span>' );
- } );
- it( 'config.model.name is given', () => {
- downcastHelpers.attributeToElement( {
- model: {
- key: 'color',
- name: '$text'
- },
- view: ( modelAttributeValue, viewWriter ) => {
- return viewWriter.createAttributeElement( 'span', { style: 'color:' + modelAttributeValue } );
- }
- } );
- downcastHelpers.elementToElement( {
- model: 'smiley',
- view: ( modelElement, viewWriter ) => {
- return viewWriter.createEmptyElement( 'img', {
- src: 'smile.jpg',
- class: 'smiley'
- } );
- }
- } );
- model.change( writer => {
- writer.insertText( 'foo', { color: '#FF0000' }, modelRoot, 0 );
- writer.insertElement( 'smiley', { color: '#FF0000' }, modelRoot, 3 );
- } );
- expectResult( '<span style="color:#FF0000">foo</span><img class="smiley" src="smile.jpg"></img>' );
- } );
- } );
- describe( '_downcastAttributeToAttribute', () => {
- testUtils.createSinonSandbox();
- beforeEach( () => {
- downcastHelpers.elementToElement( { model: 'image', view: 'img' } );
- } );
- it( 'config.view is a string', () => {
- downcastHelpers.attributeToAttribute( { model: 'source', view: 'src' } );
- model.change( writer => {
- writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
- } );
- expectResult( '<img src="foo.jpg"></img>' );
- model.change( writer => {
- writer.removeAttribute( 'source', modelRoot.getChild( 0 ) );
- } );
- expectResult( '<img></img>' );
- } );
- it( 'can be overwritten using converterPriority', () => {
- downcastHelpers.attributeToAttribute( { model: 'source', view: 'href' } );
- downcastHelpers.attributeToAttribute( { model: 'source', view: 'src', converterPriority: 'high' } );
- model.change( writer => {
- writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
- } );
- expectResult( '<img src="foo.jpg"></img>' );
- } );
- it( 'model element name specified', () => {
- downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
- downcastHelpers.attributeToAttribute( {
- model: {
- name: 'image',
- key: 'source'
- },
- view: 'src'
- } );
- model.change( writer => {
- writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
- } );
- expectResult( '<img src="foo.jpg"></img>' );
- model.change( writer => {
- writer.rename( modelRoot.getChild( 0 ), 'paragraph' );
- } );
- expectResult( '<p></p>' );
- } );
- it( 'config.view is an object, model attribute value is enum', () => {
- downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
- downcastHelpers.attributeToAttribute( {
- model: {
- key: 'styled',
- values: [ 'dark', 'light' ]
- },
- view: {
- dark: {
- key: 'class',
- value: [ 'styled', 'styled-dark' ]
- },
- light: {
- key: 'class',
- value: [ 'styled', 'styled-light' ]
- }
- }
- } );
- model.change( writer => {
- writer.insertElement( 'paragraph', { styled: 'dark' }, modelRoot, 0 );
- } );
- expectResult( '<p class="styled styled-dark"></p>' );
- model.change( writer => {
- writer.setAttribute( 'styled', 'light', modelRoot.getChild( 0 ) );
- } );
- expectResult( '<p class="styled styled-light"></p>' );
- model.change( writer => {
- writer.removeAttribute( 'styled', modelRoot.getChild( 0 ) );
- } );
- expectResult( '<p></p>' );
- } );
- it( 'config.view is an object, model attribute value is enum, view has style', () => {
- downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
- downcastHelpers.attributeToAttribute( {
- model: {
- key: 'align',
- values: [ 'right', 'center' ]
- },
- view: {
- right: {
- key: 'style',
- value: {
- 'text-align': 'right'
- }
- },
- center: {
- key: 'style',
- value: {
- 'text-align': 'center'
- }
- }
- }
- } );
- model.change( writer => {
- writer.insertElement( 'paragraph', { align: 'right' }, modelRoot, 0 );
- } );
- expectResult( '<p style="text-align:right"></p>' );
- model.change( writer => {
- writer.setAttribute( 'align', 'center', modelRoot.getChild( 0 ) );
- } );
- expectResult( '<p style="text-align:center"></p>' );
- model.change( writer => {
- writer.removeAttribute( 'align', modelRoot.getChild( 0 ) );
- } );
- expectResult( '<p></p>' );
- } );
- it( 'config.view is an object, only name and key are provided', () => {
- downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
- downcastHelpers.attributeToAttribute( {
- model: {
- name: 'paragraph',
- key: 'class'
- },
- view: {
- name: 'paragraph',
- key: 'class'
- }
- } );
- model.change( writer => {
- writer.insertElement( 'paragraph', { class: 'dark' }, modelRoot, 0 );
- } );
- expectResult( '<p class="dark"></p>' );
- model.change( writer => {
- writer.setAttribute( 'class', 'light', modelRoot.getChild( 0 ) );
- } );
- expectResult( '<p class="light"></p>' );
- model.change( writer => {
- writer.removeAttribute( 'class', modelRoot.getChild( 0 ) );
- } );
- expectResult( '<p></p>' );
- } );
- it( 'config.view is a function', () => {
- downcastHelpers.attributeToAttribute( {
- model: 'styled',
- view: attributeValue => ( { key: 'class', value: 'styled-' + attributeValue } )
- } );
- model.change( writer => {
- writer.insertElement( 'image', { styled: 'pull-out' }, modelRoot, 0 );
- } );
- expectResult( '<img class="styled-pull-out"></img>' );
- } );
- // #1587
- it( 'config.view and config.model as strings in generic conversion (elements only)', () => {
- const logSpy = testUtils.sinon.spy( log, 'warn' );
- downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
- downcastHelpers.attributeToAttribute( { model: 'test', view: 'test' } );
- model.change( writer => {
- writer.insertElement( 'paragraph', { test: '1' }, modelRoot, 0 );
- writer.insertElement( 'paragraph', { test: '2' }, modelRoot, 1 );
- } );
- expectResult( '<p test="1"></p><p test="2"></p>' );
- expect( logSpy.callCount ).to.equal( 0 );
- model.change( writer => {
- writer.removeAttribute( 'test', modelRoot.getChild( 1 ) );
- } );
- expectResult( '<p test="1"></p><p></p>' );
- } );
- // #1587
- it( 'config.view and config.model as strings in generic conversion (elements + text)', () => {
- const logSpy = testUtils.sinon.spy( log, 'warn' );
- downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
- downcastHelpers.attributeToAttribute( { model: 'test', view: 'test' } );
- model.change( writer => {
- writer.insertElement( 'paragraph', modelRoot, 0 );
- writer.insertElement( 'paragraph', { test: '1' }, modelRoot, 1 );
- writer.insertText( 'Foo', { test: '2' }, modelRoot.getChild( 0 ), 0 );
- writer.insertText( 'Bar', { test: '3' }, modelRoot.getChild( 1 ), 0 );
- } );
- expectResult( '<p>Foo</p><p test="1">Bar</p>' );
- expect( logSpy.callCount ).to.equal( 2 );
- expect( logSpy.alwaysCalledWithMatch( 'conversion-attribute-to-attribute-on-text' ) ).to.true;
- model.change( writer => {
- writer.removeAttribute( 'test', modelRoot.getChild( 1 ) );
- } );
- expectResult( '<p>Foo</p><p>Bar</p>' );
- } );
- } );
- describe( '_downcastMarkerToElement', () => {
- it( 'config.view is a string', () => {
- downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } );
- model.change( writer => {
- writer.insertText( 'foo', modelRoot, 0 );
- const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
- writer.addMarker( 'search', { range, usingOperation: false } );
- } );
- expectResult( 'f<marker-search></marker-search>o<marker-search></marker-search>o' );
- } );
- it( 'can be overwritten using converterPriority', () => {
- downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } );
- downcastHelpers.markerToElement( { model: 'search', view: 'search', converterPriority: 'high' } );
- model.change( writer => {
- writer.insertText( 'foo', modelRoot, 0 );
- const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
- writer.addMarker( 'search', { range, usingOperation: false } );
- } );
- expectResult( 'f<search></search>o<search></search>o' );
- } );
- it( 'config.view is a view element definition', () => {
- downcastHelpers.markerToElement( {
- model: 'search',
- view: {
- name: 'span',
- attributes: {
- 'data-marker': 'search'
- }
- }
- } );
- model.change( writer => {
- writer.insertText( 'foo', modelRoot, 0 );
- const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
- writer.addMarker( 'search', { range, usingOperation: false } );
- } );
- expectResult( 'f<span data-marker="search"></span>o<span data-marker="search"></span>o' );
- } );
- it( 'config.view is a function', () => {
- downcastHelpers.markerToElement( {
- model: 'search',
- view: ( data, viewWriter ) => {
- return viewWriter.createUIElement( 'span', { 'data-marker': 'search', 'data-start': data.isOpening } );
- }
- } );
- model.change( writer => {
- writer.insertText( 'foo', modelRoot, 0 );
- const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
- writer.addMarker( 'search', { range, usingOperation: false } );
- } );
- expectResult( 'f<span data-marker="search" data-start="true"></span>o<span data-marker="search" data-start="false"></span>o' );
- } );
- } );
- describe( '_downcastMarkerToHighlight', () => {
- it( 'config.view is a highlight descriptor', () => {
- downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
- model.change( writer => {
- writer.insertText( 'foo', modelRoot, 0 );
- const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
- writer.addMarker( 'comment', { range, usingOperation: false } );
- } );
- expectResult( '<span class="comment">foo</span>' );
- } );
- it( 'can be overwritten using converterPriority', () => {
- downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
- downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'new-comment' }, converterPriority: 'high' } );
- model.change( writer => {
- writer.insertText( 'foo', modelRoot, 0 );
- const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
- writer.addMarker( 'comment', { range, usingOperation: false } );
- } );
- expectResult( '<span class="new-comment">foo</span>' );
- } );
- it( 'config.view is a function', () => {
- downcastHelpers.markerToHighlight( {
- model: 'comment',
- view: data => {
- const commentType = data.markerName.split( ':' )[ 1 ];
- return {
- classes: [ 'comment', 'comment-' + commentType ]
- };
- }
- } );
- model.change( writer => {
- writer.insertText( 'foo', modelRoot, 0 );
- const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
- writer.addMarker( 'comment:abc', { range, usingOperation: false } );
- } );
- expectResult( '<span class="comment comment-abc">foo</span>' );
- } );
- } );
- function expectResult( string ) {
- expect( stringify( viewRoot, null, { ignoreRoot: true } ) ).to.equal( string );
- }
- } );
- describe( 'downcast-converters', () => {
- let dispatcher, modelDoc, modelRoot, viewRoot, controller, modelRootStart, model;
- beforeEach( () => {
- model = new Model();
- modelDoc = model.document;
- modelRoot = modelDoc.createRoot();
- controller = new EditingController( model );
- viewRoot = controller.view.document.getRoot();
- // Set name of view root the same as dom root.
- // This is a mock of attaching view root to dom root.
- controller.view.document.getRoot()._name = 'div';
- dispatcher = controller.downcastDispatcher;
- dispatcher.on(
- 'insert:paragraph',
- insertElement(
- ( modelItem, viewWriter ) => viewWriter.createContainerElement( 'p' )
- )
- );
- dispatcher.on( 'attribute:class', changeAttribute() );
- modelRootStart = model.createPositionAt( modelRoot, 0 );
- } );
- function viewAttributesToString( item ) {
- let result = '';
- for ( const key of item.getAttributeKeys() ) {
- const value = item.getAttribute( key );
- if ( value ) {
- result += ' ' + key + '="' + value + '"';
- }
- }
- return result;
- }
- function viewToString( item ) {
- let result = '';
- if ( item instanceof ViewText ) {
- result = item.data;
- } else {
- // ViewElement or ViewDocumentFragment.
- for ( const child of item.getChildren() ) {
- result += viewToString( child );
- }
- if ( item instanceof ViewElement ) {
- result = '<' + item.name + viewAttributesToString( item ) + '>' + result + '</' + item.name + '>';
- }
- }
- return result;
- }
- describe( 'insertText', () => {
- it( 'should downcast text', () => {
- model.change( writer => {
- writer.insert( new ModelText( 'foobar' ), modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div>foobar</div>' );
- } );
- it( 'should support unicode', () => {
- model.change( writer => {
- writer.insert( new ModelText( 'நிலைக்கு' ), modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div>நிலைக்கு</div>' );
- } );
- it( 'should be possible to override it', () => {
- dispatcher.on( 'insert:$text', ( evt, data, conversionApi ) => {
- conversionApi.consumable.consume( data.item, 'insert' );
- }, { converterPriority: 'high' } );
- model.change( writer => {
- writer.insert( new ModelText( 'foobar' ), modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div></div>' );
- } );
- } );
- describe( 'insertElement', () => {
- it( 'should convert element insertion in model', () => {
- const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- it( 'should not convert if creator returned null', () => {
- dispatcher.on( 'insert:div', insertElement( () => null ) );
- const modelElement = new ModelElement( 'div' );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div></div>' );
- } );
- } );
- describe( 'changeAttribute', () => {
- it( 'should convert attribute insert/change/remove on a model node', () => {
- const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );
- model.change( writer => {
- writer.setAttribute( 'class', 'bar', modelElement );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p class="bar">foobar</p></div>' );
- model.change( writer => {
- writer.removeAttribute( 'class', modelElement );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- it( 'should convert insert/change/remove with attribute generating function as a parameter', () => {
- const themeConverter = ( value, data ) => {
- if ( data.item instanceof ModelElement && data.item.childCount > 0 ) {
- value += ' fix-content';
- }
- return { key: 'class', value };
- };
- dispatcher.on( 'insert:div', insertElement( ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'div' ) ) );
- dispatcher.on( 'attribute:theme', changeAttribute( themeConverter ) );
- const modelParagraph = new ModelElement( 'paragraph', { theme: 'nice' }, new ModelText( 'foobar' ) );
- const modelDiv = new ModelElement( 'div', { theme: 'nice' } );
- model.change( writer => {
- writer.insert( [ modelParagraph, modelDiv ], modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p class="nice fix-content">foobar</p><div class="nice"></div></div>' );
- model.change( writer => {
- writer.setAttribute( 'theme', 'awesome', modelParagraph );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p class="awesome fix-content">foobar</p><div class="nice"></div></div>' );
- model.change( writer => {
- writer.removeAttribute( 'theme', modelParagraph );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p><div class="nice"></div></div>' );
- } );
- it( 'should be possible to override setAttribute', () => {
- const modelElement = new ModelElement( 'paragraph', { classes: 'foo' }, new ModelText( 'foobar' ) );
- dispatcher.on( 'attribute:class', ( evt, data, conversionApi ) => {
- conversionApi.consumable.consume( data.item, 'attribute:class' );
- }, { converterPriority: 'high' } );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- // No attribute set.
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- it( 'should not convert or consume if element creator returned null', () => {
- const callback = sinon.stub().returns( null );
- dispatcher.on( 'attribute:class', changeAttribute( callback ) );
- const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );
- sinon.assert.called( callback );
- } );
- } );
- describe( 'wrap', () => {
- it( 'should convert insert/change/remove of attribute in model into wrapping element in a view', () => {
- const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { bold: true } ) );
- const creator = ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'b' );
- dispatcher.on( 'attribute:bold', wrap( creator ) );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p><b>foobar</b></p></div>' );
- model.change( writer => {
- writer.removeAttribute( 'bold', writer.createRangeIn( modelElement ) );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- it( 'should convert insert/remove of attribute in model with wrapping element generating function as a parameter', () => {
- const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { style: 'bold' } ) );
- const elementGenerator = ( modelAttributeValue, viewWriter ) => {
- if ( modelAttributeValue == 'bold' ) {
- return viewWriter.createAttributeElement( 'b' );
- }
- };
- dispatcher.on( 'attribute:style', wrap( elementGenerator ) );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p><b>foobar</b></p></div>' );
- model.change( writer => {
- writer.removeAttribute( 'style', writer.createRangeIn( modelElement ) );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- it( 'should update range on re-wrapping attribute (#475)', () => {
- const modelElement = new ModelElement( 'paragraph', null, [
- new ModelText( 'x' ),
- new ModelText( 'foo', { link: 'http://foo.com' } ),
- new ModelText( 'x' )
- ] );
- const elementGenerator = ( modelAttributeValue, viewWriter ) => {
- return viewWriter.createAttributeElement( 'a', { href: modelAttributeValue } );
- };
- dispatcher.on( 'attribute:link', wrap( elementGenerator ) );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>x<a href="http://foo.com">foo</a>x</p></div>' );
- // Set new attribute on old link but also on non-linked characters.
- model.change( writer => {
- writer.setAttribute( 'link', 'http://foobar.com', writer.createRangeIn( modelElement ) );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p><a href="http://foobar.com">xfoox</a></p></div>' );
- } );
- it( 'should support unicode', () => {
- const modelElement = new ModelElement( 'paragraph', null, [ 'நி', new ModelText( 'லைக்', { bold: true } ), 'கு' ] );
- const creator = ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'b' );
- dispatcher.on( 'attribute:bold', wrap( creator ) );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>நி<b>லைக்</b>கு</p></div>' );
- model.change( writer => {
- writer.removeAttribute( 'bold', writer.createRangeIn( modelElement ) );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>நிலைக்கு</p></div>' );
- } );
- it( 'should be possible to override wrap', () => {
- const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { bold: true } ) );
- dispatcher.on( 'attribute:bold', wrap( ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'b' ) ) );
- dispatcher.on(
- 'attribute:bold',
- wrap( ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'strong' ) ),
- { priority: 'high' }
- );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p><strong>foobar</strong></p></div>' );
- } );
- it( 'should not convert and not consume if creator function returned null', () => {
- const elementGenerator = () => null;
- sinon.spy( dispatcher, 'fire' );
- const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { italic: true } ) );
- dispatcher.on( 'attribute:italic', wrap( elementGenerator ) );
- const spy = sinon.spy();
- dispatcher.on( 'attribute:italic', spy );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- expect( dispatcher.fire.calledWith( 'attribute:italic:$text' ) ).to.be.true;
- expect( spy.called ).to.be.true;
- } );
- } );
- describe( 'insertUIElement/removeUIElement', () => {
- let modelText, modelElement, range;
- beforeEach( () => {
- modelText = new ModelText( 'foobar' );
- modelElement = new ModelElement( 'paragraph', null, modelText );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- } );
- describe( 'collapsed range', () => {
- beforeEach( () => {
- range = model.createRange( model.createPositionAt( modelElement, 3 ), model.createPositionAt( modelElement, 3 ) );
- } );
- it( 'should insert and remove ui element', () => {
- const creator = ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } );
- dispatcher.on( 'addMarker:marker', insertUIElement( creator ) );
- dispatcher.on( 'removeMarker:marker', removeUIElement( creator ) );
- model.change( writer => {
- writer.addMarker( 'marker', { range, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo<span class="marker"></span>bar</p></div>' );
- model.change( writer => {
- writer.removeMarker( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- it( 'should not convert if consumable was consumed', () => {
- sinon.spy( dispatcher, 'fire' );
- dispatcher.on( 'addMarker:marker', insertUIElement(
- ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } ) )
- );
- dispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
- conversionApi.consumable.consume( data.markerRange, 'addMarker:marker' );
- }, { priority: 'high' } );
- model.change( writer => {
- writer.addMarker( 'marker', { range, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- expect( dispatcher.fire.calledWith( 'addMarker:marker' ) );
- } );
- it( 'should not convert if creator returned null', () => {
- dispatcher.on( 'addMarker:marker', insertUIElement( () => null ) );
- dispatcher.on( 'removeMarker:marker', removeUIElement( () => null ) );
- model.change( writer => {
- writer.addMarker( 'marker', { range, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- model.change( writer => {
- writer.removeMarker( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- } );
- describe( 'non-collapsed range', () => {
- beforeEach( () => {
- range = model.createRange( model.createPositionAt( modelElement, 2 ), model.createPositionAt( modelElement, 5 ) );
- } );
- it( 'should insert and remove ui element - element as a creator', () => {
- const creator = ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } );
- dispatcher.on( 'addMarker:marker', insertUIElement( creator ) );
- dispatcher.on( 'removeMarker:marker', removeUIElement( creator ) );
- model.change( writer => {
- writer.addMarker( 'marker', { range, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) )
- .to.equal( '<div><p>fo<span class="marker"></span>oba<span class="marker"></span>r</p></div>' );
- model.change( writer => {
- writer.removeMarker( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- it( 'should insert and remove ui element - function as a creator', () => {
- const creator = ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': data.markerName } );
- dispatcher.on( 'addMarker:marker', insertUIElement( creator ) );
- dispatcher.on( 'removeMarker:marker', removeUIElement( creator ) );
- model.change( writer => {
- writer.addMarker( 'marker', { range, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) )
- .to.equal( '<div><p>fo<span class="marker"></span>oba<span class="marker"></span>r</p></div>' );
- model.change( writer => {
- writer.removeMarker( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- it( 'should insert and remove different opening and ending element', () => {
- function creator( data, viewWriter ) {
- if ( data.isOpening ) {
- return viewWriter.createUIElement( 'span', { 'class': data.markerName, 'data-start': true } );
- }
- return viewWriter.createUIElement( 'span', { 'class': data.markerName, 'data-end': true } );
- }
- dispatcher.on( 'addMarker:marker', insertUIElement( creator ) );
- dispatcher.on( 'removeMarker:marker', removeUIElement( creator ) );
- model.change( writer => {
- writer.addMarker( 'marker', { range, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal(
- '<div><p>fo<span class="marker" data-start="true"></span>oba<span class="marker" data-end="true"></span>r</p></div>'
- );
- model.change( writer => {
- writer.removeMarker( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- it( 'should not convert if consumable was consumed', () => {
- const creator = ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } );
- sinon.spy( dispatcher, 'fire' );
- dispatcher.on( 'addMarker:marker', insertUIElement( creator ) );
- dispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
- conversionApi.consumable.consume( data.item, 'addMarker:marker' );
- }, { priority: 'high' } );
- model.change( writer => {
- writer.addMarker( 'marker', { range, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- expect( dispatcher.fire.calledWith( 'addMarker:marker' ) );
- } );
- } );
- } );
- // Remove converter is by default already added in `EditingController` instance.
- describe( 'remove', () => {
- it( 'should remove items from view accordingly to changes in model #1', () => {
- const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- model.change( writer => {
- writer.remove(
- writer.createRange( writer.createPositionAt( modelElement, 2 ), writer.createPositionAt( modelElement, 4 ) )
- );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foar</p></div>' );
- } );
- it( 'should be possible to overwrite', () => {
- dispatcher.on( 'remove', evt => evt.stop(), { priority: 'high' } );
- const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- model.change( writer => {
- writer.remove(
- writer.createRange( writer.createPositionAt( modelElement, 2 ), writer.createPositionAt( modelElement, 4 ) )
- );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
- } );
- it( 'should support unicode', () => {
- const modelElement = new ModelElement( 'paragraph', null, 'நிலைக்கு' );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- model.change( writer => {
- writer.remove(
- writer.createRange( writer.createPositionAt( modelElement, 0 ), writer.createPositionAt( modelElement, 6 ) )
- );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>கு</p></div>' );
- } );
- it( 'should not remove view ui elements that are placed next to removed content', () => {
- modelRoot._appendChild( new ModelText( 'fozbar' ) );
- viewRoot._appendChild( [
- new ViewText( 'foz' ),
- new ViewUIElement( 'span' ),
- new ViewText( 'bar' )
- ] );
- // Remove 'b'.
- model.change( writer => {
- writer.remove(
- writer.createRange( writer.createPositionAt( modelRoot, 3 ), writer.createPositionAt( modelRoot, 4 ) )
- );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div>foz<span></span>ar</div>' );
- // Remove 'z'.
- model.change( writer => {
- writer.remove(
- writer.createRange( writer.createPositionAt( modelRoot, 2 ), writer.createPositionAt( modelRoot, 3 ) )
- );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div>fo<span></span>ar</div>' );
- } );
- it( 'should remove correct amount of text when it is split by view ui element', () => {
- modelRoot._appendChild( new ModelText( 'fozbar' ) );
- viewRoot._appendChild( [
- new ViewText( 'foz' ),
- new ViewUIElement( 'span' ),
- new ViewText( 'bar' )
- ] );
- // Remove 'z<span></span>b'.
- model.change( writer => {
- writer.remove(
- writer.createRange( writer.createPositionAt( modelRoot, 2 ), writer.createPositionAt( modelRoot, 4 ) )
- );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div>foar</div>' );
- } );
- it( 'should unbind elements', () => {
- const modelElement = new ModelElement( 'paragraph' );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- const viewElement = controller.mapper.toViewElement( modelElement );
- expect( viewElement ).not.to.be.undefined;
- expect( controller.mapper.toModelElement( viewElement ) ).to.equal( modelElement );
- model.change( writer => {
- writer.remove( modelElement );
- } );
- expect( controller.mapper.toViewElement( modelElement ) ).to.be.undefined;
- expect( controller.mapper.toModelElement( viewElement ) ).to.be.undefined;
- } );
- it( 'should not break when remove() is used as part of unwrapping', () => {
- const modelP = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
- const modelWidget = new ModelElement( 'widget', null, modelP );
- dispatcher.on( 'insert:widget', insertElement(
- ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'widget' ) )
- );
- model.change( writer => {
- writer.insert( modelWidget, modelRootStart );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><widget><p>foo</p></widget></div>' );
- const viewP = controller.mapper.toViewElement( modelP );
- expect( viewP ).not.to.be.undefined;
- model.change( writer => {
- writer.unwrap( modelWidget );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p></div>' );
- // `modelP` is now bound with newly created view element.
- expect( controller.mapper.toViewElement( modelP ) ).not.to.equal( viewP );
- // `viewP` is no longer bound with model element.
- expect( controller.mapper.toModelElement( viewP ) ).to.be.undefined;
- // View element from view root is bound to `modelP`.
- expect( controller.mapper.toModelElement( viewRoot.getChild( 0 ) ) ).to.equal( modelP );
- } );
- it( 'should work correctly if container element after ui element is removed', () => {
- // Prepare a model and view structure.
- // This is done outside of conversion to put view ui elements inside easily.
- const modelP1 = new ModelElement( 'paragraph' );
- const modelP2 = new ModelElement( 'paragraph' );
- const viewP1 = new ViewContainerElement( 'p' );
- const viewUi1 = new ViewUIElement( 'span' );
- const viewUi2 = new ViewUIElement( 'span' );
- const viewP2 = new ViewContainerElement( 'p' );
- modelRoot._appendChild( [ modelP1, modelP2 ] );
- viewRoot._appendChild( [ viewP1, viewUi1, viewUi2, viewP2 ] );
- controller.mapper.bindElements( modelP1, viewP1 );
- controller.mapper.bindElements( modelP2, viewP2 );
- // Remove second paragraph element.
- model.change( writer => {
- writer.remove( writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) ) );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p></p><span></span><span></span></div>' );
- } );
- it( 'should work correctly if container element after text node is removed', () => {
- const modelText = new ModelText( 'foo' );
- const modelP = new ModelElement( 'paragraph' );
- model.change( writer => {
- writer.insert( [ modelText, modelP ], modelRootStart );
- } );
- model.change( writer => {
- writer.remove( modelP );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div>foo</div>' );
- } );
- } );
- describe( 'highlight', () => {
- describe( 'on text', () => {
- const highlightDescriptor = {
- classes: 'highlight-class',
- priority: 7,
- attributes: { title: 'title' }
- };
- let markerRange;
- beforeEach( () => {
- const modelElement1 = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
- const modelElement2 = new ModelElement( 'paragraph', null, new ModelText( 'bar' ) );
- model.change( writer => {
- writer.insert( [ modelElement1, modelElement2 ], modelRootStart );
- } );
- markerRange = model.createRangeIn( modelRoot );
- } );
- it( 'should wrap and unwrap text nodes', () => {
- dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
- dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
- dispatcher.on( 'removeMarker:marker', removeHighlight( highlightDescriptor ) );
- model.change( writer => {
- writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal(
- '<div>' +
- '<p>' +
- '<span class="highlight-class" title="title">foo</span>' +
- '</p>' +
- '<p>' +
- '<span class="highlight-class" title="title">bar</span>' +
- '</p>' +
- '</div>'
- );
- model.change( writer => {
- writer.removeMarker( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
- } );
- it( 'should be possible to overwrite', () => {
- dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
- dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
- dispatcher.on( 'removeMarker:marker', removeHighlight( highlightDescriptor ) );
- const newDescriptor = { classes: 'override-class' };
- dispatcher.on( 'addMarker:marker', highlightText( newDescriptor ), { priority: 'high' } );
- dispatcher.on( 'addMarker:marker', highlightElement( newDescriptor ), { priority: 'high' } );
- dispatcher.on( 'removeMarker:marker', removeHighlight( newDescriptor ), { priority: 'high' } );
- model.change( writer => {
- writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal(
- '<div>' +
- '<p>' +
- '<span class="override-class">foo</span>' +
- '</p>' +
- '<p>' +
- '<span class="override-class">bar</span>' +
- '</p>' +
- '</div>'
- );
- model.change( writer => {
- writer.removeMarker( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
- } );
- it( 'should do nothing if descriptor is not provided or generating function returns null', () => {
- dispatcher.on( 'addMarker:marker', highlightText( () => null ), { priority: 'high' } );
- dispatcher.on( 'addMarker:marker', highlightElement( () => null ), { priority: 'high' } );
- dispatcher.on( 'removeMarker:marker', removeHighlight( () => null ), { priority: 'high' } );
- model.change( writer => {
- writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
- model.change( writer => {
- writer.removeMarker( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
- } );
- it( 'should do nothing if collapsed marker is converted', () => {
- const descriptor = { classes: 'foo' };
- dispatcher.on( 'addMarker:marker', highlightText( descriptor ), { priority: 'high' } );
- dispatcher.on( 'addMarker:marker', highlightElement( descriptor ), { priority: 'high' } );
- dispatcher.on( 'removeMarker:marker', removeHighlight( descriptor ), { priority: 'high' } );
- markerRange = model.createRange( model.createPositionAt( modelRoot, 0 ), model.createPositionAt( modelRoot, 0 ) );
- model.change( writer => {
- writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
- model.change( () => {
- model.markers._remove( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
- } );
- it( 'should correctly wrap and unwrap multiple, intersecting markers', () => {
- const descriptorFoo = { classes: 'foo' };
- const descriptorBar = { classes: 'bar' };
- const descriptorXyz = { classes: 'xyz' };
- dispatcher.on( 'addMarker:markerFoo', highlightText( descriptorFoo ) );
- dispatcher.on( 'addMarker:markerBar', highlightText( descriptorBar ) );
- dispatcher.on( 'addMarker:markerXyz', highlightText( descriptorXyz ) );
- dispatcher.on( 'removeMarker:markerFoo', removeHighlight( descriptorFoo ) );
- dispatcher.on( 'removeMarker:markerBar', removeHighlight( descriptorBar ) );
- dispatcher.on( 'removeMarker:markerXyz', removeHighlight( descriptorXyz ) );
- const p1 = modelRoot.getChild( 0 );
- const p2 = modelRoot.getChild( 1 );
- model.change( writer => {
- const range = writer.createRange( writer.createPositionAt( p1, 0 ), writer.createPositionAt( p1, 3 ) );
- writer.addMarker( 'markerFoo', { range, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal(
- '<div>' +
- '<p>' +
- '<span class="foo">foo</span>' +
- '</p>' +
- '<p>bar</p>' +
- '</div>'
- );
- model.change( writer => {
- const range = writer.createRange( writer.createPositionAt( p1, 1 ), writer.createPositionAt( p2, 2 ) );
- writer.addMarker( 'markerBar', { range, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal(
- '<div>' +
- '<p>' +
- '<span class="foo">f</span>' +
- '<span class="bar">' +
- '<span class="foo">oo</span>' +
- '</span>' +
- '</p>' +
- '<p>' +
- '<span class="bar">ba</span>' +
- 'r' +
- '</p>' +
- '</div>'
- );
- model.change( writer => {
- const range = writer.createRange( writer.createPositionAt( p1, 2 ), writer.createPositionAt( p2, 3 ) );
- writer.addMarker( 'markerXyz', { range, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal(
- '<div>' +
- '<p>' +
- '<span class="foo">f</span>' +
- '<span class="bar">' +
- '<span class="foo">' +
- 'o' +
- '<span class="xyz">o</span>' +
- '</span>' +
- '</span>' +
- '</p>' +
- '<p>' +
- '<span class="bar">' +
- '<span class="xyz">ba</span>' +
- '</span>' +
- '<span class="xyz">r</span>' +
- '</p>' +
- '</div>'
- );
- model.change( writer => {
- writer.removeMarker( 'markerBar' );
- } );
- expect( viewToString( viewRoot ) ).to.equal(
- '<div>' +
- '<p>' +
- '<span class="foo">' +
- 'fo' +
- '<span class="xyz">o</span>' +
- '</span>' +
- '</p>' +
- '<p>' +
- '<span class="xyz">bar</span>' +
- '</p>' +
- '</div>'
- );
- model.change( writer => {
- writer.removeMarker( 'markerFoo' );
- } );
- expect( viewToString( viewRoot ) ).to.equal(
- '<div>' +
- '<p>' +
- 'fo' +
- '<span class="xyz">o</span>' +
- '</p>' +
- '<p>' +
- '<span class="xyz">bar</span>' +
- '</p>' +
- '</div>'
- );
- model.change( writer => {
- writer.removeMarker( 'markerXyz' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
- } );
- it( 'should do nothing if marker is applied and removed on empty-ish range', () => {
- dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
- dispatcher.on( 'removeMarker:marker', removeHighlight( highlightDescriptor ) );
- const p1 = modelRoot.getChild( 0 );
- const p2 = modelRoot.getChild( 1 );
- const markerRange = model.createRange( model.createPositionAt( p1, 3 ), model.createPositionAt( p2, 0 ) );
- model.change( writer => {
- writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
- model.change( writer => {
- writer.removeMarker( 'marker', { range: markerRange, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
- } );
- } );
- describe( 'on element', () => {
- const highlightDescriptor = {
- classes: 'highlight-class',
- priority: 7,
- attributes: { title: 'title' },
- id: 'customId'
- };
- let markerRange;
- beforeEach( () => {
- // Provide converter for div element. View div element will have custom highlight handling.
- dispatcher.on( 'insert:div', insertElement( () => {
- const viewContainer = new ViewContainerElement( 'div' );
- viewContainer._setCustomProperty( 'addHighlight', ( element, descriptor, writer ) => {
- writer.addClass( descriptor.classes, element );
- } );
- viewContainer._setCustomProperty( 'removeHighlight', ( element, id, writer ) => {
- writer.setAttribute( 'class', '', element );
- } );
- return viewContainer;
- } ) );
- const modelElement = new ModelElement( 'div', null, new ModelText( 'foo' ) );
- model.change( writer => {
- writer.insert( modelElement, modelRootStart );
- } );
- markerRange = model.createRangeOn( modelElement );
- dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
- dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
- dispatcher.on( 'removeMarker:marker', removeHighlight( highlightDescriptor ) );
- } );
- it( 'should use addHighlight and removeHighlight on elements and not convert children nodes', () => {
- model.change( writer => {
- writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal(
- '<div>' +
- '<div class="highlight-class">' +
- 'foo' +
- '</div>' +
- '</div>'
- );
- model.change( writer => {
- writer.removeMarker( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
- } );
- it( 'should be possible to override', () => {
- const newDescriptor = { classes: 'override-class' };
- dispatcher.on( 'addMarker:marker', highlightText( newDescriptor ), { priority: 'high' } );
- dispatcher.on( 'addMarker:marker', highlightElement( newDescriptor ), { priority: 'high' } );
- dispatcher.on( 'removeMarker:marker', removeHighlight( newDescriptor ), { priority: 'high' } );
- model.change( writer => {
- writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal(
- '<div>' +
- '<div class="override-class">' +
- 'foo' +
- '</div>' +
- '</div>'
- );
- model.change( writer => {
- writer.removeMarker( 'marker' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
- } );
- it( 'should use default priority and id if not provided', () => {
- const viewDiv = viewRoot.getChild( 0 );
- dispatcher.on( 'addMarker:marker2', highlightText( () => null ) );
- dispatcher.on( 'addMarker:marker2', highlightElement( () => null ) );
- dispatcher.on( 'removeMarker:marker2', removeHighlight( () => null ) );
- viewDiv._setCustomProperty( 'addHighlight', ( element, descriptor ) => {
- expect( descriptor.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
- expect( descriptor.id ).to.equal( 'marker:foo-bar-baz' );
- } );
- viewDiv._setCustomProperty( 'removeHighlight', ( element, id ) => {
- expect( id ).to.equal( 'marker:foo-bar-baz' );
- } );
- model.change( writer => {
- writer.addMarker( 'marker2', { range: markerRange, usingOperation: false } );
- } );
- } );
- it( 'should do nothing if descriptor is not provided', () => {
- dispatcher.on( 'addMarker:marker2', highlightText( () => null ) );
- dispatcher.on( 'addMarker:marker2', highlightElement( () => null ) );
- dispatcher.on( 'removeMarker:marker2', removeHighlight( () => null ) );
- model.change( writer => {
- writer.addMarker( 'marker2', { range: markerRange, usingOperation: false } );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
- model.change( writer => {
- writer.removeMarker( 'marker2' );
- } );
- expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
- } );
- } );
- } );
- describe( 'createViewElementFromHighlightDescriptor()', () => {
- it( 'should return attribute element from descriptor object', () => {
- const descriptor = {
- classes: 'foo-class',
- attributes: { one: '1', two: '2' },
- priority: 7
- };
- const element = createViewElementFromHighlightDescriptor( descriptor );
- expect( element.is( 'attributeElement' ) ).to.be.true;
- expect( element.name ).to.equal( 'span' );
- expect( element.priority ).to.equal( 7 );
- expect( element.hasClass( 'foo-class' ) ).to.be.true;
- for ( const key of Object.keys( descriptor.attributes ) ) {
- expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
- }
- } );
- it( 'should return attribute element from descriptor object - array with classes', () => {
- const descriptor = {
- classes: [ 'foo-class', 'bar-class' ],
- attributes: { one: '1', two: '2' },
- priority: 7
- };
- const element = createViewElementFromHighlightDescriptor( descriptor );
- expect( element.is( 'attributeElement' ) ).to.be.true;
- expect( element.name ).to.equal( 'span' );
- expect( element.priority ).to.equal( 7 );
- expect( element.hasClass( 'foo-class' ) ).to.be.true;
- expect( element.hasClass( 'bar-class' ) ).to.be.true;
- for ( const key of Object.keys( descriptor.attributes ) ) {
- expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
- }
- } );
- it( 'should create element without class', () => {
- const descriptor = {
- attributes: { one: '1', two: '2' },
- priority: 7
- };
- const element = createViewElementFromHighlightDescriptor( descriptor );
- expect( element.is( 'attributeElement' ) ).to.be.true;
- expect( element.name ).to.equal( 'span' );
- expect( element.priority ).to.equal( 7 );
- for ( const key of Object.keys( descriptor.attributes ) ) {
- expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
- }
- } );
- it( 'should create element without priority', () => {
- const descriptor = {
- classes: 'foo-class',
- attributes: { one: '1', two: '2' }
- };
- const element = createViewElementFromHighlightDescriptor( descriptor );
- expect( element.is( 'attributeElement' ) ).to.be.true;
- expect( element.name ).to.equal( 'span' );
- expect( element.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
- expect( element.hasClass( 'foo-class' ) ).to.be.true;
- for ( const key of Object.keys( descriptor.attributes ) ) {
- expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
- }
- } );
- it( 'should create element without attributes', () => {
- const descriptor = {
- classes: 'foo-class',
- priority: 7
- };
- const element = createViewElementFromHighlightDescriptor( descriptor );
- expect( element.is( 'attributeElement' ) ).to.be.true;
- expect( element.name ).to.equal( 'span' );
- expect( element.priority ).to.equal( 7 );
- expect( element.hasClass( 'foo-class' ) ).to.be.true;
- } );
- } );
- } );
|