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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 | 75 |
| 76 static bool shouldIgnoreElement(const Element& element) | 76 static bool shouldIgnoreElement(const Element& element) |
| 77 { | 77 { |
| 78 if (isHTMLScriptElement(element)) | 78 if (isHTMLScriptElement(element)) |
| 79 return true; | 79 return true; |
| 80 if (isHTMLNoScriptElement(element)) | 80 if (isHTMLNoScriptElement(element)) |
| 81 return true; | 81 return true; |
| 82 return isHTMLMetaElement(element) && toHTMLMetaElement(element).computeEncod ing().isValid(); | 82 return isHTMLMetaElement(element) && toHTMLMetaElement(element).computeEncod ing().isValid(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 bool PageSerializer::Delegate::shouldIgnoreAttribute(const Attribute&) | |
| 86 { | |
| 87 return false; | |
| 88 } | |
| 89 | |
| 90 bool PageSerializer::Delegate::rewriteLink(const Element&, String& rewrittenLink ) | |
| 91 { | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 bool PageSerializer::Delegate::shouldSkipResource(const KURL&) | |
| 96 { | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 85 class SerializerMarkupAccumulator : public MarkupAccumulator { | 100 class SerializerMarkupAccumulator : public MarkupAccumulator { |
| 86 STACK_ALLOCATED(); | 101 STACK_ALLOCATED(); |
| 87 public: | 102 public: |
| 88 SerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVect or<RawPtrWillBeMember<Node>>&); | 103 SerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVect or<RawPtrWillBeMember<Node>>&); |
| 89 ~SerializerMarkupAccumulator() override; | 104 ~SerializerMarkupAccumulator() override; |
| 90 | 105 |
| 91 protected: | 106 protected: |
| 92 void appendText(StringBuilder& out, Text&) override; | 107 void appendText(StringBuilder& out, Text&) override; |
| 93 bool shouldIgnoreAttribute(const Attribute&) override; | 108 bool shouldIgnoreAttribute(const Attribute&) override; |
| 94 void appendElement(StringBuilder& out, Element&, Namespaces*) override; | 109 void appendElement(StringBuilder& out, Element&, Namespaces*) override; |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 if (inputElement.type() == InputTypeNames::image && inputElement.ima geLoader()) { | 306 if (inputElement.type() == InputTypeNames::image && inputElement.ima geLoader()) { |
| 292 KURL url = inputElement.src(); | 307 KURL url = inputElement.src(); |
| 293 ImageResource* cachedImage = inputElement.imageLoader()->image() ; | 308 ImageResource* cachedImage = inputElement.imageLoader()->image() ; |
| 294 addImageToResources(cachedImage, url); | 309 addImageToResources(cachedImage, url); |
| 295 } | 310 } |
| 296 } else if (isHTMLLinkElement(element)) { | 311 } else if (isHTMLLinkElement(element)) { |
| 297 HTMLLinkElement& linkElement = toHTMLLinkElement(element); | 312 HTMLLinkElement& linkElement = toHTMLLinkElement(element); |
| 298 if (CSSStyleSheet* sheet = linkElement.sheet()) { | 313 if (CSSStyleSheet* sheet = linkElement.sheet()) { |
| 299 KURL url = document.completeURL(linkElement.getAttribute(HTMLNam es::hrefAttr)); | 314 KURL url = document.completeURL(linkElement.getAttribute(HTMLNam es::hrefAttr)); |
| 300 serializeCSSStyleSheet(*sheet, url); | 315 serializeCSSStyleSheet(*sheet, url); |
| 301 ASSERT(m_resourceURLs.contains(url)); | |
| 302 } | 316 } |
| 303 } else if (isHTMLStyleElement(element)) { | 317 } else if (isHTMLStyleElement(element)) { |
| 304 HTMLStyleElement& styleElement = toHTMLStyleElement(element); | 318 HTMLStyleElement& styleElement = toHTMLStyleElement(element); |
| 305 if (CSSStyleSheet* sheet = styleElement.sheet()) | 319 if (CSSStyleSheet* sheet = styleElement.sheet()) |
| 306 serializeCSSStyleSheet(*sheet, KURL()); | 320 serializeCSSStyleSheet(*sheet, KURL()); |
| 307 } | 321 } |
| 308 } | 322 } |
| 309 } | 323 } |
| 310 | 324 |
| 311 void PageSerializer::serializeCSSStyleSheet(CSSStyleSheet& styleSheet, const KUR L& url) | 325 void PageSerializer::serializeCSSStyleSheet(CSSStyleSheet& styleSheet, const KUR L& url) |
| 312 { | 326 { |
| 313 StringBuilder cssText; | 327 StringBuilder cssText; |
| 314 cssText.appendLiteral("@charset \""); | 328 cssText.appendLiteral("@charset \""); |
| 315 cssText.append(styleSheet.contents()->charset().lower()); | 329 cssText.append(styleSheet.contents()->charset().lower()); |
| 316 cssText.appendLiteral("\";\n\n"); | 330 cssText.appendLiteral("\";\n\n"); |
| 317 | 331 |
| 318 for (unsigned i = 0; i < styleSheet.length(); ++i) { | 332 for (unsigned i = 0; i < styleSheet.length(); ++i) { |
| 319 CSSRule* rule = styleSheet.item(i); | 333 CSSRule* rule = styleSheet.item(i); |
| 320 String itemText = rule->cssText(); | 334 String itemText = rule->cssText(); |
| 321 if (!itemText.isEmpty()) { | 335 if (!itemText.isEmpty()) { |
| 322 cssText.append(itemText); | 336 cssText.append(itemText); |
| 323 if (i < styleSheet.length() - 1) | 337 if (i < styleSheet.length() - 1) |
| 324 cssText.appendLiteral("\n\n"); | 338 cssText.appendLiteral("\n\n"); |
| 325 } | 339 } |
| 326 | 340 |
| 327 // Some rules have resources associated with them that we need to retrie ve. | 341 // Some rules have resources associated with them that we need to retrie ve. |
| 328 serializeCSSRule(rule); | 342 serializeCSSRule(rule); |
| 329 } | 343 } |
| 330 | 344 |
| 331 if (url.isValid() && !m_resourceURLs.contains(url)) { | 345 if (shouldAddURL(url)) { |
| 332 WTF::TextEncoding textEncoding(styleSheet.contents()->charset()); | 346 WTF::TextEncoding textEncoding(styleSheet.contents()->charset()); |
| 333 ASSERT(textEncoding.isValid()); | 347 ASSERT(textEncoding.isValid()); |
| 334 String textString = cssText.toString(); | 348 String textString = cssText.toString(); |
| 335 CString text = textEncoding.encode(textString, WTF::EntitiesForUnencodab les); | 349 CString text = textEncoding.encode(textString, WTF::EntitiesForUnencodab les); |
| 336 m_resources->append(SerializedResource(url, String("text/css"), SharedBu ffer::create(text.data(), text.length()))); | 350 m_resources->append(SerializedResource(url, String("text/css"), SharedBu ffer::create(text.data(), text.length()))); |
| 337 m_resourceURLs.add(url); | 351 m_resourceURLs.add(url); |
| 338 } | 352 } |
| 339 } | 353 } |
| 340 | 354 |
| 341 void PageSerializer::serializeCSSRule(CSSRule* rule) | 355 void PageSerializer::serializeCSSRule(CSSRule* rule) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 381 case CSSRule::VIEWPORT_RULE: | 395 case CSSRule::VIEWPORT_RULE: |
| 382 break; | 396 break; |
| 383 | 397 |
| 384 default: | 398 default: |
| 385 ASSERT_NOT_REACHED(); | 399 ASSERT_NOT_REACHED(); |
| 386 } | 400 } |
| 387 } | 401 } |
| 388 | 402 |
| 389 bool PageSerializer::shouldAddURL(const KURL& url) | 403 bool PageSerializer::shouldAddURL(const KURL& url) |
| 390 { | 404 { |
| 391 return url.isValid() && !m_resourceURLs.contains(url) && !url.protocolIsData (); | 405 return url.isValid() && !m_resourceURLs.contains(url) && !url.protocolIsData () |
| 406 && (!delegate() || !delegate()->shouldSkipResource(url)); | |
|
dcheng
2015/12/22 01:01:45
Do we ever construct this with a null delegate? Ma
Łukasz Anforowicz
2015/12/22 21:06:46
Good point. Done.
I also made similar change for
| |
| 392 } | 407 } |
| 393 | 408 |
| 394 void PageSerializer::addToResources(Resource* resource, PassRefPtr<SharedBuffer> data, const KURL& url) | 409 void PageSerializer::addToResources(Resource* resource, PassRefPtr<SharedBuffer> data, const KURL& url) |
| 395 { | 410 { |
| 396 if (!data) { | 411 if (!data) { |
| 397 WTF_LOG_ERROR("No data for resource %s", url.string().utf8().data()); | 412 WTF_LOG_ERROR("No data for resource %s", url.string().utf8().data()); |
| 398 return; | 413 return; |
| 399 } | 414 } |
| 400 | 415 |
| 401 String mimeType = resource->response().mimeType(); | 416 String mimeType = resource->response().mimeType(); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 continue; | 501 continue; |
| 487 } | 502 } |
| 488 emitsMinus = ch == '-'; | 503 emitsMinus = ch == '-'; |
| 489 builder.append(ch); | 504 builder.append(ch); |
| 490 } | 505 } |
| 491 CString escapedUrl = builder.toString().ascii(); | 506 CString escapedUrl = builder.toString().ascii(); |
| 492 return String::format("saved from url=(%04d)%s", static_cast<int>(escapedUrl .length()), escapedUrl.data()); | 507 return String::format("saved from url=(%04d)%s", static_cast<int>(escapedUrl .length()), escapedUrl.data()); |
| 493 } | 508 } |
| 494 | 509 |
| 495 } // namespace blink | 510 } // namespace blink |
| OLD | NEW |