integration.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. linkCommand = editor.commands.get( 'link' );
  18. unlinkCommand = editor.commands.get( 'unlink' );
  19. } );
  20. afterEach( () => {
  21. element.remove();
  22. return editor.destroy();
  23. } );
  24. describe( 'compatibility with images', () => {
  25. it( 'does not link a caption–less image', () => {
  26. return ClassicTestEditor.create( element, {
  27. plugins: [ Paragraph, Image, Link ]
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. doc = editor.document;
  32. setModelData( doc,
  33. '<paragraph>fo[o</paragraph>' +
  34. '<image src="foo.png"></image>' +
  35. '<paragraph>b]ar</paragraph>'
  36. );
  37. editor.execute( 'link', 'url' );
  38. expect( getModelData( doc ) ).to.equal(
  39. '<paragraph>fo[<$text linkHref="url">o</$text></paragraph>' +
  40. '<image src="foo.png"></image>' +
  41. '<paragraph><$text linkHref="url">b</$text>]ar</paragraph>'
  42. );
  43. expect( linkCommand.isEnabled ).to.be.true;
  44. expect( linkCommand.value ).to.equal( 'url' );
  45. expect( unlinkCommand.isEnabled ).to.be.true;
  46. } );
  47. } );
  48. it( 'links the image caption text if selection contains more than an image', () => {
  49. return ClassicTestEditor.create( element, {
  50. plugins: [ Paragraph, Image, ImageCaption, Link ]
  51. } )
  52. .then( newEditor => {
  53. editor = newEditor;
  54. doc = editor.document;
  55. setModelData( doc,
  56. '<paragraph>fo[o</paragraph>' +
  57. '<image src="foo.png">' +
  58. '<caption>abc</caption>' +
  59. '</image>' +
  60. '<paragraph>baz</paragraph>' +
  61. '<image src="bar.png">' +
  62. '<caption>abc</caption>' +
  63. '</image>' +
  64. '<paragraph>b]ar</paragraph>'
  65. );
  66. editor.execute( 'link', 'url' );
  67. expect( getModelData( doc ) ).to.equal(
  68. '<paragraph>fo[<$text linkHref="url">o</$text></paragraph>' +
  69. '<image src="foo.png">' +
  70. '<caption><$text linkHref="url">abc</$text></caption>' +
  71. '</image>' +
  72. '<paragraph><$text linkHref="url">baz</$text></paragraph>' +
  73. '<image src="bar.png">' +
  74. '<caption><$text linkHref="url">abc</$text></caption>' +
  75. '</image>' +
  76. '<paragraph><$text linkHref="url">b</$text>]ar</paragraph>'
  77. );
  78. expect( linkCommand.isEnabled ).to.be.true;
  79. expect( linkCommand.value ).to.equal( 'url' );
  80. expect( unlinkCommand.isEnabled ).to.be.true;
  81. } );
  82. } );
  83. // https://github.com/ckeditor/ckeditor5-link/issues/85
  84. it( 'is disabled when only the image is the only selected element', () => {
  85. return ClassicTestEditor.create( element, {
  86. plugins: [ Paragraph, Image, ImageCaption, Link ]
  87. } )
  88. .then( newEditor => {
  89. editor = newEditor;
  90. doc = editor.document;
  91. setModelData( doc, '[<image src="foo.png"></image>]' );
  92. expect( linkCommand.isEnabled ).to.be.false;
  93. setModelData( doc, '[<image src="foo.png"><caption>abc</caption></image>]' );
  94. expect( linkCommand.isEnabled ).to.be.false;
  95. setModelData( doc,
  96. '[<image src="foo.png">' +
  97. '<caption><$text linkHref="url">abc</$text></caption>' +
  98. '</image>]'
  99. );
  100. expect( linkCommand.isEnabled ).to.be.false;
  101. expect( unlinkCommand.isEnabled ).to.be.false;
  102. } );
  103. } );
  104. } );
  105. } );