integration.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import Link from '../src/link';
  7. import Image from '@ckeditor/ckeditor5-image/src/image';
  8. import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  11. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. describe( 'Link', () => {
  13. let editor, doc, element, linkCommand, unlinkCommand;
  14. beforeEach( () => {
  15. element = document.createElement( 'div' );
  16. document.body.appendChild( element );
  17. return ClassicTestEditor.create( element, {
  18. plugins: [ Paragraph, Image, ImageCaption, Link ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. doc = editor.document;
  23. linkCommand = editor.commands.get( 'link' );
  24. unlinkCommand = editor.commands.get( 'unlink' );
  25. } );
  26. } );
  27. afterEach( () => {
  28. element.remove();
  29. return editor.destroy();
  30. } );
  31. describe( 'compatibility with images', () => {
  32. it( 'does not link a caption–less image', () => {
  33. element = document.createElement( 'div' );
  34. document.body.appendChild( element );
  35. return ClassicTestEditor.create( element, {
  36. plugins: [ Paragraph, Image, Link ]
  37. } )
  38. .then( newEditor => {
  39. editor = newEditor;
  40. doc = editor.document;
  41. linkCommand = editor.commands.get( 'link' );
  42. unlinkCommand = editor.commands.get( 'unlink' );
  43. setModelData( doc,
  44. '<paragraph>fo[o</paragraph>' +
  45. '<image src="foo.png"></image>' +
  46. '<paragraph>b]ar</paragraph>'
  47. );
  48. editor.execute( 'link', 'url' );
  49. expect( getModelData( doc ) ).to.equal(
  50. '<paragraph>fo[<$text linkHref="url">o</$text></paragraph>' +
  51. '<image src="foo.png"></image>' +
  52. '<paragraph><$text linkHref="url">b</$text>]ar</paragraph>'
  53. );
  54. expect( linkCommand.isEnabled ).to.be.true;
  55. expect( linkCommand.value ).to.equal( 'url' );
  56. expect( unlinkCommand.isEnabled ).to.be.true;
  57. element.remove();
  58. return editor.destroy();
  59. } );
  60. } );
  61. it( 'links the image caption text if selection contains more than an image', () => {
  62. setModelData( doc,
  63. '<paragraph>fo[o</paragraph>' +
  64. '<image src="foo.png">' +
  65. '<caption>abc</caption>' +
  66. '</image>' +
  67. '<paragraph>baz</paragraph>' +
  68. '<image src="bar.png">' +
  69. '<caption>abc</caption>' +
  70. '</image>' +
  71. '<paragraph>b]ar</paragraph>'
  72. );
  73. editor.execute( 'link', 'url' );
  74. expect( getModelData( doc ) ).to.equal(
  75. '<paragraph>fo[<$text linkHref="url">o</$text></paragraph>' +
  76. '<image src="foo.png">' +
  77. '<caption><$text linkHref="url">abc</$text></caption>' +
  78. '</image>' +
  79. '<paragraph><$text linkHref="url">baz</$text></paragraph>' +
  80. '<image src="bar.png">' +
  81. '<caption><$text linkHref="url">abc</$text></caption>' +
  82. '</image>' +
  83. '<paragraph><$text linkHref="url">b</$text>]ar</paragraph>'
  84. );
  85. expect( linkCommand.isEnabled ).to.be.true;
  86. expect( linkCommand.value ).to.equal( 'url' );
  87. expect( unlinkCommand.isEnabled ).to.be.true;
  88. } );
  89. it( 'links the image caption text if selection is in the image caption', () => {
  90. setModelData( doc,
  91. '<paragraph>foo</paragraph>' +
  92. '<image src="foo.png">' +
  93. '<caption>a[b]c</caption>' +
  94. '</image>' +
  95. '<paragraph>bar</paragraph>'
  96. );
  97. editor.execute( 'link', 'url' );
  98. expect( getModelData( doc ) ).to.equal(
  99. '<paragraph>foo</paragraph>' +
  100. '<image src="foo.png">' +
  101. '<caption>a[<$text linkHref="url">b</$text>]c</caption>' +
  102. '</image>' +
  103. '<paragraph>bar</paragraph>'
  104. );
  105. expect( linkCommand.isEnabled ).to.be.true;
  106. expect( linkCommand.value ).to.equal( 'url' );
  107. expect( unlinkCommand.isEnabled ).to.be.true;
  108. } );
  109. // https://github.com/ckeditor/ckeditor5-link/issues/85
  110. it( 'is disabled when only the image is the only selected element', () => {
  111. setModelData( doc, '[<image src="foo.png"></image>]' );
  112. expect( linkCommand.isEnabled ).to.be.false;
  113. setModelData( doc, '[<image src="foo.png"><caption>abc</caption></image>]' );
  114. expect( linkCommand.isEnabled ).to.be.false;
  115. setModelData( doc,
  116. '[<image src="foo.png">' +
  117. '<caption><$text linkHref="url">abc</$text></caption>' +
  118. '</image>]'
  119. );
  120. expect( linkCommand.isEnabled ).to.be.false;
  121. expect( unlinkCommand.isEnabled ).to.be.false;
  122. } );
  123. } );
  124. } );