Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2acc28

Browse files
committed
fix(mention-plugin): avoid infinite loop while mentionTrigger is empty and supportWhiteSpace is on. fix replace issue while mentionTrigger is empty
1 parent 7566e49 commit d2acc28

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

draft-js-mention-plugin/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## 3.1.3
7+
- Force update regex's `lastIndex` to avoid infinite loop
8+
- Fixed replace issue while `mentionTrigger` is empty
9+
610
## 3.1.2
711
- Allow empty `mentionTrigger` with `supportWhitespace: true` #1182
812

draft-js-mention-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "draft-js-mention-plugin",
3-
"version": "3.1.2",
3+
"version": "3.1.3",
44
"description": "Mention Plugin for DraftJS",
55
"author": {
66
"name": "Nik Graf",

draft-js-mention-plugin/src/mentionSuggestionsStrategy.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ const findWithRegex = (regex, contentBlock, callback) => {
1212
const text = contentBlockText.slice(nonEntityStart, nonEntityEnd);
1313
let matchArr;
1414
let start;
15+
let prevLastIndex = regex.lastIndex;
1516

1617
// Go through all matches in the text and return the indices to the callback
18+
// Force update regex's lastIndex to avoid Infinite loop
1719
while ((matchArr = regex.exec(text)) !== null) { // eslint-disable-line
20+
if (regex.lastIndex === prevLastIndex) {
21+
regex.lastIndex += 1;
22+
}
23+
prevLastIndex = regex.lastIndex;
1824
start = nonEntityStart + matchArr.index;
1925
callback(start, start + matchArr[0].length);
2026
}

draft-js-mention-plugin/src/utils/getSearchTextAt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
export default (blockText: string, position: number, trigger: string) => {
77
const str = blockText.substr(0, position);
8-
const begin = str.lastIndexOf(trigger);
8+
const begin = trigger.length === 0 ? 0 : str.lastIndexOf(trigger);
99
const matchingString = trigger.length === 0 ? str : str.slice(begin + trigger.length);
1010
const end = str.length;
1111

0 commit comments

Comments
 (0)