jumpoverinlinefiller.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 View from '../../../src/view/view';
  7. import { INLINE_FILLER_LENGTH, isInlineFiller, startsWithFiller } from '../../../src/view/filler';
  8. import createViewRoot from '../_utils/createroot';
  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( 'View', () => {
  13. let view, viewDocument, domRoot;
  14. beforeEach( () => {
  15. domRoot = createElement( document, 'div', {
  16. contenteditable: 'true'
  17. } );
  18. document.body.appendChild( domRoot );
  19. view = new View();
  20. viewDocument = view.document;
  21. createViewRoot( viewDocument );
  22. view.attachDomRoot( domRoot );
  23. document.getSelection().removeAllRanges();
  24. viewDocument.isFocused = true;
  25. } );
  26. afterEach( () => {
  27. view.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( view, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  33. view.render();
  34. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: view.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( view, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  49. view.render();
  50. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: view.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( view, '<container:p>foo<attribute:b>{x}</attribute:b>bar</container:p>' );
  58. view.render();
  59. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: view.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( view, '<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. view.change( () => {
  78. setData( view, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  79. } );
  80. const domSelection = document.getSelection();
  81. view.change( writer => {
  82. // Insert a letter to the <b>: '<container:p>foo<attribute:b>x{}</attribute:b>bar</container:p>'
  83. // Do this both in the view and in the DOM to simulate typing and to avoid rendering (which would remove the filler).
  84. const viewB = writer.document.selection.getFirstPosition().parent;
  85. const viewTextX = parse( 'x' );
  86. viewB._appendChild( viewTextX );
  87. writer.setSelection( viewTextX, 1 );
  88. const domB = view.getDomRoot( 'main' ).querySelector( 'b' );
  89. domB.childNodes[ 0 ].data += 'x';
  90. const domRange = document.createRange();
  91. domSelection.removeAllRanges();
  92. domRange.setStart( domB.childNodes[ 0 ], INLINE_FILLER_LENGTH + 1 );
  93. domRange.collapse( true );
  94. domSelection.addRange( domRange );
  95. } );
  96. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: view.domRoots.get( 'main' ) } );
  97. expect( startsWithFiller( domSelection.anchorNode ) ).to.be.true;
  98. expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH + 1 );
  99. expect( domSelection.isCollapsed ).to.be.true;
  100. } );
  101. } );
  102. } );