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

Commit

Permalink
[ios, macos] Fix format expression parsing. (#14168) (#14178)
Browse files Browse the repository at this point in the history
CP [ios, macos] Fix format expression parsing. (#14168)
  • Loading branch information
fabian-guerra authored Mar 21, 2019
1 parent 9171b82 commit 5782cf5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
18 changes: 5 additions & 13 deletions platform/darwin/src/NSExpression+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1016,27 +1016,19 @@ - (id)mgl_jsonExpressionObject {
if ([constantValue isKindOfClass:[MGLAttributedExpression class]]) {
MGLAttributedExpression *attributedExpression = (MGLAttributedExpression *)constantValue;
id jsonObject = attributedExpression.expression.mgl_jsonExpressionObject;
NSMutableArray *attributes = [NSMutableArray array];
if ([jsonObject isKindOfClass:[NSArray class]]) {
[attributes addObjectsFromArray:jsonObject];
} else {
[attributes addObject:jsonObject];
}
NSMutableDictionary *attributedDictionary = [NSMutableDictionary dictionary];

if (attributedExpression.attributes) {
NSMutableDictionary *attributedDictionary = [NSMutableDictionary dictionaryWithDictionary:attributedExpression.attributes];
attributedDictionary = [NSMutableDictionary dictionaryWithDictionary:attributedExpression.attributes];
if (attributedDictionary[MGLFontNamesAttribute]) {
attributedDictionary[MGLFontNamesAttribute] = @[@"literal", attributedDictionary[MGLFontNamesAttribute]];
}
if (attributedDictionary[MGLFontColorAttribute] && [attributedDictionary[MGLFontColorAttribute] isKindOfClass:[MGLColor class]]) {
MGLColor *color = attributedDictionary[MGLFontColorAttribute];
attributedDictionary[MGLFontColorAttribute] = color.mgl_jsonExpressionObject;
}
[attributes addObject:attributedDictionary];
} else {
[attributes addObject:@{}];
}

return attributes;
}
return @[jsonObject, attributedDictionary];
}
return self.constantValue;
}
Expand Down
20 changes: 20 additions & 0 deletions platform/darwin/test/MGLExpressionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,14 @@ - (void)testFormatExpressionObject {
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"]] ;
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)];

NSArray *jsonExpression = @[ @"format", @"foo", @{ } ];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"]
attributes:@{ MGLFontSizeAttribute: @(1.2),
Expand All @@ -1086,6 +1094,18 @@ - (void)testFormatExpressionObject {
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionWithFormat:@"CAST(x, 'NSString')"]
attributes:@{ MGLFontSizeAttribute: @(1.2),
MGLFontColorAttribute: [MGLColor redColor],
MGLFontNamesAttribute: @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]
}] ;
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)];

NSArray *jsonExpression = @[ @"format", @[@"to-string", @[@"get", @"x"]], @{ @"font-scale": @1.2, @"text-color": @[@"rgb", @255, @0, @0] , @"text-font" : @[ @"literal", @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]]} ];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
MGLAttributedExpression *attribute1 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"foo"]
fontNames:nil
Expand Down

0 comments on commit 5782cf5

Please sign in to comment.