| OLD | NEW |
| 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 Loading... |
| 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" |
| 57 #include "core/html/parser/HTMLParserIdioms.h" | 58 #include "core/html/parser/HTMLParserIdioms.h" |
| 58 #include "core/page/Page.h" | 59 #include "core/page/Page.h" |
| 59 #include "core/rendering/RenderImage.h" | 60 #include "core/rendering/RenderImage.h" |
| 60 #include "core/rendering/style/StyleFetchedImage.h" | 61 #include "core/rendering/style/StyleFetchedImage.h" |
| 61 #include "core/rendering/style/StyleImage.h" | 62 #include "core/rendering/style/StyleImage.h" |
| 62 #include "platform/SerializedResource.h" | 63 #include "platform/SerializedResource.h" |
| 63 #include "platform/graphics/Image.h" | 64 #include "platform/graphics/Image.h" |
| 64 #include "wtf/text/CString.h" | 65 #include "wtf/text/CString.h" |
| 65 #include "wtf/text/StringBuilder.h" | 66 #include "wtf/text/StringBuilder.h" |
| 66 #include "wtf/text/TextEncoding.h" | 67 #include "wtf/text/TextEncoding.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 // FIXME: We should support all frame owners including applets. | 99 // FIXME: We should support all frame owners including applets. |
| 99 return frameOwner.hasTagName(HTMLNames::objectTag) ? HTMLNames::dataAttr : H
TMLNames::srcAttr; | 100 return frameOwner.hasTagName(HTMLNames::objectTag) ? HTMLNames::dataAttr : H
TMLNames::srcAttr; |
| 100 } | 101 } |
| 101 | 102 |
| 102 class SerializerMarkupAccumulator : public WebCore::MarkupAccumulator { | 103 class SerializerMarkupAccumulator : public WebCore::MarkupAccumulator { |
| 103 public: | 104 public: |
| 104 SerializerMarkupAccumulator(PageSerializer*, Document*, Vector<Node*>*); | 105 SerializerMarkupAccumulator(PageSerializer*, Document*, Vector<Node*>*); |
| 105 virtual ~SerializerMarkupAccumulator(); | 106 virtual ~SerializerMarkupAccumulator(); |
| 106 | 107 |
| 107 protected: | 108 protected: |
| 108 virtual void appendText(StringBuilder& out, Text*); | 109 virtual void appendText(StringBuilder&, Text*) OVERRIDE; |
| 109 virtual void appendElement(StringBuilder& out, Element*, Namespaces*); | 110 virtual void appendElement(StringBuilder&, Element*, Namespaces*) OVERRIDE; |
| 110 virtual void appendCustomAttributes(StringBuilder& out, Element*, Namespaces
*); | 111 virtual void appendCustomAttributes(StringBuilder&, Element*, Namespaces*) O
VERRIDE; |
| 111 virtual void appendEndTag(Node*); | 112 virtual void appendEndTag(Node*) OVERRIDE; |
| 112 | 113 |
| 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& out, Text* text) | 129 void SerializerMarkupAccumulator::appendText(StringBuilder& result, 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(out, text); | 133 MarkupAccumulator::appendText(result, text); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void SerializerMarkupAccumulator::appendElement(StringBuilder& out, Element* ele
ment, Namespaces* namespaces) | 136 void SerializerMarkupAccumulator::appendElement(StringBuilder& result, Element*
element, Namespaces* namespaces) |
| 137 { | 137 { |
| 138 if (!shouldIgnoreElement(element)) | 138 if (!shouldIgnoreElement(element)) |
| 139 MarkupAccumulator::appendElement(out, element, namespaces); | 139 MarkupAccumulator::appendElement(result, element, namespaces); |
| 140 | 140 |
| 141 // FIXME: Refactor MarkupAccumulator so it is easier to append an element li
ke this, without special cases for XHTML |
| 141 if (element->hasTagName(HTMLNames::headTag)) { | 142 if (element->hasTagName(HTMLNames::headTag)) { |
| 142 out.append("<meta charset=\""); | 143 result.appendLiteral("<meta http-equiv=\"Content-Type\" content=\""); |
| 143 out.append(m_document->charset()); | 144 result.append(m_document->suggestedMIMEType()); |
| 144 out.append("\">"); | 145 result.appendLiteral("; charset="); |
| 146 result.append(m_document->charset()); |
| 147 if (m_document->isXHTMLDocument()) |
| 148 result.appendLiteral("\" />"); |
| 149 else |
| 150 result.appendLiteral("\">"); |
| 145 } | 151 } |
| 146 | 152 |
| 147 // FIXME: For object (plugins) tags and video tag we could replace them by a
n image of their current contents. | 153 // FIXME: For object (plugins) tags and video tag we could replace them by a
n image of their current contents. |
| 148 } | 154 } |
| 149 | 155 |
| 150 void SerializerMarkupAccumulator::appendCustomAttributes(StringBuilder& out, Ele
ment* element, Namespaces* namespaces) | 156 void SerializerMarkupAccumulator::appendCustomAttributes(StringBuilder& result,
Element* element, Namespaces* namespaces) |
| 151 { | 157 { |
| 152 if (!element->isFrameOwnerElement()) | 158 if (!element->isFrameOwnerElement()) |
| 153 return; | 159 return; |
| 154 | 160 |
| 155 HTMLFrameOwnerElement* frameOwner = toHTMLFrameOwnerElement(element); | 161 HTMLFrameOwnerElement* frameOwner = toHTMLFrameOwnerElement(element); |
| 156 Frame* frame = frameOwner->contentFrame(); | 162 Frame* frame = frameOwner->contentFrame(); |
| 157 if (!frame) | 163 if (!frame) |
| 158 return; | 164 return; |
| 159 | 165 |
| 160 KURL url = frame->document()->url(); | 166 KURL url = frame->document()->url(); |
| 161 if (url.isValid() && !url.isBlankURL()) | 167 if (url.isValid() && !url.isBlankURL()) |
| 162 return; | 168 return; |
| 163 | 169 |
| 164 // We need to give a fake location to blank frames so they can be referenced
by the serialized frame. | 170 // We need to give a fake location to blank frames so they can be referenced
by the serialized frame. |
| 165 url = m_serializer->urlForBlankFrame(frame); | 171 url = m_serializer->urlForBlankFrame(frame); |
| 166 appendAttribute(out, element, Attribute(frameOwnerURLAttributeName(*frameOwn
er), url.string()), namespaces); | 172 appendAttribute(result, element, Attribute(frameOwnerURLAttributeName(*frame
Owner), url.string()), namespaces); |
| 167 } | 173 } |
| 168 | 174 |
| 169 void SerializerMarkupAccumulator::appendEndTag(Node* node) | 175 void SerializerMarkupAccumulator::appendEndTag(Node* node) |
| 170 { | 176 { |
| 171 if (node->isElementNode() && !shouldIgnoreElement(toElement(node))) | 177 if (node->isElementNode() && !shouldIgnoreElement(toElement(node))) |
| 172 MarkupAccumulator::appendEndTag(node); | 178 MarkupAccumulator::appendEndTag(node); |
| 173 } | 179 } |
| 174 | 180 |
| 175 PageSerializer::PageSerializer(Vector<SerializedResource>* resources) | 181 class LinkChangeSerializerMarkupAccumulator : public SerializerMarkupAccumulator
{ |
| 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) |
| 176 : m_resources(resources) | 261 : m_resources(resources) |
| 262 , m_URLs(urls) |
| 263 , m_directory(directory) |
| 177 , m_blankFrameCounter(0) | 264 , m_blankFrameCounter(0) |
| 178 { | 265 { |
| 179 } | 266 } |
| 180 | 267 |
| 181 void PageSerializer::serialize(Page* page) | 268 void PageSerializer::serialize(Page* page) |
| 182 { | 269 { |
| 183 serializeFrame(page->mainFrame()); | 270 serializeFrame(page->mainFrame()); |
| 184 } | 271 } |
| 185 | 272 |
| 186 void PageSerializer::serializeFrame(Frame* frame) | 273 void PageSerializer::serializeFrame(Frame* frame) |
| 187 { | 274 { |
| 188 Document* document = frame->document(); | 275 Document* document = frame->document(); |
| 189 KURL url = document->url(); | 276 KURL url = document->url(); |
| 190 if (!url.isValid() || url.isBlankURL()) { | 277 if (!url.isValid() || url.isBlankURL()) { |
| 191 // For blank frames we generate a fake URL so they can be referenced by
their containing frame. | 278 // For blank frames we generate a fake URL so they can be referenced by
their containing frame. |
| 192 url = urlForBlankFrame(frame); | 279 url = urlForBlankFrame(frame); |
| 193 } | 280 } |
| 194 | 281 |
| 195 if (m_resourceURLs.contains(url)) { | 282 if (m_resourceURLs.contains(url)) { |
| 196 // FIXME: We could have 2 frame with the same URL but which were dynamic
ally changed and have now | 283 // FIXME: We could have 2 frame with the same URL but which were dynamic
ally changed and have now |
| 197 // different content. So we should serialize both and somehow rename the
frame src in the containing | 284 // different content. So we should serialize both and somehow rename the
frame src in the containing |
| 198 // frame. Arg! | 285 // frame. Arg! |
| 199 return; | 286 return; |
| 200 } | 287 } |
| 201 | 288 |
| 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 |
| 202 Vector<Node*> nodes; | 296 Vector<Node*> nodes; |
| 203 SerializerMarkupAccumulator accumulator(this, document, &nodes); | 297 OwnPtr<SerializerMarkupAccumulator> accumulator; |
| 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); |
| 204 WTF::TextEncoding textEncoding(document->charset()); | 303 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); | |
| 211 CString frameHTML = textEncoding.normalizeAndEncode(text, WTF::EntitiesForUn
encodables); | 304 CString frameHTML = textEncoding.normalizeAndEncode(text, WTF::EntitiesForUn
encodables); |
| 212 m_resources->append(SerializedResource(url, document->suggestedMIMEType(), S
haredBuffer::create(frameHTML.data(), frameHTML.length()))); | 305 m_resources->append(SerializedResource(url, document->suggestedMIMEType(), S
haredBuffer::create(frameHTML.data(), frameHTML.length()))); |
| 213 m_resourceURLs.add(url); | 306 m_resourceURLs.add(url); |
| 214 | 307 |
| 215 for (Vector<Node*>::iterator iter = nodes.begin(); iter != nodes.end(); ++it
er) { | 308 for (Vector<Node*>::iterator iter = nodes.begin(); iter != nodes.end(); ++it
er) { |
| 216 Node* node = *iter; | 309 Node* node = *iter; |
| 217 if (!node->isElementNode()) | 310 if (!node->isElementNode()) |
| 218 continue; | 311 continue; |
| 219 | 312 |
| 220 Element* element = toElement(node); | 313 Element* element = toElement(node); |
| 221 // We have to process in-line style as it might contain some resources (
typically background images). | 314 // We have to process in-line style as it might contain some resources (
typically background images). |
| 222 if (element->isStyledElement()) | 315 if (element->isStyledElement()) { |
| 223 retrieveResourcesForProperties(element->inlineStyle(), document); | 316 retrieveResourcesForProperties(element->inlineStyle(), document); |
| 317 retrieveResourcesForProperties(element->presentationAttributeStyle()
, document); |
| 318 } |
| 224 | 319 |
| 225 if (element->hasTagName(HTMLNames::imgTag)) { | 320 if (element->hasTagName(HTMLNames::imgTag)) { |
| 226 HTMLImageElement* imageElement = toHTMLImageElement(element); | 321 HTMLImageElement* imageElement = toHTMLImageElement(element); |
| 227 KURL url = document->completeURL(imageElement->getAttribute(HTMLName
s::srcAttr)); | 322 KURL url = document->completeURL(imageElement->getAttribute(HTMLName
s::srcAttr)); |
| 228 ImageResource* cachedImage = imageElement->cachedImage(); | 323 ImageResource* cachedImage = imageElement->cachedImage(); |
| 229 addImageToResources(cachedImage, imageElement->renderer(), url); | 324 addImageToResources(cachedImage, imageElement->renderer(), url); |
| 230 } else if (element->hasTagName(HTMLNames::inputTag)) { | 325 } else if (element->hasTagName(HTMLNames::inputTag)) { |
| 231 HTMLInputElement* inputElement = toHTMLInputElement(element); | 326 HTMLInputElement* inputElement = toHTMLInputElement(element); |
| 232 if (inputElement->isImageButton() && inputElement->hasImageLoader())
{ | 327 if (inputElement->isImageButton() && inputElement->hasImageLoader())
{ |
| 233 KURL url = inputElement->src(); | 328 KURL url = inputElement->src(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 String mimeType = resource->response().mimeType(); | 399 String mimeType = resource->response().mimeType(); |
| 305 m_resources->append(SerializedResource(url, mimeType, data)); | 400 m_resources->append(SerializedResource(url, mimeType, data)); |
| 306 m_resourceURLs.add(url); | 401 m_resourceURLs.add(url); |
| 307 } | 402 } |
| 308 | 403 |
| 309 void PageSerializer::addImageToResources(ImageResource* image, RenderObject* ima
geRenderer, const KURL& url) | 404 void PageSerializer::addImageToResources(ImageResource* image, RenderObject* ima
geRenderer, const KURL& url) |
| 310 { | 405 { |
| 311 if (!shouldAddURL(url)) | 406 if (!shouldAddURL(url)) |
| 312 return; | 407 return; |
| 313 | 408 |
| 314 if (!image || image->image() == Image::nullImage()) | 409 if (!image || !image->hasImage() || image->image() == Image::nullImage()) |
| 315 return; | 410 return; |
| 316 | 411 |
| 317 RefPtr<SharedBuffer> data = imageRenderer ? image->imageForRenderer(imageRen
derer)->data() : 0; | 412 RefPtr<SharedBuffer> data = imageRenderer ? image->imageForRenderer(imageRen
derer)->data() : 0; |
| 318 if (!data) | 413 if (!data) |
| 319 data = image->image()->data(); | 414 data = image->image()->data(); |
| 320 | 415 |
| 321 addToResources(image, data, url); | 416 addToResources(image, data, url); |
| 322 } | 417 } |
| 323 | 418 |
| 324 void PageSerializer::addFontToResources(FontResource* font) | 419 void PageSerializer::addFontToResources(FontResource* font) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 if (iter != m_blankFrameURLs.end()) | 471 if (iter != m_blankFrameURLs.end()) |
| 377 return iter->value; | 472 return iter->value; |
| 378 String url = "wyciwyg://frame/" + String::number(m_blankFrameCounter++); | 473 String url = "wyciwyg://frame/" + String::number(m_blankFrameCounter++); |
| 379 KURL fakeURL(ParsedURLString, url); | 474 KURL fakeURL(ParsedURLString, url); |
| 380 m_blankFrameURLs.add(frame, fakeURL); | 475 m_blankFrameURLs.add(frame, fakeURL); |
| 381 | 476 |
| 382 return fakeURL; | 477 return fakeURL; |
| 383 } | 478 } |
| 384 | 479 |
| 385 } | 480 } |
| OLD | NEW |