8
0

breakattributes.js 3.7 KB

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