Chromium Code Reviews| 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 | 4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights |
| 5 * reserved. | 5 * reserved. |
| 6 * Copyright (C) 2010 Google Inc. All rights reserved. | 6 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 7 * | 7 * |
| 8 * This library is free software; you can redistribute it and/or | 8 * This library is free software; you can redistribute it and/or |
| 9 * modify it under the terms of the GNU Library General Public | 9 * modify it under the terms of the GNU Library General Public |
| 10 * License as published by the Free Software Foundation; either | 10 * License as published by the Free Software Foundation; either |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 HTMLImageElement::HTMLImageElement(Document& document, | 88 HTMLImageElement::HTMLImageElement(Document& document, |
| 89 HTMLFormElement* form, | 89 HTMLFormElement* form, |
| 90 bool createdByParser) | 90 bool createdByParser) |
| 91 : HTMLElement(imgTag, document), | 91 : HTMLElement(imgTag, document), |
| 92 ActiveScriptWrappable(this), | 92 ActiveScriptWrappable(this), |
| 93 m_imageLoader(HTMLImageLoader::create(this)), | 93 m_imageLoader(HTMLImageLoader::create(this)), |
| 94 m_imageDevicePixelRatio(1.0f), | 94 m_imageDevicePixelRatio(1.0f), |
| 95 m_source(nullptr), | 95 m_source(nullptr), |
| 96 m_layoutDisposition(LayoutDisposition::PrimaryContent), | |
| 96 m_formWasSetByParser(false), | 97 m_formWasSetByParser(false), |
| 97 m_elementCreatedByParser(createdByParser), | 98 m_elementCreatedByParser(createdByParser), |
| 98 m_useFallbackContent(false), | |
| 99 m_isFallbackImage(false), | 99 m_isFallbackImage(false), |
| 100 m_referrerPolicy(ReferrerPolicyDefault) { | 100 m_referrerPolicy(ReferrerPolicyDefault) { |
| 101 setHasCustomStyleCallbacks(); | 101 setHasCustomStyleCallbacks(); |
| 102 if (form && form->isConnected()) { | 102 if (form && form->isConnected()) { |
| 103 m_form = form; | 103 m_form = form; |
| 104 m_formWasSetByParser = true; | 104 m_formWasSetByParser = true; |
| 105 m_form->associate(*this); | 105 m_form->associate(*this); |
| 106 m_form->didAssociateByParser(); | 106 m_form->didAssociateByParser(); |
| 107 } | 107 } |
| 108 } | 108 } |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 document().devicePixelRatio(), sourceSize(*source), | 332 document().devicePixelRatio(), sourceSize(*source), |
| 333 source->fastGetAttribute(srcsetAttr), &document()); | 333 source->fastGetAttribute(srcsetAttr), &document()); |
| 334 if (candidate.isEmpty()) | 334 if (candidate.isEmpty()) |
| 335 continue; | 335 continue; |
| 336 m_source = source; | 336 m_source = source; |
| 337 return candidate; | 337 return candidate; |
| 338 } | 338 } |
| 339 return ImageCandidate(); | 339 return ImageCandidate(); |
| 340 } | 340 } |
| 341 | 341 |
| 342 bool HTMLImageElement::layoutObjectIsNeeded(const ComputedStyle& style) { | |
| 343 return m_layoutDisposition != LayoutDisposition::Collapsed && | |
| 344 HTMLElement::layoutObjectIsNeeded(style); | |
| 345 } | |
| 346 | |
| 342 LayoutObject* HTMLImageElement::createLayoutObject(const ComputedStyle& style) { | 347 LayoutObject* HTMLImageElement::createLayoutObject(const ComputedStyle& style) { |
| 343 const ContentData* contentData = style.contentData(); | 348 const ContentData* contentData = style.contentData(); |
| 344 if (contentData && contentData->isImage()) { | 349 if (contentData && contentData->isImage()) { |
| 345 const StyleImage* contentImage = toImageContentData(contentData)->image(); | 350 const StyleImage* contentImage = toImageContentData(contentData)->image(); |
| 346 bool errorOccurred = contentImage && contentImage->cachedImage() && | 351 bool errorOccurred = contentImage && contentImage->cachedImage() && |
| 347 contentImage->cachedImage()->errorOccurred(); | 352 contentImage->cachedImage()->errorOccurred(); |
| 348 if (!errorOccurred) | 353 if (!errorOccurred) |
| 349 return LayoutObject::createObject(this, style); | 354 return LayoutObject::createObject(this, style); |
| 350 } | 355 } |
| 351 | 356 |
| 352 if (m_useFallbackContent) | 357 switch (m_layoutDisposition) { |
|
dominicc (has gone to gerrit)
2016/12/07 01:51:42
Sadness, it seems the compiler doesn't work out th
engedy
2016/12/07 09:37:41
Done.
| |
| 353 return new LayoutBlockFlow(this); | 358 case LayoutDisposition::FallbackContent: |
| 354 | 359 return new LayoutBlockFlow(this); |
| 355 LayoutImage* image = new LayoutImage(this); | 360 case LayoutDisposition::PrimaryContent: { |
| 356 image->setImageResource(LayoutImageResource::create()); | 361 LayoutImage* image = new LayoutImage(this); |
| 357 image->setImageDevicePixelRatio(m_imageDevicePixelRatio); | 362 image->setImageResource(LayoutImageResource::create()); |
| 358 return image; | 363 image->setImageDevicePixelRatio(m_imageDevicePixelRatio); |
| 364 return image; | |
| 365 } | |
| 366 case LayoutDisposition::Collapsed: | |
| 367 NOTREACHED(); | |
| 368 return nullptr; | |
| 369 } | |
| 359 } | 370 } |
| 360 | 371 |
| 361 void HTMLImageElement::attachLayoutTree(const AttachContext& context) { | 372 void HTMLImageElement::attachLayoutTree(const AttachContext& context) { |
| 362 HTMLElement::attachLayoutTree(context); | 373 HTMLElement::attachLayoutTree(context); |
| 363 | 374 |
| 364 if (layoutObject() && layoutObject()->isImage()) { | 375 if (layoutObject() && layoutObject()->isImage()) { |
| 365 LayoutImage* layoutImage = toLayoutImage(layoutObject()); | 376 LayoutImage* layoutImage = toLayoutImage(layoutObject()); |
| 366 LayoutImageResource* layoutImageResource = layoutImage->imageResource(); | 377 LayoutImageResource* layoutImageResource = layoutImage->imageResource(); |
| 367 if (m_isFallbackImage) { | 378 if (m_isFallbackImage) { |
| 368 float deviceScaleFactor = blink::deviceScaleFactor(layoutImage->frame()); | 379 float deviceScaleFactor = blink::deviceScaleFactor(layoutImage->frame()); |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 763 setBestFitURLAndDPRFromImageCandidate(candidate); | 774 setBestFitURLAndDPRFromImageCandidate(candidate); |
| 764 foundURL = true; | 775 foundURL = true; |
| 765 } | 776 } |
| 766 | 777 |
| 767 if (!foundURL) { | 778 if (!foundURL) { |
| 768 candidate = bestFitSourceForImageAttributes( | 779 candidate = bestFitSourceForImageAttributes( |
| 769 document().devicePixelRatio(), sourceSize(*this), | 780 document().devicePixelRatio(), sourceSize(*this), |
| 770 fastGetAttribute(srcAttr), fastGetAttribute(srcsetAttr), &document()); | 781 fastGetAttribute(srcAttr), fastGetAttribute(srcsetAttr), &document()); |
| 771 setBestFitURLAndDPRFromImageCandidate(candidate); | 782 setBestFitURLAndDPRFromImageCandidate(candidate); |
| 772 } | 783 } |
| 784 | |
| 773 imageLoader().updateFromElement(behavior, m_referrerPolicy); | 785 imageLoader().updateFromElement(behavior, m_referrerPolicy); |
| 774 | 786 |
| 775 // Images such as data: uri's can return immediately and may already have | 787 // Images such as data: uri's can return immediately and may already have |
| 776 // errored out. | 788 // errored out. |
| 777 bool imageHasLoaded = imageLoader().image() && | 789 bool imageHasLoaded = imageLoader().image() && |
| 778 !imageLoader().image()->isLoading() && | 790 !imageLoader().image()->isLoading() && |
| 779 !imageLoader().image()->errorOccurred(); | 791 !imageLoader().image()->errorOccurred(); |
| 780 bool imageStillLoading = | 792 bool imageStillLoading = |
| 781 !imageHasLoaded && imageLoader().hasPendingActivity() && | 793 !imageHasLoaded && imageLoader().hasPendingActivity() && |
| 782 !imageLoader().hasPendingError() && !imageSourceURL().isEmpty(); | 794 !imageLoader().hasPendingError() && !imageSourceURL().isEmpty(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 794 // sufficient, because a deferred image does have pending activity, does not | 806 // sufficient, because a deferred image does have pending activity, does not |
| 795 // have a pending error, and does have a source URL, so if imageHasLoaded | 807 // have a pending error, and does have a source URL, so if imageHasLoaded |
| 796 // was correct, imageStillLoading would become wrong. | 808 // was correct, imageStillLoading would become wrong. |
| 797 // | 809 // |
| 798 // Instead of dealing with that, there's a separate check that the | 810 // Instead of dealing with that, there's a separate check that the |
| 799 // ImageResource has non-null image data associated with it, which isn't | 811 // ImageResource has non-null image data associated with it, which isn't |
| 800 // folded into imageHasLoaded above. | 812 // folded into imageHasLoaded above. |
| 801 if ((imageHasLoaded && imageHasImage) || imageStillLoading || imageIsDocument) | 813 if ((imageHasLoaded && imageHasImage) || imageStillLoading || imageIsDocument) |
| 802 ensurePrimaryContent(); | 814 ensurePrimaryContent(); |
| 803 else | 815 else |
| 804 ensureFallbackContent(); | 816 ensureCollapsedOrFallbackContent(); |
| 805 } | 817 } |
| 806 | 818 |
| 807 const KURL& HTMLImageElement::sourceURL() const { | 819 const KURL& HTMLImageElement::sourceURL() const { |
| 808 return cachedImage()->response().url(); | 820 return cachedImage()->response().url(); |
| 809 } | 821 } |
| 810 | 822 |
| 811 void HTMLImageElement::didAddUserAgentShadowRoot(ShadowRoot&) { | 823 void HTMLImageElement::didAddUserAgentShadowRoot(ShadowRoot&) { |
| 812 HTMLImageFallbackHelper::createAltTextShadowTree(*this); | 824 HTMLImageFallbackHelper::createAltTextShadowTree(*this); |
| 813 } | 825 } |
| 814 | 826 |
| 815 void HTMLImageElement::ensureFallbackForGeneratedContent() { | 827 void HTMLImageElement::ensureFallbackForGeneratedContent() { |
| 816 setUseFallbackContent(); | 828 // FIXME: The special casing for generated content in createLayoutObject |
|
dominicc (has gone to gerrit)
2016/12/07 01:51:42
TODO(whom): [what to fix] [when]
If this is just
engedy
2016/12/07 09:37:41
At this point I am optimistic that this should be
| |
| 817 reattachFallbackContent(); | 829 // breaks the invariant that the layout object attached to this element will |
| 830 // always be appropriate for |m_layoutDisposition|. Force recreate it. | |
| 831 // See example: images/content-url-broken-image-with-alt-text.html. | |
| 832 setLayoutDisposition(LayoutDisposition::FallbackContent, | |
| 833 true /* forceReattach */); | |
| 818 } | 834 } |
| 819 | 835 |
| 820 void HTMLImageElement::ensureFallbackContent() { | 836 void HTMLImageElement::ensureCollapsedOrFallbackContent() { |
| 821 if (m_useFallbackContent || m_isFallbackImage) | 837 if (m_isFallbackImage) |
| 822 return; | 838 return; |
| 823 setUseFallbackContent(); | 839 |
| 824 reattachFallbackContent(); | 840 if (imageLoader().image() && |
|
dominicc (has gone to gerrit)
2016/12/07 01:51:42
Seems OK, but maybe
bool explainingVar = that exp
engedy
2016/12/07 09:37:40
Done.
| |
| 841 imageLoader().image()->resourceError().shouldCollapseInitiator()) { | |
| 842 setLayoutDisposition(LayoutDisposition::Collapsed); | |
| 843 } else { | |
| 844 setLayoutDisposition(LayoutDisposition::FallbackContent); | |
| 845 } | |
| 825 } | 846 } |
| 826 | 847 |
| 827 void HTMLImageElement::ensurePrimaryContent() { | 848 void HTMLImageElement::ensurePrimaryContent() { |
| 828 if (!m_useFallbackContent) | 849 setLayoutDisposition(LayoutDisposition::PrimaryContent); |
| 829 return; | |
| 830 m_useFallbackContent = false; | |
| 831 reattachFallbackContent(); | |
| 832 } | 850 } |
| 833 | 851 |
| 834 void HTMLImageElement::reattachFallbackContent() { | 852 void HTMLImageElement::setLayoutDisposition(LayoutDisposition layoutDisposition, |
| 853 bool forceReattach) { | |
| 854 if (m_layoutDisposition == layoutDisposition && !forceReattach) | |
| 855 return; | |
| 856 | |
| 857 m_layoutDisposition = layoutDisposition; | |
| 858 | |
| 835 // This can happen inside of attachLayoutTree() in the middle of a recalcStyle | 859 // This can happen inside of attachLayoutTree() in the middle of a recalcStyle |
| 836 // so we need to reattach synchronously here. | 860 // so we need to reattach synchronously here. |
| 837 if (document().inStyleRecalc()) | 861 if (document().inStyleRecalc()) { |
| 838 reattachLayoutTree(); | 862 reattachLayoutTree(); |
| 839 else | 863 } else { |
| 864 if (m_layoutDisposition == LayoutDisposition::FallbackContent) { | |
| 865 EventDispatchForbiddenScope::AllowUserAgentEvents allowEvents; | |
| 866 ensureUserAgentShadowRoot(); | |
| 867 } | |
| 840 lazyReattachIfAttached(); | 868 lazyReattachIfAttached(); |
| 869 } | |
| 841 } | 870 } |
| 842 | 871 |
| 843 PassRefPtr<ComputedStyle> HTMLImageElement::customStyleForLayoutObject() { | 872 PassRefPtr<ComputedStyle> HTMLImageElement::customStyleForLayoutObject() { |
| 844 RefPtr<ComputedStyle> newStyle = originalStyleForLayoutObject(); | 873 switch (m_layoutDisposition) { |
| 845 | 874 case LayoutDisposition::PrimaryContent: // Fall through. |
| 846 if (!m_useFallbackContent) | 875 case LayoutDisposition::Collapsed: |
| 847 return newStyle; | 876 return originalStyleForLayoutObject(); |
| 848 | 877 case LayoutDisposition::FallbackContent: |
| 849 RefPtr<ComputedStyle> style = ComputedStyle::clone(*newStyle); | 878 return HTMLImageFallbackHelper::customStyleForAltText( |
| 850 return HTMLImageFallbackHelper::customStyleForAltText(*this, style); | 879 *this, ComputedStyle::clone(*originalStyleForLayoutObject())); |
| 851 } | 880 } |
| 852 | |
| 853 void HTMLImageElement::setUseFallbackContent() { | |
| 854 m_useFallbackContent = true; | |
| 855 if (document().inStyleRecalc()) | |
| 856 return; | |
| 857 EventDispatchForbiddenScope::AllowUserAgentEvents allowEvents; | |
| 858 ensureUserAgentShadowRoot(); | |
| 859 } | 881 } |
| 860 | 882 |
| 861 bool HTMLImageElement::isOpaque() const { | 883 bool HTMLImageElement::isOpaque() const { |
| 862 Image* image = const_cast<HTMLImageElement*>(this)->imageContents(); | 884 Image* image = const_cast<HTMLImageElement*>(this)->imageContents(); |
| 863 return image && image->currentFrameKnownToBeOpaque(); | 885 return image && image->currentFrameKnownToBeOpaque(); |
| 864 } | 886 } |
| 865 | 887 |
| 866 int HTMLImageElement::sourceWidth() { | 888 int HTMLImageElement::sourceWidth() { |
| 867 SourceImageStatus status; | 889 SourceImageStatus status; |
| 868 FloatSize defaultObjectSize(width(), height()); | 890 FloatSize defaultObjectSize(width(), height()); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 885 ImageResource* image = cachedImage(); | 907 ImageResource* image = cachedImage(); |
| 886 if (!image) | 908 if (!image) |
| 887 return IntSize(); | 909 return IntSize(); |
| 888 LayoutSize lSize = image->imageSize( | 910 LayoutSize lSize = image->imageSize( |
| 889 LayoutObject::shouldRespectImageOrientation(layoutObject()), 1.0f); | 911 LayoutObject::shouldRespectImageOrientation(layoutObject()), 1.0f); |
| 890 DCHECK(lSize.fraction().isZero()); | 912 DCHECK(lSize.fraction().isZero()); |
| 891 return IntSize(lSize.width().toInt(), lSize.height().toInt()); | 913 return IntSize(lSize.width().toInt(), lSize.height().toInt()); |
| 892 } | 914 } |
| 893 | 915 |
| 894 } // namespace blink | 916 } // namespace blink |
| OLD | NEW |