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

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

Issue 2624953002: Set width and height attributes for <img> if image is loaded from srcset (Closed)
Patch Set: Fix typo Created 3 years, 11 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 22 matching lines...) Expand all
33 #include "core/HTMLNames.h" 33 #include "core/HTMLNames.h"
34 #include "core/dom/Document.h" 34 #include "core/dom/Document.h"
35 #include "core/dom/Element.h" 35 #include "core/dom/Element.h"
36 #include "core/frame/Frame.h" 36 #include "core/frame/Frame.h"
37 #include "core/frame/FrameSerializer.h" 37 #include "core/frame/FrameSerializer.h"
38 #include "core/frame/LocalFrame.h" 38 #include "core/frame/LocalFrame.h"
39 #include "core/frame/RemoteFrame.h" 39 #include "core/frame/RemoteFrame.h"
40 #include "core/html/HTMLAllCollection.h" 40 #include "core/html/HTMLAllCollection.h"
41 #include "core/html/HTMLFrameElementBase.h" 41 #include "core/html/HTMLFrameElementBase.h"
42 #include "core/html/HTMLFrameOwnerElement.h" 42 #include "core/html/HTMLFrameOwnerElement.h"
43 #include "core/html/HTMLImageElement.h"
43 #include "core/html/HTMLInputElement.h" 44 #include "core/html/HTMLInputElement.h"
44 #include "core/html/HTMLTableElement.h" 45 #include "core/html/HTMLTableElement.h"
45 #include "core/loader/DocumentLoader.h" 46 #include "core/loader/DocumentLoader.h"
46 #include "platform/Histogram.h" 47 #include "platform/Histogram.h"
47 #include "platform/SerializedResource.h" 48 #include "platform/SerializedResource.h"
48 #include "platform/SharedBuffer.h" 49 #include "platform/SharedBuffer.h"
49 #include "platform/instrumentation/tracing/TraceEvent.h" 50 #include "platform/instrumentation/tracing/TraceEvent.h"
50 #include "platform/mhtml/MHTMLArchive.h" 51 #include "platform/mhtml/MHTMLArchive.h"
51 #include "platform/mhtml/MHTMLParser.h" 52 #include "platform/mhtml/MHTMLParser.h"
52 #include "platform/network/ResourceRequest.h" 53 #include "platform/network/ResourceRequest.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 WebFrameSerializer::MHTMLPartsGenerationDelegate&); 85 WebFrameSerializer::MHTMLPartsGenerationDelegate&);
85 bool shouldIgnoreElement(const Element&) override; 86 bool shouldIgnoreElement(const Element&) override;
86 bool shouldIgnoreAttribute(const Element&, const Attribute&) override; 87 bool shouldIgnoreAttribute(const Element&, const Attribute&) override;
87 bool rewriteLink(const Element&, String& rewrittenLink) override; 88 bool rewriteLink(const Element&, String& rewrittenLink) override;
88 bool shouldSkipResourceWithURL(const KURL&) override; 89 bool shouldSkipResourceWithURL(const KURL&) override;
89 bool shouldSkipResource( 90 bool shouldSkipResource(
90 FrameSerializer::ResourceHasCacheControlNoStoreHeader) override; 91 FrameSerializer::ResourceHasCacheControlNoStoreHeader) override;
91 Vector<Attribute> getCustomAttributes(const Element&) override; 92 Vector<Attribute> getCustomAttributes(const Element&) override;
92 93
93 private: 94 private:
95 void getCustomAttributesForImageElement(const HTMLImageElement&,
96 Vector<Attribute>*);
97 void getCustomAttributesForFormControlElement(const Element&,
98 Vector<Attribute>*);
99
94 WebFrameSerializer::MHTMLPartsGenerationDelegate& m_webDelegate; 100 WebFrameSerializer::MHTMLPartsGenerationDelegate& m_webDelegate;
95 }; 101 };
96 102
97 MHTMLFrameSerializerDelegate::MHTMLFrameSerializerDelegate( 103 MHTMLFrameSerializerDelegate::MHTMLFrameSerializerDelegate(
98 WebFrameSerializer::MHTMLPartsGenerationDelegate& webDelegate) 104 WebFrameSerializer::MHTMLPartsGenerationDelegate& webDelegate)
99 : m_webDelegate(webDelegate) {} 105 : m_webDelegate(webDelegate) {}
100 106
101 bool MHTMLFrameSerializerDelegate::shouldIgnoreElement(const Element& element) { 107 bool MHTMLFrameSerializerDelegate::shouldIgnoreElement(const Element& element) {
102 // Do not include elements that are are set to hidden without affecting layout 108 // Do not include elements that are are set to hidden without affecting layout
103 // by the page. For those elements that are hidden by default, they will not 109 // by the page. For those elements that are hidden by default, they will not
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 WebFrameSerializerCacheControlPolicy:: 192 WebFrameSerializerCacheControlPolicy::
187 SkipAnyFrameOrResourceMarkedNoStore && 193 SkipAnyFrameOrResourceMarkedNoStore &&
188 hasCacheControlNoStoreHeader == 194 hasCacheControlNoStoreHeader ==
189 FrameSerializer::HasCacheControlNoStoreHeader; 195 FrameSerializer::HasCacheControlNoStoreHeader;
190 } 196 }
191 197
192 Vector<Attribute> MHTMLFrameSerializerDelegate::getCustomAttributes( 198 Vector<Attribute> MHTMLFrameSerializerDelegate::getCustomAttributes(
193 const Element& element) { 199 const Element& element) {
194 Vector<Attribute> attributes; 200 Vector<Attribute> attributes;
195 201
196 // Disable all form elements in MTHML to tell the user that the form cannot be 202 if (isHTMLImageElement(element)) {
197 // worked on. MHTML is loaded in full sandboxing mode which disable the form 203 getCustomAttributesForImageElement(toHTMLImageElement(element),
198 // submission and script execution. 204 &attributes);
199 if (element.isFormControlElement() && 205 } else if (element.isFormControlElement()) {
200 !element.fastHasAttribute(HTMLNames::disabledAttr)) { 206 getCustomAttributesForFormControlElement(element, &attributes);
201 Attribute disabledAttribute(HTMLNames::disabledAttr, "");
202 attributes.push_back(disabledAttribute);
203 } 207 }
204 208
205 return attributes; 209 return attributes;
206 } 210 }
207 211
212 void MHTMLFrameSerializerDelegate::getCustomAttributesForImageElement(
213 const HTMLImageElement& element,
214 Vector<Attribute>* attributes) {
215 // Currently only the value of src is pulled into the archive and the srcset
216 // attribute is ignored (see shouldIgnoreAttribute() above). If the device
217 // has a higher DPR, a different image from srcsrt could be loaded instead.
tkent 2017/01/11 02:15:32 srcsrt -> srcset
jianli 2017/01/11 23:12:30 Done.
218 // When this occurs, we should provide the rendering width and height for
219 // <img> element if not set.
220
221 // The image should be loaded and participate the layout.
222 ImageResourceContent* image = element.cachedImage();
223 if (!image || !image->hasImage() || image->errorOccurred() ||
224 !element.layoutObject()) {
225 return;
226 }
227
228 // The width and height attributes should not be set.
229 if (element.fastHasAttribute(HTMLNames::widthAttr) ||
230 element.fastHasAttribute(HTMLNames::heightAttr)) {
231 return;
232 }
233
234 // Check if different image is loaded. naturalWidth/naturalHeight will return
235 // the image size adjusted with current DPR.
236 if (((int)element.naturalWidth()) == image->getImage()->width() &&
237 ((int)element.naturalHeight()) == image->getImage()->height()) {
238 return;
239 }
240
241 Attribute widthAttribute(HTMLNames::widthAttr,
242 AtomicString::number(element.layoutBoxWidth()));
243 attributes->push_back(widthAttribute);
244 Attribute heightAttribute(HTMLNames::heightAttr,
245 AtomicString::number(element.layoutBoxHeight()));
246 attributes->push_back(heightAttribute);
247 }
248
249 void MHTMLFrameSerializerDelegate::getCustomAttributesForFormControlElement(
250 const Element& element,
251 Vector<Attribute>* attributes) {
252 // Disable all form elements in MTHML to tell the user that the form cannot be
253 // worked on. MHTML is loaded in full sandboxing mode which disable the form
254 // submission and script execution.
255 if (element.fastHasAttribute(HTMLNames::disabledAttr))
256 return;
257 Attribute disabledAttribute(HTMLNames::disabledAttr, "");
258 attributes->push_back(disabledAttribute);
259 }
260
208 bool cacheControlNoStoreHeaderPresent( 261 bool cacheControlNoStoreHeaderPresent(
209 const WebLocalFrameImpl& webLocalFrameImpl) { 262 const WebLocalFrameImpl& webLocalFrameImpl) {
210 const ResourceResponse& response = 263 const ResourceResponse& response =
211 webLocalFrameImpl.dataSource()->response().toResourceResponse(); 264 webLocalFrameImpl.dataSource()->response().toResourceResponse();
212 if (response.cacheControlContainsNoStore()) 265 if (response.cacheControlContainsNoStore())
213 return true; 266 return true;
214 267
215 const ResourceRequest& request = 268 const ResourceRequest& request =
216 webLocalFrameImpl.dataSource()->getRequest().toResourceRequest(); 269 webLocalFrameImpl.dataSource()->getRequest().toResourceRequest();
217 return request.cacheControlContainsNoStore(); 270 return request.cacheControlContainsNoStore();
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 const WebString& baseTarget) { 416 const WebString& baseTarget) {
364 // TODO(yosin) We should call |FrameSerializer::baseTagDeclarationOf()|. 417 // TODO(yosin) We should call |FrameSerializer::baseTagDeclarationOf()|.
365 if (baseTarget.isEmpty()) 418 if (baseTarget.isEmpty())
366 return String("<base href=\".\">"); 419 return String("<base href=\".\">");
367 String baseString = "<base href=\".\" target=\"" + 420 String baseString = "<base href=\".\" target=\"" +
368 static_cast<const String&>(baseTarget) + "\">"; 421 static_cast<const String&>(baseTarget) + "\">";
369 return baseString; 422 return baseString;
370 } 423 }
371 424
372 } // namespace blink 425 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698