8
0

jumpoverinlinefiller.js 4.7 KB

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