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

Unified Diff: Source/core/html/HTMLImageElement.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 side-by-side diff with in-line comments
Download patch
Index: Source/core/html/HTMLImageElement.cpp
diff --git a/Source/core/html/HTMLImageElement.cpp b/Source/core/html/HTMLImageElement.cpp
index 088f5dd4627b4896b03d7a84b3fff6749e40b224..7ca6d672707f5e57b4a29610635764505372dcbe 100644
--- a/Source/core/html/HTMLImageElement.cpp
+++ b/Source/core/html/HTMLImageElement.cpp
@@ -32,16 +32,20 @@
#include "core/css/parser/SizesAttributeParser.h"
#include "core/dom/Attribute.h"
#include "core/dom/NodeTraversal.h"
+#include "core/dom/shadow/ShadowRoot.h"
#include "core/fetch/ImageResource.h"
#include "core/frame/UseCounter.h"
#include "core/html/HTMLAnchorElement.h"
#include "core/html/HTMLCanvasElement.h"
#include "core/html/HTMLFormElement.h"
+#include "core/html/HTMLImageFallbackHelper.h"
#include "core/html/HTMLSourceElement.h"
#include "core/html/canvas/CanvasRenderingContext.h"
#include "core/html/parser/HTMLParserIdioms.h"
#include "core/html/parser/HTMLSrcsetParser.h"
#include "core/inspector/ConsoleMessage.h"
+#include "core/page/Page.h"
+#include "core/rendering/RenderBlockFlow.h"
#include "core/rendering/RenderImage.h"
#include "platform/ContentType.h"
#include "platform/MIMETypeRegistry.h"
@@ -84,7 +88,10 @@ HTMLImageElement::HTMLImageElement(Document& document, HTMLFormElement* form, bo
, m_formWasSetByParser(false)
, m_elementCreatedByParser(createdByParser)
, m_intrinsicSizingViewportDependant(false)
+ , m_useFallbackContent(false)
+ , m_isFallbackImage(false)
{
+ setHasCustomStyleCallbacks();
if (form && form->inDocument()) {
#if ENABLE(OILPAN)
m_form = form;
@@ -235,9 +242,13 @@ void HTMLImageElement::setBestFitURLAndDPRFromImageCandidate(const ImageCandidat
void HTMLImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
- if (name == altAttr) {
- if (renderer() && renderer()->isImage())
- toRenderImage(renderer())->updateAltText();
+ if (name == altAttr || name == titleAttr) {
+ if (userAgentShadowRoot()) {
+ Element* text = userAgentShadowRoot()->getElementById("alttext");
+ String value = altText();
+ if (text && text->textContent() != value)
+ text->setTextContent(altText());
+ }
} else if (name == srcAttr || name == srcsetAttr || name == sizesAttr) {
selectSourceURL(ImageLoader::UpdateIgnorePreviousError);
} else if (name == usemapAttr) {
@@ -247,7 +258,7 @@ void HTMLImageElement::parseAttribute(const QualifiedName& name, const AtomicStr
}
}
-const AtomicString& HTMLImageElement::altText() const
+String HTMLImageElement::altText() const
{
// lets figure out the alt text.. magic stuff
// http://www.w3.org/TR/1998/REC-html40-19980424/appendix/notes.html#altgen
@@ -313,6 +324,9 @@ RenderObject* HTMLImageElement::createRenderer(RenderStyle* style)
if (style->hasContent())
return RenderObject::createObject(this, style);
+ if (m_useFallbackContent)
+ return new RenderBlockFlow(this);
+
RenderImage* image = new RenderImage(this);
image->setImageResource(RenderImageResource::create());
image->setImageDevicePixelRatio(m_imageDevicePixelRatio);
@@ -334,16 +348,18 @@ void HTMLImageElement::attach(const AttachContext& context)
if (renderer() && renderer()->isImage()) {
RenderImage* renderImage = toRenderImage(renderer());
RenderImageResource* renderImageResource = renderImage->imageResource();
+ if (m_isFallbackImage) {
+ float deviceScaleFactor = blink::deviceScaleFactor(renderImage->frame());
+ pair<Image*, float> brokenImageAndImageScaleFactor = ImageResource::brokenImage(deviceScaleFactor);
+ ImageResource* newImageResource = new ImageResource(brokenImageAndImageScaleFactor.first);
+ renderImage->imageResource()->setImageResource(newImageResource);
+ }
if (renderImageResource->hasImage())
return;
- // If we have no image at all because we have no src attribute, set
- // image height and width for the alt text instead.
if (!imageLoader().image() && !renderImageResource->cachedImage())
- renderImage->setImageSizeForAltText();
- else
- renderImageResource->setImageResource(imageLoader().image());
-
+ return;
+ renderImageResource->setImageResource(imageLoader().image());
}
}
@@ -366,7 +382,7 @@ Node::InsertionNotificationRequest HTMLImageElement::insertedInto(ContainerNode*
// If we have been inserted from a renderer-less document,
// our loader may have not fetched the image, so do it now.
if ((insertionPoint->inDocument() && !imageLoader().image()) || imageWasModified)
- imageLoader().updateFromElement(ImageLoader::UpdateNormal, m_elementCreatedByParser ? ImageLoader::ForceLoadImmediately : ImageLoader::LoadNormally);
+ imageLoader().updateFromElement(ImageLoader::UpdateNormal);
return HTMLElement::insertedInto(insertionPoint);
}
@@ -643,6 +659,11 @@ void HTMLImageElement::selectSourceURL(ImageLoader::UpdateFromElementBehavior be
document().mediaQueryMatcher().addViewportListener(m_listener);
}
imageLoader().updateFromElement(behavior);
+
+ if (imageLoader().image() || (imageLoader().hasPendingActivity() && !imageSourceURL().isEmpty()))
+ ensurePrimaryContent();
+ else
+ ensureFallbackContent();
}
const KURL& HTMLImageElement::sourceURL() const
@@ -650,4 +671,51 @@ const KURL& HTMLImageElement::sourceURL() const
return cachedImage()->response().url();
}
+void HTMLImageElement::didAddUserAgentShadowRoot(ShadowRoot&)
+{
+ HTMLImageFallbackHelper::createAltTextShadowTree(*this);
+}
+
+void HTMLImageElement::ensureFallbackContent()
+{
+ if (m_useFallbackContent || m_isFallbackImage)
+ return;
+ setUseFallbackContent();
+ reattachFallbackContent();
+}
+
+void HTMLImageElement::ensurePrimaryContent()
+{
+ if (!m_useFallbackContent)
+ return;
+ m_useFallbackContent = false;
+ reattachFallbackContent();
+}
+
+void HTMLImageElement::reattachFallbackContent()
+{
+ // This can happen inside of attach() in the middle of a recalcStyle so we need to
+ // reattach synchronously here.
+ if (document().inStyleRecalc())
+ reattach();
+ else
+ lazyReattachIfAttached();
+}
+
+PassRefPtr<RenderStyle> HTMLImageElement::customStyleForRenderer()
+{
+ RefPtr<RenderStyle> newStyle = originalStyleForRenderer();
+
+ if (!m_useFallbackContent)
+ return newStyle;
+ return HTMLImageFallbackHelper::customStyleForAltText(*this, newStyle);
+}
+
+void HTMLImageElement::setUseFallbackContent()
+{
+ m_useFallbackContent = true;
+ if (document().inStyleRecalc())
+ return;
+ ensureUserAgentShadowRoot();
+}
}

Powered by Google App Engine
This is Rietveld 408576698