From 6cecca6c4f0ffb6edea41f3ade3b7f90f92cdf81 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Thu, 6 Mar 2014 21:32:36 -0500 Subject: [PATCH] Fix CSS id for string prefixed with a digit When encoding a string as a CSS identifier, we must add an underscore if the strings starts with a digit. --- NEWS | 1 + SoObjects/Mailer/SOGoMailAccounts.m | 11 +++--- SoObjects/SOGo/NSString+Utilities.m | 35 +++++++++++++------ .../JavascriptAPIExtensions.js | 4 +++ 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index 4dcb8d11e..8e355fc77 100644 --- a/NEWS +++ b/NEWS @@ -16,6 +16,7 @@ Bug fixes - fixed returned date format for email messages in Active Sync - fixed missing 'name part' in address for email messages in Active Sync - fixed race condition when syncing huge amount of deleted messages over Active Sync + - fixed encoding of string as CSS identifier when the string starts with a digit 2.2.0 (2014-02-24) ------------------ diff --git a/SoObjects/Mailer/SOGoMailAccounts.m b/SoObjects/Mailer/SOGoMailAccounts.m index 3d792d885..ae175b00d 100644 --- a/SoObjects/Mailer/SOGoMailAccounts.m +++ b/SoObjects/Mailer/SOGoMailAccounts.m @@ -78,20 +78,23 @@ { id obj; NSArray *accounts; + NSString *key; SOGoUser *user; int keyCount; + + key = [_key fromCSSIdentifier]; /* first check attributes directly bound to the application */ - obj = [super lookupName:_key inContext:_ctx acquire:NO]; + obj = [super lookupName:key inContext:_ctx acquire:NO]; if (!obj) { user = [SOGoUser userWithLogin: [self ownerInContext: nil]]; accounts = [user mailAccounts]; - keyCount = [_key intValue]; - if ([_key isEqualToString: [NSString stringWithFormat: @"%d", keyCount]] + keyCount = [key intValue]; + if ([key isEqualToString: [NSString stringWithFormat: @"%d", keyCount]] && keyCount > -1 && keyCount < [accounts count]) - obj = [SOGoMailAccount objectWithName: _key inContainer: self]; + obj = [SOGoMailAccount objectWithName: key inContainer: self]; else obj = [NSException exceptionWithHTTPStatus: 404 /* Not Found */]; } diff --git a/SoObjects/SOGo/NSString+Utilities.m b/SoObjects/SOGo/NSString+Utilities.m index ba297cac7..25a9c403d 100644 --- a/SoObjects/SOGo/NSString+Utilities.m +++ b/SoObjects/SOGo/NSString+Utilities.m @@ -332,8 +332,7 @@ static NSCharacterSet *controlCharSet = nil; (cssEscapingCount + 1) * sizeof (unichar)); for (count = 0; count < cssEscapingCount; count++) - *(cssEscapingCharacters + count) - = [[characters objectAtIndex: count] characterAtIndex: 0]; + *(cssEscapingCharacters + count) = [[characters objectAtIndex: count] characterAtIndex: 0]; *(cssEscapingCharacters + cssEscapingCount) = 0; } @@ -360,14 +359,20 @@ static NSCharacterSet *controlCharSet = nil; cssIdentifier = [NSMutableString string]; max = [self length]; - for (count = 0; count < max; count++) + if (max > 0) { - currentChar = [self characterAtIndex: count]; - idx = [self _cssCharacterIndex: currentChar]; - if (idx > -1) - [cssIdentifier appendString: cssEscapingStrings[idx]]; - else - [cssIdentifier appendFormat: @"%C", currentChar]; + if (isdigit([self characterAtIndex: 0])) + // A CSS identifier can't start with a digit; we add an underscore + [cssIdentifier appendString: @"_"]; + for (count = 0; count < max; count++) + { + currentChar = [self characterAtIndex: count]; + idx = [self _cssCharacterIndex: currentChar]; + if (idx > -1) + [cssIdentifier appendString: cssEscapingStrings[idx]]; + else + [cssIdentifier appendFormat: @"%C", currentChar]; + } } return cssIdentifier; @@ -397,7 +402,17 @@ static NSCharacterSet *controlCharSet = nil; newString = [NSMutableString string]; max = [self length]; - for (count = 0; count < max - 2; count++) + count = 0; + if (max > 0 + && [self characterAtIndex: 0] == '_' + && isdigit([self characterAtIndex: 1])) + { + /* If the identifier starts with an underscore followed by a digit, + we remove the underscore */ + count = 1; + } + + for (; count < max - 2; count++) { currentChar = [self characterAtIndex: count]; if (currentChar == '_') diff --git a/UI/WebServerResources/JavascriptAPIExtensions.js b/UI/WebServerResources/JavascriptAPIExtensions.js index 53e65b26b..d71b92a10 100644 --- a/UI/WebServerResources/JavascriptAPIExtensions.js +++ b/UI/WebServerResources/JavascriptAPIExtensions.js @@ -106,6 +106,10 @@ String.prototype.asCSSIdentifier = function() { newString = newString.replace(re, escapeds[i]); } + if (/^\d+/.test(newString)) { + newString = '_' + newString; + } + return newString; };