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

Side by Side Diff: Source/core/html/HTMLImageFallbackHelper.h

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 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 #ifndef HTMLImageFallbackHelper_h
22 #define 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/HTMLImageElement.h"
33 #include "core/html/HTMLImageLoader.h"
34 #include "core/html/HTMLInputElement.h"
35 #include "core/html/HTMLStyleElement.h"
36 #include "wtf/PassOwnPtr.h"
37 #include "wtf/text/StringBuilder.h"
38
39 namespace blink {
40
41 using namespace HTMLNames;
42
43 static bool noImageSourceSpecified(Element* element)
esprehn 2014/09/05 00:47:51 reference
rhogan 2014/09/08 19:52:41 Done
44 {
45 bool noSrcSpecified = !element->hasAttribute(srcAttr) || element->getAttribu te(srcAttr).isNull() || element->getAttribute(srcAttr).isEmpty();
46 bool noSrcsetSpecified = !element->hasAttribute(srcsetAttr) || element->getA ttribute(srcsetAttr).isNull() || element->getAttribute(srcsetAttr).isEmpty();
47 return noSrcSpecified && noSrcsetSpecified;
48 }
49
50 static String altTextForElement(Element* element)
esprehn 2014/09/05 00:47:51 reference
rhogan 2014/09/08 19:52:42 Done
51 {
52 ASSERT(isHTMLImageElement(element) || isHTMLInputElement(element));
53 if (isHTMLImageElement(element))
54 return toHTMLImageElement(element)->altText();
55 return toHTMLInputElement(element)->altText();
esprehn 2014/09/05 00:47:51 Just add a virtual method to HTMLElement.h and rem
rhogan 2014/09/08 19:52:41 Done
56 }
57
58 static Text* createAltTextShadowTree(Element* element)
esprehn 2014/09/05 00:47:51 void.
59 {
60 ShadowRoot* root = element->userAgentShadowRoot();
esprehn 2014/09/05 00:47:51 Reference, and use ensureUserAgentShadowRoot()
rhogan 2014/09/08 19:52:41 Done
61 RefPtr<HTMLStyleElement> style = HTMLStyleElement::create(element->document( ), false);
62 style->setTextContent("#alttext-container { overflow: hidden; border: 1px so lid silver; padding: 1px; display: inline-block; box-sizing: border-box; }"
63 "#alttext { overflow: hidden; display: block; }"
64 "#alttext-image { margin: 0px; }");
65 root->appendChild(style.release());
66
67 RefPtr<HTMLDivElement> container = HTMLDivElement::create(element->document( ));
68 container->setAttribute(idAttr, AtomicString("alttext-container", AtomicStri ng::ConstructFromLiteral));
69 root->appendChild(container);
70
71 RefPtr<HTMLImageElement> brokenImage = HTMLImageElement::create(element->doc ument());
72 brokenImage->setIsFallbackImage();
73 container->appendChild(brokenImage);
74 brokenImage->setAttribute(idAttr, AtomicString("alttext-image", AtomicString ::ConstructFromLiteral));
75 brokenImage->setAttribute(widthAttr, AtomicString("16", AtomicString::Constr uctFromLiteral));
76 brokenImage->setAttribute(heightAttr, AtomicString("16", AtomicString::Const ructFromLiteral));
77 brokenImage->setAttribute(alignAttr, AtomicString("left", AtomicString::Cons tructFromLiteral));
78 brokenImage->setAttribute(srcAttr, AtomicString("data:image/png;base64,iVBOR w0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABO0lEQVR4XpWRTytEYRTG3/tHLGQr"
79 "NfEBpKQkmc8gG0ulxpRsfQ0TTXbI3kZ2ys4tsyBZjN1N8idlYUYaxJzzOKf7ztG1MfN0ens 69/x6"
80 "n/eeYPuo7npS9bCOriXDMQOCTVbO3X+6Wp9mICZWAHqquE1qAA8EQRiFcmYMsQKsAHM2DeK V4qj4"
81 "8ZHB++bHwcXTQ+Mz6u/rABwSIQO0wKW5wuLU8E5yWzlJS7OFvaUJMGelAFkkYici1NIXB4D QbH1J"
82 "Y2ggVoD4N1Kb2AAA149vUmLLxbHaTaNynIJggAz7G7jTyjB5wOv79/LupT47Cu0rcT6S/ai 755YU"
83 "iMM4cgw4/I3ERAaI3y/PiFnYPHOBtfORLKX38AaBERaJbHFeAOY3Eu8ZOYByi7Pl+hzWN+n wWjVB"
84 "15LhYHXr1PWiH0AYTw4BBjO9AAAAAElFTkSuQmCC", AtomicString::ConstructFromL iteral));
85
86 RefPtr<HTMLDivElement> altText = HTMLDivElement::create(element->document()) ;
87 altText->setAttribute(idAttr, AtomicString("alttext", AtomicString::Construc tFromLiteral));
88 container->appendChild(altText);
89
90 RefPtr<Text> text = Text::create(element->document(), altTextForElement(elem ent));
91 altText->appendChild(text);
92 return text.get();
93 }
94
95 static PassRefPtr<RenderStyle> customStyleForAltText(Element* element, PassRefPt r<RenderStyle> newStyle)
esprehn 2014/09/05 00:47:51 reference
rhogan 2014/09/08 19:52:42 Done
96 {
97 Element* placeHolder = element->userAgentShadowRoot()->getElementById("altte xt-container");
esprehn 2014/09/05 00:47:51 ensureUserAgentShadowRoot(), I would also save it
rhogan 2014/09/08 19:52:42 Done
98 Element* brokenImage = element->userAgentShadowRoot()->getElementById("altte xt-image");
99 if (element->document().inQuirksMode()) {
100 // Mimic the behaviour of the image host by setting symmetric dimensions if only one dimension is specified.
101 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isA uto())
102 newStyle->setHeight(newStyle->width());
103 else if (newStyle->height().isSpecifiedOrIntrinsic() && newStyle->width( ).isAuto())
104 newStyle->setWidth(newStyle->height());
105 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isS pecifiedOrIntrinsic() && !element->shadowRoot() && element->userAgentShadowRoot( )) {
106 placeHolder->setInlineStyleProperty(CSSPropertyVerticalAlign, CSSVal ueBaseline);
107 }
108 }
109
110 // If the image has specified dimensions allow the alt-text container expand to fill them.
111 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isSpeci fiedOrIntrinsic() && !element->shadowRoot() && element->userAgentShadowRoot()) {
esprehn 2014/09/05 00:47:51 Why do you need to keep checking for userAgentShad
rhogan 2014/09/08 19:52:41 I don't. This was cruft.
rhogan 2014/09/08 19:52:41 I don't. This was cruft.
112 placeHolder->setInlineStyleProperty(CSSPropertyWidth, 100, CSSPrimitiveV alue::CSS_PERCENTAGE);
113 placeHolder->setInlineStyleProperty(CSSPropertyHeight, 100, CSSPrimitive Value::CSS_PERCENTAGE);
114 }
115
116 // Make sure the broken image icon appears on the appropriate side of the im age for the element's writing direction.
117 if (!element->shadowRoot() && element->userAgentShadowRoot())
esprehn 2014/09/05 00:47:51 This doesn't make sense, what are you trying to ch
rhogan 2014/09/08 19:52:41 See above. It was unnecessary cruft.
118 brokenImage->setAttribute(alignAttr, AtomicString(newStyle->direction() == LTR ? "left" : "right"));
119
120 // This is an <img> with no attributes, so don't display anything.
121 if (noImageSourceSpecified(element) && !newStyle->width().isSpecifiedOrIntri nsic() && !newStyle->height().isSpecifiedOrIntrinsic() && altTextForElement(elem ent).isEmpty())
122 newStyle->setDisplay(NONE);
123
124 // This preserves legacy behaviour originally defined when alt-text was mana ged by RenderImage.
125 if (noImageSourceSpecified(element) && altTextForElement(element).isEmpty() && !element->shadowRoot() && element->userAgentShadowRoot())
126 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueNone);
127
128 return newStyle;
129 }
130
131 }
132
133 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698