Skip to content

Commit 72e345e

Browse files
authored
fix: 兼容不支持 Unicode 属性正则的运行时 (huangxd-#432)5
1 parent 251d6f9 commit 72e345e

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

danmu_api/utils/auto-match-mapping-util.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
const NON_RULE_TITLE_CHARACTERS = (() => {
2+
try {
3+
return new RegExp('[^\\p{L}\\p{N}]', 'gu');
4+
} catch {
5+
// nodejs-mobile builds without Unicode property escapes still need CJK title matching.
6+
return /[^0-9A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u052F\u05D0-\u05EA\u05EF-\u05F2\u0620-\u063F\u0640-\u064A\u0660-\u0669\u0671-\u06D3\u06D5\u06EE-\u06FC\u06FF\u1100-\u11FF\u3005-\u3007\u3031-\u3035\u3038-\u303B\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312F\u3131-\u318E\u31A0-\u31BF\u31F0-\u31FF\u3400-\u4DBF\u4E00-\u9FFF\uA960-\uA97F\uAC00-\uD7A3\uD7B0-\uD7FF\uF900-\uFAFF]/g;
7+
}
8+
})();
9+
110
function normalizeRuleTitle(value) {
211
return String(value || '')
312
.normalize('NFKC')
4-
.replace(/[^\p{L}\p{N}]/gu, '')
13+
.replace(NON_RULE_TITLE_CHARACTERS, '')
514
.toLowerCase();
615
}
716

danmu_api/worker.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,25 @@ test('worker.js API endpoints', async (t) => {
297297
});
298298

299299
await t.test('auto match mapping table', async t => {
300+
await t.test('falls back when Unicode property escapes are unavailable', async () => {
301+
const NativeRegExp = globalThis.RegExp;
302+
globalThis.RegExp = function (pattern, flags) {
303+
if (String(pattern).includes('\\p{')) throw new SyntaxError('Unicode property escapes are unavailable');
304+
return new NativeRegExp(pattern, flags);
305+
};
306+
307+
try {
308+
const compat = await import('./utils/auto-match-mapping-util.js?unicode-properties=unavailable');
309+
const { rules, warnings } = compat.parseAutoMatchMappingRules('進撃の巨人 S01E01->SPY×FAMILY S01E03');
310+
311+
assert.deepEqual(warnings, []);
312+
assert.equal(compat.resolveAutoMatchMapping(rules, { title: '進撃の 巨人!', season: 1, episode: 2 }).targetEpisode, 4);
313+
assert.equal(compat.candidateMatchesMappingTitle({ animeTitle: 'SPY FAMILY' }, rules[0]), true);
314+
} finally {
315+
globalThis.RegExp = NativeRegExp;
316+
}
317+
});
318+
300319
await t.test('parses and resolves open, bounded, qualified, and platform rules', () => {
301320
const { rules, warnings } = parseAutoMatchMappingRules([
302321
'永生 S05E02->永生 S01E58',

0 commit comments

Comments
 (0)