GTMNSString+HTML.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. //
  2. // GTMNSString+HTML.m
  3. // Dealing with NSStrings that contain HTML
  4. //
  5. // Copyright 2006-2008 Google Inc.
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. // use this file except in compliance with the License. You may obtain a copy
  9. // of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. // License for the specific language governing permissions and limitations under
  17. // the License.
  18. //
  19. #import "GTMNSString+HTML.h"
  20. typedef struct {
  21. __unsafe_unretained NSString *escapeSequence;
  22. unichar uchar;
  23. } HTMLEscapeMap;
  24. // Taken from http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_Special_characters
  25. // Ordered by uchar lowest to highest for bsearching
  26. static HTMLEscapeMap gAsciiHTMLEscapeMap[] = {
  27. // A.2.2. Special characters
  28. { @""", 34 },
  29. { @"&", 38 },
  30. { @"'", 39 },
  31. { @"<", 60 },
  32. { @">", 62 },
  33. // A.2.1. Latin-1 characters
  34. { @" ", 160 },
  35. { @"¡", 161 },
  36. { @"¢", 162 },
  37. { @"£", 163 },
  38. { @"¤", 164 },
  39. { @"¥", 165 },
  40. { @"¦", 166 },
  41. { @"§", 167 },
  42. { @"¨", 168 },
  43. { @"©", 169 },
  44. { @"ª", 170 },
  45. { @"«", 171 },
  46. { @"¬", 172 },
  47. { @"­", 173 },
  48. { @"®", 174 },
  49. { @"¯", 175 },
  50. { @"°", 176 },
  51. { @"±", 177 },
  52. { @"²", 178 },
  53. { @"³", 179 },
  54. { @"´", 180 },
  55. { @"µ", 181 },
  56. { @"¶", 182 },
  57. { @"·", 183 },
  58. { @"¸", 184 },
  59. { @"¹", 185 },
  60. { @"º", 186 },
  61. { @"»", 187 },
  62. { @"¼", 188 },
  63. { @"½", 189 },
  64. { @"¾", 190 },
  65. { @"¿", 191 },
  66. { @"À", 192 },
  67. { @"Á", 193 },
  68. { @"Â", 194 },
  69. { @"Ã", 195 },
  70. { @"Ä", 196 },
  71. { @"Å", 197 },
  72. { @"Æ", 198 },
  73. { @"Ç", 199 },
  74. { @"È", 200 },
  75. { @"É", 201 },
  76. { @"Ê", 202 },
  77. { @"Ë", 203 },
  78. { @"Ì", 204 },
  79. { @"Í", 205 },
  80. { @"Î", 206 },
  81. { @"Ï", 207 },
  82. { @"Ð", 208 },
  83. { @"Ñ", 209 },
  84. { @"Ò", 210 },
  85. { @"Ó", 211 },
  86. { @"Ô", 212 },
  87. { @"Õ", 213 },
  88. { @"Ö", 214 },
  89. { @"×", 215 },
  90. { @"Ø", 216 },
  91. { @"Ù", 217 },
  92. { @"Ú", 218 },
  93. { @"Û", 219 },
  94. { @"Ü", 220 },
  95. { @"Ý", 221 },
  96. { @"Þ", 222 },
  97. { @"ß", 223 },
  98. { @"à", 224 },
  99. { @"á", 225 },
  100. { @"â", 226 },
  101. { @"ã", 227 },
  102. { @"ä", 228 },
  103. { @"å", 229 },
  104. { @"æ", 230 },
  105. { @"ç", 231 },
  106. { @"è", 232 },
  107. { @"é", 233 },
  108. { @"ê", 234 },
  109. { @"ë", 235 },
  110. { @"ì", 236 },
  111. { @"í", 237 },
  112. { @"î", 238 },
  113. { @"ï", 239 },
  114. { @"ð", 240 },
  115. { @"ñ", 241 },
  116. { @"ò", 242 },
  117. { @"ó", 243 },
  118. { @"ô", 244 },
  119. { @"õ", 245 },
  120. { @"ö", 246 },
  121. { @"÷", 247 },
  122. { @"ø", 248 },
  123. { @"ù", 249 },
  124. { @"ú", 250 },
  125. { @"û", 251 },
  126. { @"ü", 252 },
  127. { @"ý", 253 },
  128. { @"þ", 254 },
  129. { @"ÿ", 255 },
  130. // A.2.2. Special characters cont'd
  131. { @"Œ", 338 },
  132. { @"œ", 339 },
  133. { @"Š", 352 },
  134. { @"š", 353 },
  135. { @"Ÿ", 376 },
  136. // A.2.3. Symbols
  137. { @"ƒ", 402 },
  138. // A.2.2. Special characters cont'd
  139. { @"ˆ", 710 },
  140. { @"˜", 732 },
  141. // A.2.3. Symbols cont'd
  142. { @"Α", 913 },
  143. { @"Β", 914 },
  144. { @"Γ", 915 },
  145. { @"Δ", 916 },
  146. { @"Ε", 917 },
  147. { @"Ζ", 918 },
  148. { @"Η", 919 },
  149. { @"Θ", 920 },
  150. { @"Ι", 921 },
  151. { @"Κ", 922 },
  152. { @"Λ", 923 },
  153. { @"Μ", 924 },
  154. { @"Ν", 925 },
  155. { @"Ξ", 926 },
  156. { @"Ο", 927 },
  157. { @"Π", 928 },
  158. { @"Ρ", 929 },
  159. { @"Σ", 931 },
  160. { @"Τ", 932 },
  161. { @"Υ", 933 },
  162. { @"Φ", 934 },
  163. { @"Χ", 935 },
  164. { @"Ψ", 936 },
  165. { @"Ω", 937 },
  166. { @"α", 945 },
  167. { @"β", 946 },
  168. { @"γ", 947 },
  169. { @"δ", 948 },
  170. { @"ε", 949 },
  171. { @"ζ", 950 },
  172. { @"η", 951 },
  173. { @"θ", 952 },
  174. { @"ι", 953 },
  175. { @"κ", 954 },
  176. { @"λ", 955 },
  177. { @"μ", 956 },
  178. { @"ν", 957 },
  179. { @"ξ", 958 },
  180. { @"ο", 959 },
  181. { @"π", 960 },
  182. { @"ρ", 961 },
  183. { @"ς", 962 },
  184. { @"σ", 963 },
  185. { @"τ", 964 },
  186. { @"υ", 965 },
  187. { @"φ", 966 },
  188. { @"χ", 967 },
  189. { @"ψ", 968 },
  190. { @"ω", 969 },
  191. { @"ϑ", 977 },
  192. { @"ϒ", 978 },
  193. { @"ϖ", 982 },
  194. // A.2.2. Special characters cont'd
  195. { @" ", 8194 },
  196. { @" ", 8195 },
  197. { @" ", 8201 },
  198. { @"‌", 8204 },
  199. { @"‍", 8205 },
  200. { @"‎", 8206 },
  201. { @"‏", 8207 },
  202. { @"–", 8211 },
  203. { @"—", 8212 },
  204. { @"‘", 8216 },
  205. { @"’", 8217 },
  206. { @"‚", 8218 },
  207. { @"“", 8220 },
  208. { @"”", 8221 },
  209. { @"„", 8222 },
  210. { @"†", 8224 },
  211. { @"‡", 8225 },
  212. // A.2.3. Symbols cont'd
  213. { @"•", 8226 },
  214. { @"…", 8230 },
  215. // A.2.2. Special characters cont'd
  216. { @"‰", 8240 },
  217. // A.2.3. Symbols cont'd
  218. { @"′", 8242 },
  219. { @"″", 8243 },
  220. // A.2.2. Special characters cont'd
  221. { @"‹", 8249 },
  222. { @"›", 8250 },
  223. // A.2.3. Symbols cont'd
  224. { @"‾", 8254 },
  225. { @"⁄", 8260 },
  226. // A.2.2. Special characters cont'd
  227. { @"€", 8364 },
  228. // A.2.3. Symbols cont'd
  229. { @"ℑ", 8465 },
  230. { @"℘", 8472 },
  231. { @"ℜ", 8476 },
  232. { @"™", 8482 },
  233. { @"ℵ", 8501 },
  234. { @"←", 8592 },
  235. { @"↑", 8593 },
  236. { @"→", 8594 },
  237. { @"↓", 8595 },
  238. { @"↔", 8596 },
  239. { @"↵", 8629 },
  240. { @"⇐", 8656 },
  241. { @"⇑", 8657 },
  242. { @"⇒", 8658 },
  243. { @"⇓", 8659 },
  244. { @"⇔", 8660 },
  245. { @"∀", 8704 },
  246. { @"∂", 8706 },
  247. { @"∃", 8707 },
  248. { @"∅", 8709 },
  249. { @"∇", 8711 },
  250. { @"∈", 8712 },
  251. { @"∉", 8713 },
  252. { @"∋", 8715 },
  253. { @"∏", 8719 },
  254. { @"∑", 8721 },
  255. { @"−", 8722 },
  256. { @"∗", 8727 },
  257. { @"√", 8730 },
  258. { @"∝", 8733 },
  259. { @"∞", 8734 },
  260. { @"∠", 8736 },
  261. { @"∧", 8743 },
  262. { @"∨", 8744 },
  263. { @"∩", 8745 },
  264. { @"∪", 8746 },
  265. { @"∫", 8747 },
  266. { @"∴", 8756 },
  267. { @"∼", 8764 },
  268. { @"≅", 8773 },
  269. { @"≈", 8776 },
  270. { @"≠", 8800 },
  271. { @"≡", 8801 },
  272. { @"≤", 8804 },
  273. { @"≥", 8805 },
  274. { @"⊂", 8834 },
  275. { @"⊃", 8835 },
  276. { @"⊄", 8836 },
  277. { @"⊆", 8838 },
  278. { @"⊇", 8839 },
  279. { @"⊕", 8853 },
  280. { @"⊗", 8855 },
  281. { @"⊥", 8869 },
  282. { @"⋅", 8901 },
  283. { @"⌈", 8968 },
  284. { @"⌉", 8969 },
  285. { @"⌊", 8970 },
  286. { @"⌋", 8971 },
  287. { @"⟨", 9001 },
  288. { @"⟩", 9002 },
  289. { @"◊", 9674 },
  290. { @"♠", 9824 },
  291. { @"♣", 9827 },
  292. { @"♥", 9829 },
  293. { @"♦", 9830 }
  294. };
  295. // Taken from http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_Special_characters
  296. // This is table A.2.2 Special Characters
  297. static HTMLEscapeMap gUnicodeHTMLEscapeMap[] = {
  298. // C0 Controls and Basic Latin
  299. { @""", 34 },
  300. { @"&", 38 },
  301. { @"'", 39 },
  302. { @"<", 60 },
  303. { @">", 62 },
  304. // Latin Extended-A
  305. { @"Œ", 338 },
  306. { @"œ", 339 },
  307. { @"Š", 352 },
  308. { @"š", 353 },
  309. { @"Ÿ", 376 },
  310. // Spacing Modifier Letters
  311. { @"ˆ", 710 },
  312. { @"˜", 732 },
  313. // General Punctuation
  314. { @" ", 8194 },
  315. { @" ", 8195 },
  316. { @" ", 8201 },
  317. { @"‌", 8204 },
  318. { @"‍", 8205 },
  319. { @"‎", 8206 },
  320. { @"‏", 8207 },
  321. { @"–", 8211 },
  322. { @"—", 8212 },
  323. { @"‘", 8216 },
  324. { @"’", 8217 },
  325. { @"‚", 8218 },
  326. { @"“", 8220 },
  327. { @"”", 8221 },
  328. { @"„", 8222 },
  329. { @"†", 8224 },
  330. { @"‡", 8225 },
  331. { @"‰", 8240 },
  332. { @"‹", 8249 },
  333. { @"›", 8250 },
  334. { @"€", 8364 },
  335. };
  336. // Utility function for Bsearching table above
  337. static int EscapeMapCompare(const void *ucharVoid, const void *mapVoid) {
  338. const unichar *uchar = (const unichar*)ucharVoid;
  339. const HTMLEscapeMap *map = (const HTMLEscapeMap*)mapVoid;
  340. int val;
  341. if (*uchar > map->uchar) {
  342. val = 1;
  343. } else if (*uchar < map->uchar) {
  344. val = -1;
  345. } else {
  346. val = 0;
  347. }
  348. return val;
  349. }
  350. @implementation NSString (GTMNSStringHTMLAdditions)
  351. - (NSString *)gtm_stringByEscapingHTMLUsingTable:(HTMLEscapeMap*)table
  352. ofSize:(NSUInteger)size
  353. escapingUnicode:(BOOL)escapeUnicode {
  354. NSUInteger length = [self length];
  355. if (!length) {
  356. return self;
  357. }
  358. NSMutableString *finalString = [NSMutableString string];
  359. NSMutableData *data2 = [NSMutableData dataWithCapacity:sizeof(unichar) * length];
  360. // this block is common between GTMNSString+HTML and GTMNSString+XML but
  361. // it's so short that it isn't really worth trying to share.
  362. const unichar *buffer = CFStringGetCharactersPtr((CFStringRef)self);
  363. if (!buffer) {
  364. // We want this buffer to be autoreleased.
  365. NSMutableData *data = [NSMutableData dataWithLength:length * sizeof(UniChar)];
  366. if (!data) {
  367. // COV_NF_START - Memory fail case
  368. NSLog(@"couldn't alloc buffer");
  369. return nil;
  370. // COV_NF_END
  371. }
  372. [self getCharacters:[data mutableBytes]];
  373. buffer = [data bytes];
  374. }
  375. if (!buffer || !data2) {
  376. // COV_NF_START
  377. NSLog(@"Unable to allocate buffer or data2");
  378. return nil;
  379. // COV_NF_END
  380. }
  381. unichar *buffer2 = (unichar *)[data2 mutableBytes];
  382. NSUInteger buffer2Length = 0;
  383. for (NSUInteger i = 0; i < length; ++i) {
  384. HTMLEscapeMap *val = bsearch(&buffer[i], table,
  385. size / sizeof(HTMLEscapeMap),
  386. sizeof(HTMLEscapeMap), EscapeMapCompare);
  387. if (val || (escapeUnicode && buffer[i] > 127)) {
  388. if (buffer2Length) {
  389. CFStringAppendCharacters((CFMutableStringRef)finalString,
  390. buffer2,
  391. buffer2Length);
  392. buffer2Length = 0;
  393. }
  394. if (val) {
  395. [finalString appendString:val->escapeSequence];
  396. }
  397. else {
  398. // _GTMDevAssert(escapeUnicode && buffer[i] > 127, @"Illegal Character");
  399. [finalString appendFormat:@"&#%d;", buffer[i]];
  400. }
  401. } else {
  402. buffer2[buffer2Length] = buffer[i];
  403. buffer2Length += 1;
  404. }
  405. }
  406. if (buffer2Length) {
  407. CFStringAppendCharacters((CFMutableStringRef)finalString,
  408. buffer2,
  409. buffer2Length);
  410. }
  411. return finalString;
  412. }
  413. - (NSString *)gtm_stringByEscapingForHTML {
  414. return [self gtm_stringByEscapingHTMLUsingTable:gUnicodeHTMLEscapeMap
  415. ofSize:sizeof(gUnicodeHTMLEscapeMap)
  416. escapingUnicode:NO];
  417. } // gtm_stringByEscapingHTML
  418. - (NSString *)gtm_stringByEscapingForAsciiHTML {
  419. return [self gtm_stringByEscapingHTMLUsingTable:gAsciiHTMLEscapeMap
  420. ofSize:sizeof(gAsciiHTMLEscapeMap)
  421. escapingUnicode:YES];
  422. } // gtm_stringByEscapingAsciiHTML
  423. - (NSString *)gtm_stringByUnescapingFromHTML {
  424. NSRange range = NSMakeRange(0, [self length]);
  425. NSRange subrange = [self rangeOfString:@"&" options:NSBackwardsSearch range:range];
  426. // if no ampersands, we've got a quick way out
  427. if (subrange.length == 0) return self;
  428. NSMutableString *finalString = [NSMutableString stringWithString:self];
  429. do {
  430. NSRange semiColonRange = NSMakeRange(subrange.location, NSMaxRange(range) - subrange.location);
  431. semiColonRange = [self rangeOfString:@";" options:0 range:semiColonRange];
  432. range = NSMakeRange(0, subrange.location);
  433. // if we don't find a semicolon in the range, we don't have a sequence
  434. if (semiColonRange.location == NSNotFound) {
  435. continue;
  436. }
  437. NSRange escapeRange = NSMakeRange(subrange.location, semiColonRange.location - subrange.location + 1);
  438. NSString *escapeString = [self substringWithRange:escapeRange];
  439. NSUInteger length = [escapeString length];
  440. // a squence must be longer than 3 (&lt;) and less than 11 (&thetasym;)
  441. if (length > 3 && length < 11) {
  442. if ([escapeString characterAtIndex:1] == '#') {
  443. unichar char2 = [escapeString characterAtIndex:2];
  444. if (char2 == 'x' || char2 == 'X') {
  445. // Hex escape squences &#xa3;
  446. NSString *hexSequence = [escapeString substringWithRange:NSMakeRange(3, length - 4)];
  447. NSScanner *scanner = [NSScanner scannerWithString:hexSequence];
  448. unsigned value;
  449. if ([scanner scanHexInt:&value] &&
  450. value < USHRT_MAX &&
  451. value > 0
  452. && [scanner scanLocation] == length - 4) {
  453. unichar uchar = value;
  454. NSString *charString = [NSString stringWithCharacters:&uchar length:1];
  455. [finalString replaceCharactersInRange:escapeRange withString:charString];
  456. }
  457. } else {
  458. // Decimal Sequences &#123;
  459. NSString *numberSequence = [escapeString substringWithRange:NSMakeRange(2, length - 3)];
  460. NSScanner *scanner = [NSScanner scannerWithString:numberSequence];
  461. int value;
  462. if ([scanner scanInt:&value] &&
  463. value < USHRT_MAX &&
  464. value > 0
  465. && [scanner scanLocation] == length - 3) {
  466. unichar uchar = value;
  467. NSString *charString = [NSString stringWithCharacters:&uchar length:1];
  468. [finalString replaceCharactersInRange:escapeRange withString:charString];
  469. }
  470. }
  471. } else {
  472. // "standard" sequences
  473. for (unsigned i = 0; i < sizeof(gAsciiHTMLEscapeMap) / sizeof(HTMLEscapeMap); ++i) {
  474. if ([escapeString isEqualToString:gAsciiHTMLEscapeMap[i].escapeSequence]) {
  475. [finalString replaceCharactersInRange:escapeRange withString:[NSString stringWithCharacters:&gAsciiHTMLEscapeMap[i].uchar length:1]];
  476. break;
  477. }
  478. }
  479. }
  480. }
  481. } while ((subrange = [self rangeOfString:@"&" options:NSBackwardsSearch range:range]).length != 0);
  482. return finalString;
  483. } // gtm_stringByUnescapingHTML
  484. @end