text.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Node from '../../src/view/node';
  6. import Text from '../../src/view/text';
  7. import Document from '../../src/view/document';
  8. import { StylesProcessor } from '../../src/view/stylesmap';
  9. describe( 'Text', () => {
  10. let document, stylesProcessor;
  11. beforeEach( () => {
  12. stylesProcessor = new StylesProcessor();
  13. document = new Document( stylesProcessor );
  14. } );
  15. describe( 'constructor()', () => {
  16. it( 'should create element without attributes', () => {
  17. const text = new Text( document, 'foo' );
  18. expect( text ).to.be.an.instanceof( Node );
  19. expect( text.data ).to.equal( 'foo' );
  20. expect( text ).to.have.property( 'parent' ).that.is.null;
  21. } );
  22. } );
  23. describe( 'is()', () => {
  24. let text;
  25. before( () => {
  26. text = new Text( document, 'foo' );
  27. } );
  28. it( 'should return true for node, text', () => {
  29. expect( text.is( 'node' ) ).to.be.true;
  30. expect( text.is( 'view:node' ) ).to.be.true;
  31. expect( text.is( 'text' ) ).to.be.true;
  32. expect( text.is( 'view:text' ) ).to.be.true;
  33. } );
  34. it( 'should return false for other accept values', () => {
  35. expect( text.is( 'textProxy' ) ).to.be.false;
  36. expect( text.is( 'view:textProxy' ) ).to.be.false;
  37. expect( text.is( 'element' ) ).to.be.false;
  38. expect( text.is( 'view:element' ) ).to.be.false;
  39. expect( text.is( 'containerElement' ) ).to.be.false;
  40. expect( text.is( 'attributeElement' ) ).to.be.false;
  41. expect( text.is( 'uiElement' ) ).to.be.false;
  42. expect( text.is( 'emptyElement' ) ).to.be.false;
  43. expect( text.is( 'rootElement' ) ).to.be.false;
  44. expect( text.is( 'documentFragment' ) ).to.be.false;
  45. expect( text.is( 'model:text' ) ).to.be.false;
  46. expect( text.is( 'model:node' ) ).to.be.false;
  47. } );
  48. } );
  49. describe( '_clone()', () => {
  50. it( 'should return new text with same data', () => {
  51. const text = new Text( document, 'foo bar' );
  52. const clone = text._clone();
  53. expect( clone ).to.not.equal( text );
  54. expect( clone.data ).to.equal( text.data );
  55. } );
  56. } );
  57. describe( 'isSimilar', () => {
  58. const text = new Text( document, 'foo' );
  59. it( 'should return false when comparing to non-text', () => {
  60. expect( text.isSimilar( null ) ).to.be.false;
  61. expect( text.isSimilar( {} ) ).to.be.false;
  62. } );
  63. it( 'should return true when the same text node is provided', () => {
  64. expect( text.isSimilar( text ) ).to.be.true;
  65. } );
  66. it( 'should return true when data is the same', () => {
  67. const other = new Text( document, 'foo' );
  68. expect( text.isSimilar( other ) ).to.be.true;
  69. } );
  70. it( 'should return false when data is not the same', () => {
  71. const other = text._clone();
  72. other._data = 'not-foo';
  73. expect( text.isSimilar( other ) ).to.be.false;
  74. } );
  75. } );
  76. describe( 'setText', () => {
  77. it( 'should change the text', () => {
  78. const text = new Text( document, 'foo' );
  79. text._data = 'bar';
  80. expect( text.data ).to.equal( 'bar' );
  81. } );
  82. it( 'works when using addition assignment operator (+=)', () => {
  83. const foo = new Text( document, 'foo' );
  84. const bar = new Text( document, 'bar' );
  85. foo._data += bar.data;
  86. expect( foo.data ).to.equal( 'foobar' );
  87. } );
  88. } );
  89. } );