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

Side by Side Diff: Source/core/html/forms/ImageInputType.cpp

Issue 481753002: Use Shadow DOM to display fallback content for images (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Updated Created 6 years 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All r ights reserved. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All r ights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Copyright (C) 2012 Samsung Electronics. All rights reserved. 4 * Copyright (C) 2012 Samsung Electronics. All rights reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
11 * This library is distributed in the hope that it will be useful, 11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details. 14 * Library General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU Library General Public License 16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to 17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA. 19 * Boston, MA 02110-1301, USA.
20 * 20 *
21 */ 21 */
22 22
23 #include "config.h" 23 #include "config.h"
24 #include "core/html/forms/ImageInputType.h" 24 #include "core/html/forms/ImageInputType.h"
25 25
26 #include "core/HTMLNames.h" 26 #include "core/HTMLNames.h"
27 #include "core/InputTypeNames.h" 27 #include "core/InputTypeNames.h"
28 #include "core/dom/shadow/ShadowRoot.h"
28 #include "core/events/MouseEvent.h" 29 #include "core/events/MouseEvent.h"
29 #include "core/fetch/ImageResource.h" 30 #include "core/fetch/ImageResource.h"
30 #include "core/html/FormDataList.h" 31 #include "core/html/FormDataList.h"
31 #include "core/html/HTMLFormElement.h" 32 #include "core/html/HTMLFormElement.h"
33 #include "core/html/HTMLImageFallbackHelper.h"
32 #include "core/html/HTMLImageLoader.h" 34 #include "core/html/HTMLImageLoader.h"
33 #include "core/html/HTMLInputElement.h" 35 #include "core/html/HTMLInputElement.h"
34 #include "core/html/parser/HTMLParserIdioms.h" 36 #include "core/html/parser/HTMLParserIdioms.h"
37 #include "core/rendering/RenderBlockFlow.h"
35 #include "core/rendering/RenderImage.h" 38 #include "core/rendering/RenderImage.h"
36 #include "wtf/PassOwnPtr.h" 39 #include "wtf/PassOwnPtr.h"
37 #include "wtf/text/StringBuilder.h" 40 #include "wtf/text/StringBuilder.h"
38 41
39 namespace blink { 42 namespace blink {
40 43
41 using namespace HTMLNames; 44 using namespace HTMLNames;
42 45
43 inline ImageInputType::ImageInputType(HTMLInputElement& element) 46 inline ImageInputType::ImageInputType(HTMLInputElement& element)
44 : BaseButtonInputType(element) 47 : BaseButtonInputType(element)
48 , m_useFallbackContent(false)
45 { 49 {
46 } 50 }
47 51
48 PassRefPtrWillBeRawPtr<InputType> ImageInputType::create(HTMLInputElement& eleme nt) 52 PassRefPtrWillBeRawPtr<InputType> ImageInputType::create(HTMLInputElement& eleme nt)
49 { 53 {
50 return adoptRefWillBeNoop(new ImageInputType(element)); 54 return adoptRefWillBeNoop(new ImageInputType(element));
51 } 55 }
52 56
53 const AtomicString& ImageInputType::formControlType() const 57 const AtomicString& ImageInputType::formControlType() const
54 { 58 {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 RefPtrWillBeRawPtr<HTMLInputElement> element(this->element()); 114 RefPtrWillBeRawPtr<HTMLInputElement> element(this->element());
111 if (element->isDisabledFormControl() || !element->form()) 115 if (element->isDisabledFormControl() || !element->form())
112 return; 116 return;
113 element->setActivatedSubmit(true); 117 element->setActivatedSubmit(true);
114 m_clickLocation = extractClickLocation(event); 118 m_clickLocation = extractClickLocation(event);
115 element->form()->prepareForSubmission(event); // Event handlers can run. 119 element->form()->prepareForSubmission(event); // Event handlers can run.
116 element->setActivatedSubmit(false); 120 element->setActivatedSubmit(false);
117 event->setDefaultHandled(); 121 event->setDefaultHandled();
118 } 122 }
119 123
120 RenderObject* ImageInputType::createRenderer(RenderStyle*) const 124 RenderObject* ImageInputType::createRenderer(RenderStyle* style) const
121 { 125 {
126 if (m_useFallbackContent)
127 return new RenderBlockFlow(&element());
122 RenderImage* image = new RenderImage(&element()); 128 RenderImage* image = new RenderImage(&element());
123 image->setImageResource(RenderImageResource::create()); 129 image->setImageResource(RenderImageResource::create());
124 return image; 130 return image;
125 } 131 }
126 132
127 void ImageInputType::altAttributeChanged() 133 void ImageInputType::altAttributeChanged()
128 { 134 {
129 RenderImage* image = toRenderImage(element().renderer()); 135 if (element().userAgentShadowRoot()) {
130 if (!image) 136 Element* text = element().userAgentShadowRoot()->getElementById("alttext ");
131 return; 137 String value = element().altText();
132 image->updateAltText(); 138 if (text && text->textContent() != value)
139 text->setTextContent(element().altText());
140 }
133 } 141 }
134 142
135 void ImageInputType::srcAttributeChanged() 143 void ImageInputType::srcAttributeChanged()
136 { 144 {
137 if (!element().renderer()) 145 if (!element().renderer())
138 return; 146 return;
139 element().ensureImageLoader().updateFromElement(ImageLoader::UpdateIgnorePre viousError); 147 element().ensureImageLoader().updateFromElement(ImageLoader::UpdateIgnorePre viousError);
140 } 148 }
141 149
150 void ImageInputType::valueAttributeChanged()
151 {
152 if (m_useFallbackContent)
153 return;
154 BaseButtonInputType::valueAttributeChanged();
155 }
156
142 void ImageInputType::startResourceLoading() 157 void ImageInputType::startResourceLoading()
143 { 158 {
144 BaseButtonInputType::startResourceLoading(); 159 BaseButtonInputType::startResourceLoading();
145 160
146 HTMLImageLoader& imageLoader = element().ensureImageLoader(); 161 HTMLImageLoader& imageLoader = element().ensureImageLoader();
147 imageLoader.updateFromElement(); 162 imageLoader.updateFromElement();
148 163
149 RenderImage* renderer = toRenderImage(element().renderer()); 164 RenderObject* renderer = element().renderer();
150 if (!renderer) 165 if (!renderer || !renderer->isRenderImage())
151 return; 166 return;
152 167
153 RenderImageResource* imageResource = renderer->imageResource(); 168 RenderImageResource* imageResource = toRenderImage(renderer)->imageResource( );
154 imageResource->setImageResource(imageLoader.image()); 169 imageResource->setImageResource(imageLoader.image());
155
156 // If we have no image at all because we have no src attribute, set
157 // image height and width for the alt text instead.
158 if (!imageLoader.image() && !imageResource->cachedImage())
159 renderer->setImageSizeForAltText();
160 } 170 }
161 171
162 bool ImageInputType::shouldRespectAlignAttribute() 172 bool ImageInputType::shouldRespectAlignAttribute()
163 { 173 {
164 return true; 174 return true;
165 } 175 }
166 176
167 bool ImageInputType::canBeSuccessfulSubmitButton() 177 bool ImageInputType::canBeSuccessfulSubmitButton()
168 { 178 {
169 return true; 179 return true;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 bool ImageInputType::hasLegalLinkAttribute(const QualifiedName& name) const 236 bool ImageInputType::hasLegalLinkAttribute(const QualifiedName& name) const
227 { 237 {
228 return name == srcAttr || BaseButtonInputType::hasLegalLinkAttribute(name); 238 return name == srcAttr || BaseButtonInputType::hasLegalLinkAttribute(name);
229 } 239 }
230 240
231 const QualifiedName& ImageInputType::subResourceAttributeName() const 241 const QualifiedName& ImageInputType::subResourceAttributeName() const
232 { 242 {
233 return srcAttr; 243 return srcAttr;
234 } 244 }
235 245
246 void ImageInputType::ensureFallbackContent()
247 {
248 if (m_useFallbackContent)
249 return;
250 setUseFallbackContent();
251 reattachFallbackContent();
252 }
253
254 void ImageInputType::setUseFallbackContent()
255 {
256 if (m_useFallbackContent)
257 return;
258 m_useFallbackContent = true;
259 if (element().document().inStyleRecalc())
260 return;
261 if (ShadowRoot* root = element().userAgentShadowRoot())
262 root->removeChildren();
263 createShadowSubtree();
264 }
265
266 void ImageInputType::ensurePrimaryContent()
267 {
268 if (!m_useFallbackContent)
269 return;
270 m_useFallbackContent = false;
271 reattachFallbackContent();
272 }
273
274 void ImageInputType::reattachFallbackContent()
275 {
276 // This can happen inside of attach() in the middle of a recalcStyle so we n eed to
277 // reattach synchronously here.
278 if (element().document().inStyleRecalc())
279 element().reattach();
280 else
281 element().lazyReattachIfAttached();
282 }
283
284 void ImageInputType::createShadowSubtree()
285 {
286 if (!m_useFallbackContent) {
287 BaseButtonInputType::createShadowSubtree();
288 return;
289 }
290 HTMLImageFallbackHelper::createAltTextShadowTree(element());
291 }
292
293 PassRefPtr<RenderStyle> ImageInputType::customStyleForRenderer(PassRefPtr<Render Style> newStyle)
294 {
295 if (!m_useFallbackContent)
296 return newStyle;
297
298 return HTMLImageFallbackHelper::customStyleForAltText(element(), newStyle);
299 }
300
236 } // namespace blink 301 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698