breakattributes.js 3.5 KB

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