images.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import MarkdownDataProcessor from '/ckeditor5/markdown-gfm/gfmdataprocessor.js';
  6. import DocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  7. import { stringify, parse } from '/tests/engine/_utils/view.js';
  8. describe( 'GFMDataProcessor', () => {
  9. let dataProcessor;
  10. beforeEach( () => {
  11. dataProcessor = new MarkdownDataProcessor();
  12. } );
  13. describe( 'images', () => {
  14. describe( 'toView', () => {
  15. it( 'should process images', () => {
  16. const viewFragment = dataProcessor.toView( '![alt text](http://example.com/image.png "title text")' );
  17. expect( stringify( viewFragment ) ).to.equal(
  18. '<p>' +
  19. '<img alt="alt text" src="http://example.com/image.png" title="title text"></img>' +
  20. '</p>'
  21. );
  22. } );
  23. it( 'should process images without title', () => {
  24. const viewFragment = dataProcessor.toView( '![alt text](http://example.com/image.png)' );
  25. expect( stringify( viewFragment ) ).to.equal(
  26. '<p>' +
  27. '<img alt="alt text" src="http://example.com/image.png"></img>' +
  28. '</p>'
  29. );
  30. } );
  31. it( 'should process images without alt text', () => {
  32. const viewFragment = dataProcessor.toView( '![](http://example.com/image.png "title text")' );
  33. expect( stringify( viewFragment ) ).to.equal(
  34. '<p>' +
  35. '<img alt="" src="http://example.com/image.png" title="title text"></img>' +
  36. '</p>'
  37. );
  38. } );
  39. it( 'should process referenced images', () => {
  40. const viewFragment = dataProcessor.toView(
  41. '![alt text][logo]\n' +
  42. '[logo]: http://example.com/image.png "title text"'
  43. );
  44. expect( stringify( viewFragment ) ).to.equal(
  45. '<p>' +
  46. '<img alt="alt text" src="http://example.com/image.png" title="title text"></img>' +
  47. '</p>'
  48. );
  49. } );
  50. it( 'should process referenced images without title', () => {
  51. const viewFragment = dataProcessor.toView(
  52. '![alt text][logo]\n' +
  53. '[logo]: http://example.com/image.png'
  54. );
  55. expect( stringify( viewFragment ) ).to.equal(
  56. '<p>' +
  57. '<img alt="alt text" src="http://example.com/image.png"></img>' +
  58. '</p>'
  59. );
  60. } );
  61. it( 'should process referenced images without alt text', () => {
  62. const viewFragment = dataProcessor.toView(
  63. '![][logo]\n' +
  64. '[logo]: http://example.com/image.png "title text"'
  65. );
  66. expect( stringify( viewFragment ) ).to.equal(
  67. '<p>' +
  68. '<img alt="" src="http://example.com/image.png" title="title text"></img>' +
  69. '</p>'
  70. );
  71. } );
  72. it( 'should process referenced images without alt text and title', () => {
  73. const viewFragment = dataProcessor.toView(
  74. '![][logo]\n' +
  75. '[logo]: http://example.com/image.png'
  76. );
  77. expect( stringify( viewFragment ) ).to.equal(
  78. '<p>' +
  79. '<img alt="" src="http://example.com/image.png"></img>' +
  80. '</p>'
  81. );
  82. } );
  83. } );
  84. describe( 'toData', () => {
  85. let viewFragment;
  86. beforeEach( () => {
  87. viewFragment = new DocumentFragment();
  88. } );
  89. it( 'should process image element', () => {
  90. viewFragment.appendChildren( parse(
  91. '<p>' +
  92. '<img alt="alt text" src="http://example.com/image.png" title="title text"></img>' +
  93. '</p>'
  94. ) );
  95. expect( dataProcessor.toData( viewFragment ) ).to.equal( '![alt text](http://example.com/image.png "title text")' );
  96. } );
  97. it( 'should process image element without alt text', () => {
  98. viewFragment.appendChildren( parse(
  99. '<p>' +
  100. '<img src="http://example.com/image.png" title="title text"></img>' +
  101. '</p>'
  102. ) );
  103. expect( dataProcessor.toData( viewFragment ) ).to.equal( '![](http://example.com/image.png "title text")' );
  104. } );
  105. it( 'should process image element without title', () => {
  106. viewFragment.appendChildren( parse(
  107. '<p>' +
  108. '<img alt="alt text" src="http://example.com/image.png"></img>' +
  109. '</p>'
  110. ) );
  111. expect( dataProcessor.toData( viewFragment ) ).to.equal( '![alt text](http://example.com/image.png)' );
  112. } );
  113. it( 'should process image element without title and alt text', () => {
  114. viewFragment.appendChildren( parse(
  115. '<p>' +
  116. '<img src="http://example.com/image.png"></img>' +
  117. '</p>'
  118. ) );
  119. expect( dataProcessor.toData( viewFragment ) ).to.equal( '![](http://example.com/image.png)' );
  120. } );
  121. } );
  122. } );
  123. } );