FBW3CActionsHelpers.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import "FBW3CActionsHelpers.h"
  10. #import "FBErrorBuilder.h"
  11. #import "XCUIElement.h"
  12. #import "FBLogger.h"
  13. static NSString *const FB_ACTION_ITEM_KEY_VALUE = @"value";
  14. static NSString *const FB_ACTION_ITEM_KEY_DURATION = @"duration";
  15. NSString *FBRequireValue(NSDictionary<NSString *, id> *actionItem, NSError **error)
  16. {
  17. id value = [actionItem objectForKey:FB_ACTION_ITEM_KEY_VALUE];
  18. if (![value isKindOfClass:NSString.class] || [value length] == 0) {
  19. NSString *description = [NSString stringWithFormat:@"Key value must be present and should be a valid non-empty string for '%@'", actionItem];
  20. if (error) {
  21. *error = [[FBErrorBuilder.builder withDescription:description] build];
  22. }
  23. return nil;
  24. }
  25. NSRange r = [(NSString *)value rangeOfComposedCharacterSequenceAtIndex:0];
  26. return [(NSString *)value substringWithRange:r];
  27. }
  28. NSNumber *_Nullable FBOptDuration(NSDictionary<NSString *, id> *actionItem, NSNumber *defaultValue, NSError **error)
  29. {
  30. NSNumber *durationObj = [actionItem objectForKey:FB_ACTION_ITEM_KEY_DURATION];
  31. if (nil == durationObj) {
  32. if (nil == defaultValue) {
  33. NSString *description = [NSString stringWithFormat:@"Duration must be present for '%@' action item", actionItem];
  34. if (error) {
  35. *error = [[FBErrorBuilder.builder withDescription:description] build];
  36. }
  37. return nil;
  38. }
  39. return defaultValue;
  40. }
  41. if ([durationObj doubleValue] < 0.0) {
  42. NSString *description = [NSString stringWithFormat:@"Duration must be a valid positive number for '%@' action item", actionItem];
  43. if (error) {
  44. *error = [[FBErrorBuilder.builder withDescription:description] build];
  45. }
  46. return nil;
  47. }
  48. return durationObj;
  49. }
  50. NSString *FBMapIfSpecialCharacter(NSString *value)
  51. {
  52. if (0 == [value length]) {
  53. return value;
  54. }
  55. unichar charCode = [value characterAtIndex:0];
  56. switch (charCode) {
  57. case 0xE000:
  58. return @"";
  59. case 0xE003:
  60. return [NSString stringWithFormat:@"%C", 0x0008];
  61. case 0xE004:
  62. return [NSString stringWithFormat:@"%C", 0x0009];
  63. case 0xE006:
  64. return [NSString stringWithFormat:@"%C", 0x000D];
  65. case 0xE007:
  66. return [NSString stringWithFormat:@"%C", 0x000A];
  67. case 0xE00C:
  68. return [NSString stringWithFormat:@"%C", 0x001B];
  69. case 0xE00D:
  70. case 0xE05D:
  71. return @" ";
  72. case 0xE017:
  73. return [NSString stringWithFormat:@"%C", 0x007F];
  74. case 0xE018:
  75. return @";";
  76. case 0xE019:
  77. return @"=";
  78. case 0xE01A:
  79. return @"0";
  80. case 0xE01B:
  81. return @"1";
  82. case 0xE01C:
  83. return @"2";
  84. case 0xE01D:
  85. return @"3";
  86. case 0xE01E:
  87. return @"4";
  88. case 0xE01F:
  89. return @"5";
  90. case 0xE020:
  91. return @"6";
  92. case 0xE021:
  93. return @"7";
  94. case 0xE022:
  95. return @"8";
  96. case 0xE023:
  97. return @"9";
  98. case 0xE024:
  99. return @"*";
  100. case 0xE025:
  101. return @"+";
  102. case 0xE026:
  103. return @",";
  104. case 0xE027:
  105. return @"-";
  106. case 0xE028:
  107. return @".";
  108. case 0xE029:
  109. return @"/";
  110. default:
  111. return charCode >= 0xE000 && charCode <= 0xE05D ? @"" : value;
  112. }
  113. }