8
0

linkcommand.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from 'tests/core/_utils/modeltesteditor.js';
  6. import LinkCommand from 'ckeditor5/link/linkcommand.js';
  7. import { setData, getData } from 'ckeditor5/engine/dev-utils/model.js';
  8. describe( 'LinkCommand', () => {
  9. let editor, document, command;
  10. beforeEach( () => {
  11. return ModelTestEditor.create()
  12. .then( newEditor => {
  13. editor = newEditor;
  14. document = editor.document;
  15. command = new LinkCommand( editor );
  16. // Allow text in $root.
  17. document.schema.allow( { name: '$text', inside: '$root' } );
  18. // Allow text with `linkHref` attribute in paragraph.
  19. document.schema.registerItem( 'p', '$block' );
  20. document.schema.allow( { name: '$text', attributes: 'linkHref', inside: '$root' } );
  21. document.schema.allow( { name: '$text', attributes: 'linkHref', inside: 'p' } );
  22. } );
  23. } );
  24. afterEach( () => {
  25. command.destroy();
  26. } );
  27. describe( 'value', () => {
  28. describe( 'collapsed selection', () => {
  29. it( 'should be equal attribute value when selection is placed inside element with `linkHref` attribute', () => {
  30. setData( document, `<$text linkHref="url">foo[]bar</$text>` );
  31. expect( command.value ).to.equal( 'url' );
  32. } );
  33. it( 'should be undefined when selection is placed inside element without `linkHref` attribute', () => {
  34. setData( document, `<$text bold="true">foo[]bar</$text>` );
  35. expect( command.value ).to.undefined;
  36. } );
  37. } );
  38. describe( 'non-collapsed selection', () => {
  39. it( 'should be equal attribute value when selection contains only elements with `linkHref` attribute', () => {
  40. setData( document, 'fo[<$text linkHref="url">ob</$text>]ar' );
  41. expect( command.value ).to.equal( 'url' );
  42. } );
  43. it( 'should be undefined when selection contains not only elements with `linkHref` attribute', () => {
  44. setData( document, 'f[o<$text linkHref="url">ob</$text>]ar' );
  45. expect( command.value ).to.undefined;
  46. } );
  47. } );
  48. } );
  49. describe( '_doExecute', () => {
  50. describe( 'non-collapsed selection', () => {
  51. it( 'should set `linkHref` attribute to selected text', () => {
  52. setData( document, 'f[ooba]r' );
  53. expect( command.value ).to.undefined;
  54. command._doExecute( 'url' );
  55. expect( getData( document ) ).to.equal( 'f[<$text linkHref="url">ooba</$text>]r' );
  56. expect( command.value ).to.equal( 'url' );
  57. } );
  58. it( 'should set `linkHref` attribute to selected text when text already has attributes', () => {
  59. setData( document, 'f[o<$text bold="true">oba]r</$text>' );
  60. expect( command.value ).to.undefined;
  61. command._doExecute( 'url' );
  62. expect( command.value ).to.equal( 'url' );
  63. expect( getData( document ) ).to.equal(
  64. 'f[<$text linkHref="url">o</$text>' +
  65. '<$text bold="true" linkHref="url">oba</$text>]' +
  66. '<$text bold="true">r</$text>'
  67. );
  68. } );
  69. it( 'should overwrite existing `linkHref` attribute when selected text wraps text with `linkHref` attribute', () => {
  70. setData( document, 'f[o<$text linkHref="other url">o</$text>ba]r' );
  71. expect( command.value ).to.undefined;
  72. command._doExecute( 'url' );
  73. expect( getData( document ) ).to.equal( 'f[<$text linkHref="url">ooba</$text>]r' );
  74. expect( command.value ).to.equal( 'url' );
  75. } );
  76. it( 'should split text and overwrite attribute value when selection is inside text with `linkHref` attribute', () => {
  77. setData( document, 'f<$text linkHref="other url">o[ob]a</$text>r' );
  78. expect( command.value ).to.equal( 'other url' );
  79. command._doExecute( 'url' );
  80. expect( getData( document ) ).to.equal(
  81. 'f' +
  82. '<$text linkHref="other url">o</$text>' +
  83. '[<$text linkHref="url">ob</$text>]' +
  84. '<$text linkHref="other url">a</$text>' +
  85. 'r'
  86. );
  87. expect( command.value ).to.equal( 'url' );
  88. } );
  89. it(
  90. 'should overwrite `linkHref` attribute of selected text only, when selection start inside text with `linkHref` attribute',
  91. () => {
  92. setData( document, 'f<$text linkHref="other url">o[o</$text>ba]r' );
  93. expect( command.value ).to.equal( 'other url' );
  94. command._doExecute( 'url' );
  95. expect( getData( document ) ).to.equal( 'f<$text linkHref="other url">o</$text>[<$text linkHref="url">oba</$text>]r' );
  96. expect( command.value ).to.equal( 'url' );
  97. } );
  98. it( 'should overwrite `linkHref` attribute of selected text only, when selection end inside text with `linkHref` attribute', () => {
  99. setData( document, 'f[o<$text linkHref="other url">ob]a</$text>r' );
  100. expect( command.value ).to.undefined;
  101. command._doExecute( 'url' );
  102. expect( getData( document ) ).to.equal( 'f[<$text linkHref="url">oob</$text>]<$text linkHref="other url">a</$text>r' );
  103. expect( command.value ).to.equal( 'url' );
  104. } );
  105. it( 'should set `linkHref` attribute to selected text when text is split by $block element', () => {
  106. setData( document, '<p>f[oo</p><p>ba]r</p>' );
  107. expect( command.value ).to.undefined;
  108. command._doExecute( 'url' );
  109. expect( getData( document ) )
  110. .to.equal( '<p>f[<$text linkHref="url">oo</$text></p><p><$text linkHref="url">ba</$text>]r</p>' );
  111. expect( command.value ).to.equal( 'url' );
  112. } );
  113. it( 'should set `linkHref` attribute only to allowed elements and omit disallowed', () => {
  114. // Disallow text in img.
  115. document.schema.registerItem( 'img', '$inline' );
  116. setData( document, '<p>f[oo<img></img>ba]r</p>' );
  117. expect( command.value ).to.undefined;
  118. command._doExecute( 'url' );
  119. expect( getData( document ) )
  120. .to.equal( '<p>f[<$text linkHref="url">oo</$text><img></img><$text linkHref="url">ba</$text>]r</p>' );
  121. expect( command.value ).to.equal( 'url' );
  122. } );
  123. } );
  124. describe( 'collapsed selection', () => {
  125. it( 'should insert text with `linkHref` attribute, text data equal to href and select new link', () => {
  126. setData( document, 'foo[]bar' );
  127. command._doExecute( 'url' );
  128. expect( getData( document ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  129. } );
  130. it( 'should update `linkHref` attribute and select whole link when selection is inside text with `linkHref` attribute', () => {
  131. setData( document, '<$text linkHref="other url">foo[]bar</$text>' );
  132. command._doExecute( 'url' );
  133. expect( getData( document ) ).to.equal( '[<$text linkHref="url">foobar</$text>]' );
  134. } );
  135. it( 'should not insert text with `linkHref` attribute when is not allowed in parent', () => {
  136. document.schema.disallow( { name: '$text', attributes: 'linkHref', inside: 'p' } );
  137. setData( document, '<p>foo[]bar</p>' );
  138. command._doExecute( 'url' );
  139. expect( getData( document ) ).to.equal( '<p>foo[]bar</p>' );
  140. } );
  141. } );
  142. } );
  143. describe( '_checkEnabled', () => {
  144. // This test doesn't tests every possible case.
  145. // Method `_checkEnabled` uses `isAttributeAllowedInSelection` helper which is fully tested in his own test.
  146. beforeEach( () => {
  147. document.schema.registerItem( 'x', '$block' );
  148. document.schema.disallow( { name: '$text', inside: 'x', attributes: 'linkHref' } );
  149. } );
  150. describe( 'when selection is collapsed', () => {
  151. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  152. setData( document, '<p>f[]oo</p>' );
  153. expect( command._checkEnabled() ).to.be.true;
  154. } );
  155. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  156. setData( document, '<x>fo[]o</x>' );
  157. expect( command._checkEnabled() ).to.be.false;
  158. } );
  159. } );
  160. describe( 'when selection is not collapsed', () => {
  161. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  162. setData( document, '<p>[foo]</p>' );
  163. expect( command._checkEnabled() ).to.be.true;
  164. } );
  165. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  166. setData( document, '<x>[foo]</x>' );
  167. expect( command._checkEnabled() ).to.be.false;
  168. } );
  169. } );
  170. } );
  171. } );