Skip to content
Snippets Groups Projects
Commit 3bd581b6 authored by George Nachman's avatar George Nachman
Browse files

The paste helper tests were broken because of the change to escape tabs. This exposed a few bugs:

- Tabs should be expanded to spaces before being escaped with backslashes
- Do not double-escape with backslash and ^V. Prefer ^V.

This commit also fixes the tests by adding tab escapes where needed.
parent cc27fb89
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -139,7 +139,7 @@ static const double kFloatingPointTolerance = 0.00001;
 
- (void)testSanitizeEscapeSpecialCharacters {
[self sanitizeString:kTestString
expect:@"a\\ \\(\t\r\r\n" @"\x16" @"“”‘’–—b"
expect:@"a\\ \\(\\\t\r\r\n" @"\x16" @"“”‘’–—b"
flags:kPasteFlagsEscapeSpecialCharacters
tabTransform:kTabTransformNone
spacesPerTab:0];
Loading
Loading
@@ -186,7 +186,7 @@ static const double kFloatingPointTolerance = 0.00001;
}
 
- (void)testSanitizeAllFlagsOn {
NSString *expectedString = @"a\\ \\(\t\r\r\\\"\\\"\\'\\'--b";
NSString *expectedString = @"a\\ \\(\\\t\r\r\\\"\\\"\\'\\'--b";
NSData *data = [expectedString dataUsingEncoding:NSUTF8StringEncoding];
NSString *expected = [data stringWithBase64EncodingWithLineBreak:@"\r"];
[self sanitizeString:kTestString
Loading
Loading
@@ -242,6 +242,30 @@ static const double kFloatingPointTolerance = 0.00001;
XCTAssert([_writeBuffer isEqualToString:expected]);
}
 
- (void)testExpandTabsBeforeEscaping {
[_helper pasteString:@"\t"
slowly:NO
escapeShellChars:YES
commands:NO
tabTransform:kTabTransformConvertToSpaces
spacesPerTab:4];
[self runTimer];
NSString *expected = @"\\ \\ \\ \\ ";
XCTAssert([_writeBuffer isEqualToString:expected]);
}
- (void)testEscapeDoesNotEscapeCarriageReturn {
[_helper pasteString:@"\r"
slowly:NO
escapeShellChars:YES
commands:NO
tabTransform:kTabTransformConvertToSpaces
spacesPerTab:4];
[self runTimer];
NSString *expected = @"\r";
XCTAssert([_writeBuffer isEqualToString:expected]);
}
- (void)testPasteStringWithFlagsAndConvertToSpacesTabTransform {
[_helper pasteString:kTestString
slowly:NO
Loading
Loading
@@ -250,7 +274,31 @@ static const double kFloatingPointTolerance = 0.00001;
tabTransform:kTabTransformConvertToSpaces
spacesPerTab:4];
[self runTimer];
NSString *expected = @"a\\ \\( \r\r“”‘’–—b";
NSString *expected = @"a\\ \\(\\ \\ \\ \\ \r\r“”‘’–—b";
XCTAssert([_writeBuffer isEqualToString:expected]);
}
- (void)testDoNotEscapeNonAscii {
[_helper pasteString:@"“"
slowly:NO
escapeShellChars:YES
commands:NO
tabTransform:kTabTransformEscapeWithCtrlV
spacesPerTab:0];
[self runTimer];
NSString *expected = @"“";
XCTAssert([_writeBuffer isEqualToString:expected]);
}
- (void)testStripControlV {
[_helper pasteString:@"\x16"
slowly:NO
escapeShellChars:YES
commands:NO
tabTransform:kTabTransformEscapeWithCtrlV
spacesPerTab:0];
[self runTimer];
NSString *expected = @"";
XCTAssert([_writeBuffer isEqualToString:expected]);
}
 
Loading
Loading
Loading
Loading
@@ -67,6 +67,7 @@ int decode_utf8_char(const unsigned char * restrict datap,
// Returns the number of lines in a string.
- (NSUInteger)numberOfLines;
- (NSString *)stringWithEscapedShellCharactersIncludingNewlines:(BOOL)includingNewlines;
- (NSString *)stringWithEscapedShellCharactersExceptTabAndNewline;
 
// Replaces tab with ^V + tab.
- (NSString *)stringWithShellEscapedTabs;
Loading
Loading
Loading
Loading
@@ -110,6 +110,12 @@
return [NSString stringWithString:aMutableString];
}
 
- (NSString *)stringWithEscapedShellCharactersExceptTabAndNewline {
NSMutableString *aMutableString = [[[NSMutableString alloc] initWithString:self] autorelease];
[aMutableString escapeShellCharactersExceptTabAndNewline];
return [NSString stringWithString:aMutableString];
}
- (NSString *)stringWithShellEscapedTabs
{
const int kLNEXT = 22;
Loading
Loading
@@ -1589,11 +1595,20 @@ static TECObjectRef CreateTECConverterForUTF8Variants(TextEncodingVariant varian
}
}
 
- (void)escapeShellCharactersExceptTabAndNewline {
NSString *charsToEscape = [[NSString shellEscapableCharacters] stringByReplacingOccurrencesOfString:@"\t" withString:@""];
return [self escapeCharacters:charsToEscape];
}
- (void)escapeShellCharactersIncludingNewlines:(BOOL)includingNewlines {
NSString* charsToEscape = [NSString shellEscapableCharacters];
if (includingNewlines) {
charsToEscape = [charsToEscape stringByAppendingString:@"\r\n"];
}
[self escapeCharacters:charsToEscape];
}
- (void)escapeCharacters:(NSString *)charsToEscape {
for (int i = 0; i < [charsToEscape length]; i++) {
NSString* before = [charsToEscape substringWithRange:NSMakeRange(i, 1)];
NSString* after = [@"\\" stringByAppendingString:before];
Loading
Loading
Loading
Loading
@@ -127,17 +127,13 @@ const int kNumberOfSpacesPerTabNoConversion = -1;
range:NSMakeRange(0, theString.length)];
}
 
if (flags & kPasteFlagsEscapeSpecialCharacters) {
// Put backslash before anything the shell might interpret.
theString = [theString stringWithEscapedShellCharactersIncludingNewlines:NO];
}
if (flags & kPasteFlagsRemovingUnsafeControlCodes) {
// All control codes except tab (9), newline (10), form feed (12), and carriage return (13)
// are removed.
// All control codes except tab (9), newline (10), form feed (12), carriage return (13),
// and shell escape (22) are removed. This must be done before transforming tabs since
// that transformation might add ^Vs.
theString =
[[theString componentsSeparatedByCharactersInSet:[iTermPasteHelper unsafeControlCodeSet]]
componentsJoinedByString:@""];
[[theString componentsSeparatedByCharactersInSet:[iTermPasteHelper unsafeControlCodeSet]]
componentsJoinedByString:@""];
}
 
switch (pasteEvent.tabTransform) {
Loading
Loading
@@ -156,6 +152,16 @@ const int kNumberOfSpacesPerTabNoConversion = -1;
break;
}
 
if (flags & kPasteFlagsEscapeSpecialCharacters) {
// Put backslash before anything the shell might interpret.
if (pasteEvent.tabTransform != kTabTransformEscapeWithCtrlV) {
theString = [theString stringWithEscapedShellCharactersIncludingNewlines:NO];
} else {
// Avoid double-escaping tabs with both ^V and backslash.
theString = [theString stringWithEscapedShellCharactersExceptTabAndNewline];
}
}
if (pasteEvent.flags & kPasteFlagsUseRegexSubstitution && pasteEvent.regex.length > 0) {
NSString *replacement = nil;
@try {
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment