breakattributes.js 3.7 KB

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