@@ -10,12 +10,15 @@ import UnlinkCommand from '../src/unlinkcommand';
1010import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor' ;
1111import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph' ;
1212import Enter from '@ckeditor/ckeditor5-enter/src/enter' ;
13+ import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range' ;
1314import { getData as getModelData , setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model' ;
1415import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view' ;
1516import { isLinkElement } from '../src/utils' ;
1617import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard' ;
17-
18- import { downcastAttributeToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters' ;
18+ import {
19+ downcastMarkerToHighlight ,
20+ downcastAttributeToElement
21+ } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters' ;
1922
2023/* global document */
2124
@@ -146,27 +149,15 @@ describe( 'LinkEditing', () => {
146149 } ) ;
147150 } ) ;
148151
149- describe ( 'highlight link boundaries' , ( ) => {
150- it ( 'should create marker in model when selection is inside a link' , ( ) => {
151- expect ( model . markers . has ( 'linkBoundaries' ) ) . to . be . false ;
152-
153- setModelData ( model ,
154- '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
155- ) ;
156-
157- expect ( model . markers . has ( 'linkBoundaries' ) ) . to . be . true ;
158- const marker = model . markers . get ( 'linkBoundaries' ) ;
159- expect ( marker . getStart ( ) . path ) . to . deep . equal ( [ 0 , 4 ] ) ;
160- expect ( marker . getEnd ( ) . path ) . to . deep . equal ( [ 0 , 7 ] ) ;
161- } ) ;
162-
163- it ( 'should convert link boundaries marker to proper view' , ( ) => {
152+ describe ( 'link highlighting' , ( ) => {
153+ it ( 'should convert the highlight to a proper view classes' , ( ) => {
164154 setModelData ( model ,
165155 '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
166156 ) ;
167157
158+ expect ( model . document . selection . hasAttribute ( 'linkHref' ) ) . to . be . true ;
168159 expect ( getViewData ( view ) ) . to . equal (
169- '<p>foo <span class="ck ck-link_selected"><a href="url">b{}ar</a></span > baz</p>'
160+ '<p>foo <a class="ck ck-link_selected" href="url">b{}ar</a> baz</p>'
170161 ) ;
171162 } ) ;
172163
@@ -182,13 +173,8 @@ describe( 'LinkEditing', () => {
182173 } ) ;
183174
184175 expect ( model . document . selection . hasAttribute ( 'linkHref' ) ) . to . be . true ;
185- expect ( model . markers . has ( 'linkBoundaries' ) ) . to . be . true ;
186- const marker = model . markers . get ( 'linkBoundaries' ) ;
187- expect ( marker . getStart ( ) . path ) . to . deep . equal ( [ 0 , 4 ] ) ;
188- expect ( marker . getEnd ( ) . path ) . to . deep . equal ( [ 0 , 7 ] ) ;
189-
190176 expect ( getViewData ( view ) ) . to . equal (
191- '<p>foo <span class="ck ck-link_selected"><a href="url">{}bar</a></span > baz</p>'
177+ '<p>foo <a class="ck ck-link_selected" href="url">{}bar</a> baz</p>'
192178 ) ;
193179 } ) ;
194180
@@ -198,13 +184,8 @@ describe( 'LinkEditing', () => {
198184 ) ;
199185
200186 expect ( model . document . selection . hasAttribute ( 'linkHref' ) ) . to . be . true ;
201- expect ( model . markers . has ( 'linkBoundaries' ) ) . to . be . true ;
202- const marker = model . markers . get ( 'linkBoundaries' ) ;
203- expect ( marker . getStart ( ) . path ) . to . deep . equal ( [ 0 , 4 ] ) ;
204- expect ( marker . getEnd ( ) . path ) . to . deep . equal ( [ 0 , 7 ] ) ;
205-
206187 expect ( getViewData ( view ) ) . to . equal (
207- '<p>foo <span class="ck ck-link_selected"><a href="url">bar{}</a></span > baz</p>'
188+ '<p>foo <a class="ck ck-link_selected" href="url">bar{}</a> baz</p>'
208189 ) ;
209190 } ) ;
210191
@@ -221,25 +202,19 @@ describe( 'LinkEditing', () => {
221202 ) ;
222203
223204 expect ( model . document . selection . hasAttribute ( 'linkHref' ) ) . to . be . true ;
224- expect ( model . markers . has ( 'linkBoundaries' ) ) . to . be . true ;
225- const marker = model . markers . get ( 'linkBoundaries' ) ;
226- expect ( marker . getStart ( ) . path ) . to . deep . equal ( [ 1 , 0 ] ) ;
227- expect ( marker . getEnd ( ) . path ) . to . deep . equal ( [ 1 , 2 ] ) ;
228205 } ) ;
229206
230- it ( 'should remove marker when selection is moved out from the link' , ( ) => {
207+ it ( 'should remove classes when selection is moved out from the link' , ( ) => {
231208 setModelData ( model ,
232209 '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
233210 ) ;
234211
235212 expect ( getViewData ( view ) ) . to . equal (
236- '<p>foo <span class="ck ck-link_selected"><a href="url">li{}nk</a></span > baz</p>'
213+ '<p>foo <a class="ck ck-link_selected" href="url">li{}nk</a> baz</p>'
237214 ) ;
238215
239- expect ( model . markers . has ( 'linkBoundaries' ) ) . to . be . true ;
240216 model . change ( writer => writer . setSelection ( model . document . getRoot ( ) . getChild ( 0 ) , 0 ) ) ;
241217
242- expect ( model . markers . has ( 'linkBoundaries' ) ) . to . be . false ;
243218 expect ( getViewData ( view ) ) . to . equal (
244219 '<p>{}foo <a href="url">link</a> baz</p>'
245220 ) ;
@@ -251,16 +226,106 @@ describe( 'LinkEditing', () => {
251226 ) ;
252227
253228 expect ( getViewData ( view ) ) . to . equal (
254- '<p>foo <span class="ck ck-link_selected"><a href="url">li{}nk</a></span > baz</p>'
229+ '<p>foo <a class="ck ck-link_selected" href="url">li{}nk</a> baz</p>'
255230 ) ;
256231
257- expect ( model . markers . has ( 'linkBoundaries' ) ) . to . be . true ;
258232 model . change ( writer => writer . setSelection ( model . document . getRoot ( ) . getChild ( 0 ) , 5 ) ) ;
259233
260- expect ( model . markers . has ( 'linkBoundaries' ) ) . to . be . true ;
261234 expect ( getViewData ( view ) ) . to . equal (
262- '<p>foo <span class="ck ck-link_selected"><a href="url">l{}ink</a></span > baz</p>'
235+ '<p>foo <a class="ck ck-link_selected" href="url">l{}ink</a> baz</p>'
263236 ) ;
264237 } ) ;
238+
239+ describe ( 'downcast conversion integration' , ( ) => {
240+ it ( 'works for the #insert event' , ( ) => {
241+ setModelData ( model ,
242+ '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
243+ ) ;
244+
245+ model . change ( writer => {
246+ writer . insertText ( 'FOO' , { linkHref : 'url' } , model . document . selection . getFirstPosition ( ) ) ;
247+ } ) ;
248+
249+ expect ( getViewData ( view ) ) . to . equal (
250+ '<p>foo <a class="ck ck-link_selected" href="url">liFOO{}nk</a> baz</p>'
251+ ) ;
252+ } ) ;
253+
254+ it ( 'works for the #remove event' , ( ) => {
255+ setModelData ( model ,
256+ '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
257+ ) ;
258+
259+ model . change ( writer => {
260+ writer . remove ( ModelRange . createFromParentsAndOffsets (
261+ model . document . getRoot ( ) . getChild ( 0 ) , 0 ,
262+ model . document . getRoot ( ) . getChild ( 0 ) , 5 )
263+ ) ;
264+ } ) ;
265+
266+ expect ( getViewData ( view ) ) . to . equal (
267+ '<p><a class="ck ck-link_selected" href="url">i{}nk</a> baz</p>'
268+ ) ;
269+ } ) ;
270+
271+ it ( 'works for the #attribute event' , ( ) => {
272+ setModelData ( model ,
273+ '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
274+ ) ;
275+
276+ model . change ( writer => {
277+ writer . setAttribute ( 'linkHref' , 'new-url' , new ModelRange (
278+ model . document . selection . getFirstPosition ( ) . getShiftedBy ( - 1 ) ,
279+ model . document . selection . getFirstPosition ( ) . getShiftedBy ( 1 ) )
280+ ) ;
281+ } ) ;
282+
283+ expect ( getViewData ( view ) ) . to . equal (
284+ '<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>'
285+ ) ;
286+ } ) ;
287+
288+ it ( 'works for the #selection event' , ( ) => {
289+ setModelData ( model ,
290+ '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
291+ ) ;
292+
293+ model . change ( writer => {
294+ writer . setSelection ( new ModelRange (
295+ model . document . selection . getFirstPosition ( ) . getShiftedBy ( - 1 ) ,
296+ model . document . selection . getFirstPosition ( ) . getShiftedBy ( 1 ) )
297+ ) ;
298+ } ) ;
299+
300+ expect ( getViewData ( view ) ) . to . equal (
301+ '<p>foo <a class="ck ck-link_selected" href="url">l{in}k</a> baz</p>'
302+ ) ;
303+ } ) ;
304+
305+ it ( 'works for the #addMarker and #removeMarker events' , ( ) => {
306+ downcastMarkerToHighlight ( { model : 'fooMarker' , view : { } } ) ( editor . editing . downcastDispatcher ) ;
307+
308+ setModelData ( model ,
309+ '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
310+ ) ;
311+
312+ model . change ( writer => {
313+ writer . setMarker ( 'fooMarker' , ModelRange . createFromParentsAndOffsets (
314+ model . document . getRoot ( ) . getChild ( 0 ) , 0 ,
315+ model . document . getRoot ( ) . getChild ( 0 ) , 5 )
316+ ) ;
317+ } ) ;
318+
319+ expect ( getViewData ( view ) ) . to . equal (
320+ '<p><span>foo </span><a class="ck ck-link_selected" href="url"><span>l</span>i{}nk</a> baz</p>'
321+ ) ;
322+
323+ model . change ( writer => writer . removeMarker ( 'fooMarker' ) ) ;
324+
325+ expect ( getViewData ( view ) ) . to . equal (
326+ '<p>foo <a class="ck ck-link_selected" href="url">li{}nk</a> baz</p>'
327+ ) ;
328+ } ) ;
329+ } ) ;
265330 } ) ;
266331} ) ;
0 commit comments