8
0

jumpoverinlinefiller.js 5.0 KB

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