OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/html/HTMLImageFallbackHelper.h" | 5 #include "core/html/HTMLImageFallbackHelper.h" |
6 | 6 |
7 #include "core/HTMLNames.h" | 7 #include "core/HTMLNames.h" |
8 #include "core/InputTypeNames.h" | 8 #include "core/InputTypeNames.h" |
9 #include "core/dom/ElementRareData.h" | 9 #include "core/dom/ElementRareData.h" |
10 #include "core/dom/Text.h" | 10 #include "core/dom/Text.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
97 bool image_has_intrinsic_dimensions = | 97 bool image_has_intrinsic_dimensions = |
98 new_style->Width().IsSpecifiedOrIntrinsic() && | 98 new_style->Width().IsSpecifiedOrIntrinsic() && |
99 new_style->Height().IsSpecifiedOrIntrinsic(); | 99 new_style->Height().IsSpecifiedOrIntrinsic(); |
100 bool image_has_no_alt_attribute = ToHTMLElement(element).AltText().IsNull(); | 100 bool image_has_no_alt_attribute = ToHTMLElement(element).AltText().IsNull(); |
101 bool treat_as_replaced = | 101 bool treat_as_replaced = |
102 image_has_intrinsic_dimensions && | 102 image_has_intrinsic_dimensions && |
103 (element.GetDocument().InQuirksMode() || image_has_no_alt_attribute); | 103 (element.GetDocument().InQuirksMode() || image_has_no_alt_attribute); |
104 // 16px for the image and 2px for its top/left border/padding offset. | 104 if (treat_as_replaced) { |
105 int pixels_for_alt_image = 18; | |
106 if (treat_as_replaced && | |
107 !ImageSmallerThanAltImage(pixels_for_alt_image, new_style->Width(), | |
108 new_style->Height())) { | |
109 // https://html.spec.whatwg.org/multipage/rendering.html#images-3: | 105 // https://html.spec.whatwg.org/multipage/rendering.html#images-3: |
110 // "If the element does not represent an image, but the element already has | 106 // "If the element does not represent an image, but the element already has |
111 // intrinsic dimensions (e.g. from the dimension attributes or CSS rules), | 107 // intrinsic dimensions (e.g. from the dimension attributes or CSS rules), |
112 // and either: the user agent has reason to believe that the image will | 108 // and either: the user agent has reason to believe that the image will |
113 // become available and be rendered in due course, or the element has no alt | 109 // become available and be rendered in due course, or the element has no alt |
114 // attribute, or the Document is in quirks mode The user agent is expected | 110 // attribute, or the Document is in quirks mode The user agent is expected |
115 // to treat the element as a replaced element whose content is the text that | 111 // to treat the element as a replaced element whose content is the text that |
116 // the element represents, if any." | 112 // the element represents, if any." |
117 place_holder->SetInlineStyleProperty(CSSPropertyOverflow, CSSValueHidden); | 113 place_holder->SetInlineStyleProperty(CSSPropertyOverflow, CSSValueHidden); |
118 place_holder->SetInlineStyleProperty(CSSPropertyDisplay, | 114 place_holder->SetInlineStyleProperty(CSSPropertyDisplay, |
119 CSSValueInlineBlock); | 115 CSSValueInlineBlock); |
120 place_holder->SetInlineStyleProperty(CSSPropertyBorderWidth, 1, | |
121 CSSPrimitiveValue::UnitType::kPixels); | |
122 place_holder->SetInlineStyleProperty(CSSPropertyBorderStyle, CSSValueSolid); | |
123 place_holder->SetInlineStyleProperty(CSSPropertyBorderColor, | |
124 CSSValueSilver); | |
125 place_holder->SetInlineStyleProperty(CSSPropertyPadding, 1, | |
126 CSSPrimitiveValue::UnitType::kPixels); | |
127 place_holder->SetInlineStyleProperty(CSSPropertyBoxSizing, | |
128 CSSValueBorderBox); | |
129 CSSPrimitiveValue::UnitType unit = | 116 CSSPrimitiveValue::UnitType unit = |
130 new_style->Height().IsPercent() | 117 new_style->Height().IsPercent() |
131 ? CSSPrimitiveValue::UnitType::kPercentage | 118 ? CSSPrimitiveValue::UnitType::kPercentage |
132 : CSSPrimitiveValue::UnitType::kPixels; | 119 : CSSPrimitiveValue::UnitType::kPixels; |
133 place_holder->SetInlineStyleProperty(CSSPropertyHeight, | 120 place_holder->SetInlineStyleProperty(CSSPropertyHeight, |
134 new_style->Height().Value(), unit); | 121 new_style->Height().Value(), unit); |
135 place_holder->SetInlineStyleProperty(CSSPropertyWidth, | 122 place_holder->SetInlineStyleProperty(CSSPropertyWidth, |
136 new_style->Width().Value(), unit); | 123 new_style->Width().Value(), unit); |
137 broken_image->SetInlineStyleProperty(CSSPropertyDisplay, CSSValueInline); | 124 |
138 // Make sure the broken image icon appears on the appropriate side of the | 125 // 16px for the image and 2px for its top/left border/padding offset. |
139 // image for the element's writing direction. | 126 int pixels_for_alt_image = 18; |
140 broken_image->SetInlineStyleProperty( | 127 if (ImageSmallerThanAltImage(pixels_for_alt_image, new_style->Width(), |
141 CSSPropertyFloat, | 128 new_style->Height())) { |
142 AtomicString(new_style->Direction() == TextDirection::kLtr ? "left" | 129 broken_image->SetInlineStyleProperty(CSSPropertyDisplay, CSSValueNone); |
143 : "right")); | 130 } else { |
| 131 place_holder->SetInlineStyleProperty( |
| 132 CSSPropertyBorderWidth, 1, CSSPrimitiveValue::UnitType::kPixels); |
| 133 place_holder->SetInlineStyleProperty(CSSPropertyBorderStyle, |
| 134 CSSValueSolid); |
| 135 place_holder->SetInlineStyleProperty(CSSPropertyBorderColor, |
| 136 CSSValueSilver); |
| 137 place_holder->SetInlineStyleProperty( |
| 138 CSSPropertyPadding, 1, CSSPrimitiveValue::UnitType::kPixels); |
| 139 place_holder->SetInlineStyleProperty(CSSPropertyBoxSizing, |
| 140 CSSValueBorderBox); |
| 141 broken_image->SetInlineStyleProperty(CSSPropertyDisplay, CSSValueInline); |
| 142 // Make sure the broken image icon appears on the appropriate side of the |
| 143 // image for the element's writing direction. |
| 144 broken_image->SetInlineStyleProperty( |
| 145 CSSPropertyFloat, |
| 146 AtomicString(new_style->Direction() == TextDirection::kLtr |
| 147 ? "left" |
| 148 : "right")); |
| 149 } |
144 } else { | 150 } else { |
145 // "If the element is an img element that represents nothing and the user | 151 // "If the element is an img element that represents nothing and the user |
146 // agent does not expect this to change the user agent is expected to treat | 152 // agent does not expect this to change the user agent is expected to treat |
147 // the element as an empty inline element." | 153 // the element as an empty inline element." |
148 // - We achieve this by hiding the broken image so that the span is empty. | 154 // - We achieve this by hiding the broken image so that the span is empty. |
149 // "If the element is an img element that represents some text and the user | 155 // "If the element is an img element that represents some text and the user |
150 // agent does not expect this to change the user agent is expected to treat | 156 // agent does not expect this to change the user agent is expected to treat |
151 // the element as a non-replaced phrasing element whose content is the text, | 157 // the element as a non-replaced phrasing element whose content is the text, |
152 // optionally with an icon indicating that an image is missing, so that the | 158 // optionally with an icon indicating that an image is missing, so that the |
153 // user can request the image be displayed or investigate why it is not | 159 // user can request the image be displayed or investigate why it is not |
154 // rendering." | 160 // rendering." |
155 // - We opt not to display an icon, like Firefox. | 161 // - We opt not to display an icon, like Firefox. |
156 if (!treat_as_replaced && new_style->Display() == EDisplay::kInline) { | 162 if (new_style->Display() == EDisplay::kInline) { |
157 new_style->SetWidth(Length()); | 163 new_style->SetWidth(Length()); |
158 new_style->SetHeight(Length()); | 164 new_style->SetHeight(Length()); |
159 } | 165 } |
160 broken_image->SetInlineStyleProperty(CSSPropertyDisplay, CSSValueNone); | 166 broken_image->SetInlineStyleProperty(CSSPropertyDisplay, CSSValueNone); |
161 } | 167 } |
162 | 168 |
163 return new_style; | 169 return new_style; |
164 } | 170 } |
165 | 171 |
166 } // namespace blink | 172 } // namespace blink |
OLD | NEW |