jumpoverinlinefiller.js 5.1 KB

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