Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 79 #include "web/WebPageSerializerImpl.h" | 79 #include "web/WebPageSerializerImpl.h" |
| 80 | 80 |
| 81 #include "core/HTMLNames.h" | 81 #include "core/HTMLNames.h" |
| 82 #include "core/dom/Document.h" | 82 #include "core/dom/Document.h" |
| 83 #include "core/dom/DocumentType.h" | 83 #include "core/dom/DocumentType.h" |
| 84 #include "core/dom/Element.h" | 84 #include "core/dom/Element.h" |
| 85 #include "core/editing/serializers/Serialization.h" | 85 #include "core/editing/serializers/Serialization.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 "core/page/PageSerializer.h" | 94 #include "core/page/PageSerializer.h" |
| 94 #include "public/platform/WebVector.h" | 95 #include "public/platform/WebVector.h" |
| 95 #include "web/WebLocalFrameImpl.h" | 96 #include "web/WebLocalFrameImpl.h" |
| 96 #include "wtf/text/TextEncoding.h" | 97 #include "wtf/text/TextEncoding.h" |
| 97 | 98 |
| 98 namespace blink { | 99 namespace blink { |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 | 273 |
| 273 CString encodedContent = param->textEncoding.encode(content, WTF::EntitiesFo rUnencodables); | 274 CString encodedContent = param->textEncoding.encode(content, WTF::EntitiesFo rUnencodables); |
| 274 | 275 |
| 275 // Send result to the client. | 276 // Send result to the client. |
| 276 m_client->didSerializeDataForFrame(WebCString(encodedContent), status); | 277 m_client->didSerializeDataForFrame(WebCString(encodedContent), status); |
| 277 } | 278 } |
| 278 | 279 |
| 279 // TODO(yosin): We should utilize |MarkupFormatter| here to share code, | 280 // TODO(yosin): We should utilize |MarkupFormatter| here to share code, |
| 280 // especially escaping attribute values, done by |WebEntities| |m_htmlEntities| | 281 // especially escaping attribute values, done by |WebEntities| |m_htmlEntities| |
| 281 // and |m_xmlEntities|. | 282 // and |m_xmlEntities|. |
| 283 void WebPageSerializerImpl::appendAttribute( | |
| 284 StringBuilder& result, | |
| 285 bool isHTMLDocument, | |
| 286 const String& attrName, | |
| 287 const String& attrValue) { | |
| 288 result.append(' '); | |
| 289 result.append(attrName); | |
| 290 result.appendLiteral("=\""); | |
| 291 if (isHTMLDocument) | |
| 292 result.append(m_htmlEntities.convertEntitiesInString(attrValue)); | |
| 293 else | |
| 294 result.append(m_xmlEntities.convertEntitiesInString(attrValue)); | |
| 295 result.append('\"'); | |
| 296 } | |
| 297 | |
| 282 void WebPageSerializerImpl::openTagToString(Element* element, | 298 void WebPageSerializerImpl::openTagToString(Element* element, |
| 283 SerializeDomParam* param) | 299 SerializeDomParam* param) |
| 284 { | 300 { |
| 285 bool needSkip; | 301 bool needSkip; |
| 286 StringBuilder result; | 302 StringBuilder result; |
| 287 // Do pre action for open tag. | 303 // Do pre action for open tag. |
| 288 result.append(preActionBeforeSerializeOpenTag(element, param, &needSkip)); | 304 result.append(preActionBeforeSerializeOpenTag(element, param, &needSkip)); |
| 289 if (needSkip) | 305 if (needSkip) |
| 290 return; | 306 return; |
| 291 // Add open tag | 307 // Add open tag |
| 292 result.append('<'); | 308 result.append('<'); |
| 293 result.append(element->nodeName().lower()); | 309 result.append(element->nodeName().lower()); |
| 310 | |
| 311 // Find out if this element owns a frame. | |
| 312 Frame* frame = nullptr; | |
| 313 if (element->isFrameOwnerElement()) { | |
| 314 frame = toHTMLFrameOwnerElement(element)->contentFrame(); | |
|
Łukasz Anforowicz
2015/12/16 21:39:02
There is some risk that this toHTMLFrameOwnerEleme
| |
| 315 } | |
| 316 | |
| 294 // Go through all attributes and serialize them. | 317 // Go through all attributes and serialize them. |
| 295 AttributeCollection attributes = element->attributes(); | 318 for (const auto& it : element->attributes()) { |
| 296 AttributeCollection::iterator end = attributes.end(); | 319 const QualifiedName& attrName = it.name(); |
| 297 for (AttributeCollection::iterator it = attributes.begin(); it != end; ++it) { | 320 String attrValue = it.value(); |
| 298 result.append(' '); | |
| 299 // Add attribute pair | |
| 300 result.append(it->name().toString()); | |
| 301 result.appendLiteral("=\""); | |
| 302 if (!it->value().isEmpty()) { | |
| 303 const String& attrValue = it->value(); | |
| 304 | 321 |
| 305 // Check whether we need to replace some resource links | 322 // Rewrite the attribute value if requested. |
| 306 // with local resource paths. | 323 if (element->hasLegalLinkAttribute(attrName)) { |
| 307 const QualifiedName& attrName = it->name(); | 324 // For links start with "javascript:", we do not change it. |
| 308 if (element->hasLegalLinkAttribute(attrName)) { | 325 if (!attrValue.startsWith("javascript:", TextCaseInsensitive)) { |
| 309 // For links start with "javascript:", we do not change it. | 326 // Get the absolute link. |
| 310 if (attrValue.startsWith("javascript:", TextCaseInsensitive)) { | 327 String completeURL = param->document->completeURL(attrValue); |
| 311 result.append(m_htmlEntities.convertEntitiesInString(attrVal ue)); | 328 |
| 329 // Check whether we have a local file to link to. | |
| 330 if (frame && m_frameToLocalPath.contains(frame)) { | |
| 331 attrValue = m_frameToLocalPath.get(frame); | |
| 332 } else if (m_urlToLocalPath.contains(completeURL)) { | |
| 333 attrValue = m_urlToLocalPath.get(completeURL); | |
| 312 } else { | 334 } else { |
| 313 // Get the absolute link | 335 attrValue = completeURL; |
| 314 String completeURL = param->document->completeURL(attrValue) ; | |
| 315 // Check whether we have local files for those link. | |
| 316 if (m_localLinks.contains(completeURL)) { | |
| 317 result.append(m_htmlEntities.convertEntitiesInString(m_l ocalLinks.get(completeURL))); | |
| 318 } else { | |
| 319 result.append(m_htmlEntities.convertEntitiesInString(com pleteURL)); | |
| 320 } | |
| 321 } | 336 } |
| 322 } else { | |
| 323 if (param->isHTMLDocument) | |
| 324 result.append(m_htmlEntities.convertEntitiesInString(attrVal ue)); | |
| 325 else | |
| 326 result.append(m_xmlEntities.convertEntitiesInString(attrValu e)); | |
| 327 } | 337 } |
| 328 } | 338 } |
| 329 result.append('\"'); | 339 |
| 340 appendAttribute(result, param->isHTMLDocument, attrName.toString(), attr Value); | |
| 330 } | 341 } |
| 331 | 342 |
| 332 // Do post action for open tag. | 343 // Do post action for open tag. |
| 333 String addedContents = postActionAfterSerializeOpenTag(element, param); | 344 String addedContents = postActionAfterSerializeOpenTag(element, param); |
| 334 // Complete the open tag for element when it has child/children. | 345 // Complete the open tag for element when it has child/children. |
| 335 if (element->hasChildren() || param->haveAddedContentsBeforeEnd) | 346 if (element->hasChildren() || param->haveAddedContentsBeforeEnd) |
| 336 result.append('>'); | 347 result.append('>'); |
| 337 // Append the added contents generate in post action of open tag. | 348 // Append the added contents generate in post action of open tag. |
| 338 result.append(addedContents); | 349 result.append(addedContents); |
| 339 // Save the result to data buffer. | 350 // Save the result to data buffer. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 default: | 416 default: |
| 406 // For other type node, call default action. | 417 // For other type node, call default action. |
| 407 saveHTMLContentToBuffer(createMarkup(node), param); | 418 saveHTMLContentToBuffer(createMarkup(node), param); |
| 408 break; | 419 break; |
| 409 } | 420 } |
| 410 } | 421 } |
| 411 | 422 |
| 412 WebPageSerializerImpl::WebPageSerializerImpl( | 423 WebPageSerializerImpl::WebPageSerializerImpl( |
| 413 WebLocalFrame* frame, | 424 WebLocalFrame* frame, |
| 414 WebPageSerializerClient* client, | 425 WebPageSerializerClient* client, |
| 415 const WebVector<std::pair<WebURL, WebString>>& urlsToLocalPaths) | 426 const WebVector<std::pair<WebURL, WebString>>& urlsToLocalPaths, |
| 427 const WebVector<std::pair<WebFrame*, WebString>>& framesToLocalPaths) | |
| 416 : m_client(client) | 428 : m_client(client) |
| 417 , m_htmlEntities(false) | 429 , m_htmlEntities(false) |
| 418 , m_xmlEntities(true) | 430 , m_xmlEntities(true) |
| 419 { | 431 { |
| 420 // Must specify available webframe. | 432 // Must specify available webframe. |
| 421 ASSERT(frame); | 433 ASSERT(frame); |
| 422 m_specifiedWebLocalFrameImpl = toWebLocalFrameImpl(frame); | 434 m_specifiedWebLocalFrameImpl = toWebLocalFrameImpl(frame); |
| 423 // Make sure we have non 0 client. | 435 // Make sure we have non 0 client. |
| 424 ASSERT(client); | 436 ASSERT(client); |
| 425 // Build local resources map. | 437 // Build url to local path map. |
| 426 for (const auto& it : urlsToLocalPaths) { | 438 for (const auto& it : urlsToLocalPaths) { |
| 427 KURL url = it.first; | 439 KURL url = it.first; |
| 428 ASSERT(!m_localLinks.contains(url.string())); | 440 ASSERT(!m_urlToLocalPath.contains(url.string())); |
| 429 m_localLinks.set(url.string(), it.second); | 441 m_urlToLocalPath.set(url.string(), it.second); |
| 442 } | |
| 443 // Build frame to local path map. | |
| 444 for (const auto& it : framesToLocalPaths) { | |
| 445 WebFrame* webFrame = it.first; | |
| 446 ASSERT(webFrame); | |
| 447 Frame* frame = webFrame->toImplBase()->frame(); | |
| 448 ASSERT(!m_frameToLocalPath.contains(frame)); | |
| 449 m_frameToLocalPath.set(frame, it.second); | |
| 430 } | 450 } |
| 431 | 451 |
| 432 ASSERT(m_dataBuffer.isEmpty()); | 452 ASSERT(m_dataBuffer.isEmpty()); |
| 433 } | 453 } |
| 434 | 454 |
| 435 bool WebPageSerializerImpl::serialize() | 455 bool WebPageSerializerImpl::serialize() |
| 436 { | 456 { |
| 437 bool didSerialization = false; | 457 bool didSerialization = false; |
| 438 | 458 |
| 439 Document* document = m_specifiedWebLocalFrameImpl->frame()->document(); | 459 Document* document = m_specifiedWebLocalFrameImpl->frame()->document(); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 459 // Report empty contents for invalid URLs. | 479 // Report empty contents for invalid URLs. |
| 460 m_client->didSerializeDataForFrame( | 480 m_client->didSerializeDataForFrame( |
| 461 WebCString(), WebPageSerializerClient::CurrentFrameIsFinished); | 481 WebCString(), WebPageSerializerClient::CurrentFrameIsFinished); |
| 462 } | 482 } |
| 463 | 483 |
| 464 ASSERT(m_dataBuffer.isEmpty()); | 484 ASSERT(m_dataBuffer.isEmpty()); |
| 465 return didSerialization; | 485 return didSerialization; |
| 466 } | 486 } |
| 467 | 487 |
| 468 } // namespace blink | 488 } // namespace blink |
| OLD | NEW |