breakAt.js 3.3 KB

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