2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #import "WebArchiver.h"
31 #import "WebArchive.h"
32 #import "WebDOMOperationsPrivate.h"
33 #import "WebDataSource.h"
34 #import "WebDocument.h"
36 #import "WebFrameInternal.h"
37 #import "WebResource.h"
38 #import <JavaScriptCore/Assertions.h>
39 #import <WebCore/Frame.h>
40 #import <WebCore/SelectionController.h>
41 #import <WebKit/DOM.h>
43 using namespace WebCore;
45 @implementation WebArchiver
47 + (NSArray *)_subframeArchivesForFrame:(WebFrame *)frame
49 NSEnumerator *enumerator = [[frame childFrames] objectEnumerator];
50 NSMutableArray *subframeArchives = [NSMutableArray array];
52 while ((childFrame = [enumerator nextObject])) {
53 WebArchive *childFrameArchive = [self archiveFrame:childFrame];
54 if (childFrameArchive)
55 [subframeArchives addObject:childFrameArchive];
58 return subframeArchives;
61 + (WebArchive *)archiveFrame:(WebFrame *)frame;
63 return [[[WebArchive alloc] initWithMainResource:[[frame _dataSource] mainResource]
64 subresources:[[frame _dataSource] subresources]
65 subframeArchives:[self _subframeArchivesForFrame:frame]] autorelease];
68 + (WebArchive *)archiveMainResourceForFrame:(WebFrame *)frame;
70 return [[[WebArchive alloc] initWithMainResource:[[frame _dataSource] mainResource]
72 subframeArchives:nil] autorelease];
75 + (WebArchive *)_archiveCurrentStateForFrame:(WebFrame *)frame
77 if ([frame DOMDocument])
78 return [self archiveNode:[frame DOMDocument]];
80 return [self archiveFrame:frame];
83 + (WebArchive *)_archiveWithMarkupString:(NSString *)markupString fromFrame:(WebFrame *)frame nodes:(NSArray *)nodes
85 NSURLResponse *response = [[frame _dataSource] response];
86 NSURL *responseURL = [response URL];
88 // it's possible to have a response without a URL here
89 // <rdar://problem/5454935>
91 responseURL = [NSURL URLWithString:@""];
93 WebResource *mainResource = [[WebResource alloc] initWithData:[markupString dataUsingEncoding:NSUTF8StringEncoding]
95 MIMEType:[response MIMEType]
96 textEncodingName:@"UTF-8"
97 frameName:[frame name]];
99 NSMutableArray *subframeArchives = [[NSMutableArray alloc] init];
100 NSMutableArray *subresources = [[NSMutableArray alloc] init];
101 NSMutableSet *uniqueSubresources = [[NSMutableSet alloc] init];
102 NSEnumerator *enumerator = [nodes objectEnumerator];
104 while ((node = [enumerator nextObject]) != nil) {
105 WebFrame *childFrame;
106 if (([node isKindOfClass:[DOMHTMLFrameElement class]] ||
107 [node isKindOfClass:[DOMHTMLIFrameElement class]] ||
108 [node isKindOfClass:[DOMHTMLObjectElement class]]) &&
109 ((childFrame = [(DOMHTMLFrameElement *)node contentFrame]) != nil)) {
110 [subframeArchives addObject:[self _archiveCurrentStateForFrame:childFrame]];
112 NSEnumerator *enumerator = [[node _subresourceURLs] objectEnumerator];
114 while ((URL = [enumerator nextObject]) != nil) {
115 if ([uniqueSubresources containsObject:URL])
117 [uniqueSubresources addObject:URL];
118 WebResource *subresource = [[frame _dataSource] subresourceForURL:URL];
120 [subresources addObject:subresource];
122 // FIXME: should do something better than spew to console here
123 LOG_ERROR("Failed to archive subresource for %@", URL);
128 WebArchive *archive = [[[WebArchive alloc] initWithMainResource:mainResource subresources:subresources subframeArchives:subframeArchives] autorelease];
129 [mainResource release];
130 [uniqueSubresources release];
131 [subresources release];
132 [subframeArchives release];
137 + (WebArchive *)archiveRange:(DOMRange *)range
139 WebFrame *frame = [[[range startContainer] ownerDocument] webFrame];
141 NSString *markupString = [frame _markupStringFromRange:range nodes:&nodes];
142 return [self _archiveWithMarkupString:markupString fromFrame:frame nodes:nodes];
145 + (WebArchive *)archiveNode:(DOMNode *)node
147 WebFrame *frame = [[node ownerDocument] webFrame];
149 NSString *markupString = [frame _markupStringFromNode:node nodes:&nodes];
150 return [self _archiveWithMarkupString:markupString fromFrame:frame nodes:nodes];
153 + (WebArchive *)archiveSelectionInFrame:(WebFrame *)frame
155 Frame* coreFrame = core(frame);
160 NSString *markupString = [frame _markupStringFromRange:kit(coreFrame->selectionController()->toRange().get()) nodes:&nodes];
161 WebArchive *archive = [self _archiveWithMarkupString:markupString fromFrame:frame nodes:nodes];
163 if (coreFrame->isFrameSet()) {
164 // Wrap the frameset document in an iframe so it can be pasted into
165 // another document (which will have a body or frameset of its own).
167 NSString *iframeMarkup = [[NSString alloc] initWithFormat:@"<iframe frameborder=\"no\" marginwidth=\"0\" marginheight=\"0\" width=\"98%%\" height=\"98%%\" src=\"%@\"></iframe>", [[[frame _dataSource] response] URL]];
168 WebResource *iframeResource = [[WebResource alloc] initWithData:[iframeMarkup dataUsingEncoding:NSUTF8StringEncoding]
170 MIMEType:@"text/html"
171 textEncodingName:@"UTF-8"
174 NSArray *subframeArchives = [NSArray arrayWithObject:archive];
175 archive = [[[WebArchive alloc] initWithMainResource:iframeResource subresources:nil subframeArchives:subframeArchives] autorelease];
177 [iframeResource release];
178 [iframeMarkup release];