jumpoverinlinefiller.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ViewRange from '../../../src/view/range';
  7. import ViewDocument from '../../../src/view/document';
  8. import { INLINE_FILLER_LENGTH, isInlineFiller, startsWithFiller } from '../../../src/view/filler';
  9. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  10. import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
  11. import { parse, setData } from '../../../src/dev-utils/view';
  12. describe( 'Document', () => {
  13. let viewDocument, domRoot;
  14. beforeEach( () => {
  15. domRoot = createElement( document, 'div', {
  16. contenteditable: 'true'
  17. } );
  18. document.body.appendChild( domRoot );
  19. viewDocument = new ViewDocument();
  20. viewDocument.createRoot( domRoot );
  21. document.getSelection().removeAllRanges();
  22. viewDocument.isFocused = true;
  23. } );
  24. afterEach( () => {
  25. viewDocument.destroy();
  26. domRoot.parentElement.removeChild( domRoot );
  27. } );
  28. describe( 'jump over inline filler hack', () => {
  29. it( 'should jump over inline filler when left arrow is pressed after inline filler', () => {
  30. setData( viewDocument, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  31. viewDocument.render();
  32. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
  33. const domSelection = document.getSelection();
  34. expect( domSelection.anchorNode.data ).to.equal( 'foo' );
  35. expect( domSelection.anchorOffset ).to.equal( 3 );
  36. expect( domSelection.isCollapsed ).to.be.true;
  37. } );
  38. it( 'should do nothing when another key is pressed', () => {
  39. setData( viewDocument, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  40. viewDocument.render();
  41. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
  42. const domSelection = document.getSelection();
  43. expect( isInlineFiller( domSelection.anchorNode ) ).to.be.true;
  44. expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH );
  45. expect( domSelection.isCollapsed ).to.be.true;
  46. } );
  47. it( 'should do nothing if range is not collapsed', () => {
  48. setData( viewDocument, '<container:p>foo<attribute:b>{x}</attribute:b>bar</container:p>' );
  49. viewDocument.render();
  50. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
  51. const domSelection = document.getSelection();
  52. expect( domSelection.anchorNode.data ).to.equal( 'x' );
  53. expect( domSelection.anchorOffset ).to.equal( 0 );
  54. expect( domSelection.focusNode.data ).to.equal( 'x' );
  55. expect( domSelection.focusOffset ).to.equal( 1 );
  56. } );
  57. // See #664
  58. // it( 'should do nothing if node does not start with the filler', () => {
  59. // setData( viewDocument, '<container:p>foo<attribute:b>{}x</attribute:b>bar</container:p>' );
  60. // viewDocument.render();
  61. // viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
  62. // const domSelection = document.getSelection();
  63. // expect( domSelection.anchorNode.data ).to.equal( 'x' );
  64. // expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH );
  65. // expect( domSelection.isCollapsed ).to.be.true;
  66. // } );
  67. it( 'should do nothing if caret is not directly before the filler', () => {
  68. setData( viewDocument, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  69. viewDocument.render();
  70. // Insert a letter to the <b>: '<container:p>foo<attribute:b>x{}</attribute:b>bar</container:p>'
  71. // Do this both in the view and in the DOM to simulate typing and to avoid rendering (which would remove the filler).
  72. const viewB = viewDocument.selection.getFirstPosition().parent;
  73. const viewTextX = parse( 'x' );
  74. viewB.appendChildren( viewTextX );
  75. viewDocument.selection.removeAllRanges();
  76. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewTextX, 1, viewTextX, 1 ) );
  77. const domB = viewDocument.getDomRoot( 'main' ).querySelector( 'b' );
  78. const domSelection = document.getSelection();
  79. domB.childNodes[ 0 ].data += 'x';
  80. const domRange = document.createRange();
  81. domSelection.removeAllRanges();
  82. domRange.setStart( domB.childNodes[ 0 ], INLINE_FILLER_LENGTH + 1 );
  83. domRange.collapse( true );
  84. domSelection.addRange( domRange );
  85. viewDocument.render();
  86. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
  87. expect( startsWithFiller( domSelection.anchorNode ) ).to.be.true;
  88. expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH + 1 );
  89. expect( domSelection.isCollapsed ).to.be.true;
  90. } );
  91. } );
  92. } );