8
0

filler.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  6. import isText from '@ckeditor/ckeditor5-utils/src/dom/istext';
  7. /**
  8. * Set of utils related to block and inline fillers handling.
  9. *
  10. * Browsers do not allow to put caret in elements which does not have height. Because of it, we need to fill all
  11. * empty elements which should be selectable with elements or characters called "fillers". Unfortunately there is no one
  12. * universal filler, this is why two types are uses:
  13. *
  14. * * Block filler is an element which fill block elements, like `<p>`. CKEditor uses `<br>` as a block filler during the editing,
  15. * as browsers do natively. So instead of an empty `<p>` there will be `<p><br></p>`. The advantage of block filler is that
  16. * it is transparent for the selection, so when the caret is before the `<br>` and user presses right arrow he will be
  17. * moved to the next paragraph, not after the `<br>`. The disadvantage is that it breaks a block, so it can not be used
  18. * in the middle of a line of text. The {@link module:engine/view/filler~BR_FILLER `<br>` filler} can be replaced with any other
  19. * character in the data output, for instance {@link module:engine/view/filler~NBSP_FILLER non-breaking space}.
  20. *
  21. * * Inline filler is a filler which does not break a line of text, so it can be used inside the text, for instance in the empty
  22. * `<b>` surrendered by text: `foo<b></b>bar`, if we want to put the caret there. CKEditor uses a sequence of the zero-width
  23. * spaces as an {@link module:engine/view/filler~INLINE_FILLER inline filler} having the predetermined
  24. * {@link module:engine/view/filler~INLINE_FILLER_LENGTH length}. A sequence is used, instead of a single character to
  25. * avoid treating random zero-width spaces as the inline filler. Disadvantage of the inline filler is that it is not
  26. * transparent for the selection. The arrow key moves the caret between zero-width spaces characters, so the additional
  27. * code is needed to handle the caret.
  28. *
  29. * Both inline and block fillers are handled by the {@link module:engine/view/renderer~Renderer renderer} and are not present in the
  30. * view.
  31. *
  32. * @module engine/view/filler
  33. */
  34. /**
  35. * Non-breaking space filler creator. This is a function which creates `&nbsp;` text node.
  36. * It defines how the filler is created.
  37. *
  38. * @see module:engine/view/filler~BR_FILLER
  39. * @function
  40. */
  41. export const NBSP_FILLER = domDocument => domDocument.createTextNode( '\u00A0' );
  42. /**
  43. * `<br>` filler creator. This is a function which creates `<br data-cke-filler="true">` element.
  44. * It defines how the filler is created.
  45. *
  46. * @see module:engine/view/filler~NBSP_FILLER
  47. * @function
  48. */
  49. export const BR_FILLER = domDocument => {
  50. const fillerBr = domDocument.createElement( 'br' );
  51. fillerBr.dataset.ckeFiller = true;
  52. return fillerBr;
  53. };
  54. /**
  55. * Length of the {@link module:engine/view/filler~INLINE_FILLER INLINE_FILLER}.
  56. */
  57. export const INLINE_FILLER_LENGTH = 7;
  58. /**
  59. * Inline filler which is a sequence of the zero width spaces.
  60. *
  61. * @type {String}
  62. */
  63. export const INLINE_FILLER = ( () => {
  64. let inlineFiller = '';
  65. for ( let i = 0; i < INLINE_FILLER_LENGTH; i++ ) {
  66. inlineFiller += '\u200b';
  67. }
  68. return inlineFiller;
  69. } )(); // Usu IIF so the INLINE_FILLER appears as a constant in the docs.
  70. /**
  71. * Checks if the node is a text node which starts with the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
  72. *
  73. * startsWithFiller( document.createTextNode( INLINE_FILLER ) ); // true
  74. * startsWithFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ); // true
  75. * startsWithFiller( document.createTextNode( 'foo' ) ); // false
  76. * startsWithFiller( document.createElement( 'p' ) ); // false
  77. *
  78. * @param {Node} domNode DOM node.
  79. * @returns {Boolean} True if the text node starts with the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
  80. */
  81. export function startsWithFiller( domNode ) {
  82. return isText( domNode ) && ( domNode.data.substr( 0, INLINE_FILLER_LENGTH ) === INLINE_FILLER );
  83. }
  84. /**
  85. * Checks if the text node contains only the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
  86. *
  87. * isInlineFiller( document.createTextNode( INLINE_FILLER ) ); // true
  88. * isInlineFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ); // false
  89. *
  90. * @param {Text} domText DOM text node.
  91. * @returns {Boolean} True if the text node contains only the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
  92. */
  93. export function isInlineFiller( domText ) {
  94. return domText.data.length == INLINE_FILLER_LENGTH && startsWithFiller( domText );
  95. }
  96. /**
  97. * Get string data from the text node, removing an {@link module:engine/view/filler~INLINE_FILLER inline filler} from it,
  98. * if text node contains it.
  99. *
  100. * getDataWithoutFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ) == 'foo' // true
  101. * getDataWithoutFiller( document.createTextNode( 'foo' ) ) == 'foo' // true
  102. *
  103. * @param {Text} domText DOM text node, possible with inline filler.
  104. * @returns {String} Data without filler.
  105. */
  106. export function getDataWithoutFiller( domText ) {
  107. if ( startsWithFiller( domText ) ) {
  108. return domText.data.slice( INLINE_FILLER_LENGTH );
  109. } else {
  110. return domText.data;
  111. }
  112. }
  113. /**
  114. * Assign key observer which move cursor from the end of the inline filler to the beginning of it when
  115. * the left arrow is pressed, so the filler does not break navigation.
  116. *
  117. * @param {module:engine/view/view~View} view View controller instance we should inject quirks handling on.
  118. */
  119. export function injectQuirksHandling( view ) {
  120. view.document.on( 'keydown', jumpOverInlineFiller );
  121. }
  122. // Move cursor from the end of the inline filler to the beginning of it when, so the filler does not break navigation.
  123. function jumpOverInlineFiller( evt, data ) {
  124. if ( data.keyCode == keyCodes.arrowleft ) {
  125. const domSelection = data.domTarget.ownerDocument.defaultView.getSelection();
  126. if ( domSelection.rangeCount == 1 && domSelection.getRangeAt( 0 ).collapsed ) {
  127. const domParent = domSelection.getRangeAt( 0 ).startContainer;
  128. const domOffset = domSelection.getRangeAt( 0 ).startOffset;
  129. if ( startsWithFiller( domParent ) && domOffset <= INLINE_FILLER_LENGTH ) {
  130. domSelection.collapse( domParent, 0 );
  131. }
  132. }
  133. }
  134. }