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. } );
  22. } );
  23. afterEach( () => {
  24. command.destroy();
  25. } );
  26. describe( 'value', () => {
  27. describe( 'collapsed selection', () => {
  28. it( 'should be equal attribute value when selection is placed inside element with `linkHref` attribute', () => {
  29. setData( document, `<$text linkHref="url">foo[]bar</$text>` );
  30. expect( command.value ).to.equal( 'url' );
  31. } );
  32. it( 'should be undefined when selection is placed inside element without `linkHref` attribute', () => {
  33. setData( document, `<$text bold="true">foo[]bar</$text>` );
  34. expect( command.value ).to.undefined;
  35. } );
  36. } );
  37. describe( 'non-collapsed selection', () => {
  38. it( 'should be equal attribute value when selection contains only elements with `linkHref` attribute', () => {
  39. setData( document, 'fo[<$text linkHref="url">ob</$text>]ar' );
  40. expect( command.value ).to.equal( 'url' );
  41. } );
  42. it( 'should be undefined when selection contains not only elements with `linkHref` attribute', () => {
  43. setData( document, 'f[o<$text linkHref="url">ob</$text>]ar' );
  44. expect( command.value ).to.undefined;
  45. } );
  46. } );
  47. } );
  48. describe( '_doExecute', () => {
  49. describe( 'non-collapsed selection', () => {
  50. it( 'should set `linkHref` attribute to selected text', () => {
  51. setData( document, 'f[ooba]r' );
  52. expect( command.value ).to.undefined;
  53. command._doExecute( 'url' );
  54. expect( getData( document ) ).to.equal( 'f[<$text linkHref="url">ooba</$text>]r' );
  55. expect( command.value ).to.equal( 'url' );
  56. } );
  57. it( 'should set `linkHref` attribute to selected text when text already has attributes', () => {
  58. setData( document, 'f[o<$text bold="true">oba]r</$text>' );
  59. expect( command.value ).to.undefined;
  60. command._doExecute( 'url' );
  61. expect( command.value ).to.equal( 'url' );
  62. expect( getData( document ) ).to.equal(
  63. 'f[<$text linkHref="url">o</$text>' +
  64. '<$text bold="true" linkHref="url">oba</$text>]' +
  65. '<$text bold="true">r</$text>'
  66. );
  67. } );
  68. it( 'should overwrite existing `linkHref` attribute when selected text wraps text with `linkHref` attribute', () => {
  69. setData( document, 'f[o<$text linkHref="other url">o</$text>ba]r' );
  70. expect( command.value ).to.undefined;
  71. command._doExecute( 'url' );
  72. expect( getData( document ) ).to.equal( 'f[<$text linkHref="url">ooba</$text>]r' );
  73. expect( command.value ).to.equal( 'url' );
  74. } );
  75. it( 'should split text and overwrite attribute value when selection is inside text with `linkHref` attribute', () => {
  76. setData( document, 'f<$text linkHref="other url">o[ob]a</$text>r' );
  77. expect( command.value ).to.equal( 'other url' );
  78. command._doExecute( 'url' );
  79. expect( getData( document ) ).to.equal(
  80. 'f' +
  81. '<$text linkHref="other url">o</$text>' +
  82. '[<$text linkHref="url">ob</$text>]' +
  83. '<$text linkHref="other url">a</$text>' +
  84. 'r'
  85. );
  86. expect( command.value ).to.equal( 'url' );
  87. } );
  88. it(
  89. 'should overwrite `linkHref` attribute of selected text only, when selection start inside text with `linkHref` attribute',
  90. () => {
  91. setData( document, 'f<$text linkHref="other url">o[o</$text>ba]r' );
  92. expect( command.value ).to.equal( 'other url' );
  93. command._doExecute( 'url' );
  94. expect( getData( document ) ).to.equal( 'f<$text linkHref="other url">o</$text>[<$text linkHref="url">oba</$text>]r' );
  95. expect( command.value ).to.equal( 'url' );
  96. } );
  97. it( 'should overwrite `linkHref` attribute of selected text only, when selection end inside text with `linkHref` attribute', () => {
  98. setData( document, 'f[o<$text linkHref="other url">ob]a</$text>r' );
  99. expect( command.value ).to.undefined;
  100. command._doExecute( 'url' );
  101. expect( getData( document ) ).to.equal( 'f[<$text linkHref="url">oob</$text>]<$text linkHref="other url">a</$text>r' );
  102. expect( command.value ).to.equal( 'url' );
  103. } );
  104. it( 'should set `linkHref` attribute to selected text when text is split by $block element', () => {
  105. setData( document, '<p>f[oo</p><p>ba]r</p>' );
  106. expect( command.value ).to.undefined;
  107. command._doExecute( 'url' );
  108. expect( getData( document ) )
  109. .to.equal( '<p>f[<$text linkHref="url">oo</$text></p><p><$text linkHref="url">ba</$text>]r</p>' );
  110. expect( command.value ).to.equal( 'url' );
  111. } );
  112. it( 'should set `linkHref` attribute only to allowed elements and omit disallowed', () => {
  113. // Disallow text in img.
  114. document.schema.registerItem( 'img', '$block' );
  115. document.schema.disallow( { name: '$text', attributes: 'linkHref', inside: 'img' } );
  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. } );