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 14 matching lines...) Expand all Loading... | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "core/page/PageSerializer.h" | 32 #include "core/page/PageSerializer.h" |
33 | 33 |
34 #include "HTMLNames.h" | 34 #include "HTMLNames.h" |
35 #include "core/css/CSSFontFaceRule.h" | |
36 #include "core/css/CSSFontFaceSrcValue.h" | |
35 #include "core/css/CSSImageValue.h" | 37 #include "core/css/CSSImageValue.h" |
36 #include "core/css/CSSImportRule.h" | 38 #include "core/css/CSSImportRule.h" |
39 #include "core/css/CSSStyleDeclaration.h" | |
37 #include "core/css/CSSStyleRule.h" | 40 #include "core/css/CSSStyleRule.h" |
41 #include "core/css/CSSValueList.h" | |
38 #include "core/css/StylePropertySet.h" | 42 #include "core/css/StylePropertySet.h" |
39 #include "core/css/StyleRule.h" | 43 #include "core/css/StyleRule.h" |
40 #include "core/css/StyleSheetContents.h" | 44 #include "core/css/StyleSheetContents.h" |
41 #include "core/dom/Document.h" | 45 #include "core/dom/Document.h" |
42 #include "core/dom/Element.h" | 46 #include "core/dom/Element.h" |
43 #include "core/dom/Text.h" | 47 #include "core/dom/Text.h" |
44 #include "core/editing/MarkupAccumulator.h" | 48 #include "core/editing/MarkupAccumulator.h" |
49 #include "core/fetch/FontResource.h" | |
45 #include "core/fetch/ImageResource.h" | 50 #include "core/fetch/ImageResource.h" |
46 #include "core/html/HTMLFrameOwnerElement.h" | 51 #include "core/html/HTMLFrameOwnerElement.h" |
47 #include "core/html/HTMLImageElement.h" | 52 #include "core/html/HTMLImageElement.h" |
48 #include "core/html/HTMLInputElement.h" | 53 #include "core/html/HTMLInputElement.h" |
49 #include "core/html/HTMLLinkElement.h" | 54 #include "core/html/HTMLLinkElement.h" |
50 #include "core/html/HTMLStyleElement.h" | 55 #include "core/html/HTMLStyleElement.h" |
51 #include "core/html/parser/HTMLMetaCharsetParser.h" | 56 #include "core/html/parser/HTMLMetaCharsetParser.h" |
52 #include "core/page/Frame.h" | 57 #include "core/page/Frame.h" |
53 #include "core/page/Page.h" | 58 #include "core/page/Page.h" |
54 #include "core/platform/SerializedResource.h" | 59 #include "core/platform/SerializedResource.h" |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 } | 268 } |
264 Document* document = styleSheet->ownerDocument(); | 269 Document* document = styleSheet->ownerDocument(); |
265 // Some rules have resources associated with them that we need to retrie ve. | 270 // Some rules have resources associated with them that we need to retrie ve. |
266 if (rule->type() == CSSRule::IMPORT_RULE) { | 271 if (rule->type() == CSSRule::IMPORT_RULE) { |
267 CSSImportRule* importRule = static_cast<CSSImportRule*>(rule); | 272 CSSImportRule* importRule = static_cast<CSSImportRule*>(rule); |
268 KURL importURL = document->completeURL(importRule->href()); | 273 KURL importURL = document->completeURL(importRule->href()); |
269 if (m_resourceURLs.contains(importURL)) | 274 if (m_resourceURLs.contains(importURL)) |
270 continue; | 275 continue; |
271 serializeCSSStyleSheet(importRule->styleSheet(), importURL); | 276 serializeCSSStyleSheet(importRule->styleSheet(), importURL); |
272 } else if (rule->type() == CSSRule::FONT_FACE_RULE) { | 277 } else if (rule->type() == CSSRule::FONT_FACE_RULE) { |
273 // FIXME: Add support for font face rule. It is not clear to me at t his point if the actual otf/eot file can | 278 retrieveResourcesForProperties((static_cast<CSSFontFaceRule*>(rule)- >styleRule())->properties(), document); |
Julien - ping for review
2013/09/05 17:38:48
Doing direct static_cast should be avoided. You sh
| |
274 // be retrieved from the CSSFontFaceRule object. | |
275 } else if (rule->type() == CSSRule::STYLE_RULE) { | 279 } else if (rule->type() == CSSRule::STYLE_RULE) { |
276 retrieveResourcesForRule(static_cast<CSSStyleRule*>(rule)->styleRule (), document); | 280 retrieveResourcesForProperties((static_cast<CSSStyleRule*>(rule)->st yleRule())->properties(), document); |
Julien - ping for review
2013/09/05 17:38:48
I would apply the same pattern here too. I won't r
| |
277 } | 281 } |
278 } | 282 } |
279 | 283 |
280 if (url.isValid() && !m_resourceURLs.contains(url)) { | 284 if (url.isValid() && !m_resourceURLs.contains(url)) { |
281 // FIXME: We should check whether a charset has been specified and if no ne was found add one. | 285 // FIXME: We should check whether a charset has been specified and if no ne was found add one. |
282 WTF::TextEncoding textEncoding(styleSheet->contents()->charset()); | 286 WTF::TextEncoding textEncoding(styleSheet->contents()->charset()); |
283 ASSERT(textEncoding.isValid()); | 287 ASSERT(textEncoding.isValid()); |
284 String textString = cssText.toString(); | 288 String textString = cssText.toString(); |
285 CString text = textEncoding.normalizeAndEncode(textString, WTF::Entities ForUnencodables); | 289 CString text = textEncoding.normalizeAndEncode(textString, WTF::Entities ForUnencodables); |
286 m_resources->append(SerializedResource(url, String("text/css"), SharedBu ffer::create(text.data(), text.length()))); | 290 m_resources->append(SerializedResource(url, String("text/css"), SharedBu ffer::create(text.data(), text.length()))); |
287 m_resourceURLs.add(url); | 291 m_resourceURLs.add(url); |
288 } | 292 } |
289 } | 293 } |
290 | 294 |
295 bool PageSerializer::shouldAddURL(const KURL& url) | |
296 { | |
297 return url.isValid() && !m_resourceURLs.contains(url) && !url.protocolIsData (); | |
298 } | |
299 | |
300 void PageSerializer::addToResources(Resource *resource, PassRefPtr<SharedBuffer> data, const KURL& url) | |
Julien - ping for review
2013/09/05 17:38:48
Style violation, it should be: Resource* resource,
| |
301 { | |
302 if (!data) { | |
303 LOG_ERROR("No data for resource %s", url.string().utf8().data()); | |
304 return; | |
305 } | |
306 | |
307 String mimeType = resource->response().mimeType(); | |
308 m_resources->append(SerializedResource(url, mimeType, data)); | |
309 m_resourceURLs.add(url); | |
310 } | |
311 | |
291 void PageSerializer::addImageToResources(ImageResource* image, RenderObject* ima geRenderer, const KURL& url) | 312 void PageSerializer::addImageToResources(ImageResource* image, RenderObject* ima geRenderer, const KURL& url) |
292 { | 313 { |
293 if (!url.isValid() || m_resourceURLs.contains(url) || url.protocolIsData()) | 314 if (!shouldAddURL(url)) |
294 return; | 315 return; |
295 | 316 |
296 if (!image || image->image() == Image::nullImage()) | 317 if (!image || image->image() == Image::nullImage()) |
297 return; | 318 return; |
298 | 319 |
299 RefPtr<SharedBuffer> data = imageRenderer ? image->imageForRenderer(imageRen derer)->data() : 0; | 320 RefPtr<SharedBuffer> data = imageRenderer ? image->imageForRenderer(imageRen derer)->data() : 0; |
300 if (!data) | 321 if (!data) |
301 data = image->image()->data(); | 322 data = image->image()->data(); |
302 | 323 |
303 if (!data) { | 324 addToResources(image, data, url); |
304 LOG_ERROR("No data for image %s", url.string().utf8().data()); | 325 } |
326 | |
327 void PageSerializer::addFontToResources(FontResource* font) | |
328 { | |
329 if (!font || !shouldAddURL(font->url()) || !font->isLoaded() || !font->resou rceBuffer()) { | |
305 return; | 330 return; |
306 } | 331 } |
332 RefPtr<SharedBuffer> data(font->resourceBuffer()); | |
307 | 333 |
308 String mimeType = image->response().mimeType(); | 334 addToResources(font, data, font->url()); |
309 m_resources->append(SerializedResource(url, mimeType, data)); | |
310 m_resourceURLs.add(url); | |
311 } | |
312 | |
313 void PageSerializer::retrieveResourcesForRule(StyleRule* rule, Document* documen t) | |
314 { | |
315 retrieveResourcesForProperties(rule->properties(), document); | |
316 } | 335 } |
317 | 336 |
318 void PageSerializer::retrieveResourcesForProperties(const StylePropertySet* styl eDeclaration, Document* document) | 337 void PageSerializer::retrieveResourcesForProperties(const StylePropertySet* styl eDeclaration, Document* document) |
319 { | 338 { |
320 if (!styleDeclaration) | 339 if (!styleDeclaration) |
321 return; | 340 return; |
322 | 341 |
323 // The background-image and list-style-image (for ul or ol) are the CSS prop erties | 342 // The background-image and list-style-image (for ul or ol) are the CSS prop erties |
324 // that make use of images. We iterate to make sure we include any other | 343 // that make use of images. We iterate to make sure we include any other |
325 // image properties there might be. | 344 // image properties there might be. |
326 unsigned propertyCount = styleDeclaration->propertyCount(); | 345 unsigned propertyCount = styleDeclaration->propertyCount(); |
327 for (unsigned i = 0; i < propertyCount; ++i) { | 346 for (unsigned i = 0; i < propertyCount; ++i) { |
328 RefPtr<CSSValue> cssValue = styleDeclaration->propertyAt(i).value(); | 347 RefPtr<CSSValue> cssValue = styleDeclaration->propertyAt(i).value(); |
329 if (!cssValue->isImageValue()) | 348 retrieveResourcesForCSSValue(cssValue.get(), document); |
330 continue; | 349 } |
350 } | |
331 | 351 |
332 CSSImageValue* imageValue = toCSSImageValue(cssValue.get()); | 352 void PageSerializer::retrieveResourcesForCSSValue(CSSValue* cssValue, Document* document) |
353 { | |
354 if (cssValue->isImageValue()) { | |
355 CSSImageValue* imageValue = toCSSImageValue(cssValue); | |
333 StyleImage* styleImage = imageValue->cachedOrPendingImage(); | 356 StyleImage* styleImage = imageValue->cachedOrPendingImage(); |
334 // Non cached-images are just place-holders and do not contain data. | 357 // Non cached-images are just place-holders and do not contain data. |
335 if (!styleImage || !styleImage->isImageResource()) | 358 if (!styleImage || !styleImage->isImageResource()) |
336 continue; | 359 return; |
337 | 360 |
338 ImageResource* image = static_cast<StyleFetchedImage*>(styleImage)->cach edImage(); | 361 ImageResource* image = static_cast<StyleFetchedImage*>(styleImage)->cach edImage(); |
339 addImageToResources(image, 0, image->url()); | 362 addImageToResources(image, 0, image->url()); |
363 } else if (cssValue->isFontFaceSrcValue()) { | |
364 CSSFontFaceSrcValue* fontFaceSrcValue = static_cast<CSSFontFaceSrcValue* >(cssValue); | |
365 if (fontFaceSrcValue->isLocal()) { | |
366 return; | |
367 } | |
368 | |
369 addFontToResources(fontFaceSrcValue->fetch(document)); | |
370 } else if (cssValue->isValueList()) { | |
371 CSSValueList* cssValueList = static_cast<CSSValueList*>(cssValue); | |
372 for (unsigned i = 0; i < cssValueList->length(); i++) { | |
Julien - ping for review
2013/09/05 17:38:48
One line statement don't need curly braces.
| |
373 retrieveResourcesForCSSValue(cssValueList->item(i), document); | |
374 } | |
340 } | 375 } |
341 } | 376 } |
342 | 377 |
343 KURL PageSerializer::urlForBlankFrame(Frame* frame) | 378 KURL PageSerializer::urlForBlankFrame(Frame* frame) |
344 { | 379 { |
345 HashMap<Frame*, KURL>::iterator iter = m_blankFrameURLs.find(frame); | 380 HashMap<Frame*, KURL>::iterator iter = m_blankFrameURLs.find(frame); |
346 if (iter != m_blankFrameURLs.end()) | 381 if (iter != m_blankFrameURLs.end()) |
347 return iter->value; | 382 return iter->value; |
348 String url = "wyciwyg://frame/" + String::number(m_blankFrameCounter++); | 383 String url = "wyciwyg://frame/" + String::number(m_blankFrameCounter++); |
349 KURL fakeURL(ParsedURLString, url); | 384 KURL fakeURL(ParsedURLString, url); |
350 m_blankFrameURLs.add(frame, fakeURL); | 385 m_blankFrameURLs.add(frame, fakeURL); |
351 | 386 |
352 return fakeURL; | 387 return fakeURL; |
353 } | 388 } |
354 | 389 |
355 } | 390 } |
OLD | NEW |