-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathInputHtmlParser.mm
More file actions
228 lines (202 loc) · 8.66 KB
/
Copy pathInputHtmlParser.mm
File metadata and controls
228 lines (202 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#import "InputHtmlParser.h"
#import "AlignmentEntry.h"
#import "EnrichedTextInputView.h"
#import "HtmlParser.h"
#import "StringExtension.h"
#import "StyleHeaders.h"
#import "StyleUtils.h"
#import "TextInsertionUtils.h"
#import <React/RCTLog.h>
@implementation InputHtmlParser {
EnrichedTextInputView __weak *_input;
}
- (instancetype)initWithInput:(id)input {
self = [super init];
_input = (EnrichedTextInputView *)input;
return self;
}
- (void)replaceWholeFromHtml:(NSString *_Nonnull)html {
// reset the text first and reset typing attributes
_input->textView.text = @"";
_input->textView.typingAttributes = _input->defaultTypingAttributes;
@try {
NSArray *processingResult =
[HtmlParser getTextAndStylesFromHtml:html config:_input.config];
NSString *plainText = (NSString *)processingResult[0];
NSArray *stylesInfo = (NSArray *)processingResult[1];
NSArray *alignments = (NSArray *)processingResult[2];
// set new text
_input->textView.text = plainText;
// re-apply the styles
[self applyProcessedStyles:stylesInfo
offsetFromBeginning:0
plainTextLength:plainText.length];
[self applyProcessedAlignments:alignments offset:0];
[_input anyTextMayHaveBeenModified];
} @catch (NSException *exception) {
RCTLogWarn(@"[EnrichedTextInput]: Failed to parse HTML: (%@), falling back "
@"to raw input.",
exception.reason);
// set new text
_input->textView.text = html;
}
}
- (void)replaceFromHtml:(NSString *_Nonnull)html range:(NSRange)range {
@try {
NSArray *processingResult =
[HtmlParser getTextAndStylesFromHtml:html config:_input.config];
NSString *plainText = (NSString *)processingResult[0];
NSArray *stylesInfo = (NSArray *)processingResult[1];
NSArray *alignments = (NSArray *)processingResult[2];
// we can use ready replace util
[TextInsertionUtils replaceText:plainText
at:range
additionalAttributes:nil
host:_input
withSelection:YES];
[self applyProcessedStyles:stylesInfo
offsetFromBeginning:range.location
plainTextLength:plainText.length];
[self applyProcessedAlignments:alignments offset:range.location];
[_input anyTextMayHaveBeenModified];
} @catch (NSException *exception) {
RCTLogWarn(@"[EnrichedTextInput]: Failed to parse HTML: (%@), falling back "
@"to raw input.",
exception.reason);
[TextInsertionUtils replaceText:html
at:range
additionalAttributes:nil
host:_input
withSelection:YES];
}
}
- (void)insertFromHtml:(NSString *_Nonnull)html location:(NSInteger)location {
@try {
NSArray *processingResult =
[HtmlParser getTextAndStylesFromHtml:html config:_input.config];
NSString *plainText = (NSString *)processingResult[0];
NSArray *stylesInfo = (NSArray *)processingResult[1];
NSArray *alignments = (NSArray *)processingResult[2];
// same here, insertion utils got our back
[TextInsertionUtils insertText:plainText
at:location
additionalAttributes:nil
host:_input
withSelection:YES];
[self applyProcessedStyles:stylesInfo
offsetFromBeginning:location
plainTextLength:plainText.length];
[self applyProcessedAlignments:alignments offset:location];
[_input anyTextMayHaveBeenModified];
} @catch (NSException *exception) {
RCTLogWarn(@"[EnrichedTextInput]: Failed to parse HTML: (%@), falling back "
@"to raw input.",
exception.reason);
[TextInsertionUtils insertText:html
at:location
additionalAttributes:nil
host:_input
withSelection:YES];
}
}
- (void)applyProcessedStyles:(NSArray *)processedStyles
offsetFromBeginning:(NSInteger)offset
plainTextLength:(NSUInteger)plainTextLength {
// Some paragraph styles (codeblock, blockquote, etc.) insert \u200B
// into empty lines, mutating NSTextStorage length. We need to
// shift subsequent ranges by this offset.
NSInteger zeroWidthSpaceOffset = 0;
for (NSArray *arr in processedStyles) {
// unwrap all info from processed style
NSNumber *styleType = (NSNumber *)arr[0];
StylePair *stylePair = (StylePair *)arr[1];
StyleBase *baseStyle = _input->stylesDict[styleType];
NSRange parsedRange = [stylePair.rangeValue rangeValue];
NSUInteger textLengthBeforeStyleApplied =
_input->textView.textStorage.string.length;
// range must be taking zeroWidthSpaceOffset and offest into consideration
// because processed styles ranges are relative to only the new text while
// we need absolute ranges relative to the whole existing text
NSRange styleRange =
NSMakeRange(offset + zeroWidthSpaceOffset + parsedRange.location,
parsedRange.length);
// of course any changes here need to take blocks and conflicts into
// consideration
if ([StyleUtils handleStyleBlocksAndConflicts:[[baseStyle class] getType]
range:styleRange
forHost:_input]) {
BOOL shouldAddTypingAttr =
styleRange.location + styleRange.length ==
plainTextLength + offset + zeroWidthSpaceOffset;
if ([styleType isEqualToNumber:@([LinkStyle getType])]) {
LinkData *linkData = (LinkData *)stylePair.styleValue;
[((LinkStyle *)baseStyle) addLink:linkData
range:styleRange
withSelection:NO];
} else if ([styleType isEqualToNumber:@([MentionStyle getType])]) {
MentionParams *params = (MentionParams *)stylePair.styleValue;
[((MentionStyle *)baseStyle) addMentionAtRange:styleRange
params:params];
} else if ([styleType isEqualToNumber:@([ImageStyle getType])]) {
ImageData *imgData = (ImageData *)stylePair.styleValue;
[((ImageStyle *)baseStyle) addImageAtRange:styleRange
imageData:imgData
withSelection:NO
withDirtyRange:YES];
} else if ([styleType isEqualToNumber:@([CheckboxListStyle getType])]) {
NSDictionary *checkboxStates = (NSDictionary *)stylePair.styleValue;
CheckboxListStyle *cbLStyle = (CheckboxListStyle *)baseStyle;
// First apply the checkbox list style to the entire range with
// unchecked value
[cbLStyle addWithChecked:NO
range:styleRange
withTyping:shouldAddTypingAttr
withDirtyRange:YES];
if (checkboxStates && checkboxStates.count > 0) {
// Then toggle checked checkboxes
for (NSNumber *key in checkboxStates) {
NSUInteger checkboxPosition =
offset + zeroWidthSpaceOffset + [key unsignedIntegerValue];
BOOL isChecked = [checkboxStates[key] boolValue];
if (isChecked) {
[cbLStyle toggleCheckedAt:checkboxPosition withDirtyRange:YES];
}
}
}
} else {
[baseStyle add:styleRange
withTyping:shouldAddTypingAttr
withDirtyRange:YES];
}
}
NSInteger delta = (NSInteger)_input->textView.textStorage.string.length -
(NSInteger)textLengthBeforeStyleApplied;
// Image shifts are already handled by _precedingImageCount during tag
// finalization.
if (delta != 0 && ![styleType isEqualToNumber:@([ImageStyle getType])]) {
zeroWidthSpaceOffset += delta;
}
}
}
- (void)applyProcessedAlignments:(NSArray<AlignmentEntry *> *)alignments
offset:(NSInteger)offset {
AlignmentStyle *alignmentStyle =
_input.stylesDict[@([AlignmentStyle getType])];
if (alignmentStyle == nil) {
return;
}
for (AlignmentEntry *entry in alignments) {
// Offset the range (e.g. if inserting into the middle of text)
NSRange finalRange =
NSMakeRange(offset + entry.range.location, entry.range.length);
[alignmentStyle addAlignment:entry.alignment
range:finalRange
withTyping:NO
withDirtyRange:NO];
}
}
- (NSString *_Nullable)initiallyProcessHtml:(NSString *_Nonnull)html {
return [HtmlParser initiallyProcessHtml:html
useHtmlNormalizer:_input->useHtmlNormalizer];
}
@end