Skip to content

Commit e5705ad

Browse files
committed
refactor: extract priorityStyling sorting logic
1 parent f412b2e commit e5705ad

4 files changed

Lines changed: 28 additions & 21 deletions

File tree

ios/inputAttributesManager/InputAttributesManager.mm

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#import "ParagraphAttributesUtils.h"
66
#import "RangeUtils.h"
77
#import "StyleHeaders.h"
8+
#import "StyleUtils.h"
89
#import "ZeroWidthSpaceUtils.h"
910

1011
@implementation InputAttributesManager {
@@ -99,17 +100,11 @@ - (void)handleDirtyRangesStyling {
99100
// attributes (e.g. foreground color, font) are laid down before inline
100101
// styles override them on their specific sub-ranges. Inline styles among
101102
// themselves follow their stylePriority.
102-
NSArray *sortedStyleTypes = [presentStyles.allKeys
103-
sortedArrayWithOptions:NSSortStable
104-
usingComparator:^NSComparisonResult(NSNumber *a, NSNumber *b) {
105-
NSInteger aOrder = [_input->stylesDict[a] stylePriority];
106-
NSInteger bOrder = [_input->stylesDict[b] stylePriority];
107-
if (aOrder == bOrder) {
108-
return [a compare:b];
109-
}
110-
return aOrder < bOrder ? NSOrderedAscending
111-
: NSOrderedDescending;
112-
}];
103+
NSArray *sortedStyleTypes =
104+
[StyleUtils sortedArray:presentStyles.allKeys
105+
bySortKey:^NSInteger(NSNumber *styleType) {
106+
return [_input->stylesDict[styleType] stylePriority];
107+
}];
113108

114109
// re-apply meta-attributes and apply visual styling following the saved
115110
// occurences.

ios/textHtmlParser/TextHtmlParser.mm

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,11 @@ - (void)applyProcessedStyles:(NSArray *_Nonnull)processedStyles {
154154
}
155155

156156
// Respect the styling priority
157-
NSArray *sortedInlineApply = [pendingInlineApply
158-
sortedArrayWithOptions:NSSortStable
159-
usingComparator:^NSComparisonResult(NSArray *a, NSArray *b) {
160-
NSInteger aPriority = [((StyleBase *)a[0]) stylePriority];
161-
NSInteger bPriority = [((StyleBase *)b[0]) stylePriority];
162-
if (aPriority == bPriority)
163-
return NSOrderedSame;
164-
return aPriority < bPriority ? NSOrderedAscending
165-
: NSOrderedDescending;
166-
}];
157+
NSArray *sortedInlineApply =
158+
[StyleUtils sortedArray:pendingInlineApply
159+
bySortKey:^NSInteger(NSArray *entry) {
160+
return [((StyleBase *)entry[0]) stylePriority];
161+
}];
167162

168163
// Apply visual styling for inline styles
169164
for (NSArray *entry in sortedInlineApply) {

ios/utils/StyleUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@
3030
+ (void)removeStyleConflict:(StyleType)conflicting
3131
from:(StyleType)conflicted
3232
forHost:(id<EnrichedViewHost>)host;
33+
34+
+ (NSArray *)sortedArray:(NSArray *)array
35+
bySortKey:(NSInteger (^)(id item))sortKeyForItem;
3336
@end

ios/utils/StyleUtils.mm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,18 @@ + (void)removeStyleConflict:(StyleType)conflicting
287287
}
288288
}
289289

290+
+ (NSArray *)sortedArray:(NSArray *)array
291+
bySortKey:(NSInteger (^)(id item))sortKeyForItem {
292+
return [array sortedArrayWithOptions:NSSortStable
293+
usingComparator:^NSComparisonResult(id a, id b) {
294+
NSInteger aKey = sortKeyForItem(a);
295+
NSInteger bKey = sortKeyForItem(b);
296+
if (aKey == bKey) {
297+
return NSOrderedSame;
298+
}
299+
return aKey < bKey ? NSOrderedAscending
300+
: NSOrderedDescending;
301+
}];
302+
}
303+
290304
@end

0 commit comments

Comments
 (0)