view.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 DocumentFragment from '/ckeditor5/engine/treeview/documentfragment.js';
  8. import Element from '/ckeditor5/engine/treeview/element.js';
  9. import AttributeElement from '/ckeditor5/engine/treeview/attributeelement.js';
  10. import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
  11. import Text from '/ckeditor5/engine/treeview/text.js';
  12. import Selection from '/ckeditor5/engine/treeview/selection.js';
  13. import Range from '/ckeditor5/engine/treeview/range.js';
  14. describe( 'view test utils', () => {
  15. describe( 'getData', () => {
  16. it( 'should write elements and texts', () => {
  17. const text = new Text( 'foobar' );
  18. const b = new Element( 'b', null, text );
  19. const p = new Element( 'p', null, b );
  20. expect( getData( p ) ).to.equal( '<p><b>foobar</b></p>' );
  21. } );
  22. it( 'should write elements with attributes', () => {
  23. const text = new Text( 'foobar' );
  24. const b = new Element( 'b', {
  25. foo: 'bar'
  26. }, text );
  27. const p = new Element( 'p', {
  28. baz: 'qux',
  29. bar: 'taz',
  30. class: 'short wide'
  31. }, b );
  32. expect( getData( p ) ).to.equal( '<p class="short wide" baz="qux" bar="taz"><b foo="bar">foobar</b></p>' );
  33. } );
  34. it( 'should write selection ranges inside elements', () => {
  35. const text1 = new Text( 'foobar' );
  36. const text2 = new Text( 'bazqux' );
  37. const b1 = new Element( 'b', null, text1 );
  38. const b2 = new Element( 'b', null, text2 );
  39. const p = new Element( 'p', null, [ b1, b2 ] );
  40. const range = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  41. const selection = new Selection();
  42. selection.addRange( range );
  43. expect( getData( p, selection ) ).to.equal( '<p><b>foobar</b>[<b>bazqux</b>]</p>' );
  44. } );
  45. it( 'should write collapsed selection ranges inside elements', () => {
  46. const text = new Text( 'foobar' );
  47. const p = new Element( 'p', null, text );
  48. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  49. const selection = new Selection();
  50. selection.addRange( range );
  51. expect( getData( p, selection ) ).to.equal( '<p>[]foobar</p>' );
  52. } );
  53. it( 'should write selection ranges inside text', () => {
  54. const text1 = new Text( 'foobar' );
  55. const text2 = new Text( 'bazqux' );
  56. const b1 = new Element( 'b', null, text1 );
  57. const b2 = new Element( 'b', null, text2 );
  58. const p = new Element( 'p', null, [ b1, b2 ] );
  59. const range = Range.createFromParentsAndOffsets( text1, 1, text1, 5 );
  60. const selection = new Selection();
  61. selection.addRange( range );
  62. expect( getData( p, selection ) ).to.equal( '<p><b>f{ooba}r</b><b>bazqux</b></p>' );
  63. } );
  64. it( 'should write ranges that start inside text end ends between elements', () => {
  65. const text1 = new Text( 'foobar' );
  66. const text2 = new Text( 'bazqux' );
  67. const b1 = new Element( 'b', null, text1 );
  68. const b2 = new Element( 'b', null, text2 );
  69. const p = new Element( 'p', null, [ b1, b2 ] );
  70. const range = Range.createFromParentsAndOffsets( p, 0, text2, 5 );
  71. const selection = new Selection();
  72. selection.addRange( range );
  73. expect( getData( p, selection ) ).to.equal( '<p>[<b>foobar</b><b>bazqu}x</b></p>' );
  74. } );
  75. it( 'should write elements types as namespaces when needed', () => {
  76. const text = new Text( 'foobar' );
  77. const b = new AttributeElement( 'b', null, text );
  78. const p = new ContainerElement( 'p', null, b );
  79. expect( getData( p, null, { showType: true } ) )
  80. .to.equal( '<container:p><attribute:b>foobar</attribute:b></container:p>' );
  81. } );
  82. it( 'should write elements priorities when needed', () => {
  83. const text = new Text( 'foobar' );
  84. const b = new AttributeElement( 'b', null, text );
  85. const p = new ContainerElement( 'p', null, b );
  86. expect( getData( p, null, { showType: true, showPriority: true } ) )
  87. .to.equal( '<container:p><attribute:b priority=10>foobar</attribute:b></container:p>' );
  88. } );
  89. it( 'should parse DocumentFragment as root', () => {
  90. const text1 = new Text( 'foobar' );
  91. const text2 = new Text( 'bazqux' );
  92. const b1 = new Element( 'b', null, text1 );
  93. const b2 = new Element( 'b', null, text2 );
  94. const fragment = new DocumentFragment( [ b1, b2 ] );
  95. expect( getData( fragment, null ) ).to.equal( '<b>foobar</b><b>bazqux</b>' );
  96. } );
  97. // TODO: test when range is outside element/text
  98. } );
  99. } );