filler.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window, Range, Text */
  6. import { keyCodes } from '../../utils/keyboard.js';
  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 engine.view.filler.BR_FILLER `<br>` filler} can be replaced with any other
  19. * character in the data output, for instance {@link 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 engine.view.filler.INLINE_FILLER inline filler} having the predetermined
  24. * {@link 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 engine.view.renderer renderer} and are not present in the
  30. * view.
  31. *
  32. * @namespace 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 engine.view.filler.NBSP_FILLER_FILLER
  39. * @member {Function} engine.view.filler.BR_FILLER
  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 engine.view.filler.BR_FILLER
  51. * @member {Function} engine.view.filler.NBSP_FILLER_FILLER
  52. */
  53. export const NBSP_FILLER = ( domDocument ) => domDocument.createTextNode( '\u00A0' );
  54. /**
  55. * Length of the {@link engine.view.filler.INLINE_FILLER INLINE_FILLER}.
  56. *
  57. * @member {Function} engine.view.filler.INLINE_FILLER_LENGTH
  58. */
  59. export const INLINE_FILLER_LENGTH = 7;
  60. /**
  61. * Inline filler which is sequence of the zero width spaces.
  62. *
  63. * @member {String} engine.view.filler.INLINE_FILLER
  64. */
  65. export let INLINE_FILLER = '';
  66. for ( let i = 0; i < INLINE_FILLER_LENGTH; i++ ) {
  67. INLINE_FILLER += '\u200b';
  68. }
  69. /**
  70. * Checks if the node is a text node which starts with the {@link engine.view.filler.INLINE_FILLER inline filler}.
  71. *
  72. * startsWithFiller( document.createTextNode( INLINE_FILLER ) ); // true
  73. * startsWithFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ); // true
  74. * startsWithFiller( document.createTextNode( 'foo' ) ); // false
  75. * startsWithFiller( document.createElement( 'p' ) ); // false
  76. *
  77. * @param {Node} domNode DOM node.
  78. * @returns {Boolean} True if the text node starts with the {@link engine.view.filler.INLINE_FILLER inline filler}.
  79. */
  80. export function startsWithFiller( domNode ) {
  81. return ( domNode instanceof Text ) && ( domNode.data.substr( 0, INLINE_FILLER_LENGTH ) === INLINE_FILLER );
  82. }
  83. /**
  84. * Checks if the text node contains only the {@link engine.view.filler.INLINE_FILLER inline filler}.
  85. *
  86. * isInlineFiller( document.createTextNode( INLINE_FILLER ) ); // true
  87. * isInlineFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ); // false
  88. *
  89. * @param {Text} domText DOM text node.
  90. * @returns {Boolean} True if the text node contains only the {@link engine.view.filler.INLINE_FILLER inline filler}.
  91. */
  92. export function isInlineFiller( domText ) {
  93. return domText.data.length == INLINE_FILLER_LENGTH && startsWithFiller( domText );
  94. }
  95. /**
  96. * Get string data from the text node, removing an {@link engine.view.filler.INLINE_FILLER inline filler} from it,
  97. * if text node contains it.
  98. *
  99. * getDataWithoutFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ) == 'foo' // true
  100. * getDataWithoutFiller( document.createTextNode( 'foo' ) ) == 'foo' // true
  101. *
  102. * @param {Text} domText DOM text node, possible with inline filler.
  103. * @returns {String} Data without filler.
  104. */
  105. export function getDataWithoutFiller( domText ) {
  106. if ( startsWithFiller( domText ) ) {
  107. return domText.data.slice( INLINE_FILLER_LENGTH );
  108. } else {
  109. return domText.data;
  110. }
  111. }
  112. // Cache block fillers templates to improve performance.
  113. const templateBlockFillers = new WeakMap();
  114. /**
  115. * Checks if the node is an instance of the block filler of the given type.
  116. *
  117. * const brFillerInstance = BR_FILLER( document );
  118. * isBlockFiller( brFillerInstance, BR_FILLER ); // true
  119. *
  120. * @param {Node} domNode DOM node to check.
  121. * @param {Function} blockFiller Block filler creator.
  122. * @returns {Boolean} True if text node contains only {@link engine.view.filler.INLINE_FILLER inline filler}.
  123. */
  124. export function isBlockFiller( domNode, blockFiller ) {
  125. let templateBlockFiller = templateBlockFillers.get( blockFiller );
  126. if ( !templateBlockFiller ) {
  127. templateBlockFiller = blockFiller( window.document );
  128. templateBlockFillers.set( blockFiller, templateBlockFiller );
  129. }
  130. return domNode.isEqualNode( templateBlockFiller );
  131. }
  132. /**
  133. * Assign key observer which move cursor from the end of the inline filler to the begging of it when
  134. * the left arrow is pressed, so the filler does not break navigation.
  135. *
  136. * @param {engine.view.Document} document Document instance we should inject quirks handling on.
  137. */
  138. export function injectQuirksHandling( document ) {
  139. document.on( 'keydown', jumpOverInlineFiller );
  140. }
  141. // Move cursor from the end of the inline filler to the begging of it when, so the filler does not break navigation.
  142. function jumpOverInlineFiller( evt, data ) {
  143. if ( data.keyCode == keyCodes.arrowleft ) {
  144. const domSelection = data.domTarget.ownerDocument.defaultView.getSelection();
  145. if ( domSelection.rangeCount == 1 && domSelection.getRangeAt( 0 ).collapsed ) {
  146. const domParent = domSelection.getRangeAt( 0 ).startContainer;
  147. const domOffset = domSelection.getRangeAt( 0 ).startOffset;
  148. if ( startsWithFiller( domParent ) && domOffset <= INLINE_FILLER_LENGTH ) {
  149. const domRange = new Range();
  150. domRange.setStart( domParent, 0 );
  151. domRange.collapse( true );
  152. domSelection.removeAllRanges();
  153. domSelection.addRange( domRange );
  154. }
  155. }
  156. }
  157. }