integration.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import RemoveFormat from '../src/removeformat';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  9. import Image from '@ckeditor/ckeditor5-image/src/image';
  10. import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
  11. import Link from '@ckeditor/ckeditor5-link/src/link';
  12. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  13. import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
  14. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  15. import {
  16. getData as getModelData,
  17. setData as setModelData
  18. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  19. describe( 'RemoveFormat', () => {
  20. let editor, model, element;
  21. beforeEach( () => {
  22. element = document.createElement( 'div' );
  23. document.body.appendChild( element );
  24. return ClassicTestEditor
  25. .create( element, {
  26. plugins: [ Paragraph, Heading, Image, Bold, Underline, RemoveFormat, Image, ImageCaption, Link ]
  27. } )
  28. .then( newEditor => {
  29. editor = newEditor;
  30. model = editor.model;
  31. } );
  32. } );
  33. afterEach( () => {
  34. element.remove();
  35. return editor.destroy();
  36. } );
  37. describe( 'works correctly with multiline ranges', () => {
  38. it( 'does remove pure formatting markup', () => {
  39. setModelData( model, '<paragraph>foo[<$text bold="true">foo</$text></paragraph>' +
  40. '<paragraph><$text bold="true">bar</$text>]bar</paragraph>' );
  41. editor.execute( 'removeFormat' );
  42. expect( getModelData( model ) )
  43. .to.equal( '<paragraph>foo[foo</paragraph><paragraph>bar]bar</paragraph>' );
  44. } );
  45. it( 'does not touch non-formatting markup', () => {
  46. setModelData( model, '<paragraph>[<$text linkHref="url">foo</$text></paragraph><image src="assets/sample.png">' +
  47. '<caption>caption</caption></image><paragraph>bar]</paragraph>' );
  48. editor.execute( 'removeFormat' );
  49. expect( getModelData( model ) )
  50. .to.equal( '<paragraph>[<$text linkHref="url">foo</$text></paragraph>' +
  51. '<image src="assets/sample.png"><caption>caption</caption></image><paragraph>bar]</paragraph>' );
  52. } );
  53. it( 'removes the content from within widget editable', () => {
  54. setModelData( model, '<paragraph>[</paragraph>' +
  55. '<image src="assets/sample.png"><caption><$text bold="true">foo</$text></caption></image><paragraph>bar]</paragraph>' );
  56. editor.execute( 'removeFormat' );
  57. expect( getModelData( model ) )
  58. .to.equal( '<paragraph>' +
  59. '[</paragraph><image src="assets/sample.png"><caption>foo</caption></image><paragraph>bar]</paragraph>' );
  60. } );
  61. } );
  62. describe.only( 'Handles correctly known issues', () => {
  63. it( 'doesn\'t break after removing format from attribute wrapped around another attribute', () => {
  64. // https://github.com/ckeditor/ckeditor5-remove-format/pull/1#pullrequestreview-220515609
  65. // setModelData( model, '<paragraph>f[o<$text bold="true">ob</$text>a]r</paragraph>' );
  66. // editor.execute( 'underline' );
  67. setModelData( model, '<paragraph>' +
  68. 'f[<$text underline="true">o</$text>' +
  69. '<$text underline="true" bold="true">ob</$text>' +
  70. '<$text underline="true">a</$text>]r' +
  71. '</paragraph>' );
  72. // editor.execute( 'underline' );
  73. editor.execute( 'removeFormat' );
  74. expect( getModelData( model ) ).to.equal( '<paragraph>foobar</paragraph>' );
  75. } );
  76. } );
  77. describe.only( 'Test external bug', () => {
  78. it( 'sel rem attr', () => {
  79. setModelData( model, '<paragraph>' +
  80. 'f[<$text underline="true">o</$text>' +
  81. '<$text underline="true" bold="true">ob</$text>' +
  82. '<$text underline="true">a</$text>]r' +
  83. '</paragraph>' );
  84. model.change( writer => {
  85. const modelRoot = model.document.getRoot();
  86. // const itemInQuestion = modelRoot.getChild( 0 ).getChild( 1 );
  87. for ( const item of model.document.selection.getFirstRange() ) {
  88. // console.log( item );
  89. // console.log( 'has', item.item.hasAttribute( 'bold' ) );
  90. writer.removeAttribute( 'bold', item.item );
  91. writer.removeAttribute( 'underline', item.item );
  92. }
  93. // for ( const i of [ 1, 2, 3 ] ) {
  94. // writer.removeAttribute( 'underline', modelRoot.getChild( 0 ).getChild( i ) );
  95. // writer.removeAttribute( 'bold', modelRoot.getChild( 0 ).getChild( i ) );
  96. // }
  97. // for ( const item of modelRoot ) {
  98. // console.log( item );
  99. // }
  100. // debugger;
  101. // writer.removeAttribute( 'underline', itemInQuestion );
  102. // writer.removeAttribute( 'bold', itemInQuestion );
  103. } );
  104. // model.createRange( 0 )
  105. // editor.execute( 'underline' );
  106. // editor.execute( 'removeFormat' );
  107. expect( getModelData( model ) ).to.equal( '<paragraph>f[ooba]r</paragraph>' );
  108. } );
  109. } );
  110. } );