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

Side by Side Diff: third_party/WebKit/Source/web/WebFrameSerializerImpl.cpp

Issue 1502563004: Save-Page-As-Complete-Html: Each frame links to a distinct local file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@no-url-deduping-for-frame-and-adding-save-item-id
Patch Set: Rebasing... Created 4 years, 10 months 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 79
80 #include "core/HTMLNames.h" 80 #include "core/HTMLNames.h"
81 #include "core/dom/Document.h" 81 #include "core/dom/Document.h"
82 #include "core/dom/DocumentType.h" 82 #include "core/dom/DocumentType.h"
83 #include "core/dom/Element.h" 83 #include "core/dom/Element.h"
84 #include "core/editing/serializers/Serialization.h" 84 #include "core/editing/serializers/Serialization.h"
85 #include "core/frame/FrameSerializer.h" 85 #include "core/frame/FrameSerializer.h"
86 #include "core/html/HTMLAllCollection.h" 86 #include "core/html/HTMLAllCollection.h"
87 #include "core/html/HTMLElement.h" 87 #include "core/html/HTMLElement.h"
88 #include "core/html/HTMLFormElement.h" 88 #include "core/html/HTMLFormElement.h"
89 #include "core/html/HTMLFrameOwnerElement.h"
89 #include "core/html/HTMLHtmlElement.h" 90 #include "core/html/HTMLHtmlElement.h"
90 #include "core/html/HTMLMetaElement.h" 91 #include "core/html/HTMLMetaElement.h"
91 #include "core/loader/DocumentLoader.h" 92 #include "core/loader/DocumentLoader.h"
92 #include "core/loader/FrameLoader.h" 93 #include "core/loader/FrameLoader.h"
93 #include "public/platform/WebVector.h" 94 #include "public/platform/WebVector.h"
94 #include "web/WebLocalFrameImpl.h" 95 #include "web/WebLocalFrameImpl.h"
95 #include "wtf/text/TextEncoding.h" 96 #include "wtf/text/TextEncoding.h"
96 97
97 namespace blink { 98 namespace blink {
98 99
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 268
268 CString encodedContent = param->textEncoding.encode(content, WTF::EntitiesFo rUnencodables); 269 CString encodedContent = param->textEncoding.encode(content, WTF::EntitiesFo rUnencodables);
269 270
270 // Send result to the client. 271 // Send result to the client.
271 m_client->didSerializeDataForFrame(WebCString(encodedContent), status); 272 m_client->didSerializeDataForFrame(WebCString(encodedContent), status);
272 } 273 }
273 274
274 // TODO(yosin): We should utilize |MarkupFormatter| here to share code, 275 // TODO(yosin): We should utilize |MarkupFormatter| here to share code,
275 // especially escaping attribute values, done by |WebEntities| |m_htmlEntities| 276 // especially escaping attribute values, done by |WebEntities| |m_htmlEntities|
276 // and |m_xmlEntities|. 277 // and |m_xmlEntities|.
278 void WebFrameSerializerImpl::appendAttribute(
279 StringBuilder& result,
280 bool isHTMLDocument,
281 const String& attrName,
282 const String& attrValue) {
283 result.append(' ');
284 result.append(attrName);
285 result.appendLiteral("=\"");
286 if (isHTMLDocument)
287 result.append(m_htmlEntities.convertEntitiesInString(attrValue));
288 else
289 result.append(m_xmlEntities.convertEntitiesInString(attrValue));
290 result.append('\"');
291 }
292
277 void WebFrameSerializerImpl::openTagToString( 293 void WebFrameSerializerImpl::openTagToString(
278 Element* element, 294 Element* element,
279 SerializeDomParam* param) 295 SerializeDomParam* param)
280 { 296 {
281 bool needSkip; 297 bool needSkip;
282 StringBuilder result; 298 StringBuilder result;
283 // Do pre action for open tag. 299 // Do pre action for open tag.
284 result.append(preActionBeforeSerializeOpenTag(element, param, &needSkip)); 300 result.append(preActionBeforeSerializeOpenTag(element, param, &needSkip));
285 if (needSkip) 301 if (needSkip)
286 return; 302 return;
287 // Add open tag 303 // Add open tag
288 result.append('<'); 304 result.append('<');
289 result.append(element->nodeName().lower()); 305 result.append(element->nodeName().lower());
306
307 // Find out if this element owns a frame.
308 WebFrame* frame = nullptr;
309 if (element->isFrameOwnerElement()) {
310 frame = WebFrame::fromFrame(
311 toHTMLFrameOwnerElement(element)->contentFrame());
312 }
313
290 // Go through all attributes and serialize them. 314 // Go through all attributes and serialize them.
291 AttributeCollection attributes = element->attributes(); 315 for (const auto& it : element->attributes()) {
292 AttributeCollection::iterator end = attributes.end(); 316 const QualifiedName& attrName = it.name();
293 for (AttributeCollection::iterator it = attributes.begin(); it != end; ++it) { 317 String attrValue = it.value();
294 result.append(' ');
295 // Add attribute pair
296 result.append(it->name().toString());
297 result.appendLiteral("=\"");
298 if (!it->value().isEmpty()) {
299 const String& attrValue = it->value();
300 318
301 // Check whether we need to replace some resource links 319 // Rewrite the attribute value if requested.
302 // with local resource paths. 320 if (element->hasLegalLinkAttribute(attrName)) {
303 const QualifiedName& attrName = it->name(); 321 // For links start with "javascript:", we do not change it.
304 if (element->hasLegalLinkAttribute(attrName)) { 322 if (!attrValue.startsWith("javascript:", TextCaseInsensitive)) {
305 // For links start with "javascript:", we do not change it. 323 // Get the absolute link.
306 if (attrValue.startsWith("javascript:", TextCaseInsensitive)) { 324 KURL completeURL = param->document->completeURL(attrValue);
307 result.append(m_htmlEntities.convertEntitiesInString(attrVal ue)); 325
326 // Check whether we have a local file to link to.
327 WebString rewrittenLink;
328 if (frame && m_delegate->rewriteFrameSource(frame, &rewrittenLin k)) {
329 attrValue = rewrittenLink;
330 } else if (m_delegate->rewriteLink(completeURL, &rewrittenLink)) {
331 attrValue = rewrittenLink;
308 } else { 332 } else {
309 // Get the absolute link 333 attrValue = completeURL;
310 String completeURL = param->document->completeURL(attrValue) ;
311 // Check whether we have local files for those link.
312 if (m_localLinks.contains(completeURL)) {
313 result.append(m_htmlEntities.convertEntitiesInString(m_l ocalLinks.get(completeURL)));
314 } else {
315 result.append(m_htmlEntities.convertEntitiesInString(com pleteURL));
316 }
317 } 334 }
318 } else {
319 if (param->isHTMLDocument)
320 result.append(m_htmlEntities.convertEntitiesInString(attrVal ue));
321 else
322 result.append(m_xmlEntities.convertEntitiesInString(attrValu e));
323 } 335 }
324 } 336 }
325 result.append('\"'); 337
338 appendAttribute(result, param->isHTMLDocument, attrName.toString(), attr Value);
326 } 339 }
327 340
328 // Do post action for open tag. 341 // Do post action for open tag.
329 String addedContents = postActionAfterSerializeOpenTag(element, param); 342 String addedContents = postActionAfterSerializeOpenTag(element, param);
330 // Complete the open tag for element when it has child/children. 343 // Complete the open tag for element when it has child/children.
331 if (element->hasChildren() || param->haveAddedContentsBeforeEnd) 344 if (element->hasChildren() || param->haveAddedContentsBeforeEnd)
332 result.append('>'); 345 result.append('>');
333 // Append the added contents generate in post action of open tag. 346 // Append the added contents generate in post action of open tag.
334 result.append(addedContents); 347 result.append(addedContents);
335 // Save the result to data buffer. 348 // Save the result to data buffer.
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 default: 416 default:
404 // For other type node, call default action. 417 // For other type node, call default action.
405 saveHTMLContentToBuffer(createMarkup(node), param); 418 saveHTMLContentToBuffer(createMarkup(node), param);
406 break; 419 break;
407 } 420 }
408 } 421 }
409 422
410 WebFrameSerializerImpl::WebFrameSerializerImpl( 423 WebFrameSerializerImpl::WebFrameSerializerImpl(
411 WebLocalFrame* frame, 424 WebLocalFrame* frame,
412 WebFrameSerializerClient* client, 425 WebFrameSerializerClient* client,
413 const WebVector<std::pair<WebURL, WebString>>& urlsToLocalPaths) 426 WebFrameSerializer::LinkRewritingDelegate* delegate)
414 : m_client(client) 427 : m_client(client)
428 , m_delegate(delegate)
415 , m_htmlEntities(false) 429 , m_htmlEntities(false)
416 , m_xmlEntities(true) 430 , m_xmlEntities(true)
417 { 431 {
418 // Must specify available webframe. 432 // Must specify available webframe.
419 ASSERT(frame); 433 ASSERT(frame);
420 m_specifiedWebLocalFrameImpl = toWebLocalFrameImpl(frame); 434 m_specifiedWebLocalFrameImpl = toWebLocalFrameImpl(frame);
421 // Make sure we have non 0 client. 435 // Make sure we have non null client and delegate.
422 ASSERT(client); 436 ASSERT(client);
423 // Build local resources map. 437 ASSERT(delegate);
424 for (const auto& it : urlsToLocalPaths) {
425 KURL url = it.first;
426 ASSERT(!m_localLinks.contains(url.string()));
427 m_localLinks.set(url.string(), it.second);
428 }
429 438
430 ASSERT(m_dataBuffer.isEmpty()); 439 ASSERT(m_dataBuffer.isEmpty());
431 } 440 }
432 441
433 bool WebFrameSerializerImpl::serialize() 442 bool WebFrameSerializerImpl::serialize()
434 { 443 {
435 bool didSerialization = false; 444 bool didSerialization = false;
436 445
437 Document* document = m_specifiedWebLocalFrameImpl->frame()->document(); 446 Document* document = m_specifiedWebLocalFrameImpl->frame()->document();
438 const KURL& url = document->url(); 447 const KURL& url = document->url();
(...skipping 18 matching lines...) Expand all
457 // Report empty contents for invalid URLs. 466 // Report empty contents for invalid URLs.
458 m_client->didSerializeDataForFrame( 467 m_client->didSerializeDataForFrame(
459 WebCString(), WebFrameSerializerClient::CurrentFrameIsFinished); 468 WebCString(), WebFrameSerializerClient::CurrentFrameIsFinished);
460 } 469 }
461 470
462 ASSERT(m_dataBuffer.isEmpty()); 471 ASSERT(m_dataBuffer.isEmpty());
463 return didSerialization; 472 return didSerialization;
464 } 473 }
465 474
466 } // namespace blink 475 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/web/WebFrameSerializerImpl.h ('k') | third_party/WebKit/Source/web/tests/WebFrameSerializerTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698