Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: trunk/Source/core/page/PageSerializer.cpp

Issue 107233005: Revert 164094 "This review merges the two existing page serializ..." (Closed) Base URL: svn://svn.chromium.org/blink/
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « trunk/Source/core/page/PageSerializer.h ('k') | trunk/Source/web/WebPageSerializer.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 #include "core/dom/Text.h" 47 #include "core/dom/Text.h"
48 #include "core/editing/MarkupAccumulator.h" 48 #include "core/editing/MarkupAccumulator.h"
49 #include "core/fetch/FontResource.h" 49 #include "core/fetch/FontResource.h"
50 #include "core/fetch/ImageResource.h" 50 #include "core/fetch/ImageResource.h"
51 #include "core/frame/Frame.h" 51 #include "core/frame/Frame.h"
52 #include "core/html/HTMLFrameOwnerElement.h" 52 #include "core/html/HTMLFrameOwnerElement.h"
53 #include "core/html/HTMLImageElement.h" 53 #include "core/html/HTMLImageElement.h"
54 #include "core/html/HTMLInputElement.h" 54 #include "core/html/HTMLInputElement.h"
55 #include "core/html/HTMLLinkElement.h" 55 #include "core/html/HTMLLinkElement.h"
56 #include "core/html/HTMLStyleElement.h" 56 #include "core/html/HTMLStyleElement.h"
57 #include "core/html/ImageDocument.h"
58 #include "core/html/parser/HTMLMetaCharsetParser.h" 57 #include "core/html/parser/HTMLMetaCharsetParser.h"
59 #include "core/page/Page.h" 58 #include "core/page/Page.h"
60 #include "core/rendering/RenderImage.h" 59 #include "core/rendering/RenderImage.h"
61 #include "core/rendering/style/StyleFetchedImage.h" 60 #include "core/rendering/style/StyleFetchedImage.h"
62 #include "core/rendering/style/StyleImage.h" 61 #include "core/rendering/style/StyleImage.h"
63 #include "platform/SerializedResource.h" 62 #include "platform/SerializedResource.h"
64 #include "platform/graphics/Image.h" 63 #include "platform/graphics/Image.h"
65 #include "wtf/text/CString.h" 64 #include "wtf/text/CString.h"
66 #include "wtf/text/StringBuilder.h" 65 #include "wtf/text/StringBuilder.h"
67 #include "wtf/text/TextEncoding.h" 66 #include "wtf/text/TextEncoding.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 // FIXME: We should support all frame owners including applets. 98 // FIXME: We should support all frame owners including applets.
100 return frameOwner.hasTagName(HTMLNames::objectTag) ? HTMLNames::dataAttr : H TMLNames::srcAttr; 99 return frameOwner.hasTagName(HTMLNames::objectTag) ? HTMLNames::dataAttr : H TMLNames::srcAttr;
101 } 100 }
102 101
103 class SerializerMarkupAccumulator : public WebCore::MarkupAccumulator { 102 class SerializerMarkupAccumulator : public WebCore::MarkupAccumulator {
104 public: 103 public:
105 SerializerMarkupAccumulator(PageSerializer*, Document*, Vector<Node*>*); 104 SerializerMarkupAccumulator(PageSerializer*, Document*, Vector<Node*>*);
106 virtual ~SerializerMarkupAccumulator(); 105 virtual ~SerializerMarkupAccumulator();
107 106
108 protected: 107 protected:
109 virtual void appendText(StringBuilder&, Text*) OVERRIDE; 108 virtual void appendText(StringBuilder& out, Text*);
110 virtual void appendElement(StringBuilder&, Element*, Namespaces*) OVERRIDE; 109 virtual void appendElement(StringBuilder& out, Element*, Namespaces*);
111 virtual void appendCustomAttributes(StringBuilder&, Element*, Namespaces*) O VERRIDE; 110 virtual void appendCustomAttributes(StringBuilder& out, Element*, Namespaces *);
112 virtual void appendEndTag(Node*) OVERRIDE; 111 virtual void appendEndTag(Node*);
113 112
113 private:
114 PageSerializer* m_serializer; 114 PageSerializer* m_serializer;
115 Document* m_document; 115 Document* m_document;
116 }; 116 };
117 117
118 SerializerMarkupAccumulator::SerializerMarkupAccumulator(PageSerializer* seriali zer, Document* document, Vector<Node*>* nodes) 118 SerializerMarkupAccumulator::SerializerMarkupAccumulator(PageSerializer* seriali zer, Document* document, Vector<Node*>* nodes)
119 : MarkupAccumulator(nodes, ResolveAllURLs) 119 : MarkupAccumulator(nodes, ResolveAllURLs)
120 , m_serializer(serializer) 120 , m_serializer(serializer)
121 , m_document(document) 121 , m_document(document)
122 { 122 {
123 } 123 }
124 124
125 SerializerMarkupAccumulator::~SerializerMarkupAccumulator() 125 SerializerMarkupAccumulator::~SerializerMarkupAccumulator()
126 { 126 {
127 } 127 }
128 128
129 void SerializerMarkupAccumulator::appendText(StringBuilder& result, Text* text) 129 void SerializerMarkupAccumulator::appendText(StringBuilder& out, Text* text)
130 { 130 {
131 Element* parent = text->parentElement(); 131 Element* parent = text->parentElement();
132 if (parent && !shouldIgnoreElement(parent)) 132 if (parent && !shouldIgnoreElement(parent))
133 MarkupAccumulator::appendText(result, text); 133 MarkupAccumulator::appendText(out, text);
134 } 134 }
135 135
136 void SerializerMarkupAccumulator::appendElement(StringBuilder& result, Element* element, Namespaces* namespaces) 136 void SerializerMarkupAccumulator::appendElement(StringBuilder& out, Element* ele ment, Namespaces* namespaces)
137 { 137 {
138 if (!shouldIgnoreElement(element)) 138 if (!shouldIgnoreElement(element))
139 MarkupAccumulator::appendElement(result, element, namespaces); 139 MarkupAccumulator::appendElement(out, element, namespaces);
140 140
141 // FIXME: Refactor MarkupAccumulator so it is easier to append an element li ke this, without special cases for XHTML
142 if (element->hasTagName(HTMLNames::headTag)) { 141 if (element->hasTagName(HTMLNames::headTag)) {
143 result.appendLiteral("<meta http-equiv=\"Content-Type\" content=\""); 142 out.append("<meta charset=\"");
144 result.append(m_document->suggestedMIMEType()); 143 out.append(m_document->charset());
145 result.appendLiteral("; charset="); 144 out.append("\">");
146 result.append(m_document->charset());
147 if (m_document->isXHTMLDocument())
148 result.appendLiteral("\" />");
149 else
150 result.appendLiteral("\">");
151 } 145 }
152 146
153 // FIXME: For object (plugins) tags and video tag we could replace them by a n image of their current contents. 147 // FIXME: For object (plugins) tags and video tag we could replace them by a n image of their current contents.
154 } 148 }
155 149
156 void SerializerMarkupAccumulator::appendCustomAttributes(StringBuilder& result, Element* element, Namespaces* namespaces) 150 void SerializerMarkupAccumulator::appendCustomAttributes(StringBuilder& out, Ele ment* element, Namespaces* namespaces)
157 { 151 {
158 if (!element->isFrameOwnerElement()) 152 if (!element->isFrameOwnerElement())
159 return; 153 return;
160 154
161 HTMLFrameOwnerElement* frameOwner = toHTMLFrameOwnerElement(element); 155 HTMLFrameOwnerElement* frameOwner = toHTMLFrameOwnerElement(element);
162 Frame* frame = frameOwner->contentFrame(); 156 Frame* frame = frameOwner->contentFrame();
163 if (!frame) 157 if (!frame)
164 return; 158 return;
165 159
166 KURL url = frame->document()->url(); 160 KURL url = frame->document()->url();
167 if (url.isValid() && !url.isBlankURL()) 161 if (url.isValid() && !url.isBlankURL())
168 return; 162 return;
169 163
170 // We need to give a fake location to blank frames so they can be referenced by the serialized frame. 164 // We need to give a fake location to blank frames so they can be referenced by the serialized frame.
171 url = m_serializer->urlForBlankFrame(frame); 165 url = m_serializer->urlForBlankFrame(frame);
172 appendAttribute(result, element, Attribute(frameOwnerURLAttributeName(*frame Owner), url.string()), namespaces); 166 appendAttribute(out, element, Attribute(frameOwnerURLAttributeName(*frameOwn er), url.string()), namespaces);
173 } 167 }
174 168
175 void SerializerMarkupAccumulator::appendEndTag(Node* node) 169 void SerializerMarkupAccumulator::appendEndTag(Node* node)
176 { 170 {
177 if (node->isElementNode() && !shouldIgnoreElement(toElement(node))) 171 if (node->isElementNode() && !shouldIgnoreElement(toElement(node)))
178 MarkupAccumulator::appendEndTag(node); 172 MarkupAccumulator::appendEndTag(node);
179 } 173 }
180 174
181 class LinkChangeSerializerMarkupAccumulator : public SerializerMarkupAccumulator { 175 PageSerializer::PageSerializer(Vector<SerializedResource>* resources)
182 public:
183 LinkChangeSerializerMarkupAccumulator(PageSerializer*, Document*, Vector<Nod e*>*, LinkLocalPathMap*, String);
184
185 protected:
186 virtual void appendElement(StringBuilder&, Element*, Namespaces*) OVERRIDE;
187 virtual void appendAttribute(StringBuilder&, Element*, const Attribute&, Nam espaces*) OVERRIDE;
188
189 private:
190 // m_replaceLinks include all pair of local resource path and corresponding original link.
191 LinkLocalPathMap* m_replaceLinks;
192 String m_directoryName;
193 };
194
195 LinkChangeSerializerMarkupAccumulator::LinkChangeSerializerMarkupAccumulator(Pag eSerializer* serializer, Document* document, Vector<Node*>* nodes, LinkLocalPath Map* links, String directoryName)
196 : SerializerMarkupAccumulator(serializer, document, nodes)
197 , m_replaceLinks(links)
198 , m_directoryName(directoryName)
199 {
200 }
201
202 void LinkChangeSerializerMarkupAccumulator::appendElement(StringBuilder& result, Element* element, Namespaces* namespaces)
203 {
204 // FIXME: We could move the uncommenting to appendOpenTag and appendCloseTag , or just remove it
205 if (element->hasTagName(HTMLNames::baseTag)) {
206 // Comment the BASE tag when serializing dom.
207 result.append("<!--");
208 } else if (element->hasTagName(HTMLNames::htmlTag)) {
209 // Add MOTW (Mark of the Web) declaration before html tag.
210 // See http://msdn2.microsoft.com/en-us/library/ms537628(VS.85).aspx.
211 result.append(String::format("\n<!-- saved from url=(%04d)%s -->\n",
212 static_cast<int>(m_document->url().string().utf8().length()),
213 m_document->url().string().utf8().data()));
214 }
215
216 SerializerMarkupAccumulator::appendElement(result, element, namespaces);
217
218 if (element->hasTagName(HTMLNames::baseTag)) {
219 // Comment the BASE tag when serializing dom.
220 result.appendLiteral("-->");
221
222 // FIXME: Refactor MarkupAccumulator so it is easier to append an elemen t like this, without special cases for XHTML
223 // Append a new base tag declaration.
224 result.appendLiteral("<base href=\".\"");
225 if (!m_document->baseTarget().isEmpty()) {
226 result.appendLiteral(" target=\"");
227 result.append(m_document->baseTarget());
228 result.append('"');
229 }
230 if (m_document->isXHTMLDocument())
231 result.appendLiteral(" />");
232 else
233 result.appendLiteral(">");
234 }
235 }
236 void LinkChangeSerializerMarkupAccumulator::appendAttribute(StringBuilder& resul t, Element* element, const Attribute& attribute, Namespaces* namespaces)
237 {
238 if (m_replaceLinks && element->isURLAttribute(attribute) && !element->isJava ScriptURLAttribute(attribute)) {
239
240 String completeURL = m_document->completeURL(attribute.value());
241
242 if (m_replaceLinks->contains(completeURL)) {
243 // FIXME: Refactor MarkupAccumulator so it is easier to append an at tribute like this.
244 result.append(' ');
245 result.append(attribute.name().toString());
246 result.appendLiteral("=\"");
247 if (!m_directoryName.isEmpty()) {
248 result.appendLiteral("./");
249 result.append(m_directoryName);
250 result.append('/');
251 }
252 result.append(m_replaceLinks->get(completeURL));
253 result.appendLiteral("\"");
254 return;
255 }
256 }
257 MarkupAccumulator::appendAttribute(result, element, attribute, namespaces);
258 }
259
260 PageSerializer::PageSerializer(Vector<SerializedResource>* resources, LinkLocalP athMap* urls, String directory)
261 : m_resources(resources) 176 : m_resources(resources)
262 , m_URLs(urls)
263 , m_directory(directory)
264 , m_blankFrameCounter(0) 177 , m_blankFrameCounter(0)
265 { 178 {
266 } 179 }
267 180
268 void PageSerializer::serialize(Page* page) 181 void PageSerializer::serialize(Page* page)
269 { 182 {
270 serializeFrame(page->mainFrame()); 183 serializeFrame(page->mainFrame());
271 } 184 }
272 185
273 void PageSerializer::serializeFrame(Frame* frame) 186 void PageSerializer::serializeFrame(Frame* frame)
274 { 187 {
275 Document* document = frame->document(); 188 Document* document = frame->document();
276 KURL url = document->url(); 189 KURL url = document->url();
277 if (!url.isValid() || url.isBlankURL()) { 190 if (!url.isValid() || url.isBlankURL()) {
278 // For blank frames we generate a fake URL so they can be referenced by their containing frame. 191 // For blank frames we generate a fake URL so they can be referenced by their containing frame.
279 url = urlForBlankFrame(frame); 192 url = urlForBlankFrame(frame);
280 } 193 }
281 194
282 if (m_resourceURLs.contains(url)) { 195 if (m_resourceURLs.contains(url)) {
283 // FIXME: We could have 2 frame with the same URL but which were dynamic ally changed and have now 196 // FIXME: We could have 2 frame with the same URL but which were dynamic ally changed and have now
284 // different content. So we should serialize both and somehow rename the frame src in the containing 197 // different content. So we should serialize both and somehow rename the frame src in the containing
285 // frame. Arg! 198 // frame. Arg!
286 return; 199 return;
287 } 200 }
288 201
289 // If frame is an image document, add the image and don't continue
290 if (document->isImageDocument()) {
291 ImageDocument* imageDocument = toImageDocument(document);
292 addImageToResources(imageDocument->cachedImage(), imageDocument->imageEl ement()->renderer(), url);
293 return;
294 }
295
296 Vector<Node*> nodes; 202 Vector<Node*> nodes;
297 OwnPtr<SerializerMarkupAccumulator> accumulator; 203 SerializerMarkupAccumulator accumulator(this, document, &nodes);
298 if (m_URLs)
299 accumulator = adoptPtr(new LinkChangeSerializerMarkupAccumulator(this, d ocument, &nodes, m_URLs, m_directory));
300 else
301 accumulator = adoptPtr(new SerializerMarkupAccumulator(this, document, & nodes));
302 String text = accumulator->serializeNodes(document, IncludeNode);
303 WTF::TextEncoding textEncoding(document->charset()); 204 WTF::TextEncoding textEncoding(document->charset());
205 CString data;
206 if (!textEncoding.isValid()) {
207 // FIXME: iframes used as images trigger this. We should deal with them correctly.
208 return;
209 }
210 String text = accumulator.serializeNodes(document, IncludeNode);
304 CString frameHTML = textEncoding.normalizeAndEncode(text, WTF::EntitiesForUn encodables); 211 CString frameHTML = textEncoding.normalizeAndEncode(text, WTF::EntitiesForUn encodables);
305 m_resources->append(SerializedResource(url, document->suggestedMIMEType(), S haredBuffer::create(frameHTML.data(), frameHTML.length()))); 212 m_resources->append(SerializedResource(url, document->suggestedMIMEType(), S haredBuffer::create(frameHTML.data(), frameHTML.length())));
306 m_resourceURLs.add(url); 213 m_resourceURLs.add(url);
307 214
308 for (Vector<Node*>::iterator iter = nodes.begin(); iter != nodes.end(); ++it er) { 215 for (Vector<Node*>::iterator iter = nodes.begin(); iter != nodes.end(); ++it er) {
309 Node* node = *iter; 216 Node* node = *iter;
310 if (!node->isElementNode()) 217 if (!node->isElementNode())
311 continue; 218 continue;
312 219
313 Element* element = toElement(node); 220 Element* element = toElement(node);
314 // We have to process in-line style as it might contain some resources ( typically background images). 221 // We have to process in-line style as it might contain some resources ( typically background images).
315 if (element->isStyledElement()) { 222 if (element->isStyledElement())
316 retrieveResourcesForProperties(element->inlineStyle(), document); 223 retrieveResourcesForProperties(element->inlineStyle(), document);
317 retrieveResourcesForProperties(element->presentationAttributeStyle() , document);
318 }
319 224
320 if (element->hasTagName(HTMLNames::imgTag)) { 225 if (element->hasTagName(HTMLNames::imgTag)) {
321 HTMLImageElement* imageElement = toHTMLImageElement(element); 226 HTMLImageElement* imageElement = toHTMLImageElement(element);
322 KURL url = document->completeURL(imageElement->getAttribute(HTMLName s::srcAttr)); 227 KURL url = document->completeURL(imageElement->getAttribute(HTMLName s::srcAttr));
323 ImageResource* cachedImage = imageElement->cachedImage(); 228 ImageResource* cachedImage = imageElement->cachedImage();
324 addImageToResources(cachedImage, imageElement->renderer(), url); 229 addImageToResources(cachedImage, imageElement->renderer(), url);
325 } else if (element->hasTagName(HTMLNames::inputTag)) { 230 } else if (element->hasTagName(HTMLNames::inputTag)) {
326 HTMLInputElement* inputElement = toHTMLInputElement(element); 231 HTMLInputElement* inputElement = toHTMLInputElement(element);
327 if (inputElement->isImageButton() && inputElement->hasImageLoader()) { 232 if (inputElement->isImageButton() && inputElement->hasImageLoader()) {
328 KURL url = inputElement->src(); 233 KURL url = inputElement->src();
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 String mimeType = resource->response().mimeType(); 304 String mimeType = resource->response().mimeType();
400 m_resources->append(SerializedResource(url, mimeType, data)); 305 m_resources->append(SerializedResource(url, mimeType, data));
401 m_resourceURLs.add(url); 306 m_resourceURLs.add(url);
402 } 307 }
403 308
404 void PageSerializer::addImageToResources(ImageResource* image, RenderObject* ima geRenderer, const KURL& url) 309 void PageSerializer::addImageToResources(ImageResource* image, RenderObject* ima geRenderer, const KURL& url)
405 { 310 {
406 if (!shouldAddURL(url)) 311 if (!shouldAddURL(url))
407 return; 312 return;
408 313
409 if (!image || !image->hasImage() || image->image() == Image::nullImage()) 314 if (!image || image->image() == Image::nullImage())
410 return; 315 return;
411 316
412 RefPtr<SharedBuffer> data = imageRenderer ? image->imageForRenderer(imageRen derer)->data() : 0; 317 RefPtr<SharedBuffer> data = imageRenderer ? image->imageForRenderer(imageRen derer)->data() : 0;
413 if (!data) 318 if (!data)
414 data = image->image()->data(); 319 data = image->image()->data();
415 320
416 addToResources(image, data, url); 321 addToResources(image, data, url);
417 } 322 }
418 323
419 void PageSerializer::addFontToResources(FontResource* font) 324 void PageSerializer::addFontToResources(FontResource* font)
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 if (iter != m_blankFrameURLs.end()) 376 if (iter != m_blankFrameURLs.end())
472 return iter->value; 377 return iter->value;
473 String url = "wyciwyg://frame/" + String::number(m_blankFrameCounter++); 378 String url = "wyciwyg://frame/" + String::number(m_blankFrameCounter++);
474 KURL fakeURL(ParsedURLString, url); 379 KURL fakeURL(ParsedURLString, url);
475 m_blankFrameURLs.add(frame, fakeURL); 380 m_blankFrameURLs.add(frame, fakeURL);
476 381
477 return fakeURL; 382 return fakeURL;
478 } 383 }
479 384
480 } 385 }
OLDNEW
« no previous file with comments | « trunk/Source/core/page/PageSerializer.h ('k') | trunk/Source/web/WebPageSerializer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698