author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 03 May 2010 13:17:34 +0300 | |
changeset 19 | fcece45ef507 |
parent 0 | 1918ee327afb |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
|
3 |
* (C) 1999 Antti Koivisto (koivisto@kde.org) |
|
4 |
* Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
|
5 |
* |
|
6 |
* This library is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU Library General Public |
|
8 |
* License as published by the Free Software Foundation; either |
|
9 |
* version 2 of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This library is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* Library General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU Library General Public License |
|
17 |
* along with this library; see the file COPYING.LIB. If not, write to |
|
18 |
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
19 |
* Boston, MA 02110-1301, USA. |
|
20 |
*/ |
|
21 |
||
22 |
#include "config.h" |
|
23 |
#include "HTMLImageElement.h" |
|
24 |
||
25 |
#include "CSSHelper.h" |
|
26 |
#include "CSSPropertyNames.h" |
|
27 |
#include "CSSValueKeywords.h" |
|
28 |
#include "EventNames.h" |
|
29 |
#include "Frame.h" |
|
30 |
#include "HTMLDocument.h" |
|
31 |
#include "HTMLFormElement.h" |
|
32 |
#include "HTMLNames.h" |
|
33 |
#include "MappedAttribute.h" |
|
34 |
#include "RenderImage.h" |
|
35 |
#include "ScriptEventListener.h" |
|
36 |
||
37 |
using namespace std; |
|
38 |
||
39 |
namespace WebCore { |
|
40 |
||
41 |
using namespace HTMLNames; |
|
42 |
||
43 |
HTMLImageElement::HTMLImageElement(const QualifiedName& tagName, Document* doc, HTMLFormElement* form) |
|
44 |
: HTMLElement(tagName, doc) |
|
45 |
, m_imageLoader(this) |
|
46 |
, ismap(false) |
|
47 |
, m_form(form) |
|
48 |
, m_compositeOperator(CompositeSourceOver) |
|
49 |
{ |
|
50 |
ASSERT(hasTagName(imgTag)); |
|
51 |
if (form) |
|
52 |
form->registerImgElement(this); |
|
53 |
} |
|
54 |
||
55 |
HTMLImageElement::~HTMLImageElement() |
|
56 |
{ |
|
57 |
if (m_form) |
|
58 |
m_form->removeImgElement(this); |
|
59 |
} |
|
60 |
||
61 |
bool HTMLImageElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const |
|
62 |
{ |
|
63 |
if (attrName == widthAttr || |
|
64 |
attrName == heightAttr || |
|
65 |
attrName == vspaceAttr || |
|
66 |
attrName == hspaceAttr || |
|
67 |
attrName == valignAttr) { |
|
68 |
result = eUniversal; |
|
69 |
return false; |
|
70 |
} |
|
71 |
||
72 |
if (attrName == borderAttr || attrName == alignAttr) { |
|
73 |
result = eReplaced; // Shared with embed and iframe elements. |
|
74 |
return false; |
|
75 |
} |
|
76 |
||
77 |
return HTMLElement::mapToEntry(attrName, result); |
|
78 |
} |
|
79 |
||
80 |
void HTMLImageElement::parseMappedAttribute(MappedAttribute* attr) |
|
81 |
{ |
|
82 |
const QualifiedName& attrName = attr->name(); |
|
83 |
if (attrName == altAttr) { |
|
84 |
if (renderer() && renderer()->isImage()) |
|
85 |
toRenderImage(renderer())->updateAltText(); |
|
86 |
} else if (attrName == srcAttr) |
|
87 |
m_imageLoader.updateFromElementIgnoringPreviousError(); |
|
88 |
else if (attrName == widthAttr) |
|
89 |
addCSSLength(attr, CSSPropertyWidth, attr->value()); |
|
90 |
else if (attrName == heightAttr) |
|
91 |
addCSSLength(attr, CSSPropertyHeight, attr->value()); |
|
92 |
else if (attrName == borderAttr) { |
|
93 |
// border="noborder" -> border="0" |
|
94 |
addCSSLength(attr, CSSPropertyBorderWidth, attr->value().toInt() ? attr->value() : "0"); |
|
95 |
addCSSProperty(attr, CSSPropertyBorderTopStyle, CSSValueSolid); |
|
96 |
addCSSProperty(attr, CSSPropertyBorderRightStyle, CSSValueSolid); |
|
97 |
addCSSProperty(attr, CSSPropertyBorderBottomStyle, CSSValueSolid); |
|
98 |
addCSSProperty(attr, CSSPropertyBorderLeftStyle, CSSValueSolid); |
|
99 |
} else if (attrName == vspaceAttr) { |
|
100 |
addCSSLength(attr, CSSPropertyMarginTop, attr->value()); |
|
101 |
addCSSLength(attr, CSSPropertyMarginBottom, attr->value()); |
|
102 |
} else if (attrName == hspaceAttr) { |
|
103 |
addCSSLength(attr, CSSPropertyMarginLeft, attr->value()); |
|
104 |
addCSSLength(attr, CSSPropertyMarginRight, attr->value()); |
|
105 |
} else if (attrName == alignAttr) |
|
106 |
addHTMLAlignment(attr); |
|
107 |
else if (attrName == valignAttr) |
|
108 |
addCSSProperty(attr, CSSPropertyVerticalAlign, attr->value()); |
|
109 |
else if (attrName == usemapAttr) { |
|
110 |
if (attr->value().string()[0] == '#') |
|
111 |
usemap = attr->value(); |
|
112 |
else |
|
113 |
usemap = document()->completeURL(deprecatedParseURL(attr->value())).string(); |
|
114 |
setIsLink(!attr->isNull()); |
|
115 |
} else if (attrName == ismapAttr) |
|
116 |
ismap = true; |
|
117 |
else if (attrName == onabortAttr) |
|
118 |
setAttributeEventListener(eventNames().abortEvent, createAttributeEventListener(this, attr)); |
|
119 |
else if (attrName == onloadAttr) |
|
120 |
setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr)); |
|
121 |
else if (attrName == onbeforeloadAttr) |
|
122 |
setAttributeEventListener(eventNames().beforeloadEvent, createAttributeEventListener(this, attr)); |
|
123 |
else if (attrName == compositeAttr) { |
|
124 |
if (!parseCompositeOperator(attr->value(), m_compositeOperator)) |
|
125 |
m_compositeOperator = CompositeSourceOver; |
|
126 |
} else if (attrName == nameAttr) { |
|
127 |
const AtomicString& newName = attr->value(); |
|
128 |
if (inDocument() && document()->isHTMLDocument()) { |
|
129 |
HTMLDocument* document = static_cast<HTMLDocument*>(this->document()); |
|
130 |
document->removeNamedItem(m_name); |
|
131 |
document->addNamedItem(newName); |
|
132 |
} |
|
133 |
m_name = newName; |
|
134 |
} else if (attr->name() == idAttr) { |
|
135 |
const AtomicString& newId = attr->value(); |
|
136 |
if (inDocument() && document()->isHTMLDocument()) { |
|
137 |
HTMLDocument* document = static_cast<HTMLDocument*>(this->document()); |
|
138 |
document->removeExtraNamedItem(m_id); |
|
139 |
document->addExtraNamedItem(newId); |
|
140 |
} |
|
141 |
m_id = newId; |
|
142 |
// also call superclass |
|
143 |
HTMLElement::parseMappedAttribute(attr); |
|
144 |
} else |
|
145 |
HTMLElement::parseMappedAttribute(attr); |
|
146 |
} |
|
147 |
||
148 |
String HTMLImageElement::altText() const |
|
149 |
{ |
|
150 |
// lets figure out the alt text.. magic stuff |
|
151 |
// http://www.w3.org/TR/1998/REC-html40-19980424/appendix/notes.html#altgen |
|
152 |
// also heavily discussed by Hixie on bugzilla |
|
153 |
String alt = getAttribute(altAttr); |
|
154 |
// fall back to title attribute |
|
155 |
if (alt.isNull()) |
|
156 |
alt = getAttribute(titleAttr); |
|
157 |
return alt; |
|
158 |
} |
|
159 |
||
160 |
RenderObject* HTMLImageElement::createRenderer(RenderArena* arena, RenderStyle* style) |
|
161 |
{ |
|
162 |
if (style->contentData()) |
|
163 |
return RenderObject::createObject(this, style); |
|
164 |
||
165 |
return new (arena) RenderImage(this); |
|
166 |
} |
|
167 |
||
168 |
void HTMLImageElement::attach() |
|
169 |
{ |
|
170 |
HTMLElement::attach(); |
|
171 |
||
172 |
if (renderer() && renderer()->isImage() && m_imageLoader.haveFiredBeforeLoadEvent()) { |
|
173 |
RenderImage* imageObj = toRenderImage(renderer()); |
|
174 |
if (imageObj->hasImage()) |
|
175 |
return; |
|
176 |
imageObj->setCachedImage(m_imageLoader.image()); |
|
177 |
||
178 |
// If we have no image at all because we have no src attribute, set |
|
179 |
// image height and width for the alt text instead. |
|
180 |
if (!m_imageLoader.image() && !imageObj->cachedImage()) |
|
181 |
imageObj->setImageSizeForAltText(); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
void HTMLImageElement::insertedIntoDocument() |
|
186 |
{ |
|
187 |
if (document()->isHTMLDocument()) { |
|
188 |
HTMLDocument* document = static_cast<HTMLDocument*>(this->document()); |
|
189 |
document->addNamedItem(m_name); |
|
190 |
document->addExtraNamedItem(m_id); |
|
191 |
} |
|
192 |
||
193 |
// If we have been inserted from a renderer-less document, |
|
194 |
// our loader may have not fetched the image, so do it now. |
|
195 |
if (!m_imageLoader.image()) |
|
196 |
m_imageLoader.updateFromElement(); |
|
197 |
||
198 |
HTMLElement::insertedIntoDocument(); |
|
199 |
} |
|
200 |
||
201 |
void HTMLImageElement::removedFromDocument() |
|
202 |
{ |
|
203 |
if (document()->isHTMLDocument()) { |
|
204 |
HTMLDocument* document = static_cast<HTMLDocument*>(this->document()); |
|
205 |
document->removeNamedItem(m_name); |
|
206 |
document->removeExtraNamedItem(m_id); |
|
207 |
} |
|
208 |
||
209 |
HTMLElement::removedFromDocument(); |
|
210 |
} |
|
211 |
||
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
void HTMLImageElement::insertedIntoTree(bool deep) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
213 |
{ |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
214 |
if (!m_form) { |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
215 |
// m_form can be non-null if it was set in constructor. |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
216 |
for (Node* ancestor = parentNode(); ancestor; ancestor = ancestor->parentNode()) { |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
217 |
if (ancestor->hasTagName(formTag)) { |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
m_form = static_cast<HTMLFormElement*>(ancestor); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
m_form->registerImgElement(this); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
break; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
} |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
} |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
} |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
224 |
|
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
225 |
HTMLElement::insertedIntoTree(deep); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
226 |
} |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
227 |
|
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
228 |
void HTMLImageElement::removedFromTree(bool deep) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
229 |
{ |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
230 |
if (m_form) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
m_form->removeImgElement(this); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
232 |
m_form = 0; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
233 |
HTMLElement::removedFromTree(deep); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
234 |
} |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
|
0 | 236 |
int HTMLImageElement::width(bool ignorePendingStylesheets) const |
237 |
{ |
|
238 |
if (!renderer()) { |
|
239 |
// check the attribute first for an explicit pixel value |
|
240 |
bool ok; |
|
241 |
int width = getAttribute(widthAttr).toInt(&ok); |
|
242 |
if (ok) |
|
243 |
return width; |
|
244 |
||
245 |
// if the image is available, use its width |
|
246 |
if (m_imageLoader.image()) { |
|
247 |
float zoomFactor = document()->frame() ? document()->frame()->pageZoomFactor() : 1.0f; |
|
248 |
return m_imageLoader.image()->imageSize(zoomFactor).width(); |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
if (ignorePendingStylesheets) |
|
253 |
document()->updateLayoutIgnorePendingStylesheets(); |
|
254 |
else |
|
255 |
document()->updateLayout(); |
|
256 |
||
257 |
return renderBox() ? renderBox()->contentWidth() : 0; |
|
258 |
} |
|
259 |
||
260 |
int HTMLImageElement::height(bool ignorePendingStylesheets) const |
|
261 |
{ |
|
262 |
if (!renderer()) { |
|
263 |
// check the attribute first for an explicit pixel value |
|
264 |
bool ok; |
|
265 |
int height = getAttribute(heightAttr).toInt(&ok); |
|
266 |
if (ok) |
|
267 |
return height; |
|
268 |
||
269 |
// if the image is available, use its height |
|
270 |
if (m_imageLoader.image()) { |
|
271 |
float zoomFactor = document()->frame() ? document()->frame()->pageZoomFactor() : 1.0f; |
|
272 |
return m_imageLoader.image()->imageSize(zoomFactor).height(); |
|
273 |
} |
|
274 |
} |
|
275 |
||
276 |
if (ignorePendingStylesheets) |
|
277 |
document()->updateLayoutIgnorePendingStylesheets(); |
|
278 |
else |
|
279 |
document()->updateLayout(); |
|
280 |
||
281 |
return renderBox() ? renderBox()->contentHeight() : 0; |
|
282 |
} |
|
283 |
||
284 |
int HTMLImageElement::naturalWidth() const |
|
285 |
{ |
|
286 |
if (!m_imageLoader.image()) |
|
287 |
return 0; |
|
288 |
||
289 |
return m_imageLoader.image()->imageSize(1.0f).width(); |
|
290 |
} |
|
291 |
||
292 |
int HTMLImageElement::naturalHeight() const |
|
293 |
{ |
|
294 |
if (!m_imageLoader.image()) |
|
295 |
return 0; |
|
296 |
||
297 |
return m_imageLoader.image()->imageSize(1.0f).height(); |
|
298 |
} |
|
299 |
||
300 |
bool HTMLImageElement::isURLAttribute(Attribute* attr) const |
|
301 |
{ |
|
302 |
return attr->name() == srcAttr |
|
303 |
|| attr->name() == lowsrcAttr |
|
304 |
|| attr->name() == longdescAttr |
|
305 |
|| (attr->name() == usemapAttr && attr->value().string()[0] != '#'); |
|
306 |
} |
|
307 |
||
308 |
const AtomicString& HTMLImageElement::alt() const |
|
309 |
{ |
|
310 |
return getAttribute(altAttr); |
|
311 |
} |
|
312 |
||
313 |
bool HTMLImageElement::draggable() const |
|
314 |
{ |
|
315 |
// Image elements are draggable by default. |
|
316 |
return !equalIgnoringCase(getAttribute(draggableAttr), "false"); |
|
317 |
} |
|
318 |
||
319 |
void HTMLImageElement::setHeight(int value) |
|
320 |
{ |
|
321 |
setAttribute(heightAttr, String::number(value)); |
|
322 |
} |
|
323 |
||
324 |
int HTMLImageElement::hspace() const |
|
325 |
{ |
|
326 |
// ### return actual value |
|
327 |
return getAttribute(hspaceAttr).toInt(); |
|
328 |
} |
|
329 |
||
330 |
void HTMLImageElement::setHspace(int value) |
|
331 |
{ |
|
332 |
setAttribute(hspaceAttr, String::number(value)); |
|
333 |
} |
|
334 |
||
335 |
bool HTMLImageElement::isMap() const |
|
336 |
{ |
|
337 |
return !getAttribute(ismapAttr).isNull(); |
|
338 |
} |
|
339 |
||
340 |
void HTMLImageElement::setIsMap(bool isMap) |
|
341 |
{ |
|
342 |
setAttribute(ismapAttr, isMap ? "" : 0); |
|
343 |
} |
|
344 |
||
345 |
KURL HTMLImageElement::longDesc() const |
|
346 |
{ |
|
347 |
return document()->completeURL(getAttribute(longdescAttr)); |
|
348 |
} |
|
349 |
||
350 |
void HTMLImageElement::setLongDesc(const String& value) |
|
351 |
{ |
|
352 |
setAttribute(longdescAttr, value); |
|
353 |
} |
|
354 |
||
355 |
KURL HTMLImageElement::lowsrc() const |
|
356 |
{ |
|
357 |
return document()->completeURL(getAttribute(lowsrcAttr)); |
|
358 |
} |
|
359 |
||
360 |
void HTMLImageElement::setLowsrc(const String& value) |
|
361 |
{ |
|
362 |
setAttribute(lowsrcAttr, value); |
|
363 |
} |
|
364 |
||
365 |
KURL HTMLImageElement::src() const |
|
366 |
{ |
|
367 |
return document()->completeURL(getAttribute(srcAttr)); |
|
368 |
} |
|
369 |
||
370 |
void HTMLImageElement::setSrc(const String& value) |
|
371 |
{ |
|
372 |
setAttribute(srcAttr, value); |
|
373 |
} |
|
374 |
||
375 |
int HTMLImageElement::vspace() const |
|
376 |
{ |
|
377 |
// ### return actual vspace |
|
378 |
return getAttribute(vspaceAttr).toInt(); |
|
379 |
} |
|
380 |
||
381 |
void HTMLImageElement::setVspace(int value) |
|
382 |
{ |
|
383 |
setAttribute(vspaceAttr, String::number(value)); |
|
384 |
} |
|
385 |
||
386 |
void HTMLImageElement::setWidth(int value) |
|
387 |
{ |
|
388 |
setAttribute(widthAttr, String::number(value)); |
|
389 |
} |
|
390 |
||
391 |
int HTMLImageElement::x() const |
|
392 |
{ |
|
393 |
RenderObject* r = renderer(); |
|
394 |
if (!r) |
|
395 |
return 0; |
|
396 |
||
397 |
// FIXME: This doesn't work correctly with transforms. |
|
398 |
FloatPoint absPos = r->localToAbsolute(); |
|
399 |
return absPos.x(); |
|
400 |
} |
|
401 |
||
402 |
int HTMLImageElement::y() const |
|
403 |
{ |
|
404 |
RenderObject* r = renderer(); |
|
405 |
if (!r) |
|
406 |
return 0; |
|
407 |
||
408 |
// FIXME: This doesn't work correctly with transforms. |
|
409 |
FloatPoint absPos = r->localToAbsolute(); |
|
410 |
return absPos.y(); |
|
411 |
} |
|
412 |
||
413 |
bool HTMLImageElement::complete() const |
|
414 |
{ |
|
415 |
return m_imageLoader.imageComplete(); |
|
416 |
} |
|
417 |
||
418 |
void HTMLImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const |
|
419 |
{ |
|
420 |
HTMLElement::addSubresourceAttributeURLs(urls); |
|
421 |
||
422 |
addSubresourceURL(urls, src()); |
|
423 |
// FIXME: What about when the usemap attribute begins with "#"? |
|
424 |
addSubresourceURL(urls, document()->completeURL(getAttribute(usemapAttr))); |
|
425 |
} |
|
426 |
||
427 |
} |