@cometchat/chat-uikit-react-native 5.4.1 → 5.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/reactnativecometchatuikit/CometChatClipboardModule.kt +34 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +562 -5
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorViewManager.kt +28 -1
- package/ios/CometChatClipboardModule.m +5 -0
- package/ios/CometChatClipboardModule.swift +22 -0
- package/ios/RichTextEditorView.swift +636 -31
- package/ios/RichTextEditorViewManager.m +3 -0
- package/ios/RichTextEditorViewManager.swift +24 -0
- package/package.json +5 -2
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +283 -64
- package/src/CometChatCompactMessageComposer/index.ts +2 -0
- package/src/CometChatConversations/CometChatConversations.tsx +299 -15
- package/src/CometChatGroupMembers/CometChatGroupMembers.tsx +2 -2
- package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +15 -3
- package/src/CometChatMessageHeader/CometChatMessageHeader.tsx +181 -12
- package/src/CometChatMessageInformation/CometChatMessageInformation.tsx +10 -1
- package/src/CometChatMessageList/CometChatMessageList.tsx +572 -16
- package/src/CometChatMessageList/components/MessageModals.tsx +30 -0
- package/src/CometChatMessageList/components/MessageOptionsSheet.tsx +21 -9
- package/src/CometChatMessageList/components/MessageQuickActions.tsx +162 -0
- package/src/CometChatPinnedMessages/CometChatPinnedMessages.tsx +964 -0
- package/src/CometChatPinnedMessages/index.ts +5 -0
- package/src/CometChatPinnedMessages/style.ts +216 -0
- package/src/CometChatRichTextEditor/RichTextEditorViewNativeComponent.ts +5 -0
- package/src/CometChatRichTextEditor/index.tsx +31 -1
- package/src/CometChatRichTextEditor/types.ts +21 -1
- package/src/CometChatSavedMessages/CometChatSavedMessages.tsx +871 -0
- package/src/CometChatSavedMessages/index.ts +5 -0
- package/src/CometChatSavedMessages/style.ts +159 -0
- package/src/CometChatSearch/CometChatSearch.tsx +71 -341
- package/src/CometChatThreadHeader/CometChatThreadHeader.tsx +22 -2
- package/src/index.ts +60 -0
- package/src/shared/CometChatUiKit/CometChatUIKit.ts +18 -1
- package/src/shared/base/Types.ts +5 -0
- package/src/shared/constants/UIKitConstants.ts +16 -17
- package/src/shared/constants/textPatterns.ts +27 -0
- package/src/shared/events/CometChatUIEventHandler/CometChatUIEventHandler.ts +25 -225
- package/src/shared/events/CometChatUIEventHandler/Listener.ts +37 -0
- package/src/shared/events/ListenerInitializer.ts +20 -1
- package/src/shared/events/messages.ts +18 -0
- package/src/shared/formatters/CometChatRichTextFormatter/index.tsx +123 -25
- package/src/shared/formatters/CometChatTextFormatter.ts +22 -0
- package/src/shared/formatters/applyRawFormatters.ts +31 -0
- package/src/shared/formatters/index.ts +14 -0
- package/src/shared/formatters/richTextWireFormat.ts +165 -0
- package/src/shared/framework/MessageDataSource.tsx +309 -10
- package/src/shared/icons/components/thread-follow.tsx +13 -0
- package/src/shared/icons/components/thread-unfollow.tsx +13 -0
- package/src/shared/icons/components/unsave.tsx +13 -0
- package/src/shared/icons/icon-mapping.tsx +6 -0
- package/src/shared/index.ts +37 -0
- package/src/shared/resources/CometChatLocalizeNew/CometChatI18nProvider.tsx +15 -30
- package/src/shared/resources/CometChatLocalizeNew/CometChatLocalizationHelper.ts +138 -19
- package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +74 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +54 -1
- package/src/shared/utils/CometChatMessagePreview/CometChatMessagePreview.tsx +11 -0
- package/src/shared/utils/MarkdownUtils.ts +57 -6
- package/src/shared/utils/MessageUtils.tsx +80 -5
- package/src/shared/utils/PinConversationHelper.ts +245 -0
- package/src/shared/utils/PinSaveFeatureGates.ts +178 -0
- package/src/shared/utils/PinSaveHelper.ts +549 -0
- package/src/shared/utils/PinnedMessagesHelper.ts +65 -0
- package/src/shared/utils/SavedMessagesHelper.ts +267 -0
- package/src/shared/utils/ThreadSubscriptionHelper.ts +427 -0
- package/src/shared/utils/__testUtils__/messagesRequestBuilderMock.ts +37 -0
- package/src/shared/utils/cloneRequestBuilder.ts +25 -0
- package/src/shared/utils/conversationUtils.ts +8 -1
- package/src/shared/utils/stripRichText.ts +7 -0
- package/src/shared/views/ClipboardPasteHandler/ClipboardPasteHandler.ts +32 -0
- package/src/shared/views/ClipboardPasteHandler/NativeCometChatClipboardModule.ts +6 -0
- package/src/shared/views/CometChatActionSheet/CometChatActionSheet.tsx +4 -0
- package/src/shared/views/CometChatAudiosBubble/CometChatAudiosBubble.tsx +5 -0
- package/src/shared/views/CometChatAvatar/CometChatAvatar.tsx +17 -5
- package/src/shared/views/CometChatBottomSheet/CometChatBottomSheet.tsx +5 -1
- package/src/shared/views/CometChatCaptionText/CometChatCaptionText.tsx +8 -1
- package/src/shared/views/CometChatConfirmDialog/CometChatConfirmDialog.tsx +2 -0
- package/src/shared/views/CometChatConversationStarter/CometChatConversationStarter.tsx +91 -20
- package/src/shared/views/CometChatFilesBubble/CometChatFilesBubble.tsx +5 -0
- package/src/shared/views/CometChatImagesBubble/CometChatImagesBubble.tsx +5 -0
- package/src/shared/views/CometChatList/CometChatList.tsx +6 -1
- package/src/shared/views/CometChatList/Header.tsx +8 -2
- package/src/shared/views/CometChatListItem/CometChatListItem.tsx +4 -0
- package/src/shared/views/CometChatRetryButton/CometChatRetryButton.tsx +1 -0
- package/src/shared/views/CometChatSuggestionList/CometChatSuggestionList.tsx +4 -0
- package/src/shared/views/CometChatVideosBubble/CometChatVideosBubble.tsx +5 -0
- package/src/shared/views/CometChatVoiceNoteBubble/CometChatVoiceNoteBubble.tsx +5 -0
- package/src/shared/views/ErrorEmptyView/ErrorEmptyView.tsx +4 -1
- package/src/theme/default/default.ts +6 -0
- package/src/theme/type.ts +4 -0
|
@@ -39,6 +39,15 @@ type MessageUIEvents = {
|
|
|
39
39
|
onMessageModerated?: Function;
|
|
40
40
|
onAIAssistantMessageReceived?: Function;
|
|
41
41
|
onMessageReply?: Function;
|
|
42
|
+
onMessagePinned?: Function;
|
|
43
|
+
onMessageUnpinned?: Function;
|
|
44
|
+
onMessageSaved?: Function;
|
|
45
|
+
onMessageUnsaved?: Function;
|
|
46
|
+
ccMessagePinned?: Function;
|
|
47
|
+
ccMessageUnpinned?: Function;
|
|
48
|
+
ccMessageSaved?: Function;
|
|
49
|
+
ccMessageUnsaved?: Function;
|
|
50
|
+
ccThreadSubscriptionChanged?: Function;
|
|
42
51
|
};
|
|
43
52
|
export class MessageUIEventListener {
|
|
44
53
|
ccMessageSent?: Function = undefined;
|
|
@@ -70,7 +79,17 @@ export class MessageUIEventListener {
|
|
|
70
79
|
onMessageModerated?: Function = undefined;
|
|
71
80
|
onAIAssistantMessageReceived?: Function = undefined;
|
|
72
81
|
onMessageReply?: Function = undefined;
|
|
82
|
+
onMessagePinned?: Function = undefined;
|
|
83
|
+
onMessageUnpinned?: Function = undefined;
|
|
84
|
+
onMessageSaved?: Function = undefined;
|
|
85
|
+
onMessageUnsaved?: Function = undefined;
|
|
86
|
+
ccMessagePinned?: Function = undefined;
|
|
87
|
+
ccMessageUnpinned?: Function = undefined;
|
|
88
|
+
ccMessageSaved?: Function = undefined;
|
|
89
|
+
ccMessageUnsaved?: Function = undefined;
|
|
90
|
+
ccThreadSubscriptionChanged?: Function = undefined;
|
|
73
91
|
constructor({
|
|
92
|
+
ccThreadSubscriptionChanged,
|
|
74
93
|
ccMessageSent,
|
|
75
94
|
ccMessageEdited,
|
|
76
95
|
ccMessageDeleted,
|
|
@@ -100,6 +119,14 @@ export class MessageUIEventListener {
|
|
|
100
119
|
onMessageModerated,
|
|
101
120
|
onAIAssistantMessageReceived,
|
|
102
121
|
onMessageReply,
|
|
122
|
+
onMessagePinned,
|
|
123
|
+
onMessageUnpinned,
|
|
124
|
+
onMessageSaved,
|
|
125
|
+
onMessageUnsaved,
|
|
126
|
+
ccMessagePinned,
|
|
127
|
+
ccMessageUnpinned,
|
|
128
|
+
ccMessageSaved,
|
|
129
|
+
ccMessageUnsaved,
|
|
103
130
|
}: MessageUIEvents) {
|
|
104
131
|
if (!isFalsy(ccMessageError)) this.ccMessageError = ccMessageError;
|
|
105
132
|
if (!isFalsy(ccMessageDelivered)) this.ccMessageDelivered = ccMessageDelivered;
|
|
@@ -137,6 +164,16 @@ export class MessageUIEventListener {
|
|
|
137
164
|
if (!isFalsy(onAIAssistantMessageReceived)) this.onAIAssistantMessageReceived = onAIAssistantMessageReceived;
|
|
138
165
|
if (!isFalsy(onMessageReply)) this.onMessageReply = onMessageReply;
|
|
139
166
|
if (!isFalsy(onMessageModerated)) this.onMessageModerated = onMessageModerated;
|
|
167
|
+
if (!isFalsy(onMessagePinned)) this.onMessagePinned = onMessagePinned;
|
|
168
|
+
if (!isFalsy(onMessageUnpinned)) this.onMessageUnpinned = onMessageUnpinned;
|
|
169
|
+
if (!isFalsy(onMessageSaved)) this.onMessageSaved = onMessageSaved;
|
|
170
|
+
if (!isFalsy(onMessageUnsaved)) this.onMessageUnsaved = onMessageUnsaved;
|
|
171
|
+
if (!isFalsy(ccMessagePinned)) this.ccMessagePinned = ccMessagePinned;
|
|
172
|
+
if (!isFalsy(ccMessageUnpinned)) this.ccMessageUnpinned = ccMessageUnpinned;
|
|
173
|
+
if (!isFalsy(ccMessageSaved)) this.ccMessageSaved = ccMessageSaved;
|
|
174
|
+
if (!isFalsy(ccMessageUnsaved)) this.ccMessageUnsaved = ccMessageUnsaved;
|
|
175
|
+
if (!isFalsy(ccThreadSubscriptionChanged))
|
|
176
|
+
this.ccThreadSubscriptionChanged = ccThreadSubscriptionChanged;
|
|
140
177
|
}
|
|
141
178
|
}
|
|
142
179
|
|
|
@@ -100,7 +100,26 @@ export class ListenerInitializer {
|
|
|
100
100
|
},
|
|
101
101
|
onCardMessageReceived: (cardMessage: any) => {
|
|
102
102
|
CometChatUIEventHandler.emitMessageEvent(MessageEvents.onCardMessageReceived, cardMessage);
|
|
103
|
-
}
|
|
103
|
+
},
|
|
104
|
+
// Pin & Save realtime (§5.5/§6.6). Each carries a FULL BaseMessage with the
|
|
105
|
+
// pin/save attrs already on it, so a consumer replaces its copy and is done
|
|
106
|
+
// — no getMessageDetails round-trip.
|
|
107
|
+
//
|
|
108
|
+
// Pin/unpin is broadcast to the conversation. Save/unsave is private and
|
|
109
|
+
// reaches only this user's own devices, INCLUDING the one that made the
|
|
110
|
+
// call, which is why the SDK exempts it from the self-session guard.
|
|
111
|
+
onMessagePinned: (message: CometChat.BaseMessage) => {
|
|
112
|
+
CometChatUIEventHandler.emitMessageEvent(MessageEvents.onMessagePinned, message);
|
|
113
|
+
},
|
|
114
|
+
onMessageUnpinned: (message: CometChat.BaseMessage) => {
|
|
115
|
+
CometChatUIEventHandler.emitMessageEvent(MessageEvents.onMessageUnpinned, message);
|
|
116
|
+
},
|
|
117
|
+
onMessageSaved: (message: CometChat.BaseMessage) => {
|
|
118
|
+
CometChatUIEventHandler.emitMessageEvent(MessageEvents.onMessageSaved, message);
|
|
119
|
+
},
|
|
120
|
+
onMessageUnsaved: (message: CometChat.BaseMessage) => {
|
|
121
|
+
CometChatUIEventHandler.emitMessageEvent(MessageEvents.onMessageUnsaved, message);
|
|
122
|
+
},
|
|
104
123
|
});
|
|
105
124
|
}
|
|
106
125
|
}
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
export enum MessageEvents {
|
|
2
|
+
/**
|
|
3
|
+
* The logged-in user's subscription to a thread changed. Emitted optimistically
|
|
4
|
+
* on tap and again on the server's answer (or on revert), so the thread header
|
|
5
|
+
* and the message action sheet never disagree without a refetch.
|
|
6
|
+
* Also the channel an integrator's own thread list should listen on.
|
|
7
|
+
*/
|
|
8
|
+
ccThreadSubscriptionChanged = "ccThreadSubscriptionChanged",
|
|
2
9
|
ccMessageSent = "ccMessageSent",
|
|
3
10
|
ccMessageDelivered = "ccMessageDelivered",
|
|
4
11
|
ccMessageRead = "ccMessageRead",
|
|
@@ -28,4 +35,15 @@ export enum MessageEvents {
|
|
|
28
35
|
onAIAssistantMessageReceived = "onAIAssistantMessageReceived",
|
|
29
36
|
onMessagesDeliveredToAll = "onMessagesDeliveredToAll",
|
|
30
37
|
onMessagesReadByAll = "onMessagesReadByAll",
|
|
38
|
+
// Pin & Save (§6.6). The `on*` pair is realtime off the socket; `cc*` is the
|
|
39
|
+
// kit's own optimistic emit, so a consumer can tell "the server said so" from
|
|
40
|
+
// "this device just did it and may still revert".
|
|
41
|
+
onMessagePinned = "onMessagePinned",
|
|
42
|
+
onMessageUnpinned = "onMessageUnpinned",
|
|
43
|
+
onMessageSaved = "onMessageSaved",
|
|
44
|
+
onMessageUnsaved = "onMessageUnsaved",
|
|
45
|
+
ccMessagePinned = "ccMessagePinned",
|
|
46
|
+
ccMessageUnpinned = "ccMessageUnpinned",
|
|
47
|
+
ccMessageSaved = "ccMessageSaved",
|
|
48
|
+
ccMessageUnsaved = "ccMessageUnsaved",
|
|
31
49
|
}
|
|
@@ -2,7 +2,9 @@ import { CometChat } from "@cometchat/chat-sdk-react-native";
|
|
|
2
2
|
import React, { JSX } from "react";
|
|
3
3
|
import { Text, TextStyle, ViewStyle, Platform, Linking, View } from "react-native";
|
|
4
4
|
import { CometChatTextFormatter } from "../CometChatTextFormatter";
|
|
5
|
+
import { COLOR_OPEN_TAG, HEX_COLOR_REGEX, findColorTag, stripColorTags } from "../richTextWireFormat";
|
|
5
6
|
import { isTextElement, isViewElement } from "../../utils/elementType";
|
|
7
|
+
import { urlPattern, emailPattern } from "../../constants/textPatterns";
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
* Style configuration for rich text formatting
|
|
@@ -14,6 +16,8 @@ export interface RichTextStyle {
|
|
|
14
16
|
strikethroughStyle?: TextStyle;
|
|
15
17
|
inlineCodeStyle?: TextStyle;
|
|
16
18
|
inlineCodeContainerStyle?: TextStyle;
|
|
19
|
+
/** Base style for a `<color=#rrggbb>` run. The token's own hex always overrides `color`. */
|
|
20
|
+
textColorStyle?: TextStyle;
|
|
17
21
|
codeBlockStyle?: TextStyle;
|
|
18
22
|
codeBlockContainerStyle?: ViewStyle;
|
|
19
23
|
blockquoteContainerStyle?: ViewStyle;
|
|
@@ -33,6 +37,12 @@ const QUOTE_BULLET_REGEX = /^- (.*)$/;
|
|
|
33
37
|
const QUOTE_ORDERED_REGEX = /^(\s*)(\d+)\.\s(.*)$/;
|
|
34
38
|
// Mention pattern — used to exclude mention UIDs from markdown marker detection
|
|
35
39
|
const MENTION_PATTERN_REGEX = /<@(?:uid|all):[^>]*>/g;
|
|
40
|
+
// URL pattern — used to exclude URLs from markdown marker detection (ENG-38182)
|
|
41
|
+
const URL_PATTERN_REGEX = new RegExp(urlPattern, "gi");
|
|
42
|
+
// Emails are stashed BEFORE urls: the url pattern matches only the DOMAIN of an address, so
|
|
43
|
+
// `my_name@corp.co.uk` had its local part left exposed and two such addresses in one message
|
|
44
|
+
// paired their underscores into italics — `my_name@` and `my_alt@` rendered as `myname@`/`myalt@`.
|
|
45
|
+
const EMAIL_PATTERN_REGEX = new RegExp(emailPattern, "gi");
|
|
36
46
|
|
|
37
47
|
// Pre-allocated style objects — avoids creating new objects on every render
|
|
38
48
|
const LIST_ROW_STYLE = { flexDirection: 'row' as const, flexShrink: 1 as const };
|
|
@@ -226,6 +236,7 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
226
236
|
if (cleaned.indexOf("`") >= 0) return true;
|
|
227
237
|
if (cleaned.indexOf("_") >= 0) return true;
|
|
228
238
|
if (cleaned.indexOf("[") >= 0) return true;
|
|
239
|
+
if (cleaned.indexOf(COLOR_OPEN_TAG) >= 0) return true;
|
|
229
240
|
if (cleaned.indexOf("- ") >= 0) return true;
|
|
230
241
|
if (cleaned.indexOf("> ") >= 0) return true;
|
|
231
242
|
if (cleaned.indexOf("▎ ") >= 0) return true;
|
|
@@ -253,7 +264,10 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
253
264
|
// Check for single-line ```content``` (opening and closing on same line)
|
|
254
265
|
const afterOpen = trimmedLine.substring(3);
|
|
255
266
|
const firstClose = afterOpen.indexOf("```");
|
|
256
|
-
|
|
267
|
+
// `>= 0`, not `> 0`: an EMPTY code block is six backticks, so its close sits at offset 0.
|
|
268
|
+
// Reading that as "no close on this line" fell through to the fenced branch below, which
|
|
269
|
+
// swallowed every following line into the code block (ENG-38253).
|
|
270
|
+
if (firstClose >= 0) {
|
|
257
271
|
const afterClose = afterOpen.substring(firstClose + 3).trim();
|
|
258
272
|
if (afterClose.length === 0) {
|
|
259
273
|
// Standalone ```content``` — render as block-level code block (same as fenced)
|
|
@@ -271,7 +285,11 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
271
285
|
this.style.codeBlockContainerStyle,
|
|
272
286
|
]}
|
|
273
287
|
>
|
|
274
|
-
|
|
288
|
+
{/* A code block owns its styling, so a `<color=…>` token inside one is not a
|
|
289
|
+
style here — but it must never reach the reader as literal markup either.
|
|
290
|
+
Messages sent before the serializer fix still carry the token inside the
|
|
291
|
+
fence, so strip rather than render (ENG-38253). */}
|
|
292
|
+
<Text style={[this.style.codeBlockStyle]}>{stripColorTags(content)}</Text>
|
|
275
293
|
</View>
|
|
276
294
|
);
|
|
277
295
|
lineIndex++;
|
|
@@ -320,7 +338,7 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
320
338
|
this.style.codeBlockStyle,
|
|
321
339
|
]}
|
|
322
340
|
>
|
|
323
|
-
{codeLines.join("\n")}
|
|
341
|
+
{stripColorTags(codeLines.join("\n"))}
|
|
324
342
|
</Text>
|
|
325
343
|
</View>
|
|
326
344
|
);
|
|
@@ -548,33 +566,38 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
548
566
|
const textWithoutMentions = text.replace(MENTION_PATTERN_REGEX, '');
|
|
549
567
|
if (textWithoutMentions.indexOf("**") < 0 && textWithoutMentions.indexOf("__") < 0 && textWithoutMentions.indexOf("<u>") < 0 &&
|
|
550
568
|
textWithoutMentions.indexOf("~~") < 0 && textWithoutMentions.indexOf("`") < 0 && textWithoutMentions.indexOf("_") < 0 &&
|
|
551
|
-
textWithoutMentions.indexOf("[") < 0) {
|
|
569
|
+
textWithoutMentions.indexOf("[") < 0 && textWithoutMentions.indexOf(COLOR_OPEN_TAG) < 0) {
|
|
552
570
|
return text;
|
|
553
571
|
}
|
|
554
572
|
|
|
555
|
-
// Replace mention patterns with null-byte placeholders before parsing so
|
|
556
|
-
// findItalic/findNextFormat never see underscores
|
|
557
|
-
|
|
558
|
-
const
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
mentionSlots.push(match);
|
|
573
|
+
// Replace mention patterns and URLs with null-byte placeholders before parsing so
|
|
574
|
+
// findItalic/findNextFormat never see underscores inside UIDs or URLs.
|
|
575
|
+
const protectedSlots: string[] = [];
|
|
576
|
+
const stash = (match: string) => {
|
|
577
|
+
const idx = protectedSlots.length;
|
|
578
|
+
protectedSlots.push(match);
|
|
562
579
|
return `\x00M${idx}\x00`;
|
|
563
|
-
}
|
|
580
|
+
};
|
|
581
|
+
let sanitized = text.replace(MENTION_PATTERN_REGEX, stash);
|
|
582
|
+
try {
|
|
583
|
+
sanitized = sanitized.replace(EMAIL_PATTERN_REGEX, stash);
|
|
584
|
+
sanitized = sanitized.replace(URL_PATTERN_REGEX, stash);
|
|
585
|
+
} catch {
|
|
586
|
+
}
|
|
564
587
|
|
|
565
|
-
// Restore mention placeholders in a string or JSX tree
|
|
566
|
-
const
|
|
588
|
+
// Restore mention/URL placeholders in a string or JSX tree
|
|
589
|
+
const restoreProtected = (node: JSX.Element | string): JSX.Element | string => {
|
|
567
590
|
if (typeof node === 'string') {
|
|
568
|
-
if (
|
|
569
|
-
return node.replace(/\x00M(\d+)\x00/g, (_, idx) =>
|
|
591
|
+
if (protectedSlots.length === 0 || node.indexOf('\x00') < 0) return node;
|
|
592
|
+
return node.replace(/\x00M(\d+)\x00/g, (_, idx) => protectedSlots[Number(idx)] ?? _);
|
|
570
593
|
}
|
|
571
594
|
if (!React.isValidElement(node)) return node;
|
|
572
595
|
const el = node as React.ReactElement<any>;
|
|
573
596
|
const children = el.props.children;
|
|
574
597
|
if (!children) return node;
|
|
575
598
|
const restored = React.Children.map(children, (child: any) => {
|
|
576
|
-
if (typeof child === 'string') return
|
|
577
|
-
if (React.isValidElement(child)) return
|
|
599
|
+
if (typeof child === 'string') return restoreProtected(child);
|
|
600
|
+
if (React.isValidElement(child)) return restoreProtected(child as JSX.Element);
|
|
578
601
|
return child;
|
|
579
602
|
});
|
|
580
603
|
return React.cloneElement(el, {}, ...(restored || []));
|
|
@@ -591,16 +614,16 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
591
614
|
const match = this.findNextFormat(remaining);
|
|
592
615
|
if (match) {
|
|
593
616
|
if (match.startIndex > 0) {
|
|
594
|
-
elements.push(
|
|
617
|
+
elements.push(restoreProtected(remaining.substring(0, match.startIndex)) as string);
|
|
595
618
|
}
|
|
596
|
-
// Restore mentions in matched content before recursive parsing
|
|
619
|
+
// Restore mentions/URLs in matched content before recursive parsing
|
|
597
620
|
const restoredContent = (typeof match.content === 'string' && match.content.indexOf('\x00') >= 0)
|
|
598
|
-
?
|
|
621
|
+
? restoreProtected(match.content) as string
|
|
599
622
|
: match.content;
|
|
600
623
|
if (match.type === "link" && match.url) {
|
|
601
624
|
// Render link as tappable blue underlined text (same as CometChatUrlsFormatter)
|
|
602
625
|
const innerContent = this.parseInlineFormats(restoredContent);
|
|
603
|
-
const linkUrl = match.url;
|
|
626
|
+
const linkUrl = restoreProtected(match.url) as string;
|
|
604
627
|
elements.push(
|
|
605
628
|
<Text
|
|
606
629
|
key={`fmt-${keyCounter++}`}
|
|
@@ -634,6 +657,24 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
634
657
|
style={[this.style.inlineCodeContainerStyle, this.style.inlineCodeStyle]}
|
|
635
658
|
>{restoredContent}</Text>
|
|
636
659
|
);
|
|
660
|
+
} else if (match.type === "textColor" && match.color) {
|
|
661
|
+
// Re-validate at render time: the hex reaching here came off the wire, and an
|
|
662
|
+
// unparseable value must fall back to the base style rather than reach RN.
|
|
663
|
+
const safeColor = HEX_COLOR_REGEX.test(match.color) ? match.color : undefined;
|
|
664
|
+
// React Native resolves a nested Text's `color` over its parent's, so a run like
|
|
665
|
+
// `<color=#e11d48>\`code\`</color>` rendered in the inline-code foreground and the
|
|
666
|
+
// consumer's colour vanished — visible in the bubble even though the wire carried it
|
|
667
|
+
// (ENG-38248). Push the colour onto the descendants that set their own so it wins,
|
|
668
|
+
// matching the native composer, which also lets a consumer colour override the
|
|
669
|
+
// inline-code default while leaving the code background alone.
|
|
670
|
+
const inner = this.parseInlineFormats(restoredContent);
|
|
671
|
+
const innerContent = safeColor ? this.enforceTextColor(inner, safeColor) : inner;
|
|
672
|
+
elements.push(
|
|
673
|
+
<Text
|
|
674
|
+
key={`fmt-${keyCounter++}`}
|
|
675
|
+
style={safeColor ? [this.style.textColorStyle, { color: safeColor }] : this.style.textColorStyle}
|
|
676
|
+
>{innerContent}</Text>
|
|
677
|
+
);
|
|
637
678
|
} else {
|
|
638
679
|
const style = this.getStyleForFormat(match.type);
|
|
639
680
|
const innerContent = this.parseInlineFormats(restoredContent);
|
|
@@ -646,7 +687,7 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
646
687
|
}
|
|
647
688
|
remaining = remaining.substring(match.endIndex);
|
|
648
689
|
} else {
|
|
649
|
-
elements.push(
|
|
690
|
+
elements.push(restoreProtected(remaining) as string);
|
|
650
691
|
remaining = "";
|
|
651
692
|
}
|
|
652
693
|
}
|
|
@@ -656,8 +697,11 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
656
697
|
return <Text key={`inline-${keyCounter}`}>{elements}</Text>;
|
|
657
698
|
}
|
|
658
699
|
|
|
659
|
-
private findNextFormat(text: string): { type: string; content: string; startIndex: number; endIndex: number; url?: string } | null {
|
|
660
|
-
const matches: Array<{ type: string; content: string; startIndex: number; endIndex: number; url?: string }> = [];
|
|
700
|
+
private findNextFormat(text: string): { type: string; content: string; startIndex: number; endIndex: number; url?: string; color?: string } | null {
|
|
701
|
+
const matches: Array<{ type: string; content: string; startIndex: number; endIndex: number; url?: string; color?: string }> = [];
|
|
702
|
+
|
|
703
|
+
const textColorMatch = this.findTextColor(text);
|
|
704
|
+
if (textColorMatch) matches.push({ ...textColorMatch, type: "textColor" });
|
|
661
705
|
|
|
662
706
|
const codeBlockMatch = this.findPair(text, "```", "```");
|
|
663
707
|
if (codeBlockMatch) matches.push({ ...codeBlockMatch, type: "codeBlock" });
|
|
@@ -748,6 +792,21 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
748
792
|
return null;
|
|
749
793
|
}
|
|
750
794
|
|
|
795
|
+
/**
|
|
796
|
+
* Locates the first `<color=#rrggbb>…</color>` run. The grammar lives in richTextWireFormat
|
|
797
|
+
* so this stays in lock-step with the composer that produces it.
|
|
798
|
+
*/
|
|
799
|
+
private findTextColor(text: string): { content: string; startIndex: number; endIndex: number; color: string } | null {
|
|
800
|
+
const match = findColorTag(text);
|
|
801
|
+
if (!match) return null;
|
|
802
|
+
return {
|
|
803
|
+
content: text.substring(match.contentStart, match.contentEnd),
|
|
804
|
+
startIndex: match.startIndex,
|
|
805
|
+
endIndex: match.endIndex,
|
|
806
|
+
color: match.hex,
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
|
|
751
810
|
private findLink(text: string): { type: string; content: string; startIndex: number; endIndex: number; url?: string } | null {
|
|
752
811
|
const match = LINK_REGEX.exec(text);
|
|
753
812
|
if (!match) return null;
|
|
@@ -818,6 +877,45 @@ export class CometChatRichTextFormatter extends CometChatTextFormatter {
|
|
|
818
877
|
return React.cloneElement(el, {}, ...children);
|
|
819
878
|
}
|
|
820
879
|
|
|
880
|
+
/**
|
|
881
|
+
* Forces `color` onto every descendant that declares its own.
|
|
882
|
+
*
|
|
883
|
+
* Needed because React Native gives a nested `<Text>`'s `color` precedence over its parent's —
|
|
884
|
+
* so wrapping inline code (or bold, or anything else carrying a foreground) in a colour run left
|
|
885
|
+
* the inner style winning and the colour invisible (ENG-38248). Only descendants that actually
|
|
886
|
+
* set a colour are touched; everything else already inherits correctly.
|
|
887
|
+
*
|
|
888
|
+
* Links are deliberately exempt: their colour is an affordance that tells a user the text is
|
|
889
|
+
* tappable, not decoration, so a consumer colour must not repaint it. They are identified by the
|
|
890
|
+
* `onPress` the link branch attaches.
|
|
891
|
+
*/
|
|
892
|
+
private enforceTextColor(
|
|
893
|
+
inner: JSX.Element | string,
|
|
894
|
+
color: string
|
|
895
|
+
): JSX.Element | string {
|
|
896
|
+
if (typeof inner === "string" || !React.isValidElement(inner)) return inner;
|
|
897
|
+
|
|
898
|
+
const el = inner as React.ReactElement<any>;
|
|
899
|
+
if (el.props?.onPress) return inner; // a link — leave its colour alone
|
|
900
|
+
|
|
901
|
+
const children = React.Children.map(el.props.children, (child) =>
|
|
902
|
+
this.enforceTextColor(child as JSX.Element | string, color)
|
|
903
|
+
);
|
|
904
|
+
|
|
905
|
+
const style = el.props.style;
|
|
906
|
+
const flat = Array.isArray(style) ? Object.assign({}, ...style.filter(Boolean)) : style;
|
|
907
|
+
const declaresColor = flat && flat.color !== undefined;
|
|
908
|
+
|
|
909
|
+
if (!declaresColor) {
|
|
910
|
+
return children ? React.cloneElement(el, {}, ...children) : inner;
|
|
911
|
+
}
|
|
912
|
+
return React.cloneElement(
|
|
913
|
+
el,
|
|
914
|
+
{ style: Array.isArray(style) ? [...style, { color }] : { ...flat, color } },
|
|
915
|
+
...(children ?? [])
|
|
916
|
+
);
|
|
917
|
+
}
|
|
918
|
+
|
|
821
919
|
private getStyleForFormat(type: string): TextStyle {
|
|
822
920
|
switch (type) {
|
|
823
921
|
case "bold": return this.style.boldStyle || {};
|
|
@@ -214,6 +214,28 @@ export abstract class CometChatTextFormatter {
|
|
|
214
214
|
return inputText;
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
+
/**
|
|
218
|
+
* Rewrites the message's stored text before the UI Kit parses any of it.
|
|
219
|
+
*
|
|
220
|
+
* Runs on every surface a message appears on — bubble, conversation row, reply/edit preview,
|
|
221
|
+
* search result, media caption, message information — and on the text loaded back into the
|
|
222
|
+
* composer for editing, always BEFORE the built-in markdown, mention and link handling.
|
|
223
|
+
*
|
|
224
|
+
* This is where a formatter maps its own wire token onto markup the UI Kit already draws,
|
|
225
|
+
* e.g. `(#e5484d)red()` to `<color=#e5484d>red</color>`. `getFormattedText` cannot do that
|
|
226
|
+
* job: by then markdown has already turned the text into elements, so a pattern that spans
|
|
227
|
+
* any of that markup no longer matches.
|
|
228
|
+
*
|
|
229
|
+
* Returns the text untouched by default, so a formatter that does not override it behaves
|
|
230
|
+
* exactly as it did before this hook existed.
|
|
231
|
+
*
|
|
232
|
+
* @param text - The message text as stored on the server.
|
|
233
|
+
* @returns The text to render, written in the UI Kit's own grammar.
|
|
234
|
+
*/
|
|
235
|
+
formatRawText(text: string): string {
|
|
236
|
+
return text;
|
|
237
|
+
}
|
|
238
|
+
|
|
217
239
|
/**
|
|
218
240
|
* Handles the message before sending it.
|
|
219
241
|
* @param message - The message to handle.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { CometChatTextFormatter } from "./CometChatTextFormatter";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runs every formatter's `formatRawText` over a message's stored text, in the order the
|
|
5
|
+
* consumer supplied them, before any built-in parsing sees it.
|
|
6
|
+
*
|
|
7
|
+
* Every surface that displays message text calls this first, so a consumer's own wire token
|
|
8
|
+
* renders the same way in a bubble, a conversation row, a preview, a search result and a
|
|
9
|
+
* caption — the guarantee the web and Android kits already give.
|
|
10
|
+
*
|
|
11
|
+
* A formatter that throws is skipped instead of taking the surface down with it: a bad regex
|
|
12
|
+
* in a consumer's formatter must never blank a conversation row.
|
|
13
|
+
*/
|
|
14
|
+
export function applyRawFormatters(
|
|
15
|
+
text: string,
|
|
16
|
+
formatters?: Array<CometChatTextFormatter>
|
|
17
|
+
): string {
|
|
18
|
+
if (!text || !formatters?.length) return text;
|
|
19
|
+
|
|
20
|
+
let formatted = text;
|
|
21
|
+
for (const formatter of formatters) {
|
|
22
|
+
try {
|
|
23
|
+
const next = formatter.formatRawText?.(formatted);
|
|
24
|
+
// A consumer writing JS can return anything; only a string is usable here.
|
|
25
|
+
if (typeof next === "string") formatted = next;
|
|
26
|
+
} catch {
|
|
27
|
+
// Keep the text as it was and let the remaining formatters run.
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return formatted;
|
|
31
|
+
}
|
|
@@ -11,3 +11,17 @@ export {
|
|
|
11
11
|
MentionTextStyle,
|
|
12
12
|
RichTextStyle,
|
|
13
13
|
};
|
|
14
|
+
|
|
15
|
+
// Rich-text wire grammar — shared by the composer (producer) and formatter (renderer).
|
|
16
|
+
// Exported so a custom formatter can render the same tokens the built-in one does.
|
|
17
|
+
export {
|
|
18
|
+
COLOR_OPEN_TAG,
|
|
19
|
+
COLOR_CLOSE_TAG,
|
|
20
|
+
HEX_COLOR_REGEX,
|
|
21
|
+
normalizeHex,
|
|
22
|
+
openColorTag,
|
|
23
|
+
matchColorTagAt,
|
|
24
|
+
findColorTag,
|
|
25
|
+
stripColorTags,
|
|
26
|
+
} from "./richTextWireFormat";
|
|
27
|
+
export type { ColorTagMatch } from "./richTextWireFormat";
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rich-text wire grammar — the on-the-wire representation of inline styles that
|
|
3
|
+
* both the composer (which *produces* it while editing) and the rich-text
|
|
4
|
+
* formatter (which *renders* it in the bubble) must agree on.
|
|
5
|
+
*
|
|
6
|
+
* This module is deliberately neutral: it belongs to neither side. The formatter
|
|
7
|
+
* is swappable/developer-supplied, so the composer must not import from it (or
|
|
8
|
+
* vice-versa) — both import the grammar from here instead, so the token spec
|
|
9
|
+
* lives in exactly one place and the two sides can never drift.
|
|
10
|
+
*
|
|
11
|
+
* Currently the only value-carrying inline style: text colour `<color=#rrggbb>…</color>`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Opening tag prefix, e.g. `<color=#ff0000>`. */
|
|
15
|
+
export const COLOR_OPEN_TAG = '<color=';
|
|
16
|
+
/** Closing tag. */
|
|
17
|
+
export const COLOR_CLOSE_TAG = '</color>';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Valid colour attribute: `#rgb` or `#rrggbb` (case-insensitive).
|
|
21
|
+
* Anchored and non-global on purpose — a /g regex keeps lastIndex between
|
|
22
|
+
* .test() calls, which would silently let every other value through.
|
|
23
|
+
* 8-digit `#rrggbbaa` is rejected deliberately: a transparent run is an
|
|
24
|
+
* invisible-message vector.
|
|
25
|
+
*/
|
|
26
|
+
export const HEX_COLOR_REGEX = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Expands `#rgb` to `#rrggbb` and lowercases. Native always reports the active
|
|
30
|
+
* colour as 6-digit, so a 3-digit swatch would never compare equal to it.
|
|
31
|
+
*/
|
|
32
|
+
export const normalizeHex = (hex: string): string => {
|
|
33
|
+
const lower = hex.toLowerCase();
|
|
34
|
+
if (lower.length !== 4) return lower;
|
|
35
|
+
return `#${lower[1]}${lower[1]}${lower[2]}${lower[2]}${lower[3]}${lower[3]}`;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Builds the opening tag for a hex colour, e.g. `openColorTag('#f00')` → `<color=#f00>`. */
|
|
39
|
+
export const openColorTag = (hex: string): string => `${COLOR_OPEN_TAG}${hex}>`;
|
|
40
|
+
|
|
41
|
+
/** Boundaries of a well-formed `<color=…>…</color>` run. */
|
|
42
|
+
export interface ColorTagMatch {
|
|
43
|
+
/** Validated, lowercased hex attribute. */
|
|
44
|
+
hex: string;
|
|
45
|
+
/** Index of the run's first content character (just after `>`). */
|
|
46
|
+
contentStart: number;
|
|
47
|
+
/** Index of the matching close tag (exclusive end of content). */
|
|
48
|
+
contentEnd: number;
|
|
49
|
+
/** Index just past the whole run (after `</color>`). */
|
|
50
|
+
endIndex: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Given `openIdx` pointing at a `<color=` open tag, return the run's boundaries,
|
|
55
|
+
* or `null` if the tag is malformed (no `>`), carries an invalid hex, or is
|
|
56
|
+
* unterminated. Depth-aware: `<color=#a>x<color=#b>y</color>z</color>` captures
|
|
57
|
+
* the whole outer run instead of stopping at the inner close tag.
|
|
58
|
+
*/
|
|
59
|
+
export function matchColorTagAt(text: string, openIdx: number): ColorTagMatch | null {
|
|
60
|
+
const lower = text.toLowerCase();
|
|
61
|
+
if (!lower.startsWith(COLOR_OPEN_TAG, openIdx)) return null;
|
|
62
|
+
|
|
63
|
+
const attrStart = openIdx + COLOR_OPEN_TAG.length;
|
|
64
|
+
const attrEnd = text.indexOf('>', attrStart);
|
|
65
|
+
if (attrEnd < 0) return null;
|
|
66
|
+
|
|
67
|
+
const hex = text.substring(attrStart, attrEnd).trim().toLowerCase();
|
|
68
|
+
if (!HEX_COLOR_REGEX.test(hex)) return null;
|
|
69
|
+
|
|
70
|
+
const contentStart = attrEnd + 1;
|
|
71
|
+
let depth = 1;
|
|
72
|
+
let cursor = contentStart;
|
|
73
|
+
let contentEnd = -1;
|
|
74
|
+
|
|
75
|
+
while (cursor < lower.length) {
|
|
76
|
+
const nextClose = lower.indexOf(COLOR_CLOSE_TAG, cursor);
|
|
77
|
+
if (nextClose < 0) break;
|
|
78
|
+
const nextOpen = lower.indexOf(COLOR_OPEN_TAG, cursor);
|
|
79
|
+
if (nextOpen >= 0 && nextOpen < nextClose) {
|
|
80
|
+
depth++;
|
|
81
|
+
cursor = nextOpen + COLOR_OPEN_TAG.length;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
depth--;
|
|
85
|
+
if (depth === 0) {
|
|
86
|
+
contentEnd = nextClose;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
cursor = nextClose + COLOR_CLOSE_TAG.length;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (contentEnd < 0) return null;
|
|
93
|
+
return { hex, contentStart, contentEnd, endIndex: contentEnd + COLOR_CLOSE_TAG.length };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Find the first well-formed `<color=…>…</color>` run at or after `from`.
|
|
98
|
+
* Malformed, unterminated, or empty runs are skipped so one bad token can't
|
|
99
|
+
* disable colour for the rest of the text. Returns `null` if none.
|
|
100
|
+
*/
|
|
101
|
+
export function findColorTag(
|
|
102
|
+
text: string,
|
|
103
|
+
from = 0
|
|
104
|
+
): (ColorTagMatch & { startIndex: number }) | null {
|
|
105
|
+
const lower = text.toLowerCase();
|
|
106
|
+
let searchFrom = from;
|
|
107
|
+
|
|
108
|
+
while (searchFrom < lower.length) {
|
|
109
|
+
const openIdx = lower.indexOf(COLOR_OPEN_TAG, searchFrom);
|
|
110
|
+
if (openIdx < 0) return null;
|
|
111
|
+
|
|
112
|
+
const match = matchColorTagAt(text, openIdx);
|
|
113
|
+
if (!match) {
|
|
114
|
+
// Malformed / invalid hex / unterminated — skip past this open tag.
|
|
115
|
+
searchFrom = openIdx + COLOR_OPEN_TAG.length;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (match.contentEnd === match.contentStart) {
|
|
119
|
+
// Empty run — skip the whole thing and keep scanning.
|
|
120
|
+
searchFrom = match.endIndex;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
return { ...match, startIndex: openIdx };
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Strips every `<color=…>` / `</color>` token, leaving the content. Used by the
|
|
130
|
+
* plain-text surfaces (search subtitles, AI history) that render raw strings and
|
|
131
|
+
* would otherwise show the markup to users.
|
|
132
|
+
*
|
|
133
|
+
* Deliberately removes tags even when unbalanced — a half-written token must never
|
|
134
|
+
* be shown to a user, and this is a display-only path.
|
|
135
|
+
*/
|
|
136
|
+
export function stripColorTags(text: string): string {
|
|
137
|
+
if (!text) return text;
|
|
138
|
+
if (text.toLowerCase().indexOf(COLOR_OPEN_TAG) < 0 &&
|
|
139
|
+
text.toLowerCase().indexOf(COLOR_CLOSE_TAG) < 0) {
|
|
140
|
+
return text;
|
|
141
|
+
}
|
|
142
|
+
let out = '';
|
|
143
|
+
let cursor = 0;
|
|
144
|
+
const lower = text.toLowerCase();
|
|
145
|
+
|
|
146
|
+
while (cursor < text.length) {
|
|
147
|
+
const openIdx = lower.indexOf(COLOR_OPEN_TAG, cursor);
|
|
148
|
+
const closeIdx = lower.indexOf(COLOR_CLOSE_TAG, cursor);
|
|
149
|
+
|
|
150
|
+
// Whichever token comes first (either may be absent).
|
|
151
|
+
const next = openIdx < 0 ? closeIdx : closeIdx < 0 ? openIdx : Math.min(openIdx, closeIdx);
|
|
152
|
+
if (next < 0) break;
|
|
153
|
+
|
|
154
|
+
out += text.substring(cursor, next);
|
|
155
|
+
if (next === closeIdx && (openIdx < 0 || closeIdx < openIdx)) {
|
|
156
|
+
cursor = next + COLOR_CLOSE_TAG.length;
|
|
157
|
+
} else {
|
|
158
|
+
// Drop through to the tag's `>`; if there is none the rest is malformed markup.
|
|
159
|
+
const attrEnd = text.indexOf('>', next + COLOR_OPEN_TAG.length);
|
|
160
|
+
if (attrEnd < 0) return out;
|
|
161
|
+
cursor = attrEnd + 1;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return out + text.substring(cursor);
|
|
165
|
+
}
|