8
0

linkcommand.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 '/tests/engine/_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 link attribute in paragraph.
  19. document.schema.registerItem( 'p', '$block' );
  20. document.schema.allow( { name: '$text', attributes: 'link', inside: '$root' } );
  21. } );
  22. } );
  23. afterEach( () => {
  24. command.destroy();
  25. } );
  26. describe( 'isValue', () => {
  27. describe( 'collapsed selection', () => {
  28. it( 'should be equal `true` when selection is placed inside element with link attribute', () => {
  29. setData( document, `<$text link="url">foo[]bar</$text>` );
  30. expect( command.isValue ).to.true;
  31. } );
  32. it( 'should be equal `false` when selection is placed inside element without link attribute', () => {
  33. setData( document, `<$text bold="true">foo[]bar</$text>` );
  34. expect( command.isValue ).to.false;
  35. } );
  36. } );
  37. describe( 'non-collapsed selection', () => {
  38. it( 'should be equal `true` when selection contains only elements with link attribute', () => {
  39. setData( document, 'fo[<$text link="url">ob</$text>]ar' );
  40. expect( command.isValue ).to.true;
  41. } );
  42. it( 'should be equal `false` when selection contains not only elements with link attribute', () => {
  43. setData( document, 'f[o<$text link="url">ob</$text>]ar' );
  44. expect( command.isValue ).to.false;
  45. } );
  46. } );
  47. } );
  48. describe( '_doExecute', () => {
  49. describe( 'non-collapsed selection', () => {
  50. it( 'should set link attribute to selected text', () => {
  51. setData( document, 'f[ooba]r' );
  52. expect( command.isValue ).to.false;
  53. command._doExecute( 'url' );
  54. expect( getData( document ) ).to.equal( 'f[<$text link="url">ooba</$text>]r' );
  55. expect( command.isValue ).to.true;
  56. } );
  57. it( 'should set link attribute to selected text when text already has attributes', () => {
  58. setData( document, 'f[o<$text bold="true">oba]r</$text>' );
  59. expect( command.isValue ).to.false;
  60. command._doExecute( 'url' );
  61. expect( command.isValue ).to.true;
  62. expect( getData( document ) )
  63. .to.equal( 'f[<$text link="url">o</$text><$text bold="true" link="url">oba</$text>]<$text bold="true">r</$text>' );
  64. } );
  65. it( 'should overwrite existing link attribute when selected text wraps text with link attribute', () => {
  66. setData( document, 'f[o<$text link="other url">o</$text>ba]r' );
  67. expect( command.isValue ).to.false;
  68. command._doExecute( 'url' );
  69. expect( getData( document ) ).to.equal( 'f[<$text link="url">ooba</$text>]r' );
  70. expect( command.isValue ).to.true;
  71. } );
  72. it( 'should split text and overwrite attribute value when selection is inside text with link attribute', () => {
  73. setData( document, 'f<$text link="other url">o[ob]a</$text>r' );
  74. expect( command.isValue ).to.true;
  75. command._doExecute( 'url' );
  76. expect( getData( document ) )
  77. .to.equal( 'f<$text link="other url">o</$text>[<$text link="url">ob</$text>]<$text link="other url">a</$text>r' );
  78. expect( command.isValue ).to.true;
  79. } );
  80. it( 'should overwrite link attribute of selected text only, when selection start inside text with link attribute', () => {
  81. setData( document, 'f<$text link="other url">o[o</$text>ba]r' );
  82. expect( command.isValue ).to.true;
  83. command._doExecute( 'url' );
  84. expect( getData( document ) ).to.equal( 'f<$text link="other url">o</$text>[<$text link="url">oba</$text>]r' );
  85. expect( command.isValue ).to.true;
  86. } );
  87. it( 'should overwrite link attribute of selected text only, when selection end inside text with link attribute', () => {
  88. setData( document, 'f[o<$text link="other url">ob]a</$text>r' );
  89. expect( command.isValue ).to.false;
  90. command._doExecute( 'url' );
  91. expect( getData( document ) ).to.equal( 'f[<$text link="url">oob</$text>]<$text link="other url">a</$text>r' );
  92. expect( command.isValue ).to.true;
  93. } );
  94. it( 'should set link attribute to selected text when text is split by $block element', () => {
  95. setData( document, '<p>f[oo</p><p>ba]r</p>' );
  96. expect( command.isValue ).to.false;
  97. command._doExecute( 'url' );
  98. expect( getData( document ) )
  99. .to.equal( '<p>f[<$text link="url">oo</$text></p><p><$text link="url">ba</$text>]r</p>' );
  100. expect( command.isValue ).to.true;
  101. } );
  102. it( 'should set link attribute only to allowed elements and omit disallowed', () => {
  103. // Disallow text in img.
  104. document.schema.registerItem( 'img', '$block' );
  105. document.schema.disallow( { name: '$text', attributes: 'link', inside: 'img' } );
  106. setData( document, '<p>f[oo<img></img>ba]r</p>' );
  107. expect( command.isValue ).to.false;
  108. command._doExecute( 'url' );
  109. expect( getData( document ) )
  110. .to.equal( '<p>f[<$text link="url">oo</$text><img></img><$text link="url">ba</$text>]r</p>' );
  111. expect( command.isValue ).to.true;
  112. } );
  113. } );
  114. describe( 'collapsed selection', () => {
  115. it( 'should insert text with link attribute and data equal to href', () => {
  116. setData( document, 'foo[]bar' );
  117. command._doExecute( 'url' );
  118. expect( getData( document ) ).to.equal( 'foo[<$text link="url">url</$text>]bar' );
  119. } );
  120. it( 'should insert text with link attribute and data equal to href when selection is inside text with link attribute', () => {
  121. setData( document, '<$text link="other url">foo[]bar</$text>' );
  122. command._doExecute( 'url' );
  123. expect( getData( document ) )
  124. .to.equal( '<$text link="other url">foo</$text>[<$text link="url">url</$text>]<$text link="other url">bar</$text>' );
  125. } );
  126. it( 'should not insert text with link attribute when is not allowed in parent', () => {
  127. document.schema.disallow( { name: '$text', attributes: 'link', inside: 'p' } );
  128. setData( document, '<p>foo[]bar</p>' );
  129. command._doExecute( 'url' );
  130. expect( getData( document ) ).to.equal( '<p>foo[]bar</p>' );
  131. } );
  132. } );
  133. } );
  134. describe( '_checkEnabled', () => {
  135. // This test doesn't tests every possible case.
  136. // Method `_checkEnabled` uses `isAttributeAllowedInSelection` helper which is fully tested in his own test.
  137. beforeEach( () => {
  138. document.schema.registerItem( 'x', '$block' );
  139. document.schema.disallow( { name: '$text', inside: 'x', attributes: 'link' } );
  140. } );
  141. describe( 'when selection is collapsed', () => {
  142. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  143. setData( document, '<p>f[]oo</p>' );
  144. expect( command._checkEnabled() ).to.be.true;
  145. } );
  146. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  147. setData( document, '<x>fo[]o</x>' );
  148. expect( command._checkEnabled() ).to.be.false;
  149. } );
  150. } );
  151. describe( 'when selection is not collapsed', () => {
  152. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  153. setData( document, '<p>[foo]</p>' );
  154. expect( command._checkEnabled() ).to.be.true;
  155. } );
  156. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  157. setData( document, '<x>[foo]</x>' );
  158. expect( command._checkEnabled() ).to.be.false;
  159. } );
  160. } );
  161. } );
  162. } );