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

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, 3 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 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 6590e64a2d5c2d6e847a498c7e15da308d06366e..8a4a2e9e08a66411f0fdf45d9309a126fc28d309 100644
--- a/Source/core/html/HTMLImageElement.cpp
+++ b/Source/core/html/HTMLImageElement.cpp
@@ -37,11 +37,13 @@
#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/rendering/RenderBlockFlow.h"
#include "core/rendering/RenderImage.h"
#include "platform/MIMETypeRegistry.h"
#include "platform/RuntimeEnabledFeatures.h"
@@ -85,7 +87,10 @@ HTMLImageElement::HTMLImageElement(Document& document, HTMLFormElement* form, bo
, m_elementCreatedByParser(createdByParser)
, m_intrinsicSizingViewportDependant(false)
, m_effectiveSizeViewportDependant(false)
+ , m_useFallbackContent(false)
+ , m_isFallbackImage(false)
{
+ setHasCustomStyleCallbacks();
if (form && form->inDocument()) {
#if ENABLE(OILPAN)
m_form = form;
@@ -236,9 +241,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) {
@@ -254,7 +263,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
@@ -317,6 +326,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);
@@ -341,13 +353,9 @@ void HTMLImageElement::attach(const AttachContext& context)
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());
}
}
@@ -370,7 +378,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);
}
@@ -647,6 +655,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
@@ -654,4 +667,43 @@ const KURL& HTMLImageElement::sourceURL() const
return cachedImage()->response().url();
}
+void HTMLImageElement::didAddUserAgentShadowRoot(ShadowRoot&)
+{
+ 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 customStyleForAltText(*this, newStyle);
+}
}

Powered by Google App Engine
This is Rietveld 408576698