OLD | NEW |
(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) |
| 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 Text* createAltTextShadowTree(Element& element) |
| 51 { |
| 52 ShadowRoot& root = element.ensureUserAgentShadowRoot(); |
| 53 RefPtr<HTMLStyleElement> style = HTMLStyleElement::create(element.document()
, false); |
| 54 style->setTextContent("#alttext-container { overflow: hidden; border: 1px so
lid silver; padding: 1px; display: inline-block; box-sizing: border-box; }" |
| 55 "#alttext { overflow: hidden; display: block; }" |
| 56 "#alttext-image { margin: 0px; }"); |
| 57 root.appendChild(style.release()); |
| 58 |
| 59 RefPtr<HTMLDivElement> container = HTMLDivElement::create(element.document()
); |
| 60 container->setAttribute(idAttr, AtomicString("alttext-container", AtomicStri
ng::ConstructFromLiteral)); |
| 61 root.appendChild(container); |
| 62 |
| 63 RefPtr<HTMLImageElement> brokenImage = HTMLImageElement::create(element.docu
ment()); |
| 64 brokenImage->setIsFallbackImage(); |
| 65 container->appendChild(brokenImage); |
| 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 altText->setAttribute(idAttr, AtomicString("alttext", AtomicString::Construc
tFromLiteral)); |
| 80 container->appendChild(altText); |
| 81 |
| 82 RefPtr<Text> text = Text::create(element.document(), toHTMLElement(element).
altText()); |
| 83 altText->appendChild(text); |
| 84 return text.get(); |
| 85 } |
| 86 |
| 87 static PassRefPtr<RenderStyle> customStyleForAltText(Element& element, PassRefPt
r<RenderStyle> newStyle) |
| 88 { |
| 89 Element* placeHolder = element.ensureUserAgentShadowRoot().getElementById("a
lttext-container"); |
| 90 Element* brokenImage = element.ensureUserAgentShadowRoot().getElementById("a
lttext-image"); |
| 91 |
| 92 if (element.shadowRoot()) |
| 93 return newStyle; |
| 94 |
| 95 if (element.document().inQuirksMode()) { |
| 96 // Mimic the behaviour of the image host by setting symmetric dimensions
if only one dimension is specified. |
| 97 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isA
uto()) |
| 98 newStyle->setHeight(newStyle->width()); |
| 99 else if (newStyle->height().isSpecifiedOrIntrinsic() && newStyle->width(
).isAuto()) |
| 100 newStyle->setWidth(newStyle->height()); |
| 101 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isS
pecifiedOrIntrinsic()) { |
| 102 placeHolder->setInlineStyleProperty(CSSPropertyVerticalAlign, CSSVal
ueBaseline); |
| 103 } |
| 104 } |
| 105 |
| 106 // If the image has specified dimensions allow the alt-text container expand
to fill them. |
| 107 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isSpeci
fiedOrIntrinsic()) { |
| 108 placeHolder->setInlineStyleProperty(CSSPropertyWidth, 100, CSSPrimitiveV
alue::CSS_PERCENTAGE); |
| 109 placeHolder->setInlineStyleProperty(CSSPropertyHeight, 100, CSSPrimitive
Value::CSS_PERCENTAGE); |
| 110 } |
| 111 |
| 112 // Make sure the broken image icon appears on the appropriate side of the im
age for the element's writing direction. |
| 113 brokenImage->setAttribute(alignAttr, AtomicString(newStyle->direction() == L
TR ? "left" : "right")); |
| 114 |
| 115 // This is an <img> with no attributes, so don't display anything. |
| 116 if (noImageSourceSpecified(element) && !newStyle->width().isSpecifiedOrIntri
nsic() && !newStyle->height().isSpecifiedOrIntrinsic() && toHTMLElement(element)
.altText().isEmpty()) |
| 117 newStyle->setDisplay(NONE); |
| 118 |
| 119 // This preserves legacy behaviour originally defined when alt-text was mana
ged by RenderImage. |
| 120 if (noImageSourceSpecified(element)) |
| 121 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueNone); |
| 122 else |
| 123 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueInline); |
| 124 |
| 125 return newStyle; |
| 126 } |
| 127 |
| 128 } |
| 129 |
| 130 #endif |
OLD | NEW |