fix(onebot): unify message segment type normalization
Keep empty-placeholder detection aligned with strict segment parsing.
This commit is contained in:
2 files changed
+22
-5
No files matched your search
@@ -64,6 +64,10 @@ function isSegmentArray(val: unknown): val is MessageSegment[] {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeSegmentType(type: string): string {
|
||||||
|
return type.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
function outboundInputSegmentTypes(message: JsonValue, autoEscape: boolean): string[] {
|
function outboundInputSegmentTypes(message: JsonValue, autoEscape: boolean): string[] {
|
||||||
if (typeof message === 'string') {
|
if (typeof message === 'string') {
|
||||||
if (autoEscape) return ['text'];
|
if (autoEscape) return ['text'];
|
||||||
@@ -71,7 +75,7 @@ function outboundInputSegmentTypes(message: JsonValue, autoEscape: boolean): str
|
|||||||
let lastIndex = 0;
|
let lastIndex = 0;
|
||||||
for (const match of message.matchAll(CQ_REGEX)) {
|
for (const match of message.matchAll(CQ_REGEX)) {
|
||||||
if (match.index! > lastIndex) types.push('text');
|
if (match.index! > lastIndex) types.push('text');
|
||||||
types.push((match[1] ?? '').toLowerCase());
|
types.push(normalizeSegmentType(match[1] ?? ''));
|
||||||
lastIndex = match.index! + match[0].length;
|
lastIndex = match.index! + match[0].length;
|
||||||
}
|
}
|
||||||
if (lastIndex < message.length) types.push('text');
|
if (lastIndex < message.length) types.push('text');
|
||||||
@@ -81,12 +85,12 @@ function outboundInputSegmentTypes(message: JsonValue, autoEscape: boolean): str
|
|||||||
return message.flatMap((item) => {
|
return message.flatMap((item) => {
|
||||||
if (typeof item !== 'object' || item === null || Array.isArray(item)) return [];
|
if (typeof item !== 'object' || item === null || Array.isArray(item)) return [];
|
||||||
const type = (item as { type?: unknown }).type;
|
const type = (item as { type?: unknown }).type;
|
||||||
return typeof type === 'string' ? [type.toLowerCase()] : [];
|
return typeof type === 'string' ? [normalizeSegmentType(type)] : [];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (typeof message === 'object' && message !== null) {
|
if (typeof message === 'object' && message !== null) {
|
||||||
const type = (message as { type?: unknown }).type;
|
const type = (message as { type?: unknown }).type;
|
||||||
return typeof type === 'string' ? [type.toLowerCase()] : [];
|
return typeof type === 'string' ? [normalizeSegmentType(type)] : [];
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@@ -173,7 +177,7 @@ function requireCoordinate(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function segmentToElement(type: string, data: Record<string, unknown>, options?: ParseMessageOptions): Promise<MessageElement | null> {
|
export async function segmentToElement(type: string, data: Record<string, unknown>, options?: ParseMessageOptions): Promise<MessageElement | null> {
|
||||||
const normalizedType = type.toLowerCase();
|
const normalizedType = normalizeSegmentType(type);
|
||||||
|
|
||||||
// Ordinary OneBot segment fields are scalar. Reject objects/arrays before
|
// Ordinary OneBot segment fields are scalar. Reject objects/arrays before
|
||||||
// codecs can stringify them into "[object Object]" and alter caller intent.
|
// codecs can stringify them into "[object Object]" and alter caller intent.
|
||||||
@@ -411,7 +415,7 @@ export async function parseMessage(message: JsonValue, autoEscape: boolean, opti
|
|||||||
// Some OneBot clients insert empty text between media as a separator.
|
// Some OneBot clients insert empty text between media as a separator.
|
||||||
// Only the exact empty string is a no-op; malformed and whitespace text
|
// Only the exact empty string is a no-op; malformed and whitespace text
|
||||||
// must still pass through strict element validation.
|
// must still pass through strict element validation.
|
||||||
if (seg.type.trim().toLowerCase() === 'text' && data.text === '') {
|
if (normalizeSegmentType(seg.type) === 'text' && data.text === '') {
|
||||||
log.debug('ignored explicit empty text placeholder in message segment array');
|
log.debug('ignored explicit empty text placeholder in message segment array');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -148,6 +148,19 @@ describe('parseMessage', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not treat whitespace-padded segment types as empty text placeholders', async () => {
|
||||||
|
await expect(parseMessage(
|
||||||
|
[
|
||||||
|
{ type: 'image', data: { file: 'https://example.com/image.png' } },
|
||||||
|
{ type: ' text ', data: { text: '' } },
|
||||||
|
] as any,
|
||||||
|
false,
|
||||||
|
)).rejects.toMatchObject({
|
||||||
|
code: 'UNKNOWN_TYPE',
|
||||||
|
elementType: ' text ',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('preserves whitespace-only text segments', async () => {
|
it('preserves whitespace-only text segments', async () => {
|
||||||
await expect(parseMessage(
|
await expect(parseMessage(
|
||||||
[{ type: 'text', data: { text: ' ' } }] as any,
|
[{ type: 'text', data: { text: ' ' } }] as any,
|
||||||
|
|||||||
Reference in new issue
Block a user