8
0

view.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import { getData } from '/tests/engine/_utils/view.js';
  7. import Element from '/ckeditor5/engine/treeview/element.js';
  8. import AttributeElement from '/ckeditor5/engine/treeview/attributeelement.js';
  9. import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
  10. import Text from '/ckeditor5/engine/treeview/text.js';
  11. import Selection from '/ckeditor5/engine/treeview/selection.js';
  12. import Range from '/ckeditor5/engine/treeview/range.js';
  13. describe( 'view test utils', () => {
  14. describe( 'getData', () => {
  15. it( 'should write elements and texts', () => {
  16. const text = new Text( 'foobar' );
  17. const b = new Element( 'b', null, text );
  18. const p = new Element( 'p', null, b );
  19. expect( getData( p ) ).to.equal( '<p><b>foobar</b></p>' );
  20. } );
  21. it( 'should write elements with attributes', () => {
  22. const text = new Text( 'foobar' );
  23. const b = new Element( 'b', {
  24. foo: 'bar'
  25. }, text );
  26. const p = new Element( 'p', {
  27. baz: 'qux',
  28. bar: 'taz',
  29. class: 'short wide'
  30. }, b );
  31. expect( getData( p ) ).to.equal( '<p class="short wide" baz="qux" bar="taz"><b foo="bar">foobar</b></p>' );
  32. } );
  33. it( 'should write selection ranges inside elements', () => {
  34. const text1 = new Text( 'foobar' );
  35. const text2 = new Text( 'bazqux' );
  36. const b1 = new Element( 'b', null, text1 );
  37. const b2 = new Element( 'b', null, text2 );
  38. const p = new Element( 'p', null, [ b1, b2 ] );
  39. const range = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  40. const selection = new Selection();
  41. selection.addRange( range );
  42. expect( getData( p, selection ) ).to.equal( '<p><b>foobar</b>[<b>bazqux</b>]</p>' );
  43. } );
  44. it( 'should write collapsed selection ranges inside elements', () => {
  45. const text = new Text( 'foobar' );
  46. const p = new Element( 'p', null, text );
  47. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  48. const selection = new Selection();
  49. selection.addRange( range );
  50. expect( getData( p, selection ) ).to.equal( '<p>[]foobar</p>' );
  51. } );
  52. it( 'should write selection ranges inside text', () => {
  53. const text1 = new Text( 'foobar' );
  54. const text2 = new Text( 'bazqux' );
  55. const b1 = new Element( 'b', null, text1 );
  56. const b2 = new Element( 'b', null, text2 );
  57. const p = new Element( 'p', null, [ b1, b2 ] );
  58. const range = Range.createFromParentsAndOffsets( text1, 1, text1, 5 );
  59. const selection = new Selection();
  60. selection.addRange( range );
  61. expect( getData( p, selection ) ).to.equal( '<p><b>f{ooba}r</b><b>bazqux</b></p>' );
  62. } );
  63. it( 'should write ranges that start inside text end ends between elements', () => {
  64. const text1 = new Text( 'foobar' );
  65. const text2 = new Text( 'bazqux' );
  66. const b1 = new Element( 'b', null, text1 );
  67. const b2 = new Element( 'b', null, text2 );
  68. const p = new Element( 'p', null, [ b1, b2 ] );
  69. const range = Range.createFromParentsAndOffsets( p, 0, text2, 5 );
  70. const selection = new Selection();
  71. selection.addRange( range );
  72. expect( getData( p, selection ) ).to.equal( '<p>[<b>foobar</b><b>bazqu}x</b></p>' );
  73. } );
  74. it( 'should write elements types as namespaces when needed', () => {
  75. const text = new Text( 'foobar' );
  76. const b = new AttributeElement( 'b', null, text );
  77. const p = new ContainerElement( 'p', null, b );
  78. expect( getData( p, null, { showTypes: true } ) )
  79. .to.equal( '<container:p><attribute:b>foobar</attribute:b></container:p>' );
  80. } );
  81. } );
  82. } );