Skip to content

Commit

Permalink
fix(ios): temporary remove text fix for predicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
asafkorem committed Dec 17, 2024
1 parent 6bb0fba commit eec9b5f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 77 deletions.
8 changes: 2 additions & 6 deletions detox/ios/Detox/Invocation/Predicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ class Predicate : CustomStringConvertible, CustomDebugStringConvertible {
return ValuePredicate(kind: kind, modifiers: modifiers, value: label, requiresAccessibilityElement: true, isRegex: isRegex)
} else {
//Will crash if RN app and neither class exists
let RCTTextViewClass : AnyClass =
NSClassFromString("RCTParagraphComponentView") ??
NSClassFromString("RCTText") ?? NSClassFromString("RCTTextView")!
let RCTTextViewClass : AnyClass = NSClassFromString("RCTText") ?? NSClassFromString("RCTTextView")!

let descendantPredicate = DescendantPredicate(predicate: AndCompoundPredicate(predicates: [
try KindOfPredicate(kind: Kind.type, modifiers: [], className: NSStringFromClass(RCTTextViewClass)),
Expand All @@ -91,9 +89,7 @@ class Predicate : CustomStringConvertible, CustomDebugStringConvertible {

if ReactNativeSupport.isReactNativeApp == true {
//Will crash if RN app and neither class exists
let RCTTextViewClass : AnyClass =
NSClassFromString("RCTParagraphComponentView") ??
NSClassFromString("RCTText") ?? NSClassFromString("RCTTextView")!
let RCTTextViewClass : AnyClass = NSClassFromString("RCTText") ?? NSClassFromString("RCTTextView")!
orPredicates.append(try KindOfPredicate(kind: Kind.type, modifiers: [], className: NSStringFromClass(RCTTextViewClass)))
}

Expand Down
97 changes: 26 additions & 71 deletions detox/ios/Detox/Utilities/NSObject+DontCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,36 @@

#import "NSObject+DontCrash.h"

@interface NSObject (DontCrashPrivate)
+ (Class)dtx_classForName:(NSString *)className;
- (nullable NSString *)dtx_extractTextFromRCTComponent;
@end

@implementation NSObject (DontCrash)

#pragma mark - Public Methods

- (id)_dtx_text {
if ([self respondsToSelector:@selector(text)]) {
return [(UITextView *)self text];
}

NSString *reactText = [self dtx_extractTextFromRCTComponent];
if (reactText) {
return reactText;
}

return nil;
}

- (id)_dtx_placeholder {
if ([self respondsToSelector:@selector(placeholder)]) {
return [(UITextField *)self placeholder];
}
return nil;
}

@end

@implementation NSObject (DontCrashPrivate)

#pragma mark - Private Methods

+ (Class)dtx_classForName:(NSString *)className {
static NSMutableDictionary<NSString *, Class> *classCache;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
classCache = [NSMutableDictionary dictionary];
});

Class cachedClass = classCache[className];
if (!cachedClass) {
cachedClass = NSClassFromString(className);
if (cachedClass) {
classCache[className] = cachedClass;
}
}

return cachedClass;
- (id)_dtx_text
{
if([self respondsToSelector:@selector(text)])
{
return [(UITextView*)self text];
}

static Class RCTTextView;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
RCTTextView = NSClassFromString(@"RCTTextView");
});
if(RCTTextView != nil && [self isKindOfClass:RCTTextView])
{
return [(NSTextStorage*)[self valueForKey:@"textStorage"] string];
}

return nil;
}

- (nullable NSString *)dtx_extractTextFromRCTComponent {
static Class RCTTextViewClass;
static Class RCTParagraphComponentViewClass;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
RCTTextViewClass = [self.class dtx_classForName:@"RCTTextView"];
RCTParagraphComponentViewClass = [self.class dtx_classForName:@"RCTParagraphComponentView"];
});

if (RCTTextViewClass && [self isKindOfClass:RCTTextViewClass]) {
NSTextStorage *textStorage = [self valueForKey:@"textStorage"];
return [textStorage string];
}

if (RCTParagraphComponentViewClass && [self isKindOfClass:RCTParagraphComponentViewClass]) {
NSAttributedString *attributedText = [self valueForKey:@"attributedText"];
return [attributedText string];
}

return nil;
- (id)_dtx_placeholder
{
if([self respondsToSelector:@selector(placeholder)])
{
return [(UITextField*)self placeholder];
}

return nil;
}

@end

0 comments on commit eec9b5f

Please sign in to comment.