Chromium Code Reviews| 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 19 matching lines...) Expand all Loading... | |
| 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 "core/HTMLNames.h" | 34 #include "core/HTMLNames.h" |
| 35 #include "core/InputTypeNames.h" | 35 #include "core/InputTypeNames.h" |
| 36 #include "core/css/CSSFontFaceRule.h" | 36 #include "core/css/CSSFontFaceRule.h" |
| 37 #include "core/css/CSSFontFaceSrcValue.h" | 37 #include "core/css/CSSFontFaceSrcValue.h" |
| 38 #include "core/css/CSSImageValue.h" | 38 #include "core/css/CSSImageValue.h" |
| 39 #include "core/css/CSSImportRule.h" | 39 #include "core/css/CSSImportRule.h" |
| 40 #include "core/css/CSSRuleList.h" | |
| 40 #include "core/css/CSSStyleDeclaration.h" | 41 #include "core/css/CSSStyleDeclaration.h" |
| 41 #include "core/css/CSSStyleRule.h" | 42 #include "core/css/CSSStyleRule.h" |
| 42 #include "core/css/CSSValueList.h" | 43 #include "core/css/CSSValueList.h" |
| 43 #include "core/css/StylePropertySet.h" | 44 #include "core/css/StylePropertySet.h" |
| 44 #include "core/css/StyleRule.h" | 45 #include "core/css/StyleRule.h" |
| 45 #include "core/css/StyleSheetContents.h" | 46 #include "core/css/StyleSheetContents.h" |
| 46 #include "core/dom/Document.h" | 47 #include "core/dom/Document.h" |
| 47 #include "core/dom/Element.h" | 48 #include "core/dom/Element.h" |
| 48 #include "core/dom/Text.h" | 49 #include "core/dom/Text.h" |
| 49 #include "core/editing/MarkupAccumulator.h" | 50 #include "core/editing/MarkupAccumulator.h" |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 { | 296 { |
| 296 StringBuilder cssText; | 297 StringBuilder cssText; |
| 297 for (unsigned i = 0; i < styleSheet.length(); ++i) { | 298 for (unsigned i = 0; i < styleSheet.length(); ++i) { |
| 298 CSSRule* rule = styleSheet.item(i); | 299 CSSRule* rule = styleSheet.item(i); |
| 299 String itemText = rule->cssText(); | 300 String itemText = rule->cssText(); |
| 300 if (!itemText.isEmpty()) { | 301 if (!itemText.isEmpty()) { |
| 301 cssText.append(itemText); | 302 cssText.append(itemText); |
| 302 if (i < styleSheet.length() - 1) | 303 if (i < styleSheet.length() - 1) |
| 303 cssText.appendLiteral("\n\n"); | 304 cssText.appendLiteral("\n\n"); |
| 304 } | 305 } |
| 305 ASSERT(styleSheet.ownerDocument()); | 306 |
| 306 Document& document = *styleSheet.ownerDocument(); | |
| 307 // Some rules have resources associated with them that we need to retrie ve. | 307 // Some rules have resources associated with them that we need to retrie ve. |
| 308 if (rule->type() == CSSRule::IMPORT_RULE) { | 308 serializeCSSRule(rule); |
| 309 CSSImportRule* importRule = toCSSImportRule(rule); | |
| 310 ASSERT(styleSheet.baseURL().isValid()); | |
| 311 KURL importURL = KURL(styleSheet.baseURL(), importRule->href()); | |
| 312 if (m_resourceURLs.contains(importURL)) | |
| 313 continue; | |
| 314 if (importRule->styleSheet()) | |
| 315 serializeCSSStyleSheet(*importRule->styleSheet(), importURL); | |
| 316 } else if (rule->type() == CSSRule::FONT_FACE_RULE) { | |
| 317 retrieveResourcesForProperties(&toCSSFontFaceRule(rule)->styleRule() ->properties(), document); | |
| 318 } else if (rule->type() == CSSRule::STYLE_RULE) { | |
| 319 retrieveResourcesForProperties(&toCSSStyleRule(rule)->styleRule()->p roperties(), document); | |
| 320 } | |
| 321 } | 309 } |
| 322 | 310 |
| 323 if (url.isValid() && !m_resourceURLs.contains(url)) { | 311 if (url.isValid() && !m_resourceURLs.contains(url)) { |
| 324 // FIXME: We should check whether a charset has been specified and if no ne was found add one. | 312 // FIXME: We should check whether a charset has been specified and if no ne was found add one. |
| 325 WTF::TextEncoding textEncoding(styleSheet.contents()->charset()); | 313 WTF::TextEncoding textEncoding(styleSheet.contents()->charset()); |
| 326 ASSERT(textEncoding.isValid()); | 314 ASSERT(textEncoding.isValid()); |
| 327 String textString = cssText.toString(); | 315 String textString = cssText.toString(); |
| 328 CString text = textEncoding.normalizeAndEncode(textString, WTF::Entities ForUnencodables); | 316 CString text = textEncoding.normalizeAndEncode(textString, WTF::Entities ForUnencodables); |
| 329 m_resources->append(SerializedResource(url, String("text/css"), SharedBu ffer::create(text.data(), text.length()))); | 317 m_resources->append(SerializedResource(url, String("text/css"), SharedBu ffer::create(text.data(), text.length()))); |
| 330 m_resourceURLs.add(url); | 318 m_resourceURLs.add(url); |
| 331 } | 319 } |
| 332 } | 320 } |
| 333 | 321 |
| 322 void PageSerializer::serializeCSSRule(CSSRule* rule) | |
| 323 { | |
| 324 ASSERT(rule->parentStyleSheet()->ownerDocument()); | |
| 325 Document& document = *rule->parentStyleSheet()->ownerDocument(); | |
| 326 | |
| 327 switch (rule->type()) { | |
| 328 case CSSRule::STYLE_RULE: | |
| 329 retrieveResourcesForProperties(&toCSSStyleRule(rule)->styleRule()->prope rties(), document); | |
| 330 break; | |
| 331 | |
| 332 case CSSRule::IMPORT_RULE: { | |
| 333 CSSImportRule* importRule = toCSSImportRule(rule); | |
| 334 KURL sheetBaseURL = rule->parentStyleSheet()->baseURL(); | |
| 335 ASSERT(sheetBaseURL.isValid()); | |
| 336 KURL importURL = KURL(sheetBaseURL, importRule->href()); | |
| 337 if (m_resourceURLs.contains(importURL)) | |
| 338 break; | |
| 339 if (importRule->styleSheet()) | |
| 340 serializeCSSStyleSheet(*importRule->styleSheet(), importURL); | |
| 341 break; | |
| 342 } | |
| 343 | |
| 344 // Rules inheriting CSSGroupingRule | |
| 345 case CSSRule::MEDIA_RULE: | |
| 346 case CSSRule::SUPPORTS_RULE: { | |
|
philipj_slow
2015/06/25 09:07:40
Good find with @supports
| |
| 347 CSSRuleList* ruleList = rule->cssRules(); | |
| 348 for (unsigned i = 0; i < ruleList->length(); ++i) | |
| 349 serializeCSSRule(ruleList->item(i)); | |
| 350 break; | |
| 351 } | |
| 352 | |
| 353 case CSSRule::FONT_FACE_RULE: | |
| 354 retrieveResourcesForProperties(&toCSSFontFaceRule(rule)->styleRule()->pr operties(), document); | |
| 355 break; | |
| 356 | |
| 357 // Rules in which no external resources can be referenced | |
| 358 case CSSRule::CHARSET_RULE: | |
| 359 case CSSRule::PAGE_RULE: | |
| 360 case CSSRule::KEYFRAMES_RULE: | |
| 361 case CSSRule::KEYFRAME_RULE: | |
| 362 case CSSRule::VIEWPORT_RULE: | |
| 363 break; | |
|
philipj_slow
2015/06/25 09:07:40
I guess also an ASSERT_NOT_REACH in a default bran
| |
| 364 } | |
| 365 } | |
| 366 | |
| 334 bool PageSerializer::shouldAddURL(const KURL& url) | 367 bool PageSerializer::shouldAddURL(const KURL& url) |
| 335 { | 368 { |
| 336 return url.isValid() && !m_resourceURLs.contains(url) && !url.protocolIsData (); | 369 return url.isValid() && !m_resourceURLs.contains(url) && !url.protocolIsData (); |
| 337 } | 370 } |
| 338 | 371 |
| 339 void PageSerializer::addToResources(Resource* resource, PassRefPtr<SharedBuffer> data, const KURL& url) | 372 void PageSerializer::addToResources(Resource* resource, PassRefPtr<SharedBuffer> data, const KURL& url) |
| 340 { | 373 { |
| 341 if (!data) { | 374 if (!data) { |
| 342 WTF_LOG_ERROR("No data for resource %s", url.string().utf8().data()); | 375 WTF_LOG_ERROR("No data for resource %s", url.string().utf8().data()); |
| 343 return; | 376 return; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 423 | 456 |
| 424 return fakeURL; | 457 return fakeURL; |
| 425 } | 458 } |
| 426 | 459 |
| 427 PageSerializer::Delegate* PageSerializer::delegate() | 460 PageSerializer::Delegate* PageSerializer::delegate() |
| 428 { | 461 { |
| 429 return m_delegate.get(); | 462 return m_delegate.get(); |
| 430 } | 463 } |
| 431 | 464 |
| 432 } // namespace blink | 465 } // namespace blink |
| OLD | NEW |