Adium

Changeset 13524

Show
Ignore:
Timestamp:
09/20/2005 04:58:32 PM (3 years ago)
Author:
evands
Message:
  • Right-to-left text is now surrounded by a dir="rtl" div tag; dir="rtl" div tags are decoded back to right-to-left attributed strings. This fixes display of right-to-left text in the message view. Refs #651
  • A little cleanup of code elsewhere in the decoder
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Frameworks/Adium Framework/AIHTMLDecoder.h

    r11197 r13524  
    3636                unsigned allowAIMsubprofileLinks: 1; 
    3737        } thingsToInclude; 
     38         
     39        //For the horribly ghetto span and div tags used by the HTML logs 
     40        BOOL send; 
     41        BOOL receive; 
     42        BOOL inDiv; 
     43        BOOL inLogSpan;  
    3844} 
    3945 
  • trunk/Frameworks/Adium Framework/AIHTMLDecoder.m

    r13123 r13524  
    9696- (void)processBodyTagArgs:(NSDictionary *)inArgs attributes:(AITextAttributes *)textAttributes; 
    9797- (void)processLinkTagArgs:(NSDictionary *)inArgs attributes:(AITextAttributes *)textAttributes; 
     98- (void)processSpanTagArgs:(NSDictionary *)inArgs attributes:(AITextAttributes *)textAttributes; 
     99- (void)processDivTagArgs:(NSDictionary *)inArgs attributes:(AITextAttributes *)textAttributes; 
    98100- (NSAttributedString *)processImgTagArgs:(NSDictionary *)inArgs attributes:(AITextAttributes *)textAttributes; 
    99101- (BOOL)appendImage:(NSImage *)attachmentImage toString:(NSMutableString *)string withName:(NSString *)fileSafeChunk  altString:(NSString *)attachmentString imagesPath:(NSString *)imagesPath; 
     
    225227        BOOL                     openFontTag = NO; 
    226228 
    227         //Setup the destination HTML string 
    228         NSMutableString *string = [NSMutableString string]; 
    229         if (thingsToInclude.headers) [string appendString:@"<HTML>"]; 
    230  
    231229        //Setup the incoming message as a regular string, and get its length 
    232230        NSString                *inMessageString = [inMessage string]; 
    233231        unsigned                 messageLength = [inMessageString length]; 
    234                  
     232         
     233        //Setup the destination HTML string 
     234        NSMutableString *string = [NSMutableString string]; 
     235        if (thingsToInclude.headers) { 
     236                        [string appendString:@"<HTML>"]; 
     237        } 
     238 
     239        //If the text is right-to-left, enclose all our HTML in an rtl DIV tag 
     240        BOOL    rightToLeft = NO; 
     241        if (!thingsToInclude.simpleTagsOnly) { 
     242                if ((messageLength > 0) && 
     243                        ([[inMessage attribute:NSParagraphStyleAttributeName 
     244                                                   atIndex:0 
     245                                        effectiveRange:nil] baseWritingDirection] == NSWritingDirectionRightToLeft)) { 
     246                        [string appendString:@"<DIV dir=\"rtl\">"]; 
     247                        rightToLeft = YES; 
     248                } 
     249        }        
     250         
    235251        //Setup the default attributes 
    236252        NSString                *currentFamily = [@"Helvetica" retain]; 
     
    619635         
    620636        if (thingsToInclude.fontTags && thingsToInclude.closingFontTags && openFontTag) [string appendString:CloseFontTag]; //Close any open font tag 
     637        if (rightToLeft) { 
     638                [string appendString:@"</DIV>"]; 
     639        } 
    621640        if (thingsToInclude.headers && pageColor) [string appendString:@"</BODY>"]; //Close the body tag 
    622641        if (thingsToInclude.headers) [string appendString:@"</HTML>"]; //Close the HTML 
     
    650669        NSMutableAttributedString       *attrString; 
    651670        AITextAttributes                        *textAttributes; 
    652         BOOL                                            send = NO, receive = NO, inDiv = NO, inLogSpan = NO; 
     671         
     672        //Reset the div and span ivars 
     673        send = NO; 
     674        receive = NO; 
     675        inDiv = NO; 
     676        inLogSpan = NO; 
    653677         
    654678    //set up 
     
    719743                                        //HTML 
    720744                                        if ([chunkString caseInsensitiveCompare:HTML] == NSOrderedSame) { 
    721                                                 //We ignore stuff inside the HTML tag, but don't want to see the end of it 
     745                                                //We ignore most stuff inside the HTML tag, but don't want to see the end of it. 
    722746                                                [scanner scanUpToCharactersFromSet:absoluteTagEnd intoString:&chunkString]; 
     747         
    723748                                        } else if ([chunkString caseInsensitiveCompare:CloseHTML] == NSOrderedSame) { 
    724749                                                //We are done 
     
    732757 
    733758                                                [textAttributes setTextColor:[NSColor blackColor]]; 
     759 
    734760                                        //DIV 
    735761                                        } else if ([chunkString caseInsensitiveCompare:@"DIV"] == NSOrderedSame) { 
    736                                                 [scanner scanUpToCharactersFromSet:absoluteTagEnd 
    737                                                                                                 intoString:&chunkString]; 
     762                                                if ([scanner scanUpToCharactersFromSet:absoluteTagEnd 
     763                                                                                                        intoString:&chunkString]) { 
     764                                                        [self processDivTagArgs:[self parseArguments:chunkString] attributes:textAttributes]; 
     765                                                } 
    738766                                                inDiv = YES; 
    739                                                 if ([chunkString caseInsensitiveCompare:@" class=\"send\""] == NSOrderedSame) { 
    740                                                         send = YES; 
    741                                                         receive = NO; 
    742                                                 } else if ([chunkString caseInsensitiveCompare:@" class=\"receive\""] == NSOrderedSame) { 
    743                                                         receive = YES; 
    744                                                         send = NO; 
    745                                                 } else if ([chunkString caseInsensitiveCompare:@" class=\"status\""] == NSOrderedSame) { 
    746                                                         [textAttributes setTextColor:[NSColor grayColor]]; 
    747                                                 } 
     767 
    748768                                        } else if ([chunkString caseInsensitiveCompare:@"/DIV"] == NSOrderedSame) { 
    749769                                                inDiv = NO; 
     770 
    750771                                        //LINK 
    751772                                        } else if ([chunkString caseInsensitiveCompare:@"A"] == NSOrderedSame) { 
     
    771792                                        } else if ([chunkString caseInsensitiveCompare:Font] == NSOrderedSame) { 
    772793                                                if ([scanner scanUpToCharactersFromSet:absoluteTagEnd intoString:&chunkString]) { 
    773  
    774                                                         //Process the font tag if it's in a log 
    775                                                         if ([chunkString caseInsensitiveCompare:@" class=\"sender\""] == NSOrderedSame) { 
    776                                                                 if (inDiv && send) { 
    777                                                                         [textAttributes setTextColor:[NSColor colorWithCalibratedRed:0.0 green:0.5 blue:0.0 alpha:1.0]]; 
    778                                                                 } else if (inDiv && receive) { 
    779                                                                         [textAttributes setTextColor:[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.5 alpha:1.0]]; 
    780                                                                 } 
    781                                                         } 
    782  
    783794                                                        //Process the font tag's contents 
    784795                                                        [self processFontTagArgs:[self parseArguments:chunkString] attributes:textAttributes]; 
     
    791802                                        } else if ([chunkString caseInsensitiveCompare:Span] == NSOrderedSame) { 
    792803                                                if ([scanner scanUpToCharactersFromSet:absoluteTagEnd intoString:&chunkString]) { 
    793  
    794                                                         //Process the span tag if it's in a log 
    795                                                         if ([chunkString caseInsensitiveCompare:@" class=\"sender\""] == NSOrderedSame) { 
    796                                                                 if (inDiv && send) { 
    797                                                                         [textAttributes setTextColor:[NSColor colorWithCalibratedRed:0.0  
    798                                                                                                                                                                                    green:0.5 
    799                                                                                                                                                                                         blue:0.0  
    800                                                                                                                                                                                    alpha:1.0]]; 
    801                                                                         inLogSpan = YES; 
    802                                                                 } else if (inDiv && receive) { 
    803                                                                         [textAttributes setTextColor:[NSColor colorWithCalibratedRed:0.0 
    804                                                                                                                                                                                    green:0.0 
    805                                                                                                                                                                                         blue:0.5  
    806                                                                                                                                                                                    alpha:1.0]]; 
    807                                                                         inLogSpan = YES; 
    808                                                                 } 
    809                                                         } else if ([chunkString caseInsensitiveCompare:@" class=\"timestamp\""] == NSOrderedSame) { 
    810                                                                 [textAttributes setTextColor:[NSColor grayColor]]; 
    811                                                                 inLogSpan = YES; 
    812                                                         } 
    813                                                          
    814                                                         //XXX Jabber can send a tag like so: <span style='font-family: Helvetica; font-size: small; '> 
     804                                                        [self processSpanTagArgs:[self parseArguments:chunkString] attributes:textAttributes]; 
    815805                                                } 
     806 
    816807                                        } else if ([chunkString caseInsensitiveCompare:CloseSpan] == NSOrderedSame) { 
    817808                                                if (inLogSpan) { 
     
    912903                                        // Ignore <p> for those wacky AIM express users 
    913904                                        } else if ([chunkString caseInsensitiveCompare:P] == NSOrderedSame || 
    914                                                            [chunkString caseInsensitiveCompare:CloseP] == NSOrderedSame) { 
    915  
     905                                                           ([chunkString caseInsensitiveCompare:CloseP] == NSOrderedSame)) { 
     906                                                 
    916907                                        //Invalid 
    917908                                        } else { 
     
    10591050                } else if ([arg caseInsensitiveCompare:@"LANG"] == NSOrderedSame) { 
    10601051                        [textAttributes setLanguageValue:[inArgs objectForKey:arg]]; 
    1061                 } 
     1052 
     1053                }  else if ([arg caseInsensitiveCompare:@"sender"] == NSOrderedSame) { 
     1054                        //Ghetto HTML log processing 
     1055                        if (inDiv && send) { 
     1056                                [textAttributes setTextColor:[NSColor colorWithCalibratedRed:0.0 green:0.5 blue:0.0 alpha:1.0]]; 
     1057                        } else if (inDiv && receive) { 
     1058                                [textAttributes setTextColor:[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.5 alpha:1.0]]; 
     1059                        } 
     1060                } 
     1061                 
    10621062        } 
    10631063} 
     
    10721072                if ([arg caseInsensitiveCompare:@"BGCOLOR"] == NSOrderedSame) { 
    10731073                        [textAttributes setBackgroundColor:[NSColor colorWithHTMLString:[inArgs objectForKey:arg] defaultColor:[NSColor whiteColor]]]; 
    1074                 } 
     1074 
     1075                }        
     1076        } 
     1077
     1078 
     1079- (void)processSpanTagArgs:(NSDictionary *)inArgs attributes:(AITextAttributes *)textAttributes 
     1080
     1081        NSEnumerator    *enumerator; 
     1082        NSString                *arg; 
     1083         
     1084        enumerator = [[inArgs allKeys] objectEnumerator]; 
     1085        while ((arg = [enumerator nextObject])) { 
     1086                if ([arg caseInsensitiveCompare:@"class"] == NSOrderedSame) { 
     1087                        //Process the span tag if it's in a log 
     1088                        NSString        *class = [inArgs objectForKey:arg]; 
     1089 
     1090                        if ([class caseInsensitiveCompare:@"sender"] == NSOrderedSame) { 
     1091                                if (inDiv && send) { 
     1092                                        [textAttributes setTextColor:[NSColor colorWithCalibratedRed:0.0  
     1093                                                                                                                                                   green:0.5 
     1094                                                                                                                                                        blue:0.0  
     1095                                                                                                                                                   alpha:1.0]]; 
     1096                                        inLogSpan = YES; 
     1097                                } else if (inDiv && receive) { 
     1098                                        [textAttributes setTextColor:[NSColor colorWithCalibratedRed:0.0 
     1099                                                                                                                                                   green:0.0 
     1100                                                                                                                                                        blue:0.5  
     1101                                                                                                                                                   alpha:1.0]]; 
     1102                                        inLogSpan = YES; 
     1103                                } 
     1104 
     1105                        } else if ([class caseInsensitiveCompare:@"timestamp"] == NSOrderedSame) { 
     1106                                [textAttributes setTextColor:[NSColor grayColor]]; 
     1107                                inLogSpan = YES; 
     1108                        } 
     1109                } 
     1110                 
     1111                //XXX Jabber can send a tag like so: <span style='font-family: Helvetica; font-size: small; '> 
     1112                 
    10751113        } 
    10761114} 
     
    11011139 
    11021140                        [textAttributes setLinkURL:[NSURL URLWithString:linkString]]; 
     1141                } 
     1142        } 
     1143} 
     1144 
     1145- (void)processDivTagArgs:(NSDictionary *)inArgs attributes:(AITextAttributes *)textAttributes 
     1146{ 
     1147        NSEnumerator    *enumerator; 
     1148        NSString                *arg; 
     1149         
     1150        enumerator = [[inArgs allKeys] objectEnumerator]; 
     1151        while ((arg = [enumerator nextObject])) { 
     1152                if ([arg caseInsensitiveCompare:@"dir"] == NSOrderedSame) { 
     1153                        //Right to left, left to right handling 
     1154                        NSString        *direction = [inArgs objectForKey:arg]; 
     1155                         
     1156                        if ([direction caseInsensitiveCompare:@"rtl"] == NSOrderedSame) { 
     1157                                [textAttributes setWritingDirection:NSWritingDirectionRightToLeft]; 
     1158                                 
     1159                        } else if ([direction caseInsensitiveCompare:@"ltr"] == NSOrderedSame) { 
     1160                                [textAttributes setWritingDirection:NSWritingDirectionLeftToRight]; 
     1161                        } 
     1162                         
     1163                } else if ([arg caseInsensitiveCompare:@"class"] == NSOrderedSame) { 
     1164                        NSString        *class = [inArgs objectForKey:arg]; 
     1165                        if ([class caseInsensitiveCompare:@"send"] == NSOrderedSame) { 
     1166                                send = YES; 
     1167                                receive = NO; 
     1168                        } else if ([class caseInsensitiveCompare:@"receive"] == NSOrderedSame) { 
     1169                                receive = YES; 
     1170                                send = NO; 
     1171                        } else if ([class caseInsensitiveCompare:@"status"] == NSOrderedSame) { 
     1172                                [textAttributes setTextColor:[NSColor grayColor]]; 
     1173                        } 
    11031174                } 
    11041175        }