jumpoverinlinefiller.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // There's no destroy() method yet, so let's at least kill the observers.
  27. viewDocument.disableObservers();
  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. expect( domSelection.anchorNode.data ).to.equal( 'foo' );
  37. expect( domSelection.anchorOffset ).to.equal( 3 );
  38. expect( domSelection.isCollapsed ).to.be.true;
  39. } );
  40. it( 'should do nothing when another key is pressed', () => {
  41. setData( viewDocument, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  42. viewDocument.render();
  43. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
  44. const domSelection = document.getSelection();
  45. expect( isInlineFiller( domSelection.anchorNode ) ).to.be.true;
  46. expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH );
  47. expect( domSelection.isCollapsed ).to.be.true;
  48. } );
  49. it( 'should do nothing if range is not collapsed', () => {
  50. setData( viewDocument, '<container:p>foo<attribute:b>{x}</attribute:b>bar</container:p>' );
  51. viewDocument.render();
  52. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
  53. const domSelection = document.getSelection();
  54. expect( domSelection.anchorNode.data ).to.equal( 'x' );
  55. expect( domSelection.anchorOffset ).to.equal( 0 );
  56. expect( domSelection.focusNode.data ).to.equal( 'x' );
  57. expect( domSelection.focusOffset ).to.equal( 1 );
  58. } );
  59. // See #664
  60. // it( 'should do nothing if node does not start with the filler', () => {
  61. // setData( viewDocument, '<container:p>foo<attribute:b>{}x</attribute:b>bar</container:p>' );
  62. // viewDocument.render();
  63. // viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
  64. // const domSelection = document.getSelection();
  65. // expect( domSelection.anchorNode.data ).to.equal( 'x' );
  66. // expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH );
  67. // expect( domSelection.isCollapsed ).to.be.true;
  68. // } );
  69. it( 'should do nothing if caret is not directly before the filler', () => {
  70. setData( viewDocument, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  71. viewDocument.render();
  72. // Insert a letter to the <b>: '<container:p>foo<attribute:b>x{}</attribute:b>bar</container:p>'
  73. // Do this both in the view and in the DOM to simulate typing and to avoid rendering (which would remove the filler).
  74. const viewB = viewDocument.selection.getFirstPosition().parent;
  75. const viewTextX = parse( 'x' );
  76. viewB.appendChildren( viewTextX );
  77. viewDocument.selection.removeAllRanges();
  78. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewTextX, 1, viewTextX, 1 ) );
  79. const domB = viewDocument.getDomRoot( 'main' ).querySelector( 'b' );
  80. const domSelection = document.getSelection();
  81. domB.childNodes[ 0 ].data += 'x';
  82. const domRange = new Range();
  83. domSelection.removeAllRanges();
  84. domRange.setStart( domB.childNodes[ 0 ], INLINE_FILLER_LENGTH + 1 );
  85. domRange.collapse( true );
  86. domSelection.addRange( domRange );
  87. viewDocument.render();
  88. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
  89. expect( startsWithFiller( domSelection.anchorNode ) ).to.be.true;
  90. expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH + 1 );
  91. expect( domSelection.isCollapsed ).to.be.true;
  92. } );
  93. } );
  94. } );