From b9280debe52cbcffe75d398ab12d4c8bfea8d656 Mon Sep 17 00:00:00 2001 From: Andrew Hershberger Date: Tue, 19 Apr 2022 05:26:58 -0500 Subject: [PATCH] Fix localization crash on iOS 11 and 12 (#1278) * Style.localizeLabels(into:forLayerIds:) crashed on iOS 11 and 12 if any of the symbol layers had text-field set to nil. Fixes #954 --- CHANGELOG.md | 1 + .../MapboxMaps/Style/Style+Localization.swift | 4 ++-- .../Style/Style+LocalizationTests.swift | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f103885d8f87..a5b4b7743d20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Mapbox welcomes participation and contributions from everyone. * Add support for runtime source properties. ([#1267](https://github.com/mapbox/mapbox-maps-ios/pull/1267)) * Start location services lazily. ([#1262](https://github.com/mapbox/mapbox-maps-ios/pull/1262)) +* Fix localization crash on iOS 11 and 12. ([#1278](https://github.com/mapbox/mapbox-maps-ios/pull/1278)) ## 10.5.0-beta.1 - April 7, 2022 diff --git a/Sources/MapboxMaps/Style/Style+Localization.swift b/Sources/MapboxMaps/Style/Style+Localization.swift index d4ed7d92ba26..950ae7da3827 100644 --- a/Sources/MapboxMaps/Style/Style+Localization.swift +++ b/Sources/MapboxMaps/Style/Style+Localization.swift @@ -83,8 +83,8 @@ extension Style { let expressionAbbr = try NSRegularExpression(pattern: "\\[\"get\",\\s*\"abbr\"\\]", options: .caseInsensitive) - if var stringExpression = String(data: try JSONEncoder().encode(symbolLayer.textField), encoding: .utf8), - stringExpression != "null" { + if case .expression(let textField) = symbolLayer.textField, + var stringExpression = String(data: try JSONEncoder().encode(textField), encoding: .utf8) { stringExpression.updateOnceExpression(replacement: replacement, regex: expressionCoalesce) stringExpression.updateExpression(replacement: replacement, regex: expressionAbbr) diff --git a/Tests/MapboxMapsTests/Style/Style+LocalizationTests.swift b/Tests/MapboxMapsTests/Style/Style+LocalizationTests.swift index c6a6f233c769..c9607ac9d12a 100644 --- a/Tests/MapboxMapsTests/Style/Style+LocalizationTests.swift +++ b/Tests/MapboxMapsTests/Style/Style+LocalizationTests.swift @@ -81,4 +81,21 @@ final class StyleLocalizationTests: MapViewIntegrationTestCase { } )) } + + func testSkipsSymbolLayersWhereTextFieldIsNil() throws { + var source = GeoJSONSource() + source.data = .feature(Feature(geometry: Point(CLLocationCoordinate2D(latitude: 0, longitude: 0)))) + try style.addSource(source, id: "a") + + var symbolLayer = SymbolLayer(id: "a") + symbolLayer.source = "a" + + try style.addLayer(symbolLayer) + + try style.localizeLabels(into: Locale(identifier: "de")) + + let updatedLayer = try style.layer(withId: "a", type: SymbolLayer.self) + + XCTAssertNil(updatedLayer.textField) + } }