aboutsummaryrefslogtreecommitdiffstats
path: root/Framework
diff options
context:
space:
mode:
authorTomáš Znamenáček2015-03-05 14:32:51 +0100
committerTomáš Znamenáček2015-03-05 14:32:51 +0100
commit7bbaed62276c60c2e83b35b4329f795149be27fc (patch)
tree63cd4a75013325647c23f517362e4e574d45720b /Framework
parentfdc43c1cd33fa0f7c0251268055ad9a7812249d1 (diff)
downloadMASShortcut-7bbaed62276c60c2e83b35b4329f795149be27fc.tar.bz2
Removed support for dots and spaces in user defaults keys (#64).
I could find no way to make the feature work, so I have pulled it from the code. I have also inserted asserts to warn library users who would attempt to use illegal characters in user defaults keys in the future. In short, you want your user defaults keys to be something identifier-like.
Diffstat (limited to 'Framework')
-rw-r--r--Framework/MASShortcutBinder.m25
-rw-r--r--Framework/MASShortcutBinderTests.m21
2 files changed, 13 insertions, 33 deletions
diff --git a/Framework/MASShortcutBinder.m b/Framework/MASShortcutBinder.m
index 9b9f017..92ba6e8 100644
--- a/Framework/MASShortcutBinder.m
+++ b/Framework/MASShortcutBinder.m
@@ -23,7 +23,7 @@
- (void) dealloc
{
for (NSString *bindingName in [_actions allKeys]) {
- [self unbind:[self encodeBindingName:bindingName]];
+ [self unbind:bindingName];
}
}
@@ -41,8 +41,12 @@
- (void) bindShortcutWithDefaultsKey: (NSString*) defaultsKeyName toAction: (dispatch_block_t) action
{
+ NSAssert([defaultsKeyName rangeOfString:@"."].location == NSNotFound,
+ @"Illegal character in binding name (“.”), please see http://git.io/x5YS.");
+ NSAssert([defaultsKeyName rangeOfString:@" "].location == NSNotFound,
+ @"Illegal character in binding name (“ ”), please see http://git.io/x5YS.");
[_actions setObject:[action copy] forKey:defaultsKeyName];
- [self bind:[self encodeBindingName:defaultsKeyName]
+ [self bind:defaultsKeyName
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:[@"values." stringByAppendingString:defaultsKeyName]
options:_bindingOptions];
@@ -53,7 +57,7 @@
[_shortcutMonitor unregisterShortcut:[_shortcuts objectForKey:defaultsKeyName]];
[_shortcuts removeObjectForKey:defaultsKeyName];
[_actions removeObjectForKey:defaultsKeyName];
- [self unbind:[self encodeBindingName:defaultsKeyName]];
+ [self unbind:defaultsKeyName];
}
- (void) registerDefaultShortcuts: (NSDictionary*) defaultShortcuts
@@ -76,18 +80,6 @@
#pragma mark Bindings
-static NSString *const MASDotSymbolReplacement = @"__dot__";
-
-- (NSString*) encodeBindingName: (NSString*) binding
-{
- return [binding stringByReplacingOccurrencesOfString:@"." withString:MASDotSymbolReplacement];
-}
-
-- (NSString*) decodeBindingName: (NSString*) binding
-{
- return [binding stringByReplacingOccurrencesOfString:MASDotSymbolReplacement withString:@"."];
-}
-
- (BOOL) isRegisteredAction: (NSString*) name
{
return !![_actions objectForKey:name];
@@ -95,7 +87,6 @@ static NSString *const MASDotSymbolReplacement = @"__dot__";
- (id) valueForUndefinedKey: (NSString*) key
{
- key = [self decodeBindingName:key];
return [self isRegisteredAction:key] ?
[_shortcuts objectForKey:key] :
[super valueForUndefinedKey:key];
@@ -103,8 +94,6 @@ static NSString *const MASDotSymbolReplacement = @"__dot__";
- (void) setValue: (id) value forUndefinedKey: (NSString*) key
{
- key = [self decodeBindingName:key];
-
if (![self isRegisteredAction:key]) {
[super setValue:value forUndefinedKey:key];
return;
diff --git a/Framework/MASShortcutBinderTests.m b/Framework/MASShortcutBinderTests.m
index 35542d4..ab8383e 100644
--- a/Framework/MASShortcutBinderTests.m
+++ b/Framework/MASShortcutBinderTests.m
@@ -95,22 +95,13 @@ static NSString *const SampleDefaultsKey = @"sampleShortcut";
@"Bind shortcut using a default value.");
}
-// The connection between user defaults and shortcuts is implemented
-// using Cocoa Bindings where the dot symbol (“.”) has a special meaning.
-// This means we have to escape the dot symbol used in defaults keys,
-// otherwise things blow up. Details at <http://git.io/x5YS>.
-- (void) testBindingsWithDotSymbol
+// See issue #64 <http://git.io/x5YS> for rationale and discussion.
+- (void) testIllegalSymbolsInBindingNames
{
- static NSString *const SampleDefaultsKeyWithDotSymbol = @"sample.Shortcut";
- MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:1 modifierFlags:1];
- XCTAssertNoThrow([_binder bindShortcutWithDefaultsKey:SampleDefaultsKeyWithDotSymbol toAction:^{}],
- @"Binding a shortcut to a defaults key with a dot symbol must not throw.");
- [_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:shortcut] forKey:SampleDefaultsKeyWithDotSymbol];
- XCTAssertTrue([_monitor isShortcutRegistered:shortcut],
- @"Read a shortcut value using a defaults key with a dot symbol.");
- [_binder breakBindingWithDefaultsKey:SampleDefaultsKeyWithDotSymbol];
- XCTAssertFalse([_monitor isShortcutRegistered:shortcut],
- @"Breaking a binding with a dot symbol.");
+ XCTAssertThrows([_binder bindShortcutWithDefaultsKey:@"foo.bar" toAction:^{}],
+ @"Throw for illegal binding symbols: a dot (“.”).");
+ XCTAssertThrows([_binder bindShortcutWithDefaultsKey:@"foo bar" toAction:^{}],
+ @"Throw for illegal binding symbols: a space (“ ”).");
}
@end