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

Side by Side Diff: Source/core/html/HTMLImageFallbackHelper.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, 1 month 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
(Empty)
1 /*
2 * Copyright (C) 2014 Robert Hogan <robhogan@chromium.org>.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21 #include "config.h"
22 #include "core/html/HTMLImageFallbackHelper.h"
23
24 #include "core/HTMLNames.h"
25 #include "core/InputTypeNames.h"
26 #include "core/dom/ElementRareData.h"
27 #include "core/dom/Text.h"
28 #include "core/dom/shadow/ShadowRoot.h"
29 #include "core/fetch/ImageResource.h"
30 #include "core/html/FormDataList.h"
31 #include "core/html/HTMLDivElement.h"
32 #include "core/html/HTMLElement.h"
33 #include "core/html/HTMLImageElement.h"
34 #include "core/html/HTMLImageLoader.h"
35 #include "core/html/HTMLInputElement.h"
36 #include "core/html/HTMLStyleElement.h"
37 #include "wtf/PassOwnPtr.h"
38 #include "wtf/text/StringBuilder.h"
39
40 namespace blink {
41
42 using namespace HTMLNames;
43
44 static bool noImageSourceSpecified(const Element& element)
45 {
46 bool noSrcSpecified = !element.hasAttribute(srcAttr) || element.getAttribute (srcAttr).isNull() || element.getAttribute(srcAttr).isEmpty();
47 bool noSrcsetSpecified = !element.hasAttribute(srcsetAttr) || element.getAtt ribute(srcsetAttr).isNull() || element.getAttribute(srcsetAttr).isEmpty();
48 return noSrcSpecified && noSrcsetSpecified;
49 }
50
51 void HTMLImageFallbackHelper::createAltTextShadowTree(Element& element)
52 {
53 ShadowRoot& root = element.ensureUserAgentShadowRoot();
54 RefPtrWillBeRawPtr<HTMLStyleElement> style = HTMLStyleElement::create(elemen t.document(), false);
55 root.appendChild(style);
56 style->setTextContent("#alttext-container { overflow: hidden; border: 1px so lid silver; padding: 1px; display: inline-block; box-sizing: border-box; } "
pdr. 2014/11/14 07:34:17 Having to parse these styles seems inefficient. I
57 "#alttext { overflow: hidden; display: block; } "
58 "#alttext-image { margin: 0px; }");
59
60 RefPtrWillBeRawPtr<HTMLDivElement> container = HTMLDivElement::create(elemen t.document());
61 root.appendChild(container);
62 container->setAttribute(idAttr, AtomicString("alttext-container", AtomicStri ng::ConstructFromLiteral));
63
64 RefPtrWillBeRawPtr<HTMLImageElement> brokenImage = HTMLImageElement::create( element.document());
65 container->appendChild(brokenImage);
66 brokenImage->setIsFallbackImage();
67 brokenImage->setAttribute(idAttr, AtomicString("alttext-image", AtomicString ::ConstructFromLiteral));
68 brokenImage->setAttribute(widthAttr, AtomicString("16", AtomicString::Constr uctFromLiteral));
69 brokenImage->setAttribute(heightAttr, AtomicString("16", AtomicString::Const ructFromLiteral));
70 brokenImage->setAttribute(alignAttr, AtomicString("left", AtomicString::Cons tructFromLiteral));
71 brokenImage->setAttribute(srcAttr, AtomicString("data:image/png;base64,iVBOR w0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABO0lEQVR4XpWRTytEYRTG3/tHLGQr"
pdr. 2014/11/14 07:34:17 Is this just to support the spec's suggestion of a
72 "NfEBpKQkmc8gG0ulxpRsfQ0TTXbI3kZ2ys4tsyBZjN1N8idlYUYaxJzzOKf7ztG1MfN0ens 69/x6"
73 "n/eeYPuo7npS9bCOriXDMQOCTVbO3X+6Wp9mICZWAHqquE1qAA8EQRiFcmYMsQKsAHM2DeK V4qj4"
pdr. 2014/11/14 07:34:17 Can we use ImageResource::brokenImage(...) here, a
rhogan 2014/11/14 19:23:19 I can't see a way of doing that other than to use
74 "8ZHB++bHwcXTQ+Mz6u/rABwSIQO0wKW5wuLU8E5yWzlJS7OFvaUJMGelAFkkYici1NIXB4D QbH1J"
75 "Y2ggVoD4N1Kb2AAA149vUmLLxbHaTaNynIJggAz7G7jTyjB5wOv79/LupT47Cu0rcT6S/ai 755YU"
76 "iMM4cgw4/I3ERAaI3y/PiFnYPHOBtfORLKX38AaBERaJbHFeAOY3Eu8ZOYByi7Pl+hzWN+n wWjVB"
77 "15LhYHXr1PWiH0AYTw4BBjO9AAAAAElFTkSuQmCC", AtomicString::ConstructFromL iteral));
78
79 RefPtrWillBeRawPtr<HTMLDivElement> altText = HTMLDivElement::create(element. document());
80 container->appendChild(altText);
81 altText->setAttribute(idAttr, AtomicString("alttext", AtomicString::Construc tFromLiteral));
82
83 RefPtrWillBeRawPtr<Text> text = Text::create(element.document(), toHTMLEleme nt(element).altText());
84 altText->appendChild(text);
85 }
86
87 PassRefPtr<RenderStyle> HTMLImageFallbackHelper::customStyleForAltText(Element& element, PassRefPtr<RenderStyle> newStyle)
88 {
89 // If we have an author shadow root or have not created the UA shadow root y et, bail early. We can't
90 // use ensureUserAgentShadowRoot() here because that would alter the DOM tre e during style recalc.
91 if (element.shadowRoot() || !element.userAgentShadowRoot())
92 return newStyle;
93
94 Element* placeHolder = element.userAgentShadowRoot()->getElementById("alttex t-container");
95 Element* brokenImage = element.userAgentShadowRoot()->getElementById("alttex t-image");
96
97 if (element.document().inQuirksMode()) {
98 // Mimic the behaviour of the image host by setting symmetric dimensions if only one dimension is specified.
99 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isA uto())
100 newStyle->setHeight(newStyle->width());
101 else if (newStyle->height().isSpecifiedOrIntrinsic() && newStyle->width( ).isAuto())
102 newStyle->setWidth(newStyle->height());
103 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isS pecifiedOrIntrinsic()) {
104 placeHolder->setInlineStyleProperty(CSSPropertyVerticalAlign, CSSVal ueBaseline);
105 }
106 }
107
108 // If the image has specified dimensions allow the alt-text container expand to fill them.
109 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isSpeci fiedOrIntrinsic()) {
110 placeHolder->setInlineStyleProperty(CSSPropertyWidth, 100, CSSPrimitiveV alue::CSS_PERCENTAGE);
111 placeHolder->setInlineStyleProperty(CSSPropertyHeight, 100, CSSPrimitive Value::CSS_PERCENTAGE);
112 }
113
114 // Make sure the broken image icon appears on the appropriate side of the im age for the element's writing direction.
115 brokenImage->setInlineStyleProperty(CSSPropertyFloat, AtomicString(newStyle- >direction() == LTR ? "left" : "right"));
pdr. 2014/11/14 07:34:17 Is there a test for this? Does the alt text proper
rhogan 2014/11/14 19:23:19 Yes - fast/text/bidi-img-alt-text.html - and yes,
116
117 // This is an <img> with no attributes, so don't display anything.
118 if (noImageSourceSpecified(element) && !newStyle->width().isSpecifiedOrIntri nsic() && !newStyle->height().isSpecifiedOrIntrinsic() && toHTMLElement(element) .altText().isEmpty())
119 newStyle->setDisplay(NONE);
120
121 // This preserves legacy behaviour originally defined when alt-text was mana ged by RenderImage.
122 if (noImageSourceSpecified(element))
123 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueNone);
124 else
125 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueInline);
126
127 return newStyle;
128 }
129
130 } // namespace blink
131
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698