diff --git a/Foundation/CPAttributedString.j b/Foundation/CPAttributedString.j index f1f25bef9..bbb37abe1 100755 --- a/Foundation/CPAttributedString.j +++ b/Foundation/CPAttributedString.j @@ -413,7 +413,7 @@ comparisonAttributes = [aString attributesAtIndex:0 effectiveRange:comparisonRange], length = _string.length; - while (CPMaxRange(CPUnionRange(myRange, comparisonRange)) < length) + do { if (CPIntersectionRange(myRange, comparisonRange).length > 0 && ![myAttributes isEqualToDictionary:comparisonAttributes]) @@ -424,7 +424,7 @@ myAttributes = [self attributesAtIndex:CPMaxRange(myRange) effectiveRange:myRange]; else comparisonAttributes = [aString attributesAtIndex:CPMaxRange(comparisonRange) effectiveRange:comparisonRange]; - } + } while (CPMaxRange(CPUnionRange(myRange, comparisonRange)) < length); return YES; } diff --git a/Tests/Foundation/CPAttributedStringTest.j b/Tests/Foundation/CPAttributedStringTest.j index 5314d3c41..d83afcf75 100644 --- a/Tests/Foundation/CPAttributedStringTest.j +++ b/Tests/Foundation/CPAttributedStringTest.j @@ -207,6 +207,38 @@ var sharedObject = [CPObject new]; [self assertFalse:[a isEqual:@"HELLO!"] message:"Expected a to not equal 'HELLO!', but it did"]; } +- (void)testIsEqualEmpty +{ + var a = [[CPMutableAttributedString alloc] initWithString:@""], + b = [[CPMutableAttributedString alloc] initWithString:@""]; + + [self assertTrue:[a isEqual:b]]; +} + +- (void)testIsEqualWithSimpleAttribute +{ + var a = [[CPMutableAttributedString alloc] initWithString:@"GREETINGS!"], + b = [[CPMutableAttributedString alloc] initWithString:@"GREETINGS!"]; + + [a addAttribute:"color" value:[CPColor redColor] range:CPMakeRange(0, 5)]; + [self assertFalse:[a isEqual:b] message:"red string should not equal string without color attribute"]; + [b addAttribute:"color" value:[CPColor redColor] range:CPMakeRange(0, 5)]; + [self assertTrue:[a isEqual:b]]; +} + +- (void)testIsEqualWithTwoAttributes +{ + var a = [[CPMutableAttributedString alloc] initWithString:@"GREETINGS!"], + b = [[CPMutableAttributedString alloc] initWithString:@"GREETINGS!"]; + + [a addAttribute:"color" value:[CPColor redColor] range:CPMakeRange(0, 9)]; + [b addAttribute:"color" value:[CPColor redColor] range:CPMakeRange(0, 9)]; + [a addAttribute:"font" value:"Helvetica" range:CPMakeRange(1, 4)]; + [self assertFalse:[a isEqual:b] message:"font difference from index 1 should be found"]; + [b addAttribute:"font" value:"Helvetica" range:CPMakeRange(1, 4)]; + [self assertTrue:[a isEqual:b]]; +} + //Extracting a Substring //- (CPAttributedString)attributedSubstringFromRange:(CPRange)aRange - (void)testAttributedSubstringFromRange