WebKit/mac/WebView/WebHTMLRepresentation.mm
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  *
       
     8  * 1.  Redistributions of source code must retain the above copyright
       
     9  *     notice, this list of conditions and the following disclaimer. 
       
    10  * 2.  Redistributions in binary form must reproduce the above copyright
       
    11  *     notice, this list of conditions and the following disclaimer in the
       
    12  *     documentation and/or other materials provided with the distribution. 
       
    13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
       
    14  *     its contributors may be used to endorse or promote products derived
       
    15  *     from this software without specific prior written permission. 
       
    16  *
       
    17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
       
    18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
       
    20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
       
    21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
       
    22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
       
    24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
       
    26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    27  */
       
    28 
       
    29 #import "WebHTMLRepresentation.h"
       
    30 
       
    31 #import "DOMElementInternal.h"
       
    32 #import "DOMNodeInternal.h"
       
    33 #import "DOMRangeInternal.h"
       
    34 #import "WebArchive.h"
       
    35 #import "WebBasePluginPackage.h"
       
    36 #import "WebDataSourceInternal.h"
       
    37 #import "WebDocumentPrivate.h"
       
    38 #import "WebFrameInternal.h"
       
    39 #import "WebKitNSStringExtras.h"
       
    40 #import "WebKitStatisticsPrivate.h"
       
    41 #import "WebNSAttributedStringExtras.h"
       
    42 #import "WebNSObjectExtras.h"
       
    43 #import "WebTypesInternal.h"
       
    44 #import "WebView.h"
       
    45 #import <Foundation/NSURLResponse.h>
       
    46 #import <WebCore/Document.h>
       
    47 #import <WebCore/DocumentLoader.h>
       
    48 #import <WebCore/Frame.h>
       
    49 #import <WebCore/FrameLoader.h>
       
    50 #import <WebCore/FrameLoaderClient.h>
       
    51 #import <WebCore/HTMLFormControlElement.h>
       
    52 #import <WebCore/HTMLFormElement.h>
       
    53 #import <WebCore/HTMLInputElement.h>
       
    54 #import <WebCore/HTMLNames.h>
       
    55 #import <WebCore/MIMETypeRegistry.h>
       
    56 #import <WebCore/Range.h>
       
    57 #import <WebCore/TextResourceDecoder.h>
       
    58 #import <WebKit/DOMHTMLInputElement.h>
       
    59 #import <wtf/Assertions.h>
       
    60 #import <wtf/StdLibExtras.h>
       
    61 
       
    62 using namespace WebCore;
       
    63 using namespace HTMLNames;
       
    64 
       
    65 @interface WebHTMLRepresentationPrivate : NSObject {
       
    66 @public
       
    67     WebDataSource *dataSource;
       
    68     
       
    69     BOOL hasSentResponseToPlugin;
       
    70     BOOL includedInWebKitStatistics;
       
    71 
       
    72     id <WebPluginManualLoader> manualLoader;
       
    73     NSView *pluginView;
       
    74 }
       
    75 @end
       
    76 
       
    77 @implementation WebHTMLRepresentationPrivate
       
    78 @end
       
    79 
       
    80 @implementation WebHTMLRepresentation
       
    81 
       
    82 static NSArray *stringArray(const HashSet<String>& set)
       
    83 {
       
    84     NSMutableArray *array = [NSMutableArray arrayWithCapacity:set.size()];
       
    85     HashSet<String>::const_iterator end = set.end();
       
    86     for (HashSet<String>::const_iterator it = set.begin(); it != end; ++it)
       
    87         [array addObject:(NSString *)(*it)];
       
    88     return array;
       
    89 }
       
    90 
       
    91 static NSArray *concatenateArrays(NSArray *first, NSArray *second)
       
    92 {
       
    93     NSMutableArray *result = [[first mutableCopy] autorelease];
       
    94     [result addObjectsFromArray:second];
       
    95     return result;
       
    96 }
       
    97 
       
    98 + (NSArray *)supportedMIMETypes
       
    99 {
       
   100     DEFINE_STATIC_LOCAL(RetainPtr<NSArray>, staticSupportedMIMETypes, (concatenateArrays([self supportedNonImageMIMETypes], [self supportedImageMIMETypes])));
       
   101     return staticSupportedMIMETypes.get();
       
   102 }
       
   103 
       
   104 + (NSArray *)supportedNonImageMIMETypes
       
   105 {
       
   106     DEFINE_STATIC_LOCAL(RetainPtr<NSArray>, staticSupportedNonImageMIMETypes, (stringArray(MIMETypeRegistry::getSupportedNonImageMIMETypes())));
       
   107     return staticSupportedNonImageMIMETypes.get();
       
   108 }
       
   109 
       
   110 + (NSArray *)supportedImageMIMETypes
       
   111 {
       
   112     DEFINE_STATIC_LOCAL(RetainPtr<NSArray>, staticSupportedImageMIMETypes, (stringArray(MIMETypeRegistry::getSupportedImageMIMETypes())));
       
   113     return staticSupportedImageMIMETypes.get();
       
   114 }
       
   115 
       
   116 - (id)init
       
   117 {
       
   118     self = [super init];
       
   119     if (!self)
       
   120         return nil;
       
   121     
       
   122     _private = [[WebHTMLRepresentationPrivate alloc] init];
       
   123 
       
   124     return self;
       
   125 }
       
   126 
       
   127 - (void)dealloc
       
   128 {
       
   129     if (_private && _private->includedInWebKitStatistics)
       
   130         --WebHTMLRepresentationCount;
       
   131 
       
   132     [_private release];
       
   133 
       
   134     [super dealloc];
       
   135 }
       
   136 
       
   137 - (void)finalize
       
   138 {
       
   139     if (_private && _private->includedInWebKitStatistics)
       
   140         --WebHTMLRepresentationCount;
       
   141 
       
   142     [super finalize];
       
   143 }
       
   144 
       
   145 - (void)_redirectDataToManualLoader:(id<WebPluginManualLoader>)manualLoader forPluginView:(NSView *)pluginView
       
   146 {
       
   147     _private->manualLoader = manualLoader;
       
   148     _private->pluginView = pluginView;
       
   149 }
       
   150 
       
   151 - (void)setDataSource:(WebDataSource *)dataSource
       
   152 {
       
   153     _private->dataSource = dataSource;
       
   154 
       
   155     if (!_private->includedInWebKitStatistics && [[dataSource webFrame] _isIncludedInWebKitStatistics]) {
       
   156         _private->includedInWebKitStatistics = YES;
       
   157         ++WebHTMLRepresentationCount;
       
   158     }
       
   159 }
       
   160 
       
   161 - (BOOL)_isDisplayingWebArchive
       
   162 {
       
   163     return [[_private->dataSource _responseMIMEType] _webkit_isCaseInsensitiveEqualToString:@"application/x-webarchive"];
       
   164 }
       
   165 
       
   166 - (void)receivedData:(NSData *)data withDataSource:(WebDataSource *)dataSource
       
   167 {
       
   168     WebFrame *webFrame = [dataSource webFrame];
       
   169     if (webFrame) {
       
   170         if (!_private->pluginView)
       
   171             [webFrame _receivedData:data textEncodingName:[[_private->dataSource response] textEncodingName]];
       
   172         
       
   173         // If the document is a stand-alone media document, now is the right time to cancel the WebKit load
       
   174         Frame* coreFrame = core(webFrame);
       
   175         if (coreFrame->document() && coreFrame->document()->isMediaDocument())
       
   176             coreFrame->loader()->documentLoader()->cancelMainResourceLoad(coreFrame->loader()->client()->pluginWillHandleLoadError(coreFrame->loader()->documentLoader()->response()));
       
   177 
       
   178         if (_private->pluginView) {
       
   179             if (!_private->hasSentResponseToPlugin) {
       
   180                 [_private->manualLoader pluginView:_private->pluginView receivedResponse:[dataSource response]];
       
   181                 _private->hasSentResponseToPlugin = YES;
       
   182             }
       
   183             
       
   184             [_private->manualLoader pluginView:_private->pluginView receivedData:data];
       
   185         }
       
   186     }
       
   187 }
       
   188 
       
   189 - (void)receivedError:(NSError *)error withDataSource:(WebDataSource *)dataSource
       
   190 {
       
   191     if (_private->pluginView) {
       
   192         [_private->manualLoader pluginView:_private->pluginView receivedError:error];
       
   193     }
       
   194 }
       
   195 
       
   196 - (void)finishedLoadingWithDataSource:(WebDataSource *)dataSource
       
   197 {
       
   198     WebFrame *frame = [dataSource webFrame];
       
   199 
       
   200     if (_private->pluginView) {
       
   201         [_private->manualLoader pluginViewFinishedLoading:_private->pluginView];
       
   202         return;
       
   203     }
       
   204 
       
   205     if (frame) {
       
   206         if (![self _isDisplayingWebArchive]) {
       
   207             // Telling the frame we received some data and passing nil as the data is our
       
   208             // way to get work done that is normally done when the first bit of data is
       
   209             // received, even for the case of a document with no data (like about:blank).
       
   210             [frame _receivedData:nil textEncodingName:[[_private->dataSource response] textEncodingName]];
       
   211         }
       
   212         
       
   213         WebView *webView = [frame webView];
       
   214         if ([webView isEditable])
       
   215             core(frame)->applyEditingStyleToBodyElement();
       
   216     }
       
   217 }
       
   218 
       
   219 - (BOOL)canProvideDocumentSource
       
   220 {
       
   221     return [[_private->dataSource webFrame] _canProvideDocumentSource];
       
   222 }
       
   223 
       
   224 - (BOOL)canSaveAsWebArchive
       
   225 {
       
   226     return [[_private->dataSource webFrame] _canSaveAsWebArchive];
       
   227 }
       
   228 
       
   229 - (NSString *)documentSource
       
   230 {
       
   231     if ([self _isDisplayingWebArchive]) {            
       
   232         SharedBuffer *parsedArchiveData = [_private->dataSource _documentLoader]->parsedArchiveData();
       
   233         NSData *nsData = parsedArchiveData ? parsedArchiveData->createNSData() : nil;
       
   234         NSString *result = [[NSString alloc] initWithData:nsData encoding:NSUTF8StringEncoding];
       
   235         [nsData release];
       
   236         return [result autorelease];
       
   237     }
       
   238 
       
   239     Frame* coreFrame = core([_private->dataSource webFrame]);
       
   240     if (!coreFrame)
       
   241         return nil;
       
   242     Document* document = coreFrame->document();
       
   243     if (!document)
       
   244         return nil;
       
   245     TextResourceDecoder* decoder = document->decoder();
       
   246     if (!decoder)
       
   247         return nil;
       
   248     NSData *data = [_private->dataSource data];
       
   249     if (!data)
       
   250         return nil;
       
   251     return decoder->encoding().decode(reinterpret_cast<const char*>([data bytes]), [data length]);
       
   252 }
       
   253 
       
   254 - (NSString *)title
       
   255 {
       
   256     return nsStringNilIfEmpty([_private->dataSource _documentLoader]->title());
       
   257 }
       
   258 
       
   259 - (DOMDocument *)DOMDocument
       
   260 {
       
   261     return [[_private->dataSource webFrame] DOMDocument];
       
   262 }
       
   263 
       
   264 - (NSAttributedString *)attributedText
       
   265 {
       
   266     // FIXME: Implement
       
   267     return nil;
       
   268 }
       
   269 
       
   270 - (NSAttributedString *)attributedStringFrom:(DOMNode *)startNode startOffset:(int)startOffset to:(DOMNode *)endNode endOffset:(int)endOffset
       
   271 {
       
   272     return [NSAttributedString _web_attributedStringFromRange:Range::create(core(startNode)->document(), core(startNode), startOffset, core(endNode), endOffset).get()];
       
   273 }
       
   274 
       
   275 static HTMLFormElement* formElementFromDOMElement(DOMElement *element)
       
   276 {
       
   277     Element* node = core(element);
       
   278     return node && node->hasTagName(formTag) ? static_cast<HTMLFormElement*>(node) : 0;
       
   279 }
       
   280 
       
   281 - (DOMElement *)elementWithName:(NSString *)name inForm:(DOMElement *)form
       
   282 {
       
   283     HTMLFormElement* formElement = formElementFromDOMElement(form);
       
   284     if (!formElement)
       
   285         return nil;
       
   286     const Vector<HTMLFormControlElement*>& elements = formElement->associatedElements();
       
   287     AtomicString targetName = name;
       
   288     for (unsigned i = 0; i < elements.size(); i++) {
       
   289         HTMLFormControlElement* elt = elements[i];
       
   290         if (elt->name() == targetName)
       
   291             return kit(elt);
       
   292     }
       
   293     return nil;
       
   294 }
       
   295 
       
   296 static HTMLInputElement* inputElementFromDOMElement(DOMElement* element)
       
   297 {
       
   298     Element* node = core(element);
       
   299     return node && node->hasTagName(inputTag) ? static_cast<HTMLInputElement*>(node) : 0;
       
   300 }
       
   301 
       
   302 - (BOOL)elementDoesAutoComplete:(DOMElement *)element
       
   303 {
       
   304     HTMLInputElement* inputElement = inputElementFromDOMElement(element);
       
   305     return inputElement
       
   306         && inputElement->isTextField()
       
   307         && inputElement->inputType() != HTMLInputElement::PASSWORD
       
   308         && inputElement->autoComplete();
       
   309 }
       
   310 
       
   311 - (BOOL)elementIsPassword:(DOMElement *)element
       
   312 {
       
   313     HTMLInputElement* inputElement = inputElementFromDOMElement(element);
       
   314     return inputElement
       
   315         && inputElement->inputType() == HTMLInputElement::PASSWORD;
       
   316 }
       
   317 
       
   318 - (DOMElement *)formForElement:(DOMElement *)element
       
   319 {
       
   320     HTMLInputElement* inputElement = inputElementFromDOMElement(element);
       
   321     return inputElement ? kit(inputElement->form()) : 0;
       
   322 }
       
   323 
       
   324 - (DOMElement *)currentForm
       
   325 {
       
   326     return kit(core([_private->dataSource webFrame])->currentForm());
       
   327 }
       
   328 
       
   329 - (NSArray *)controlsInForm:(DOMElement *)form
       
   330 {
       
   331     HTMLFormElement* formElement = formElementFromDOMElement(form);
       
   332     if (!formElement)
       
   333         return nil;
       
   334     NSMutableArray *results = nil;
       
   335     const Vector<HTMLFormControlElement*>& elements = formElement->associatedElements();
       
   336     for (unsigned i = 0; i < elements.size(); i++) {
       
   337         if (elements[i]->isEnumeratable()) { // Skip option elements, other duds
       
   338             DOMElement* de = kit(elements[i]);
       
   339             if (!results)
       
   340                 results = [NSMutableArray arrayWithObject:de];
       
   341             else
       
   342                 [results addObject:de];
       
   343         }
       
   344     }
       
   345     return results;
       
   346 }
       
   347 
       
   348 - (NSString *)searchForLabels:(NSArray *)labels beforeElement:(DOMElement *)element
       
   349 {
       
   350     return [self searchForLabels:labels beforeElement:element resultDistance:0 resultIsInCellAbove:0];
       
   351 }
       
   352 
       
   353 - (NSString *)searchForLabels:(NSArray *)labels beforeElement:(DOMElement *)element resultDistance:(NSUInteger*)outDistance resultIsInCellAbove:(BOOL*)outIsInCellAbove
       
   354 {
       
   355     size_t distance;
       
   356     bool isInCellAbove;
       
   357     
       
   358     NSString *result = core([_private->dataSource webFrame])->searchForLabelsBeforeElement(labels, core(element), &distance, &isInCellAbove);
       
   359     
       
   360     if (outDistance) {
       
   361         if (distance == notFound)
       
   362             *outDistance = NSNotFound;
       
   363         else
       
   364             *outDistance = distance;
       
   365     }
       
   366 
       
   367     if (outIsInCellAbove)
       
   368         *outIsInCellAbove = isInCellAbove;
       
   369     
       
   370     return result;
       
   371 }
       
   372 
       
   373 - (NSString *)matchLabels:(NSArray *)labels againstElement:(DOMElement *)element
       
   374 {
       
   375     return core([_private->dataSource webFrame])->matchLabelsAgainstElement(labels, core(element));
       
   376 }
       
   377 
       
   378 @end