8
0

text.js 3.6 KB

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