More progress on Autotype command parsing

This commit is contained in:
michael starke
2014-02-20 00:35:32 +01:00
parent ae544ae810
commit a046555aee
5 changed files with 48 additions and 29 deletions

View File

@@ -20,38 +20,31 @@
@implementation MPAutotypeCommand
+ (NSArray *)commandsForContext:(MPAutotypeContext *)context {
if([context isValid]) {
if(![context isValid]) {
return nil;
}
NSMutableArray *commands = [[NSMutableArray alloc] initWithCapacity:10];
BOOL outsideCommand = YES;
NSString *unparsedCommand = context.normalizedCommand;
while(YES) {
/* Outside Command */
if(outsideCommand) {
NSRange openingBracketRange = [unparsedCommand rangeOfString:@"{"];
if(openingBracketRange.location != NSNotFound && openingBracketRange.length == 1) {
outsideCommand = NO;
NSString *skipped = [unparsedCommand substringToIndex:openingBracketRange.location];
unparsedCommand = [unparsedCommand substringFromIndex:openingBracketRange.location + 1];
NSUInteger reserverd = [context.normalizedCommand length] / 4;
NSMutableArray *commands = [[NSMutableArray alloc] initWithCapacity:reserverd];
NSMutableArray __block *commandRanges = [[NSMutableArray alloc] initWithCapacity:reserverd];
NSRegularExpression *commandRegExp = [[NSRegularExpression alloc] initWithPattern:@"\\{[^\\}]+\\}" options:NSRegularExpressionCaseInsensitive error:0];
NSAssert(commandRegExp, @"RegExp is constant. Has to work all the time");
[commandRegExp enumerateMatchesInString:context.normalizedCommand options:0 range:NSMakeRange(0, [context.normalizedCommand length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
@autoreleasepool {
[commandRanges addObject:[NSValue valueWithRange:result.range]];
}
else {
/* No more opeing brackets, stop - or none at all */
[self appendPasteCommandForContent:unparsedCommand toCommands:commands];
break;
}];
NSUInteger skipped = 0;
for(NSValue *rangeValue in commandRanges) {
NSRange range = [rangeValue rangeValue];
/* All non-commands will get translated into paste commands */
if(range.location > skipped) {
NSString *pasteValue = [context.normalizedCommand substringWithRange:NSMakeRange(skipped, range.location - skipped)];
[self appendPasteCommandForContent:pasteValue toCommands:commands];
skipped = range.location;
}
}
/* Inside Command */
else {
NSRange closingBracketRange = [unparsedCommand rangeOfString:@"}"];
if(closingBracketRange.location == NSNotFound || closingBracketRange.length != 1) {
return nil;
}
outsideCommand = NO;
}
}
return commands;
}
+ (MPAutotypeCommand *)appendPasteCommandForContent:(NSString *)pasteContent toCommands:(NSMutableArray *)commands {
if(pasteContent) {

View File

@@ -24,8 +24,8 @@
/**
* The Autotype command as it's supplied by the entry
*/
@property (nonatomic, copy) NSString *command;
@property (nonatomic, copy) NSString *normalizedCommand;
@property (nonatomic, readonly, copy) NSString *command;
@property (nonatomic, readonly, copy) NSString *normalizedCommand;
/**
* Designated initializer
@@ -45,4 +45,6 @@
*/
- (BOOL)isValid;
- (NSString *)evaluatedCommand;
@end

View File

@@ -40,8 +40,18 @@
return copy;
}
- (BOOL)isValid {
return (self.normalizedCommand != nil);
}
- (NSString *)evaluatedCommand {
static NSString *evaluated;
if(!evaluated) {
NSString *placeholderFilled = [self.normalizedCommand evaluatePlaceholderWithEntry:self.entry];
evaluated = [placeholderFilled resolveReferencesWithTree:self.entry.tree];
}
return evaluated;
}
@end

View File

@@ -8,6 +8,10 @@
#import <XCTest/XCTest.h>
#import "NSString+Commands.h"
#import "MPAutotypeCommand.h"
#import "MPAutotypeContext.h"
#import "KPKEntry.h"
@interface KPKTestAutotypeNormalization : XCTestCase
@@ -34,4 +38,14 @@
XCTAssertTrue([@"{}{}{}{}{}{ }ThisIsValid{}{STOP}" validateCommmand]);
}
- (void)testCommandCreation {
KPKEntry *entry = [[KPKEntry alloc] init];
entry.title = @"Title";
entry.url = @"www.myurl.com";
entry.username = @"Username";
entry.password = @"Password";
MPAutotypeContext *context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"{USERNAME}{TAB}{PASSWORD}{ENTER}"];
NSArray *commands = [MPAutotypeCommand commandsForContext:context];
}
@end