utils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import { modelElementToPlainText } from '../src/utils';
  6. import Element from '@ckeditor/ckeditor5-engine/src/model/element';
  7. import Text from '@ckeditor/ckeditor5-engine/src/model/text';
  8. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
  12. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  13. import LinkEditing from '@ckeditor/ckeditor5-link/src/linkediting';
  14. import ListEditing from '@ckeditor/ckeditor5-list/src/listediting';
  15. import TableEditing from '@ckeditor/ckeditor5-table/src/tableediting';
  16. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  17. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  18. describe( 'utils', () => {
  19. describe( 'modelElementToPlainText()', () => {
  20. it( 'should extract only plain text', () => {
  21. const text1 = new Text( 'Foo' );
  22. const text2 = new Text( 'Bar', { bold: true } );
  23. const text3 = new Text( 'Baz', { bold: true, underline: true } );
  24. const innerElement1 = new Element( 'paragraph', null, [ text1 ] );
  25. const innerElement2 = new Element( 'paragraph', null, [ text2, text3 ] );
  26. const mainElement = new Element( 'container', null, [ innerElement1, innerElement2 ] );
  27. expect( modelElementToPlainText( mainElement ) ).to.equal( 'Foo\nBarBaz' );
  28. } );
  29. describe( 'complex structures', () => {
  30. let editor, model;
  31. beforeEach( () => {
  32. return VirtualTestEditor
  33. .create( {
  34. plugins: [ Enter, ShiftEnter, Paragraph, BoldEditing, LinkEditing, BlockQuoteEditing, ListEditing, TableEditing ]
  35. } )
  36. .then( newEditor => {
  37. editor = newEditor;
  38. model = editor.model;
  39. } );
  40. } );
  41. afterEach( () => {
  42. editor.destroy();
  43. } );
  44. it( 'extracts plain text from blockqoutes', () => {
  45. setModelData( model, '<blockQuote>' +
  46. '<paragraph>Hello</paragraph>' +
  47. '<listItem listIndent="0" listType="numbered">world</listItem>' +
  48. '<listItem listIndent="0" listType="numbered">foo</listItem>' +
  49. '<paragraph>bar</paragraph>' +
  50. '</blockQuote>' );
  51. expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal( 'Hello\nworld\nfoo\nbar' );
  52. } );
  53. it( 'extracts plain text from tables', () => {
  54. setModelData( model, '<table>' +
  55. '<tableRow>' +
  56. '<tableCell>' +
  57. '<paragraph>Foo</paragraph>' +
  58. '</tableCell>' +
  59. '<tableCell>' +
  60. '<paragraph>Bar</paragraph>' +
  61. '</tableCell>' +
  62. '</tableRow>' +
  63. '<tableRow>' +
  64. '<tableCell>' +
  65. '<paragraph>Baz</paragraph>' +
  66. '</tableCell>' +
  67. '<tableCell>' +
  68. '<paragraph>Foo</paragraph>' +
  69. '</tableCell>' +
  70. '</tableRow>' +
  71. '</table>' );
  72. expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal( 'Foo\nBar\nBaz\nFoo' );
  73. } );
  74. it( 'extracts plain text with soft break', () => {
  75. setModelData( model, '<paragraph>Foo<softBreak></softBreak>bar</paragraph>' );
  76. expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal( 'Foo\nbar' );
  77. } );
  78. it( 'extracts plain text with inline styles', () => {
  79. setModelData( model, '<paragraph>F<$text bold="true">oo</$text><$text href="url">Ba</$text>r</paragraph>' );
  80. expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal( 'FooBar' );
  81. } );
  82. it( 'extracts plain text from mixed structure', () => {
  83. setModelData( model, '<paragraph>' +
  84. '<$text bold="true">111</$text><$text href="url" bold="true">222</$text>333' +
  85. '</paragraph><blockQuote>' +
  86. '<paragraph>444<softBreak></softBreak>555</paragraph>' +
  87. '<table>' +
  88. '<tableRow>' +
  89. '<tableCell><paragraph>666</paragraph></tableCell>' +
  90. '<tableCell><paragraph>7<$text bold="true">7</$text>7</paragraph></tableCell>' +
  91. '</tableRow>' +
  92. '<tableRow>' +
  93. '<tableCell><paragraph>888</paragraph></tableCell>' +
  94. '<tableCell><paragraph>999</paragraph></tableCell>' +
  95. '</tableRow>' +
  96. '</table>' +
  97. '</blockQuote><table>' +
  98. '<tableRow>' +
  99. '<tableCell><paragraph>000</paragraph></tableCell>' +
  100. '<tableCell><blockQuote>' +
  101. '<listItem listIndent="0" listType="numbered">111</listItem>' +
  102. '<listItem listIndent="0" listType="numbered">222</listItem>' +
  103. '</blockQuote></tableCell>' +
  104. '</tableRow>' +
  105. '</table>' );
  106. expect( modelElementToPlainText( model.document.getRoot() ) ).to.equal(
  107. '111222333\n444\n555\n666\n777\n888\n999\n000\n111\n222'
  108. );
  109. } );
  110. } );
  111. } );
  112. } );