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

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, 2 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
« no previous file with comments | « Source/core/html/HTMLImageElement.cpp ('k') | Source/core/html/HTMLImageLoader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(const Element& element)
esprehn 2014/10/14 19:07:38 Please move these out of line. You want a .h file
44 {
45 bool noSrcSpecified = !element.hasAttribute(srcAttr) || element.getAttribute (srcAttr).isNull() || element.getAttribute(srcAttr).isEmpty();
46 bool noSrcsetSpecified = !element.hasAttribute(srcsetAttr) || element.getAtt ribute(srcsetAttr).isNull() || element.getAttribute(srcsetAttr).isEmpty();
47 return noSrcSpecified && noSrcsetSpecified;
48 }
49
50 static void createAltTextShadowTree(Element& element)
51 {
52 ShadowRoot& root = element.ensureUserAgentShadowRoot();
53 RefPtr<HTMLStyleElement> style = HTMLStyleElement::create(element.document() , false);
54 root.appendChild(style);
55 style->setTextContent("#alttext-container { overflow: hidden; border: 1px so lid silver; padding: 1px; display: inline-block; box-sizing: border-box; } "
56 "#alttext { overflow: hidden; display: block; } "
57 "#alttext-image { margin: 0px; }");
58
59 RefPtr<HTMLDivElement> container = HTMLDivElement::create(element.document() );
60 root.appendChild(container);
61 container->setAttribute(idAttr, AtomicString("alttext-container", AtomicStri ng::ConstructFromLiteral));
62
63 RefPtr<HTMLImageElement> brokenImage = HTMLImageElement::create(element.docu ment());
64 container->appendChild(brokenImage);
65 brokenImage->setIsFallbackImage();
66 brokenImage->setAttribute(idAttr, AtomicString("alttext-image", AtomicString ::ConstructFromLiteral));
67 brokenImage->setAttribute(widthAttr, AtomicString("16", AtomicString::Constr uctFromLiteral));
68 brokenImage->setAttribute(heightAttr, AtomicString("16", AtomicString::Const ructFromLiteral));
69 brokenImage->setAttribute(alignAttr, AtomicString("left", AtomicString::Cons tructFromLiteral));
70 brokenImage->setAttribute(srcAttr, AtomicString("data:image/png;base64,iVBOR w0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABO0lEQVR4XpWRTytEYRTG3/tHLGQr"
71 "NfEBpKQkmc8gG0ulxpRsfQ0TTXbI3kZ2ys4tsyBZjN1N8idlYUYaxJzzOKf7ztG1MfN0ens 69/x6"
72 "n/eeYPuo7npS9bCOriXDMQOCTVbO3X+6Wp9mICZWAHqquE1qAA8EQRiFcmYMsQKsAHM2DeK V4qj4"
73 "8ZHB++bHwcXTQ+Mz6u/rABwSIQO0wKW5wuLU8E5yWzlJS7OFvaUJMGelAFkkYici1NIXB4D QbH1J"
74 "Y2ggVoD4N1Kb2AAA149vUmLLxbHaTaNynIJggAz7G7jTyjB5wOv79/LupT47Cu0rcT6S/ai 755YU"
75 "iMM4cgw4/I3ERAaI3y/PiFnYPHOBtfORLKX38AaBERaJbHFeAOY3Eu8ZOYByi7Pl+hzWN+n wWjVB"
76 "15LhYHXr1PWiH0AYTw4BBjO9AAAAAElFTkSuQmCC", AtomicString::ConstructFromL iteral));
77
78 RefPtr<HTMLDivElement> altText = HTMLDivElement::create(element.document());
79 container->appendChild(altText);
80 altText->setAttribute(idAttr, AtomicString("alttext", AtomicString::Construc tFromLiteral));
81
82 RefPtr<Text> text = Text::create(element.document(), toHTMLElement(element). altText());
83 altText->appendChild(text);
84 }
85
86 static PassRefPtr<RenderStyle> customStyleForAltText(Element& element, PassRefPt r<RenderStyle> newStyle)
87 {
88 // If we have an author shadow root or have not created the UA shadow root y et, bail early. We can't
89 // use ensureUserAgentShadowRoot() here because that would alter the DOM tre e during style recalc.
90 if (element.shadowRoot() || !element.userAgentShadowRoot())
91 return newStyle;
92
93 Element* placeHolder = element.userAgentShadowRoot()->getElementById("alttex t-container");
94 Element* brokenImage = element.userAgentShadowRoot()->getElementById("alttex t-image");
95
96 if (element.document().inQuirksMode()) {
97 // Mimic the behaviour of the image host by setting symmetric dimensions if only one dimension is specified.
98 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isA uto())
99 newStyle->setHeight(newStyle->width());
100 else if (newStyle->height().isSpecifiedOrIntrinsic() && newStyle->width( ).isAuto())
101 newStyle->setWidth(newStyle->height());
102 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isS pecifiedOrIntrinsic()) {
103 placeHolder->setInlineStyleProperty(CSSPropertyVerticalAlign, CSSVal ueBaseline);
104 }
105 }
106
107 // If the image has specified dimensions allow the alt-text container expand to fill them.
108 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isSpeci fiedOrIntrinsic()) {
109 placeHolder->setInlineStyleProperty(CSSPropertyWidth, 100, CSSPrimitiveV alue::CSS_PERCENTAGE);
110 placeHolder->setInlineStyleProperty(CSSPropertyHeight, 100, CSSPrimitive Value::CSS_PERCENTAGE);
111 }
112
113 // Make sure the broken image icon appears on the appropriate side of the im age for the element's writing direction.
114 brokenImage->setInlineStyleProperty(CSSPropertyFloat, AtomicString(newStyle- >direction() == LTR ? "left" : "right"));
115
116 // This is an <img> with no attributes, so don't display anything.
117 if (noImageSourceSpecified(element) && !newStyle->width().isSpecifiedOrIntri nsic() && !newStyle->height().isSpecifiedOrIntrinsic() && toHTMLElement(element) .altText().isEmpty())
118 newStyle->setDisplay(NONE);
119
120 // This preserves legacy behaviour originally defined when alt-text was mana ged by RenderImage.
121 if (noImageSourceSpecified(element))
122 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueNone);
123 else
124 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueInline);
125
126 return newStyle;
127 }
128
129 }
130
131 #endif
OLDNEW
« no previous file with comments | « Source/core/html/HTMLImageElement.cpp ('k') | Source/core/html/HTMLImageLoader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698