OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) | 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserv
ed. | 4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserv
ed. |
5 * Copyright (C) 2010 Google Inc. All rights reserved. | 5 * Copyright (C) 2010 Google Inc. All rights reserved. |
6 * | 6 * |
7 * This library is free software; you can redistribute it and/or | 7 * This library is free software; you can redistribute it and/or |
8 * modify it under the terms of the GNU Library General Public | 8 * modify it under the terms of the GNU Library General Public |
9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
10 * version 2 of the License, or (at your option) any later version. | 10 * version 2 of the License, or (at your option) any later version. |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 #include "bindings/core/v8/ScriptEventListener.h" | 26 #include "bindings/core/v8/ScriptEventListener.h" |
27 #include "core/CSSPropertyNames.h" | 27 #include "core/CSSPropertyNames.h" |
28 #include "core/HTMLNames.h" | 28 #include "core/HTMLNames.h" |
29 #include "core/MediaTypeNames.h" | 29 #include "core/MediaTypeNames.h" |
30 #include "core/css/MediaQueryMatcher.h" | 30 #include "core/css/MediaQueryMatcher.h" |
31 #include "core/css/MediaValuesDynamic.h" | 31 #include "core/css/MediaValuesDynamic.h" |
32 #include "core/css/parser/SizesAttributeParser.h" | 32 #include "core/css/parser/SizesAttributeParser.h" |
33 #include "core/dom/Attribute.h" | 33 #include "core/dom/Attribute.h" |
34 #include "core/dom/NodeTraversal.h" | 34 #include "core/dom/NodeTraversal.h" |
35 #include "core/dom/shadow/ShadowRoot.h" | |
36 #include "core/fetch/ImageResource.h" | 35 #include "core/fetch/ImageResource.h" |
37 #include "core/frame/UseCounter.h" | 36 #include "core/frame/UseCounter.h" |
38 #include "core/html/HTMLAnchorElement.h" | 37 #include "core/html/HTMLAnchorElement.h" |
39 #include "core/html/HTMLCanvasElement.h" | 38 #include "core/html/HTMLCanvasElement.h" |
40 #include "core/html/HTMLFormElement.h" | 39 #include "core/html/HTMLFormElement.h" |
41 #include "core/html/HTMLImageFallbackHelper.h" | |
42 #include "core/html/HTMLSourceElement.h" | 40 #include "core/html/HTMLSourceElement.h" |
43 #include "core/html/canvas/CanvasRenderingContext.h" | 41 #include "core/html/canvas/CanvasRenderingContext.h" |
44 #include "core/html/parser/HTMLParserIdioms.h" | 42 #include "core/html/parser/HTMLParserIdioms.h" |
45 #include "core/html/parser/HTMLSrcsetParser.h" | 43 #include "core/html/parser/HTMLSrcsetParser.h" |
46 #include "core/inspector/ConsoleMessage.h" | 44 #include "core/inspector/ConsoleMessage.h" |
47 #include "core/rendering/RenderBlockFlow.h" | |
48 #include "core/rendering/RenderImage.h" | 45 #include "core/rendering/RenderImage.h" |
49 #include "platform/ContentType.h" | 46 #include "platform/ContentType.h" |
50 #include "platform/MIMETypeRegistry.h" | 47 #include "platform/MIMETypeRegistry.h" |
51 #include "platform/RuntimeEnabledFeatures.h" | 48 #include "platform/RuntimeEnabledFeatures.h" |
52 | 49 |
53 namespace blink { | 50 namespace blink { |
54 | 51 |
55 using namespace HTMLNames; | 52 using namespace HTMLNames; |
56 | 53 |
57 class HTMLImageElement::ViewportChangeListener final : public MediaQueryListList
ener { | 54 class HTMLImageElement::ViewportChangeListener final : public MediaQueryListList
ener { |
(...skipping 22 matching lines...) Expand all Loading... |
80 RawPtrWillBeMember<HTMLImageElement> m_element; | 77 RawPtrWillBeMember<HTMLImageElement> m_element; |
81 }; | 78 }; |
82 | 79 |
83 HTMLImageElement::HTMLImageElement(Document& document, HTMLFormElement* form, bo
ol createdByParser) | 80 HTMLImageElement::HTMLImageElement(Document& document, HTMLFormElement* form, bo
ol createdByParser) |
84 : HTMLElement(imgTag, document) | 81 : HTMLElement(imgTag, document) |
85 , m_imageLoader(HTMLImageLoader::create(this)) | 82 , m_imageLoader(HTMLImageLoader::create(this)) |
86 , m_imageDevicePixelRatio(1.0f) | 83 , m_imageDevicePixelRatio(1.0f) |
87 , m_formWasSetByParser(false) | 84 , m_formWasSetByParser(false) |
88 , m_elementCreatedByParser(createdByParser) | 85 , m_elementCreatedByParser(createdByParser) |
89 , m_intrinsicSizingViewportDependant(false) | 86 , m_intrinsicSizingViewportDependant(false) |
90 , m_useFallbackContent(false) | |
91 , m_isFallbackImage(false) | |
92 { | 87 { |
93 setHasCustomStyleCallbacks(); | |
94 if (form && form->inDocument()) { | 88 if (form && form->inDocument()) { |
95 #if ENABLE(OILPAN) | 89 #if ENABLE(OILPAN) |
96 m_form = form; | 90 m_form = form; |
97 #else | 91 #else |
98 m_form = form->createWeakPtr(); | 92 m_form = form->createWeakPtr(); |
99 #endif | 93 #endif |
100 m_formWasSetByParser = true; | 94 m_formWasSetByParser = true; |
101 m_form->associate(*this); | 95 m_form->associate(*this); |
102 m_form->didAssociateByParser(); | 96 m_form->didAssociateByParser(); |
103 } | 97 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 UseCounter::count(document(), UseCounter::SrcsetWDescriptor); | 228 UseCounter::count(document(), UseCounter::SrcsetWDescriptor); |
235 } else if (!candidate.srcOrigin()) { | 229 } else if (!candidate.srcOrigin()) { |
236 UseCounter::count(document(), UseCounter::SrcsetXDescriptor); | 230 UseCounter::count(document(), UseCounter::SrcsetXDescriptor); |
237 } | 231 } |
238 if (renderer() && renderer()->isImage()) | 232 if (renderer() && renderer()->isImage()) |
239 toRenderImage(renderer())->setImageDevicePixelRatio(m_imageDevicePixelRa
tio); | 233 toRenderImage(renderer())->setImageDevicePixelRatio(m_imageDevicePixelRa
tio); |
240 } | 234 } |
241 | 235 |
242 void HTMLImageElement::parseAttribute(const QualifiedName& name, const AtomicStr
ing& value) | 236 void HTMLImageElement::parseAttribute(const QualifiedName& name, const AtomicStr
ing& value) |
243 { | 237 { |
244 if (name == altAttr || name == titleAttr) { | 238 if (name == altAttr) { |
245 if (userAgentShadowRoot()) { | 239 if (renderer() && renderer()->isImage()) |
246 Element* text = userAgentShadowRoot()->getElementById("alttext"); | 240 toRenderImage(renderer())->updateAltText(); |
247 String value = altText(); | |
248 if (text && text->textContent() != value) | |
249 text->setTextContent(altText()); | |
250 } | |
251 } else if (name == srcAttr || name == srcsetAttr || name == sizesAttr) { | 241 } else if (name == srcAttr || name == srcsetAttr || name == sizesAttr) { |
252 selectSourceURL(ImageLoader::UpdateIgnorePreviousError); | 242 selectSourceURL(ImageLoader::UpdateIgnorePreviousError); |
253 } else if (name == usemapAttr) { | 243 } else if (name == usemapAttr) { |
254 setIsLink(!value.isNull()); | 244 setIsLink(!value.isNull()); |
255 } else { | 245 } else { |
256 HTMLElement::parseAttribute(name, value); | 246 HTMLElement::parseAttribute(name, value); |
257 } | 247 } |
258 } | 248 } |
259 | 249 |
260 String HTMLImageElement::altText() const | 250 const AtomicString& HTMLImageElement::altText() const |
261 { | 251 { |
262 // lets figure out the alt text.. magic stuff | 252 // lets figure out the alt text.. magic stuff |
263 // http://www.w3.org/TR/1998/REC-html40-19980424/appendix/notes.html#altgen | 253 // http://www.w3.org/TR/1998/REC-html40-19980424/appendix/notes.html#altgen |
264 // also heavily discussed by Hixie on bugzilla | 254 // also heavily discussed by Hixie on bugzilla |
265 const AtomicString& alt = fastGetAttribute(altAttr); | 255 const AtomicString& alt = fastGetAttribute(altAttr); |
266 if (!alt.isNull()) | 256 if (!alt.isNull()) |
267 return alt; | 257 return alt; |
268 // fall back to title attribute | 258 // fall back to title attribute |
269 return fastGetAttribute(titleAttr); | 259 return fastGetAttribute(titleAttr); |
270 } | 260 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 return candidate; | 306 return candidate; |
317 } | 307 } |
318 return ImageCandidate(); | 308 return ImageCandidate(); |
319 } | 309 } |
320 | 310 |
321 RenderObject* HTMLImageElement::createRenderer(RenderStyle* style) | 311 RenderObject* HTMLImageElement::createRenderer(RenderStyle* style) |
322 { | 312 { |
323 if (style->hasContent()) | 313 if (style->hasContent()) |
324 return RenderObject::createObject(this, style); | 314 return RenderObject::createObject(this, style); |
325 | 315 |
326 if (m_useFallbackContent) | |
327 return new RenderBlockFlow(this); | |
328 | |
329 RenderImage* image = new RenderImage(this); | 316 RenderImage* image = new RenderImage(this); |
330 image->setImageResource(RenderImageResource::create()); | 317 image->setImageResource(RenderImageResource::create()); |
331 image->setImageDevicePixelRatio(m_imageDevicePixelRatio); | 318 image->setImageDevicePixelRatio(m_imageDevicePixelRatio); |
332 return image; | 319 return image; |
333 } | 320 } |
334 | 321 |
335 bool HTMLImageElement::canStartSelection() const | 322 bool HTMLImageElement::canStartSelection() const |
336 { | 323 { |
337 if (shadow()) | 324 if (shadow()) |
338 return HTMLElement::canStartSelection(); | 325 return HTMLElement::canStartSelection(); |
339 | 326 |
340 return false; | 327 return false; |
341 } | 328 } |
342 | 329 |
343 void HTMLImageElement::attach(const AttachContext& context) | 330 void HTMLImageElement::attach(const AttachContext& context) |
344 { | 331 { |
345 HTMLElement::attach(context); | 332 HTMLElement::attach(context); |
346 | 333 |
347 if (renderer() && renderer()->isImage()) { | 334 if (renderer() && renderer()->isImage()) { |
348 RenderImage* renderImage = toRenderImage(renderer()); | 335 RenderImage* renderImage = toRenderImage(renderer()); |
349 RenderImageResource* renderImageResource = renderImage->imageResource(); | 336 RenderImageResource* renderImageResource = renderImage->imageResource(); |
350 if (renderImageResource->hasImage()) | 337 if (renderImageResource->hasImage()) |
351 return; | 338 return; |
352 | 339 |
| 340 // If we have no image at all because we have no src attribute, set |
| 341 // image height and width for the alt text instead. |
353 if (!imageLoader().image() && !renderImageResource->cachedImage()) | 342 if (!imageLoader().image() && !renderImageResource->cachedImage()) |
354 return; | 343 renderImage->setImageSizeForAltText(); |
355 renderImageResource->setImageResource(imageLoader().image()); | 344 else |
| 345 renderImageResource->setImageResource(imageLoader().image()); |
| 346 |
356 } | 347 } |
357 } | 348 } |
358 | 349 |
359 Node::InsertionNotificationRequest HTMLImageElement::insertedInto(ContainerNode*
insertionPoint) | 350 Node::InsertionNotificationRequest HTMLImageElement::insertedInto(ContainerNode*
insertionPoint) |
360 { | 351 { |
361 if (!m_formWasSetByParser || NodeTraversal::highestAncestorOrSelf(*insertion
Point) != NodeTraversal::highestAncestorOrSelf(*m_form.get())) | 352 if (!m_formWasSetByParser || NodeTraversal::highestAncestorOrSelf(*insertion
Point) != NodeTraversal::highestAncestorOrSelf(*m_form.get())) |
362 resetFormOwner(); | 353 resetFormOwner(); |
363 if (m_listener) | 354 if (m_listener) |
364 document().mediaQueryMatcher().addViewportListener(m_listener); | 355 document().mediaQueryMatcher().addViewportListener(m_listener); |
365 | 356 |
366 bool imageWasModified = false; | 357 bool imageWasModified = false; |
367 if (RuntimeEnabledFeatures::pictureEnabled() && document().isActive()) { | 358 if (RuntimeEnabledFeatures::pictureEnabled() && document().isActive()) { |
368 ImageCandidate candidate = findBestFitImageFromPictureParent(); | 359 ImageCandidate candidate = findBestFitImageFromPictureParent(); |
369 if (!candidate.isEmpty()) { | 360 if (!candidate.isEmpty()) { |
370 setBestFitURLAndDPRFromImageCandidate(candidate); | 361 setBestFitURLAndDPRFromImageCandidate(candidate); |
371 imageWasModified = true; | 362 imageWasModified = true; |
372 } | 363 } |
373 } | 364 } |
374 | 365 |
375 // If we have been inserted from a renderer-less document, | 366 // If we have been inserted from a renderer-less document, |
376 // our loader may have not fetched the image, so do it now. | 367 // our loader may have not fetched the image, so do it now. |
377 if ((insertionPoint->inDocument() && !imageLoader().image()) || imageWasModi
fied) | 368 if ((insertionPoint->inDocument() && !imageLoader().image()) || imageWasModi
fied) |
378 imageLoader().updateFromElement(ImageLoader::UpdateNormal); | 369 imageLoader().updateFromElement(ImageLoader::UpdateNormal, m_elementCrea
tedByParser ? ImageLoader::ForceLoadImmediately : ImageLoader::LoadNormally); |
379 | 370 |
380 return HTMLElement::insertedInto(insertionPoint); | 371 return HTMLElement::insertedInto(insertionPoint); |
381 } | 372 } |
382 | 373 |
383 void HTMLImageElement::removedFrom(ContainerNode* insertionPoint) | 374 void HTMLImageElement::removedFrom(ContainerNode* insertionPoint) |
384 { | 375 { |
385 if (!m_form || NodeTraversal::highestAncestorOrSelf(*m_form.get()) != NodeTr
aversal::highestAncestorOrSelf(*this)) | 376 if (!m_form || NodeTraversal::highestAncestorOrSelf(*m_form.get()) != NodeTr
aversal::highestAncestorOrSelf(*this)) |
386 resetFormOwner(); | 377 resetFormOwner(); |
387 if (m_listener) | 378 if (m_listener) |
388 document().mediaQueryMatcher().removeViewportListener(m_listener); | 379 document().mediaQueryMatcher().removeViewportListener(m_listener); |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 effectiveSize = parser.length(); | 638 effectiveSize = parser.length(); |
648 } | 639 } |
649 ImageCandidate candidate = bestFitSourceForImageAttributes(document().de
vicePixelRatio(), effectiveSize, fastGetAttribute(srcAttr), fastGetAttribute(src
setAttr), &document()); | 640 ImageCandidate candidate = bestFitSourceForImageAttributes(document().de
vicePixelRatio(), effectiveSize, fastGetAttribute(srcAttr), fastGetAttribute(src
setAttr), &document()); |
650 setBestFitURLAndDPRFromImageCandidate(candidate); | 641 setBestFitURLAndDPRFromImageCandidate(candidate); |
651 } | 642 } |
652 if (m_intrinsicSizingViewportDependant && !m_listener) { | 643 if (m_intrinsicSizingViewportDependant && !m_listener) { |
653 m_listener = ViewportChangeListener::create(this); | 644 m_listener = ViewportChangeListener::create(this); |
654 document().mediaQueryMatcher().addViewportListener(m_listener); | 645 document().mediaQueryMatcher().addViewportListener(m_listener); |
655 } | 646 } |
656 imageLoader().updateFromElement(behavior); | 647 imageLoader().updateFromElement(behavior); |
657 | |
658 if (imageLoader().image() || (imageLoader().hasPendingActivity() && !imageSo
urceURL().isEmpty())) | |
659 ensurePrimaryContent(); | |
660 else | |
661 ensureFallbackContent(); | |
662 } | 648 } |
663 | 649 |
664 const KURL& HTMLImageElement::sourceURL() const | 650 const KURL& HTMLImageElement::sourceURL() const |
665 { | 651 { |
666 return cachedImage()->response().url(); | 652 return cachedImage()->response().url(); |
667 } | 653 } |
668 | 654 |
669 void HTMLImageElement::didAddUserAgentShadowRoot(ShadowRoot&) | |
670 { | |
671 HTMLImageFallbackHelper::createAltTextShadowTree(*this); | |
672 } | 655 } |
673 | |
674 void HTMLImageElement::ensureFallbackContent() | |
675 { | |
676 if (m_useFallbackContent || m_isFallbackImage) | |
677 return; | |
678 setUseFallbackContent(); | |
679 reattachFallbackContent(); | |
680 } | |
681 | |
682 void HTMLImageElement::ensurePrimaryContent() | |
683 { | |
684 if (!m_useFallbackContent) | |
685 return; | |
686 m_useFallbackContent = false; | |
687 reattachFallbackContent(); | |
688 } | |
689 | |
690 void HTMLImageElement::reattachFallbackContent() | |
691 { | |
692 // This can happen inside of attach() in the middle of a recalcStyle so we n
eed to | |
693 // reattach synchronously here. | |
694 if (document().inStyleRecalc()) | |
695 reattach(); | |
696 else | |
697 lazyReattachIfAttached(); | |
698 } | |
699 | |
700 PassRefPtr<RenderStyle> HTMLImageElement::customStyleForRenderer() | |
701 { | |
702 RefPtr<RenderStyle> newStyle = originalStyleForRenderer(); | |
703 | |
704 if (!m_useFallbackContent) | |
705 return newStyle; | |
706 return HTMLImageFallbackHelper::customStyleForAltText(*this, newStyle); | |
707 } | |
708 | |
709 void HTMLImageElement::setUseFallbackContent() | |
710 { | |
711 m_useFallbackContent = true; | |
712 ensureUserAgentShadowRoot(); | |
713 } | |
714 } | |
OLD | NEW |