filler.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window, Text */
  6. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  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. * `<br> filler creator. This is a function which creates `<br data-cke-filler="true">` element.
  36. * It defines how the filler is created.
  37. *
  38. * @see module:engine/view/filler~NBSP_FILLER
  39. * @function
  40. */
  41. export const BR_FILLER = domDocument => {
  42. const fillerBr = domDocument.createElement( 'br' );
  43. fillerBr.dataset.ckeFiller = true;
  44. return fillerBr;
  45. };
  46. /**
  47. * Non-breaking space filler creator. This is a function which creates `&nbsp;` text node.
  48. * It defines how the filler is created.
  49. *
  50. * @see module:engine/view/filler~BR_FILLER
  51. * @function
  52. */
  53. export const NBSP_FILLER = domDocument => domDocument.createTextNode( '\u00A0' );
  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 sequence of the zero width spaces.
  60. */
  61. export let INLINE_FILLER = '';
  62. for ( let i = 0; i < INLINE_FILLER_LENGTH; i++ ) {
  63. INLINE_FILLER += '\u200b';
  64. }
  65. /**
  66. * Checks if the node is a text node which starts with the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
  67. *
  68. * startsWithFiller( document.createTextNode( INLINE_FILLER ) ); // true
  69. * startsWithFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ); // true
  70. * startsWithFiller( document.createTextNode( 'foo' ) ); // false
  71. * startsWithFiller( document.createElement( 'p' ) ); // false
  72. *
  73. * @param {Node} domNode DOM node.
  74. * @returns {Boolean} True if the text node starts with the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
  75. */
  76. export function startsWithFiller( domNode ) {
  77. return ( domNode instanceof Text ) && ( domNode.data.substr( 0, INLINE_FILLER_LENGTH ) === INLINE_FILLER );
  78. }
  79. /**
  80. * Checks if the text node contains only the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
  81. *
  82. * isInlineFiller( document.createTextNode( INLINE_FILLER ) ); // true
  83. * isInlineFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ); // false
  84. *
  85. * @param {Text} domText DOM text node.
  86. * @returns {Boolean} True if the text node contains only the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
  87. */
  88. export function isInlineFiller( domText ) {
  89. return domText.data.length == INLINE_FILLER_LENGTH && startsWithFiller( domText );
  90. }
  91. /**
  92. * Get string data from the text node, removing an {@link module:engine/view/filler~INLINE_FILLER inline filler} from it,
  93. * if text node contains it.
  94. *
  95. * getDataWithoutFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ) == 'foo' // true
  96. * getDataWithoutFiller( document.createTextNode( 'foo' ) ) == 'foo' // true
  97. *
  98. * @param {Text} domText DOM text node, possible with inline filler.
  99. * @returns {String} Data without filler.
  100. */
  101. export function getDataWithoutFiller( domText ) {
  102. if ( startsWithFiller( domText ) ) {
  103. return domText.data.slice( INLINE_FILLER_LENGTH );
  104. } else {
  105. return domText.data;
  106. }
  107. }
  108. // Cache block fillers templates to improve performance.
  109. const templateBlockFillers = new WeakMap();
  110. /**
  111. * Checks if the node is an instance of the block filler of the given type.
  112. *
  113. * const brFillerInstance = BR_FILLER( document );
  114. * isBlockFiller( brFillerInstance, BR_FILLER ); // true
  115. *
  116. * @param {Node} domNode DOM node to check.
  117. * @param {Function} blockFiller Block filler creator.
  118. * @returns {Boolean} True if text node contains only {@link module:engine/view/filler~INLINE_FILLER inline filler}.
  119. */
  120. export function isBlockFiller( domNode, blockFiller ) {
  121. let templateBlockFiller = templateBlockFillers.get( blockFiller );
  122. if ( !templateBlockFiller ) {
  123. templateBlockFiller = blockFiller( window.document );
  124. templateBlockFillers.set( blockFiller, templateBlockFiller );
  125. }
  126. return domNode.isEqualNode( templateBlockFiller );
  127. }
  128. /**
  129. * Assign key observer which move cursor from the end of the inline filler to the beginning of it when
  130. * the left arrow is pressed, so the filler does not break navigation.
  131. *
  132. * @param {module:engine/view/document~Document} document Document instance we should inject quirks handling on.
  133. */
  134. export function injectQuirksHandling( document ) {
  135. document.on( 'keydown', jumpOverInlineFiller );
  136. }
  137. // Move cursor from the end of the inline filler to the beginning of it when, so the filler does not break navigation.
  138. function jumpOverInlineFiller( evt, data ) {
  139. if ( data.keyCode == keyCodes.arrowleft ) {
  140. const domSelection = data.domTarget.ownerDocument.defaultView.getSelection();
  141. if ( domSelection.rangeCount == 1 && domSelection.getRangeAt( 0 ).collapsed ) {
  142. const domParent = domSelection.getRangeAt( 0 ).startContainer;
  143. const domOffset = domSelection.getRangeAt( 0 ).startOffset;
  144. if ( startsWithFiller( domParent ) && domOffset <= INLINE_FILLER_LENGTH ) {
  145. domSelection.collapse( domParent, 0 );
  146. }
  147. }
  148. }
  149. }