Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Avoid crash in NSOrthography category method #619

Merged
merged 4 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions platform/darwin/src/NSString+MGLAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ NS_ASSUME_NONNULL_BEGIN

/**
Returns a transliterated representation of the receiver using the specified
script. If transliteration fails, the receiver will be returned.
script. If transliteration fails or script is nil, the receiver will be returned.

Only supports scripts for languages used by Mapbox Streets.

@param script The four-letter code representing the name of the script, as
specified by ISO 15924.
*/
- (NSString *)mgl_stringByTransliteratingIntoScript:(NSString *)script;
- (NSString *)mgl_stringByTransliteratingIntoScript:(nullable NSString *)script;

@end

Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/NSString+MGLAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ - (NSString *)mgl_titleCasedStringWithLocale:(NSLocale *)locale {
return string;
}

- (NSString *)mgl_stringByTransliteratingIntoScript:(NSString *)script {
- (NSString *)mgl_stringByTransliteratingIntoScript:(nullable NSString *)script {
NSMutableString *string = self.mutableCopy;
NSStringTransform transform;
if ([script isEqualToString:@"Latn"]) {
Expand Down
2 changes: 2 additions & 0 deletions platform/darwin/test/MGLNSStringAdditionsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ - (void)testTransliteratedString {
XCTAssertEqualObjects([@"ロンドン" mgl_stringByTransliteratingIntoScript:@"Jpan"], @"ロンドン");
XCTAssertEqualObjects([@"ロンドン" mgl_stringByTransliteratingIntoScript:@"Kore"], @"론돈");
XCTAssertEqualObjects([@"ロンドン" mgl_stringByTransliteratingIntoScript:@"Fake"], @"ロンドン");

XCTAssertEqualObjects([@"" mgl_stringByTransliteratingIntoScript:nil], @"");
}

@end
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT

* Fixed a CocoaPods warning when integrating this SDK and the Mapbox Navigation SDK for iOS into the same application. ([#549](https://github.com/mapbox/mapbox-gl-native-ios/pull/549))
* Fixed an issue where offline packs were not invalidated before being deallocated, resulting in a crash. ([#620](https://github.com/mapbox/mapbox-gl-native-ios/pull/620))
* Fixed a crash in `-[NSOrthography mgl_dominantScriptForMapboxStreetsLanguage]`. ([#619](https://github.com/mapbox/mapbox-gl-native-ios/pull/619))

## 6.3.0 - November 10, 2020

Expand Down
11 changes: 7 additions & 4 deletions platform/ios/src/MGLMapAccessibilityElement.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ - (instancetype)initWithAccessibilityContainer:(id)container feature:(id<MGLFeat
_feature = feature;

NSString *languageCode = [MGLVectorTileSource preferredMapboxStreetsLanguage];
NSString *nameAttribute = [NSString stringWithFormat:@"name_%@", languageCode];
NSString *name = [feature attributeForKey:nameAttribute];
NSString *name;
if (languageCode != nil) {
NSString *nameAttribute = [NSString stringWithFormat:@"name_%@", languageCode];
name = [feature attributeForKey:nameAttribute];
}

NSString *dominantScript;
if (name == nil && [feature attributeForKey:@"name"] != nil) {
Expand All @@ -62,7 +65,7 @@ - (instancetype)initWithAccessibilityContainer:(id)container feature:(id<MGLFeat
// may be in the local language, which may be written in another script.
// Attempt to transform to the script of the preferred language, keeping
// the original string if no transform exists or if transformation fails.
if (!dominantScript) {
if (dominantScript == nil && languageCode != nil) {
dominantScript = [NSOrthography mgl_dominantScriptForMapboxStreetsLanguage:languageCode];
}
name = [name mgl_stringByTransliteratingIntoScript:dominantScript];
Expand All @@ -88,7 +91,7 @@ - (instancetype)initWithAccessibilityContainer:(id)container feature:(id<MGLFeat
// Announce the kind of place or POI.
NSString *languageCode = [MGLVectorTileSource preferredMapboxStreetsLanguage];
NSString *categoryAttribute = [NSString stringWithFormat:@"category_%@", languageCode];
if (attributes[categoryAttribute]) {
if (languageCode != nil && attributes[categoryAttribute]) {
[facts addObject:attributes[categoryAttribute]];
} else if (attributes[@"type"]) {
// FIXME: Unfortunately, these types aren’t a closed set that can be
Expand Down
4 changes: 4 additions & 0 deletions platform/ios/src/NSOrthography+MGLAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
@implementation NSOrthography (MGLAdditions)

+ (NSString *)mgl_dominantScriptForMapboxStreetsLanguage:(NSString *)language {
macdrevx marked this conversation as resolved.
Show resolved Hide resolved
if (language == nil) {
macdrevx marked this conversation as resolved.
Show resolved Hide resolved
return nil;
macdrevx marked this conversation as resolved.
Show resolved Hide resolved
}

if (@available(iOS 11.0, *)) {
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:language];
NSOrthography *orthography = [NSOrthography defaultOrthographyForLanguage:locale.localeIdentifier];
Expand Down
4 changes: 4 additions & 0 deletions platform/ios/test/MGLNSOrthographyAdditionsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ - (void)testStreetsLanguages {
}
}

- (void)testDominantScriptWhenLanguageIsNil {
XCTAssertNil([NSOrthography mgl_dominantScriptForMapboxStreetsLanguage:nil]);
}

@end