From 7e2d3df5ba86939a99d69683951443d74ce08754 Mon Sep 17 00:00:00 2001 From: zonble Date: Wed, 19 Jan 2022 00:49:50 +0800 Subject: [PATCH] Prevents users from adding custom phrases with more than 6 characters. Also fixes typos. --- McBopomofoTests/PreferencesTests.swift | 2 +- Source/InputMethodController.mm | 18 ++++++++++++++---- Source/en.lproj/Localizable.strings | 2 ++ Source/zh-Hant.lproj/Localizable.strings | 2 ++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/McBopomofoTests/PreferencesTests.swift b/McBopomofoTests/PreferencesTests.swift index 5310de85..03e71781 100644 --- a/McBopomofoTests/PreferencesTests.swift +++ b/McBopomofoTests/PreferencesTests.swift @@ -143,7 +143,7 @@ class PreferencesTests: XCTestCase { XCTAssert(Preferences.phraseReplacementEnabled == true) } - func testChineneConversionEngine() { + func testChineseConversionEngine() { XCTAssert(Preferences.chineseConversionEngine == 0) Preferences.chineseConversionEngine = 1 XCTAssert(Preferences.chineseConversionEngine == 1) diff --git a/Source/InputMethodController.mm b/Source/InputMethodController.mm index 4a03fc78..cd617948 100644 --- a/Source/InputMethodController.mm +++ b/Source/InputMethodController.mm @@ -55,6 +55,8 @@ using namespace McBopomofo; using namespace OpenVanilla; static const NSInteger kMinKeyLabelSize = 10; +static const NSInteger kMinMarkRangeLength = 2; +static const NSInteger kMaxMarkRangeLength = 6; // input modes static NSString *const kBopomofoModeIdentifier = @"org.openvanilla.inputmethod.McBopomofo.Bopomofo"; @@ -1421,7 +1423,10 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } size_t begin = min(_builder->markerCursorIndex(), _builder->cursorIndex()); size_t end = max(_builder->markerCursorIndex(), _builder->cursorIndex()); // A phrase should contian at least two characters. - if (end - begin < 2) { + if (end - begin < kMinMarkRangeLength) { + return @""; + } + if (end - begin > kMaxMarkRangeLength) { return @""; } @@ -1443,6 +1448,7 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } { NSString *currentMarkedPhrase = [self _currentMarkedTextAndReadings]; if (![currentMarkedPhrase length]) { + [self beep]; return NO; } @@ -1464,9 +1470,13 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } NSString *message = NSLocalizedString(@"Model based Chinese conversion is on. Not suggested to add phrase in the mode.", @""); [self _showTooltip:message client:client]; } - else if (length == 1) { - NSString *messsage = [NSString stringWithFormat:NSLocalizedString(@"You are now selecting \"%@\". You can add a phrase with two or more characters.", @""), text]; - [self _showTooltip:messsage client:client]; + else if (length < kMinMarkRangeLength) { + NSString *message = [NSString stringWithFormat:NSLocalizedString(@"You are now selecting \"%@\". You can add a phrase with two or more characters.", @""), text]; + [self _showTooltip:message client:client]; + } + else if (length > kMaxMarkRangeLength) { + NSString *message = [NSString stringWithFormat:NSLocalizedString(@"You are now selecting \"%@\". A phrase cannot be longer than 6 characters.", @""), text]; + [self _showTooltip:message client:client]; } else { NSString *messsage = [NSString stringWithFormat:NSLocalizedString(@"You are now selecting \"%@\". Press enter to add a new phrase.", @""), text]; diff --git a/Source/en.lproj/Localizable.strings b/Source/en.lproj/Localizable.strings index ad91a3bd..ba78261c 100644 --- a/Source/en.lproj/Localizable.strings +++ b/Source/en.lproj/Localizable.strings @@ -69,6 +69,8 @@ "You are now selecting \"%@\". Press enter to add a new phrase." = "You are now selecting \"%@\". Press enter to add a new phrase."; +"You are now selecting \"%@\". A phrase cannot be longer than 6 characters." = "You are now selecting \"%@\". A phrase cannot be longer than 6 characters."; + "Chinese conversion on" = "Chinese conversion on"; "Chinese conversion off" = "Chinese conversion off"; diff --git a/Source/zh-Hant.lproj/Localizable.strings b/Source/zh-Hant.lproj/Localizable.strings index d9ab108a..b7c711a0 100644 --- a/Source/zh-Hant.lproj/Localizable.strings +++ b/Source/zh-Hant.lproj/Localizable.strings @@ -69,6 +69,8 @@ "You are now selecting \"%@\". Press enter to add a new phrase." = "您目前選擇了 \"%@\"。按下 Enter 就可以加入到使用者詞彙中。"; +"You are now selecting \"%@\". A phrase cannot be longer than 6 characters." = "您目前選擇了 \"%@\"。自訂詞彙不能超過六個字元。"; + "Chinese conversion on" = "已經切換到簡體中文模式"; "Chinese conversion off" = "已經切換到繁體中文模式";