• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
HTMLImageElement(const QualifiedName & tagName,Document * doc,HTMLFormElement * form)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 
~HTMLImageElement()55 HTMLImageElement::~HTMLImageElement()
56 {
57     if (m_form)
58         m_form->removeImgElement(this);
59 }
60 
mapToEntry(const QualifiedName & attrName,MappedAttributeEntry & result) const61 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 
parseMappedAttribute(MappedAttribute * attr)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 == compositeAttr) {
122         if (!parseCompositeOperator(attr->value(), m_compositeOperator))
123             m_compositeOperator = CompositeSourceOver;
124     } else if (attrName == nameAttr) {
125         const AtomicString& newName = attr->value();
126         if (inDocument() && document()->isHTMLDocument()) {
127             HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
128             document->removeNamedItem(m_name);
129             document->addNamedItem(newName);
130         }
131         m_name = newName;
132     } else if (attr->name() == idAttr) {
133         const AtomicString& newId = attr->value();
134         if (inDocument() && document()->isHTMLDocument()) {
135             HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
136             document->removeExtraNamedItem(m_id);
137             document->addExtraNamedItem(newId);
138         }
139         m_id = newId;
140         // also call superclass
141         HTMLElement::parseMappedAttribute(attr);
142     } else
143         HTMLElement::parseMappedAttribute(attr);
144 }
145 
altText() const146 String HTMLImageElement::altText() const
147 {
148     // lets figure out the alt text.. magic stuff
149     // http://www.w3.org/TR/1998/REC-html40-19980424/appendix/notes.html#altgen
150     // also heavily discussed by Hixie on bugzilla
151     String alt = getAttribute(altAttr);
152     // fall back to title attribute
153     if (alt.isNull())
154         alt = getAttribute(titleAttr);
155     return alt;
156 }
157 
createRenderer(RenderArena * arena,RenderStyle * style)158 RenderObject* HTMLImageElement::createRenderer(RenderArena* arena, RenderStyle* style)
159 {
160      if (style->contentData())
161         return RenderObject::createObject(this, style);
162 
163      return new (arena) RenderImage(this);
164 }
165 
attach()166 void HTMLImageElement::attach()
167 {
168     HTMLElement::attach();
169 
170     if (renderer() && renderer()->isImage()) {
171         RenderImage* imageObj = toRenderImage(renderer());
172         if (imageObj->hasImage())
173             return;
174         imageObj->setCachedImage(m_imageLoader.image());
175 
176         // If we have no image at all because we have no src attribute, set
177         // image height and width for the alt text instead.
178         if (!m_imageLoader.image() && !imageObj->cachedImage())
179             imageObj->setImageSizeForAltText();
180     }
181 }
182 
insertedIntoDocument()183 void HTMLImageElement::insertedIntoDocument()
184 {
185     if (document()->isHTMLDocument()) {
186         HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
187         document->addNamedItem(m_name);
188         document->addExtraNamedItem(m_id);
189     }
190 
191     // If we have been inserted from a renderer-less document,
192     // our loader may have not fetched the image, so do it now.
193     if (!m_imageLoader.image())
194         m_imageLoader.updateFromElement();
195 
196     HTMLElement::insertedIntoDocument();
197 }
198 
removedFromDocument()199 void HTMLImageElement::removedFromDocument()
200 {
201     if (document()->isHTMLDocument()) {
202         HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
203         document->removeNamedItem(m_name);
204         document->removeExtraNamedItem(m_id);
205     }
206 
207     HTMLElement::removedFromDocument();
208 }
209 
width(bool ignorePendingStylesheets) const210 int HTMLImageElement::width(bool ignorePendingStylesheets) const
211 {
212     if (!renderer()) {
213         // check the attribute first for an explicit pixel value
214         bool ok;
215         int width = getAttribute(widthAttr).toInt(&ok);
216         if (ok)
217             return width;
218 
219         // if the image is available, use its width
220         if (m_imageLoader.image()) {
221             float zoomFactor = document()->frame() ? document()->frame()->pageZoomFactor() : 1.0f;
222             return m_imageLoader.image()->imageSize(zoomFactor).width();
223         }
224     }
225 
226     if (ignorePendingStylesheets)
227         document()->updateLayoutIgnorePendingStylesheets();
228     else
229         document()->updateLayout();
230 
231     return renderBox() ? renderBox()->contentWidth() : 0;
232 }
233 
height(bool ignorePendingStylesheets) const234 int HTMLImageElement::height(bool ignorePendingStylesheets) const
235 {
236     if (!renderer()) {
237         // check the attribute first for an explicit pixel value
238         bool ok;
239         int height = getAttribute(heightAttr).toInt(&ok);
240         if (ok)
241             return height;
242 
243         // if the image is available, use its height
244         if (m_imageLoader.image()) {
245             float zoomFactor = document()->frame() ? document()->frame()->pageZoomFactor() : 1.0f;
246             return m_imageLoader.image()->imageSize(zoomFactor).height();
247         }
248     }
249 
250     if (ignorePendingStylesheets)
251         document()->updateLayoutIgnorePendingStylesheets();
252     else
253         document()->updateLayout();
254 
255     return renderBox() ? renderBox()->contentHeight() : 0;
256 }
257 
naturalWidth() const258 int HTMLImageElement::naturalWidth() const
259 {
260     if (!m_imageLoader.image())
261         return 0;
262 
263     return m_imageLoader.image()->imageSize(1.0f).width();
264 }
265 
naturalHeight() const266 int HTMLImageElement::naturalHeight() const
267 {
268     if (!m_imageLoader.image())
269         return 0;
270 
271     return m_imageLoader.image()->imageSize(1.0f).height();
272 }
273 
isURLAttribute(Attribute * attr) const274 bool HTMLImageElement::isURLAttribute(Attribute* attr) const
275 {
276     return attr->name() == srcAttr
277         || attr->name() == lowsrcAttr
278         || attr->name() == longdescAttr
279         || (attr->name() == usemapAttr && attr->value().string()[0] != '#');
280 }
281 
name() const282 String HTMLImageElement::name() const
283 {
284     return getAttribute(nameAttr);
285 }
286 
setName(const String & value)287 void HTMLImageElement::setName(const String& value)
288 {
289     setAttribute(nameAttr, value);
290 }
291 
align() const292 String HTMLImageElement::align() const
293 {
294     return getAttribute(alignAttr);
295 }
296 
setAlign(const String & value)297 void HTMLImageElement::setAlign(const String& value)
298 {
299     setAttribute(alignAttr, value);
300 }
301 
alt() const302 String HTMLImageElement::alt() const
303 {
304     return getAttribute(altAttr);
305 }
306 
setAlt(const String & value)307 void HTMLImageElement::setAlt(const String& value)
308 {
309     setAttribute(altAttr, value);
310 }
311 
border() const312 String HTMLImageElement::border() const
313 {
314     return getAttribute(borderAttr);
315 }
316 
setBorder(const String & value)317 void HTMLImageElement::setBorder(const String& value)
318 {
319     setAttribute(borderAttr, value);
320 }
321 
draggable() const322 bool HTMLImageElement::draggable() const
323 {
324     // Image elements are draggable by default.
325     return !equalIgnoringCase(getAttribute(draggableAttr), "false");
326 }
327 
setHeight(int value)328 void HTMLImageElement::setHeight(int value)
329 {
330     setAttribute(heightAttr, String::number(value));
331 }
332 
hspace() const333 int HTMLImageElement::hspace() const
334 {
335     // ### return actual value
336     return getAttribute(hspaceAttr).toInt();
337 }
338 
setHspace(int value)339 void HTMLImageElement::setHspace(int value)
340 {
341     setAttribute(hspaceAttr, String::number(value));
342 }
343 
isMap() const344 bool HTMLImageElement::isMap() const
345 {
346     return !getAttribute(ismapAttr).isNull();
347 }
348 
setIsMap(bool isMap)349 void HTMLImageElement::setIsMap(bool isMap)
350 {
351     setAttribute(ismapAttr, isMap ? "" : 0);
352 }
353 
longDesc() const354 KURL HTMLImageElement::longDesc() const
355 {
356     return document()->completeURL(getAttribute(longdescAttr));
357 }
358 
setLongDesc(const String & value)359 void HTMLImageElement::setLongDesc(const String& value)
360 {
361     setAttribute(longdescAttr, value);
362 }
363 
lowsrc() const364 KURL HTMLImageElement::lowsrc() const
365 {
366     return document()->completeURL(getAttribute(lowsrcAttr));
367 }
368 
setLowsrc(const String & value)369 void HTMLImageElement::setLowsrc(const String& value)
370 {
371     setAttribute(lowsrcAttr, value);
372 }
373 
src() const374 KURL HTMLImageElement::src() const
375 {
376     return document()->completeURL(getAttribute(srcAttr));
377 }
378 
setSrc(const String & value)379 void HTMLImageElement::setSrc(const String& value)
380 {
381     setAttribute(srcAttr, value);
382 }
383 
useMap() const384 String HTMLImageElement::useMap() const
385 {
386     return getAttribute(usemapAttr);
387 }
388 
setUseMap(const String & value)389 void HTMLImageElement::setUseMap(const String& value)
390 {
391     setAttribute(usemapAttr, value);
392 }
393 
vspace() const394 int HTMLImageElement::vspace() const
395 {
396     // ### return actual vspace
397     return getAttribute(vspaceAttr).toInt();
398 }
399 
setVspace(int value)400 void HTMLImageElement::setVspace(int value)
401 {
402     setAttribute(vspaceAttr, String::number(value));
403 }
404 
setWidth(int value)405 void HTMLImageElement::setWidth(int value)
406 {
407     setAttribute(widthAttr, String::number(value));
408 }
409 
x() const410 int HTMLImageElement::x() const
411 {
412     RenderObject* r = renderer();
413     if (!r)
414         return 0;
415 
416     // FIXME: This doesn't work correctly with transforms.
417     FloatPoint absPos = r->localToAbsolute();
418     return absPos.x();
419 }
420 
y() const421 int HTMLImageElement::y() const
422 {
423     RenderObject* r = renderer();
424     if (!r)
425         return 0;
426 
427     // FIXME: This doesn't work correctly with transforms.
428     FloatPoint absPos = r->localToAbsolute();
429     return absPos.y();
430 }
431 
complete() const432 bool HTMLImageElement::complete() const
433 {
434     return m_imageLoader.imageComplete();
435 }
436 
addSubresourceAttributeURLs(ListHashSet<KURL> & urls) const437 void HTMLImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
438 {
439     HTMLElement::addSubresourceAttributeURLs(urls);
440 
441     addSubresourceURL(urls, src());
442     addSubresourceURL(urls, document()->completeURL(useMap()));
443 }
444 
445 }
446