Skip to content

Commit

Permalink
Fix localization crash on iOS 11 and 12
Browse files Browse the repository at this point in the history
* Style.localizeLabels(into:forLayerIds:) crashed on iOS 11 and 12 if
  any of the symbol layers had text-field set to nil.

Fixes #954
  • Loading branch information
macdrevx committed Apr 15, 2022
1 parent 966db5a commit 7741c54
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions Sources/MapboxMaps/Style/Style+Localization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 17 additions & 0 deletions Tests/MapboxMapsTests/Style/Style+LocalizationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 7741c54

Please sign in to comment.