8
0

unlinkcommand.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 UnlinkCommand from '/ckeditor5/link/unlinkcommand.js';
  7. import { setData, getData } from '/tests/engine/_utils/model.js';
  8. describe( 'UnlinkCommand', () => {
  9. let editor, document, command;
  10. beforeEach( () => {
  11. return ModelTestEditor.create()
  12. .then( newEditor => {
  13. editor = newEditor;
  14. document = editor.document;
  15. command = new UnlinkCommand( 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( '_doExecute', () => {
  27. describe( 'non-collapsed selection', () => {
  28. it( 'should remove link attribute from selected text', () => {
  29. setData( document, '<$text link="url">f[ooba]r</$text>' );
  30. command._doExecute();
  31. expect( getData( document ) ).to.equal( '<$text link="url">f</$text>[ooba]<$text link="url">r</$text>' );
  32. } );
  33. it( 'should remove link attribute from selected text and do not modified other attributes', () => {
  34. setData( document, '<$text bold="true" link="url">f[ooba]r</$text>' );
  35. command._doExecute();
  36. expect( getData( document ) )
  37. .to.equal( '<$text bold="true" link="url">f</$text>[<$text bold="true">ooba</$text>]<$text bold="true" link="url">r</$text>' );
  38. } );
  39. it( 'should remove link attribute from selected text when attributes have different value', () => {
  40. setData( document, '[<$text link="url">foo</$text><$text link="other url">bar</$text>]' );
  41. command._doExecute();
  42. expect( getData( document ) ).to.equal( '[foobar]' );
  43. } );
  44. } );
  45. describe( 'collapsed selection', () => {
  46. it( 'should remove link attribute from selection siblings with the same attribute value', () => {
  47. setData( document, '<$text link="url">foo[]bar</$text>' );
  48. command._doExecute();
  49. expect( getData( document ) ).to.equal( 'foo[]bar' );
  50. } );
  51. it( 'should remove link attribute from selection siblings with the same attribute value and do not ' +
  52. 'modified other attributes',
  53. () => {
  54. setData( document, '<$text bold="true" link="url">foo[]bar</$text>' );
  55. command._doExecute();
  56. expect( getData( document ) ).to.equal( '<$text bold="true">foo[]bar</$text>' );
  57. } );
  58. it(
  59. 'should remove link attribute from selection siblings with the same attribute value and do nothing ' +
  60. 'with other value links',
  61. () => {
  62. setData( document, '<$text link="other url">fo</$text><$text link="url">o[]b</$text><$text link="other url">ar</$text>' );
  63. command._doExecute();
  64. expect( getData( document ) ).to.equal( '<$text link="other url">fo</$text>o[]b<$text link="other url">ar</$text>' );
  65. } );
  66. it( 'should do nothing with the same value links when there is a link with other value between', () => {
  67. setData(
  68. document,
  69. '<$text link="same url">f</$text>' +
  70. '<$text link="other url">o</$text>' +
  71. '<$text link="same url">o[]b</$text>' +
  72. '<$text link="other url">a</$text>' +
  73. '<$text link="same url">r</$text>'
  74. );
  75. command._doExecute();
  76. expect( getData( document ) )
  77. .to.equal(
  78. '<$text link="same url">f</$text>' +
  79. '<$text link="other url">o</$text>' +
  80. 'o[]b' +
  81. '<$text link="other url">a</$text>' +
  82. '<$text link="same url">r</$text>'
  83. );
  84. } );
  85. it( 'should remove link attribute from selection siblings only in the same parent as selection parent', () => {
  86. setData( document, '<p><$text link="url">fo[]o</$text></p><p><$text link="url">bar</$text></p>' );
  87. command._doExecute();
  88. expect( getData( document ) ).to.equal( '<p>fo[]o</p><p><$text link="url">bar</$text></p>' );
  89. } );
  90. it( 'should remove link attribute from selection siblings when selection is at the end of link', () => {
  91. setData( document, '<$text link="url">foobar</$text>[]' );
  92. command._doExecute();
  93. expect( getData( document ) ).to.equal( 'foobar[]' );
  94. } );
  95. it( 'should remove link attribute from selection siblings when selection is at the beginning of link', () => {
  96. setData( document, '[]<$text link="url">foobar</$text>' );
  97. command._doExecute();
  98. expect( getData( document ) ).to.equal( '[]foobar' );
  99. } );
  100. it( 'should remove link attribute from selection siblings on the left side when selection is between two ' +
  101. 'elements with different link attributes',
  102. () => {
  103. setData( document, '<$text link="url">foo</$text>[]<$text link="other url">bar</$text>' );
  104. command._doExecute();
  105. expect( getData( document ) ).to.equal( 'foo[]<$text link="other url">bar</$text>' );
  106. } );
  107. } );
  108. } );
  109. } );