Changeset 2112

Show
Ignore:
Timestamp:
11/14/04 00:07:50 (4 years ago)
Author:
timothy
Message:

More direct DOM access when loading transcripts or switching styles.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/overhaul/JVChatTranscript.m

    r2102 r2112  
    958958 
    959959- (void) _prependMessages:(NSString *) messages { 
    960         NSMutableString *result = [[messages mutableCopy] autorelease]; 
    961         [result escapeCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\\\"'"]]; 
    962         [result replaceOccurrencesOfString:@"\n" withString:@"\\n" options:NSLiteralSearch range:NSMakeRange( 0, [result length] )]; 
    963         [display stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"prependMessages( \"%@\" );", result]]; 
     960        if( [[display mainFrame] respondsToSelector:@selector( DOMDocument )] ) { 
     961#ifdef _WEB_SCRIPT_OBJECT_H_ 
     962                NSMutableString *result = [messages mutableCopy]; 
     963                [result replaceOccurrencesOfString:@"  " withString:@"  " options:NSLiteralSearch range:NSMakeRange( 0, [result length] )]; 
     964 
     965                // check if we are near the bottom of the chat area, and if we should scroll down later 
     966                NSNumber *scrollNeeded = [[[display mainFrame] DOMDocument] evaluateWebScript:@"( document.body.scrollTop >= ( document.body.offsetHeight - ( window.innerHeight * 1.1 ) ) )"]; 
     967 
     968                // parses the message so we can get the DOM tree 
     969                DOMHTMLElement *element = (DOMHTMLElement *)[[[display mainFrame] DOMDocument] createElement:@"span"]; 
     970                [element setInnerHTML:result]; 
     971 
     972                [result release]; 
     973                result = nil; 
     974 
     975                DOMHTMLElement *body = [(DOMHTMLDocument *)[[display mainFrame] DOMDocument] body]; 
     976                DOMNode *firstMessage = [body firstChild]; 
     977 
     978                while( [[element children] length] ) { // append all children 
     979                        if( firstMessage ) [body insertBefore:[element firstChild] :firstMessage]; 
     980                        else [body appendChild:[element firstChild]]; 
     981                } 
     982 
     983                // scroll down if we need to 
     984                if( [scrollNeeded boolValue] ) [body setValue:[body valueForKey:@"offsetHeight"] forKey:@"scrollTop"]; 
     985#endif 
     986        } else { 
     987                NSMutableString *result = [messages mutableCopy]; 
     988                [result escapeCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\\\"'"]]; 
     989                [result replaceOccurrencesOfString:@"\n" withString:@"\\n" options:NSLiteralSearch range:NSMakeRange( 0, [result length] )]; 
     990                [result replaceOccurrencesOfString:@"  " withString:@"  " options:NSLiteralSearch range:NSMakeRange( 0, [result length] )]; 
     991                [display stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"prependMessages( \"%@\" );", result]]; 
     992                [result release]; 
     993        } 
    964994} 
    965995