breakAt.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view, browser-only */
  6. import { breakAt } from '/ckeditor5/engine/view/writer.js';
  7. import { stringify, parse } from '/tests/engine/_utils/view.js';
  8. describe( 'writer', () => {
  9. /**
  10. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  11. * test break position.
  12. *
  13. * @param {String} input
  14. * @param {String} expected
  15. */
  16. function test( input, expected ) {
  17. let { view, selection } = parse( input );
  18. const newPosition = breakAt( selection.getFirstPosition() );
  19. expect( stringify( view.root, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected );
  20. }
  21. describe( 'breakAt', () => {
  22. it( 'should not break text nodes if they are not in attribute elements - middle', () => {
  23. test(
  24. '<container:p>foo{}bar</container:p>',
  25. '<container:p>foo{}bar</container:p>'
  26. );
  27. } );
  28. it( 'should not break text nodes if they are not in attribute elements - beginning', () => {
  29. test(
  30. '<container:p>{}foobar</container:p>',
  31. '<container:p>{}foobar</container:p>'
  32. );
  33. } );
  34. it( 'should not break text nodes if they are not in attribute elements #2 - end', () => {
  35. test(
  36. '<container:p>foobar{}</container:p>',
  37. '<container:p>foobar{}</container:p>'
  38. );
  39. } );
  40. it( 'should split attribute element', () => {
  41. test(
  42. '<container:p><attribute:b view-priority="1">foo{}bar</attribute:b></container:p>',
  43. '<container:p>' +
  44. '<attribute:b view-priority="1">foo</attribute:b>[]<attribute:b view-priority="1">bar</attribute:b>' +
  45. '</container:p>'
  46. );
  47. } );
  48. it( 'should move from beginning of the nested text node to the container', () => {
  49. test(
  50. '<container:p>' +
  51. '<attribute:b view-priority="1"><attribute:u view-priority="1">{}foobar</attribute:u></attribute:b>' +
  52. '</container:p>',
  53. '<container:p>' +
  54. '[]<attribute:b view-priority="1"><attribute:u view-priority="1">foobar</attribute:u></attribute:b>' +
  55. '</container:p>'
  56. );
  57. } );
  58. it( 'should stick selection in text node if it is in container', () => {
  59. test(
  60. '<container:p>foo{}<attribute:b view-priority="1">bar</attribute:b></container:p>',
  61. '<container:p>foo{}<attribute:b view-priority="1">bar</attribute:b></container:p>'
  62. );
  63. } );
  64. it( 'should split nested attributes', () => {
  65. test(
  66. '<container:p>' +
  67. '<attribute:b view-priority="1"><attribute:u view-priority="1">foo{}bar</attribute:u></attribute:b>' +
  68. '</container:p>',
  69. '<container:p>' +
  70. '<attribute:b view-priority="1">' +
  71. '<attribute:u view-priority="1">' +
  72. 'foo' +
  73. '</attribute:u>' +
  74. '</attribute:b>' +
  75. '[]' +
  76. '<attribute:b view-priority="1">' +
  77. '<attribute:u view-priority="1">' +
  78. 'bar' +
  79. '</attribute:u>' +
  80. '</attribute:b>' +
  81. '</container:p>'
  82. );
  83. } );
  84. it( 'should move from end of the nested text node to the container', () => {
  85. test(
  86. '<container:p>' +
  87. '<attribute:b view-priority="1"><attribute:u view-priority="1">foobar{}</attribute:u></attribute:b>' +
  88. '</container:p>',
  89. '<container:p>' +
  90. '<attribute:b view-priority="1"><attribute:u view-priority="1">foobar</attribute:u></attribute:b>[]' +
  91. '</container:p>'
  92. );
  93. } );
  94. it( 'should split attribute element directly in document fragment', () => {
  95. test(
  96. '<attribute:b view-priority="1">foo{}bar</attribute:b>',
  97. '<attribute:b view-priority="1">foo</attribute:b>[]<attribute:b view-priority="1">bar</attribute:b>'
  98. );
  99. } );
  100. it( 'should not split text directly in document fragment', () => {
  101. test(
  102. 'foo{}bar',
  103. 'foo{}bar'
  104. );
  105. } );
  106. } );
  107. } );