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->setInlineStyleProperty(CSSPropertyMargin, 0, CSSPrimitiveValue:
:CSS_PX); |
| 58 |
| 59 RefPtrWillBeRawPtr<HTMLDivElement> altText = HTMLDivElement::create(element.
document()); |
| 60 container->appendChild(altText); |
| 61 altText->setInlineStyleProperty(CSSPropertyOverflow, CSSValueHidden); |
| 62 altText->setInlineStyleProperty(CSSPropertyDisplay, CSSValueBlock); |
| 63 |
| 64 RefPtrWillBeRawPtr<Text> text = Text::create(element.document(), toHTMLEleme
nt(element).altText()); |
| 65 altText->appendChild(text); |
| 66 } |
| 67 |
| 68 PassRefPtr<RenderStyle> HTMLImageFallbackHelper::customStyleForAltText(Element&
element, PassRefPtr<RenderStyle> newStyle) |
| 69 { |
| 70 // If we have an author shadow root or have not created the UA shadow root y
et, bail early. We can't |
| 71 // use ensureUserAgentShadowRoot() here because that would alter the DOM tre
e during style recalc. |
| 72 if (element.shadowRoot() || !element.userAgentShadowRoot()) |
| 73 return newStyle; |
| 74 |
| 75 Element* placeHolder = element.userAgentShadowRoot()->getElementById("alttex
t-container"); |
| 76 Element* brokenImage = element.userAgentShadowRoot()->getElementById("alttex
t-image"); |
| 77 |
| 78 if (element.document().inQuirksMode()) { |
| 79 // Mimic the behaviour of the image host by setting symmetric dimensions
if only one dimension is specified. |
| 80 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isA
uto()) |
| 81 newStyle->setHeight(newStyle->width()); |
| 82 else if (newStyle->height().isSpecifiedOrIntrinsic() && newStyle->width(
).isAuto()) |
| 83 newStyle->setWidth(newStyle->height()); |
| 84 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isS
pecifiedOrIntrinsic()) { |
| 85 placeHolder->setInlineStyleProperty(CSSPropertyVerticalAlign, CSSVal
ueBaseline); |
| 86 } |
| 87 } |
| 88 |
| 89 // If the image has specified dimensions allow the alt-text container expand
to fill them. |
| 90 if (newStyle->width().isSpecifiedOrIntrinsic() && newStyle->height().isSpeci
fiedOrIntrinsic()) { |
| 91 placeHolder->setInlineStyleProperty(CSSPropertyWidth, 100, CSSPrimitiveV
alue::CSS_PERCENTAGE); |
| 92 placeHolder->setInlineStyleProperty(CSSPropertyHeight, 100, CSSPrimitive
Value::CSS_PERCENTAGE); |
| 93 } |
| 94 |
| 95 // Make sure the broken image icon appears on the appropriate side of the im
age for the element's writing direction. |
| 96 brokenImage->setInlineStyleProperty(CSSPropertyFloat, AtomicString(newStyle-
>direction() == LTR ? "left" : "right")); |
| 97 |
| 98 // This is an <img> with no attributes, so don't display anything. |
| 99 if (noImageSourceSpecified(element) && !newStyle->width().isSpecifiedOrIntri
nsic() && !newStyle->height().isSpecifiedOrIntrinsic() && toHTMLElement(element)
.altText().isEmpty()) |
| 100 newStyle->setDisplay(NONE); |
| 101 |
| 102 // This preserves legacy behaviour originally defined when alt-text was mana
ged by RenderImage. |
| 103 if (noImageSourceSpecified(element)) |
| 104 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueNone); |
| 105 else |
| 106 brokenImage->setInlineStyleProperty(CSSPropertyDisplay, CSSValueInline); |
| 107 |
| 108 return newStyle; |
| 109 } |
| 110 |
| 111 } // namespace blink |
| 112 |
OLD | NEW |