Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions ios/parser/EnrichedHTMLToAttributedStringParser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ - (void)traverseNode:(xmlNodePtr)cur isLastRenderable:(BOOL)isLastRenderable {

BOOL hasContent = _plain.length > lengthBefore;

BOOL needsPlaceholder = isParagraphTag(tagChar) || isListItemTag(tagChar) ||
isCheckListTag(tagChar);
BOOL needsPlaceholder = isListItemTag(tagChar) || isCheckListTag(tagChar);
Comment thread
IvanIhnatsiuk marked this conversation as resolved.

if (!hasContent && isBlock && (needsPlaceholder && lengthBefore != 0)) {
[self appendEmptyBlockPlaceholder];
Expand Down
65 changes: 53 additions & 12 deletions ios/utils/Text/ZeroWidthSpaceUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ - (instancetype)initWithRange:(NSRange)range
@implementation ZeroWidthSpaceUtils
+ (void)handleZeroWidthSpacesInInput:(id)input {
EnrichedTextInputView *typedInput = (EnrichedTextInputView *)input;
if (typedInput == nullptr) {
if (typedInput == nil) {
return;
}

[self removeSpacesIfNeededinInput:typedInput];
[self addSpacesIfNeededinInput:typedInput];
NSArray<id<BaseStyleProtocol>> *zwsStyles =
[self ZWSStylesForInput:typedInput];

typedInput->blockEmitting = YES;
[self removeSpacesIfNeededinInput:typedInput zwsStyles:zwsStyles];
[self addSpacesIfNeededinInput:typedInput zwsStyles:zwsStyles];
typedInput->blockEmitting = NO;
}

+ (NSString *)stringByRemovingZWS:(NSString *)string {
Expand Down Expand Up @@ -101,12 +106,36 @@ + (BOOL)findAnyZWSStylesInInput:(EnrichedTextInputView *)input
if (attributeIndex == NSNotFound)
return NO;

id value = [storage attribute:NSParagraphStyleAttributeName
atIndex:attributeIndex
effectiveRange:nil];

for (id<BaseStyleProtocol> style in [self ZWSStylesForInput:input]) {
NSAttributedStringKey key = [[style class] attributeKey];
id value = [storage attribute:key
atIndex:attributeIndex
effectiveRange:nil];
if ([style styleCondition:value range:range]) {
return YES;
}
}
return NO;
}

+ (BOOL)findAnyZWSStylesInInput:(EnrichedTextInputView *)input
range:(NSRange)range
zwsStyles:(NSArray<id<BaseStyleProtocol>> *)zwsStyles {
NSTextStorage *storage = input->textView.textStorage;
NSUInteger length = storage.length;

NSUInteger attributeIndex = (range.location < length)
? range.location
: (length > 0 ? length - 1 : NSNotFound);

id value = [storage attribute:NSParagraphStyleAttributeName
atIndex:attributeIndex
effectiveRange:nil];

if (attributeIndex == NSNotFound)
return NO;
Comment thread
IvanIhnatsiuk marked this conversation as resolved.

for (id<BaseStyleProtocol> style in zwsStyles) {
if ([style styleCondition:value range:range]) {
return YES;
}
Expand Down Expand Up @@ -143,7 +172,9 @@ + (ZWSAdjustedRange *)rangeByEnsuringEmptyParagraphHasZWS:(NSRange)range
offsetDelta:1];
}

+ (void)removeSpacesIfNeededinInput:(EnrichedTextInputView *)input {
+ (void)removeSpacesIfNeededinInput:(EnrichedTextInputView *)input
zwsStyles:
(NSArray<id<BaseStyleProtocol>> *)zwsStyles {
NSTextStorage *storage = input->textView.textStorage;
NSString *string = storage.string;
NSUInteger length = string.length;
Expand Down Expand Up @@ -180,7 +211,9 @@ + (void)removeSpacesIfNeededinInput:(EnrichedTextInputView *)input {
}

if (!removeSpace) {
if (![self findAnyZWSStylesInInput:input range:range]) {
if (![self findAnyZWSStylesInInput:input
range:range
zwsStyles:zwsStyles]) {
removeSpace = YES;
}
}
Expand All @@ -189,7 +222,8 @@ + (void)removeSpacesIfNeededinInput:(EnrichedTextInputView *)input {
[indexesToRemove addIndex:i];
}
}

NSTextStorage *textStorage = input->textView.textStorage;
[textStorage beginEditing];
// do the removing
[indexesToRemove
enumerateIndexesWithOptions:NSEnumerationReverse
Expand All @@ -201,6 +235,7 @@ + (void)removeSpacesIfNeededinInput:(EnrichedTextInputView *)input {
input:input
withSelection:NO];
}];
[textStorage endEditing];

// fix the selection if needed
if ([input->textView isFirstResponder]) {
Expand All @@ -216,7 +251,8 @@ + (void)removeSpacesIfNeededinInput:(EnrichedTextInputView *)input {
}
}

+ (void)addSpacesIfNeededinInput:(EnrichedTextInputView *)input {
+ (void)addSpacesIfNeededinInput:(EnrichedTextInputView *)input
zwsStyles:(NSArray<id<BaseStyleProtocol>> *)zwsStyles {
NSTextStorage *storage = input->textView.textStorage;
NSString *string = storage.string;
NSUInteger length = string.length;
Expand Down Expand Up @@ -247,14 +283,18 @@ + (void)addSpacesIfNeededinInput:(EnrichedTextInputView *)input {

if (isEmptyParagraph) {
NSRange checkRange = NSMakeRange(paragraphStart, 1);
BOOL found = [self findAnyZWSStylesInInput:input range:checkRange];
BOOL found = [self findAnyZWSStylesInInput:input
range:checkRange
zwsStyles:zwsStyles];
if (found) {
[indexesToInsert addIndex:paragraphStart];
}
}

paragraphStart = i + 1;
}
NSTextStorage *textStorage = input->textView.textStorage;
[textStorage beginEditing];

[indexesToInsert
enumerateIndexesWithOptions:NSEnumerationReverse
Expand All @@ -276,6 +316,7 @@ + (void)addSpacesIfNeededinInput:(EnrichedTextInputView *)input {
withSelection:NO];
}
}];
[textStorage endEditing];

// fix selection
if ([input->textView isFirstResponder]) {
Expand Down