OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "config.h" | |
6 #include "core/html/HTMLImageFallbackHelper.h" | |
7 | |
8 #include "core/HTMLNames.h" | |
9 #include "core/InputTypeNames.h" | |
10 #include "core/dom/ElementRareData.h" | |
11 #include "core/dom/Text.h" | |
12 #include "core/dom/shadow/ShadowRoot.h" | |
13 #include "core/fetch/ImageResource.h" | |
14 #include "core/html/FormDataList.h" | |
15 #include "core/html/HTMLDivElement.h" | |
16 #include "core/html/HTMLElement.h" | |
17 #include "core/html/HTMLImageElement.h" | |
18 #include "core/html/HTMLImageLoader.h" | |
19 #include "core/html/HTMLInputElement.h" | |
20 #include "core/html/HTMLStyleElement.h" | |
21 #include "wtf/PassOwnPtr.h" | |
22 #include "wtf/text/StringBuilder.h" | |
23 | |
24 namespace blink { | |
25 | |
26 using namespace HTMLNames; | |
27 | |
28 static bool noImageSourceSpecified(const Element& element) | |
29 { | |
30 bool noSrcSpecified = !element.hasAttribute(srcAttr) || element.getAttribute (srcAttr).isNull() || element.getAttribute(srcAttr).isEmpty(); | |
31 bool noSrcsetSpecified = !element.hasAttribute(srcsetAttr) || element.getAtt ribute(srcsetAttr).isNull() || element.getAttribute(srcsetAttr).isEmpty(); | |
32 return noSrcSpecified && noSrcsetSpecified; | |
33 } | |
34 | |
35 void HTMLImageFallbackHelper::createAltTextShadowTree(Element& element) | |
36 { | |
37 ShadowRoot& root = element.ensureUserAgentShadowRoot(); | |
38 | |
39 RefPtrWillBeRawPtr<HTMLDivElement> container = HTMLDivElement::create(elemen t.document()); | |
40 root.appendChild(container); | |
41 container->setAttribute(idAttr, AtomicString("alttext-container", AtomicStri ng::ConstructFromLiteral)); | |
42 container->setInlineStyleProperty(CSSPropertyOverflow, CSSValueHidden); | |
43 container->setInlineStyleProperty(CSSPropertyBorderWidth, 1, CSSPrimitiveVal ue::CSS_PX); | |
44 container->setInlineStyleProperty(CSSPropertyBorderStyle, CSSValueSolid); | |
45 container->setInlineStyleProperty(CSSPropertyBorderColor, CSSValueSilver); | |
46 container->setInlineStyleProperty(CSSPropertyDisplay, CSSValueInlineBlock); | |
47 container->setInlineStyleProperty(CSSPropertyBoxSizing, CSSValueBorderBox); | |
48 container->setInlineStyleProperty(CSSPropertyPadding, 1, CSSPrimitiveValue:: CSS_PX); | |
49 | |
50 RefPtrWillBeRawPtr<HTMLImageElement> brokenImage = HTMLImageElement::create( element.document()); | |
51 container->appendChild(brokenImage); | |
52 brokenImage->setIsFallbackImage(); | |
53 brokenImage->setAttribute(idAttr, AtomicString("alttext-image", AtomicString ::ConstructFromLiteral)); | |
54 brokenImage->setAttribute(widthAttr, AtomicString("16", AtomicString::Constr uctFromLiteral)); | |
55 brokenImage->setAttribute(heightAttr, AtomicString("16", AtomicString::Const ructFromLiteral)); | |
56 brokenImage->setAttribute(alignAttr, AtomicString("left", AtomicString::Cons tructFromLiteral)); | |
57 brokenImage->setAttribute(srcAttr, AtomicString("data:image/png;base64," | |
pdr.
2014/11/14 19:47:59
I'd still like to use brokenImage here if possible
| |
58 "iVBORw0KGgoAAAANSUhEUgAAAA4AAAAQCAYAAAAmlE46AAABh0lEQVQoU42QSS9DURiGv7V fhKWE" | |
59 "nyA2WFizsLciJGrowDVU55VIjE3sxFhB1ZDiVlUapYYguLRor9f5DrdRuaJv8izOOc+zORQ IBFAK" | |
60 "RFRGP+f3B6BlP/CYMecpo0NRFLBXFPt8fpzf6ThO50xJ3rzLkMduIfZ6vTi51hE5fZMYM85 q6rUQ" | |
61 "8tiXsdvtQfxKx3biFb/Hd0citNlsRXBDLpdbhmbbimcRiWu4uMvhXstL+D+4IadzDPFLHRv HL6aE" | |
62 "TzLYT2ZxcPZF6lYHNzQ66kQsnUdIfS4Jdrmh4eERqOKwdqiVBLuiIVKUIagXeaxEtQLL0Uf MRKII" | |
63 "7sSK7hnhckM0OKjgSISLew8ST2getePlqJuskDTPNWI2rBrvJFxuiByOARye57Gw+wDbklf IlYXI" | |
64 "oH6yChObERIOCVc2ZLc7ZOhaDZpG31DDVDXNhRMcghuyWu1YT6TROF3zZ2TQEmxCNPUObqi /34q2" | |
65 "hdZ/I+Oud60HvdY+UGdnF9rbO8ygHxS9dVss+AQqZY47NSC2iwAAAABJRU5ErkJggg==", AtomicString::ConstructFromLiteral)); | |
66 brokenImage->setInlineStyleProperty(CSSPropertyMargin, 0, CSSPrimitiveValue: :CSS_PX); | |
67 | |
68 RefPtrWillBeRawPtr<HTMLDivElement> altText = HTMLDivElement::create(element. document()); | |
69 container->appendChild(altText); | |
70 altText->setInlineStyleProperty(CSSPropertyOverflow, CSSValueHidden); | |
71 altText->setInlineStyleProperty(CSSPropertyDisplay, CSSValueBlock); | |
72 | |
73 RefPtrWillBeRawPtr<Text> text = Text::create(element.document(), toHTMLEleme nt(element).altText()); | |
74 altText->appendChild(text); | |
75 } | |
76 | |
77 PassRefPtr<RenderStyle> HTMLImageFallbackHelper::customStyleForAltText(Element& element, PassRefPtr<RenderStyle> newStyle) | |
78 { | |
79 // If we have an author shadow root or have not created the UA shadow root y et, bail early. We can't | |
80 // use ensureUserAgentShadowRoot() here because that would alter the DOM tre e during style recalc. | |
81 if (element.shadowRoot() || !element.userAgentShadowRoot()) | |
82 return newStyle; | |
83 | |
84 Element* placeHolder = element.userAgentShadowRoot()->getElementById("alttex t-container"); | |
85 Element* brokenImage = element.userAgentShadowRoot()->getElementById("alttex t-image"); | |
86 | |
87 if (element.document().inQuirksMode()) { | |
88 // Mimic the behaviour of the image host by setting symmetric dimensions if only one dimension is specified. | |
89 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isA uto()) | |
90 newStyle->setHeight(newStyle->width()); | |
91 else if (newStyle->height().isSpecifiedOrIntrinsic() && newStyle->width( ).isAuto()) | |
92 newStyle->setWidth(newStyle->height()); | |
93 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isS pecifiedOrIntrinsic()) { | |
94 placeHolder->setInlineStyleProperty(CSSPropertyVerticalAlign, CSSVal ueBaseline); | |
95 } | |
96 } | |
97 | |
98 // If the image has specified dimensions allow the alt-text container expand to fill them. | |
99 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isSpeci fiedOrIntrinsic()) { | |
100 placeHolder->setInlineStyleProperty(CSSPropertyWidth, 100, CSSPrimitiveV alue::CSS_PERCENTAGE); | |
101 placeHolder->setInlineStyleProperty(CSSPropertyHeight, 100, CSSPrimitive Value::CSS_PERCENTAGE); | |
102 } | |
103 | |
104 // Make sure the broken image icon appears on the appropriate side of the im age for the element's writing direction. | |
105 brokenImage->setInlineStyleProperty(CSSPropertyFloat, AtomicString(newStyle- >direction() == LTR ? "left" : "right")); | |
106 | |
107 // This is an <img> with no attributes, so don't display anything. | |
108 if (noImageSourceSpecified(element) && !newStyle->width().isSpecifiedOrIntri nsic() && !newStyle->height().isSpecifiedOrIntrinsic() && toHTMLElement(element) .altText().isEmpty()) | |
109 newStyle->setDisplay(NONE); | |
110 | |
111 // This preserves legacy behaviour originally defined when alt-text was mana ged by RenderImage. | |
112 if (noImageSourceSpecified(element)) | |
113 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueNone); | |
114 else | |
115 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueInline); | |
116 | |
117 return newStyle; | |
118 } | |
119 | |
120 } // namespace blink | |
121 | |
OLD | NEW |