UNPKG

1.74 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12
13var TokenizeUtil = require("fbjs/lib/TokenizeUtil");
14
15var punctuation = TokenizeUtil.getPunctuation(); // The apostrophe and curly single quotes behave in a curious way: when
16// surrounded on both sides by word characters, they behave as word chars; when
17// either neighbor is punctuation or an end of the string, they behave as
18// punctuation.
19
20var CHAMELEON_CHARS = "['\u2018\u2019]"; // Remove the underscore, which should count as part of the removable word. The
21// "chameleon chars" also count as punctuation in this regex.
22
23var WHITESPACE_AND_PUNCTUATION = '\\s|(?![_])' + punctuation;
24var DELETE_STRING = '^' + '(?:' + WHITESPACE_AND_PUNCTUATION + ')*' + '(?:' + CHAMELEON_CHARS + '|(?!' + WHITESPACE_AND_PUNCTUATION + ').)*' + '(?:(?!' + WHITESPACE_AND_PUNCTUATION + ').)';
25var DELETE_REGEX = new RegExp(DELETE_STRING);
26var BACKSPACE_STRING = '(?:(?!' + WHITESPACE_AND_PUNCTUATION + ').)' + '(?:' + CHAMELEON_CHARS + '|(?!' + WHITESPACE_AND_PUNCTUATION + ').)*' + '(?:' + WHITESPACE_AND_PUNCTUATION + ')*' + '$';
27var BACKSPACE_REGEX = new RegExp(BACKSPACE_STRING);
28
29function getRemovableWord(text, isBackward) {
30 var matches = isBackward ? BACKSPACE_REGEX.exec(text) : DELETE_REGEX.exec(text);
31 return matches ? matches[0] : text;
32}
33
34var DraftRemovableWord = {
35 getBackward: function getBackward(text) {
36 return getRemovableWord(text, true);
37 },
38 getForward: function getForward(text) {
39 return getRemovableWord(text, false);
40 }
41};
42module.exports = DraftRemovableWord;
\No newline at end of file