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.
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.
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.
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.
23 #include "HTMLImageElement.h"
25 #include "CSSHelper.h"
26 #include "CSSPropertyNames.h"
27 #include "CSSValueKeywords.h"
28 #include "EventNames.h"
29 #include "HTMLDocument.h"
30 #include "HTMLFormElement.h"
31 #include "HTMLNames.h"
32 #include "RenderImage.h"
38 using namespace EventNames;
39 using namespace HTMLNames;
41 HTMLImageElement::HTMLImageElement(Document* doc, HTMLFormElement* f)
42 : HTMLElement(imgTag, doc)
46 , m_compositeOperator(CompositeSourceOver)
49 f->registerImgElement(this);
52 HTMLImageElement::HTMLImageElement(const QualifiedName& tagName, Document* doc)
53 : HTMLElement(tagName, doc)
57 , m_compositeOperator(CompositeSourceOver)
61 HTMLImageElement::~HTMLImageElement()
64 m_form->removeImgElement(this);
67 bool HTMLImageElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
69 if (attrName == widthAttr ||
70 attrName == heightAttr ||
71 attrName == vspaceAttr ||
72 attrName == hspaceAttr ||
73 attrName == valignAttr) {
78 if (attrName == borderAttr || attrName == alignAttr) {
79 result = eReplaced; // Shared with embed and iframe elements.
83 return HTMLElement::mapToEntry(attrName, result);
86 void HTMLImageElement::parseMappedAttribute(MappedAttribute* attr)
88 const QualifiedName& attrName = attr->name();
89 if (attrName == altAttr) {
90 if (renderer() && renderer()->isImage())
91 static_cast<RenderImage*>(renderer())->updateAltText();
92 } else if (attrName == srcAttr)
93 m_imageLoader.updateFromElement();
94 else if (attrName == widthAttr)
95 addCSSLength(attr, CSS_PROP_WIDTH, attr->value());
96 else if (attrName == heightAttr)
97 addCSSLength(attr, CSS_PROP_HEIGHT, attr->value());
98 else if (attrName == borderAttr) {
99 // border="noborder" -> border="0"
100 addCSSLength(attr, CSS_PROP_BORDER_WIDTH, attr->value().toInt() ? attr->value() : "0");
101 addCSSProperty(attr, CSS_PROP_BORDER_TOP_STYLE, CSS_VAL_SOLID);
102 addCSSProperty(attr, CSS_PROP_BORDER_RIGHT_STYLE, CSS_VAL_SOLID);
103 addCSSProperty(attr, CSS_PROP_BORDER_BOTTOM_STYLE, CSS_VAL_SOLID);
104 addCSSProperty(attr, CSS_PROP_BORDER_LEFT_STYLE, CSS_VAL_SOLID);
105 } else if (attrName == vspaceAttr) {
106 addCSSLength(attr, CSS_PROP_MARGIN_TOP, attr->value());
107 addCSSLength(attr, CSS_PROP_MARGIN_BOTTOM, attr->value());
108 } else if (attrName == hspaceAttr) {
109 addCSSLength(attr, CSS_PROP_MARGIN_LEFT, attr->value());
110 addCSSLength(attr, CSS_PROP_MARGIN_RIGHT, attr->value());
111 } else if (attrName == alignAttr)
112 addHTMLAlignment(attr);
113 else if (attrName == valignAttr)
114 addCSSProperty(attr, CSS_PROP_VERTICAL_ALIGN, attr->value());
115 else if (attrName == usemapAttr) {
116 if (attr->value().domString()[0] == '#')
117 usemap = attr->value();
119 usemap = document()->completeURL(parseURL(attr->value()));
120 m_isLink = !attr->isNull();
121 } else if (attrName == ismapAttr)
123 else if (attrName == onabortAttr)
124 setHTMLEventListener(abortEvent, attr);
125 else if (attrName == onloadAttr)
126 setHTMLEventListener(loadEvent, attr);
127 else if (attrName == compositeAttr) {
128 if (!parseCompositeOperator(attr->value(), m_compositeOperator))
129 m_compositeOperator = CompositeSourceOver;
130 } else if (attrName == nameAttr) {
131 String newNameAttr = attr->value();
132 if (inDocument() && document()->isHTMLDocument()) {
133 HTMLDocument* doc = static_cast<HTMLDocument*>(document());
134 doc->removeNamedItem(oldNameAttr);
135 doc->addNamedItem(newNameAttr);
137 oldNameAttr = newNameAttr;
138 } else if (attr->name() == idAttr) {
139 String newIdAttr = attr->value();
140 if (inDocument() && document()->isHTMLDocument()) {
141 HTMLDocument *doc = static_cast<HTMLDocument *>(document());
142 doc->removeDocExtraNamedItem(oldIdAttr);
143 doc->addDocExtraNamedItem(newIdAttr);
145 oldIdAttr = newIdAttr;
146 // also call superclass
147 HTMLElement::parseMappedAttribute(attr);
149 HTMLElement::parseMappedAttribute(attr);
152 String HTMLImageElement::altText() const
154 // lets figure out the alt text.. magic stuff
155 // http://www.w3.org/TR/1998/REC-html40-19980424/appendix/notes.html#altgen
156 // also heavily discussed by Hixie on bugzilla
157 String alt = getAttribute(altAttr);
158 // fall back to title attribute
160 alt = getAttribute(titleAttr);
164 RenderObject* HTMLImageElement::createRenderer(RenderArena* arena, RenderStyle* style)
166 if (style->contentData())
167 return RenderObject::createObject(this, style);
169 return new (arena) RenderImage(this);
172 void HTMLImageElement::attach()
174 HTMLElement::attach();
176 if (renderer() && renderer()->isImage()) {
177 RenderImage* imageObj = static_cast<RenderImage*>(renderer());
178 imageObj->setCachedImage(m_imageLoader.image());
180 // If we have no image at all because we have no src attribute, set
181 // image height and width for the alt text instead.
182 if (!m_imageLoader.image() && !imageObj->cachedImage())
183 imageObj->setImageSizeForAltText();
187 void HTMLImageElement::insertedIntoDocument()
189 if (document()->isHTMLDocument()) {
190 HTMLDocument* doc = static_cast<HTMLDocument*>(document());
192 doc->addNamedItem(oldNameAttr);
193 doc->addDocExtraNamedItem(oldIdAttr);
196 HTMLElement::insertedIntoDocument();
199 void HTMLImageElement::removedFromDocument()
201 if (document()->isHTMLDocument()) {
202 HTMLDocument* doc = static_cast<HTMLDocument*>(document());
204 doc->removeNamedItem(oldNameAttr);
205 doc->removeDocExtraNamedItem(oldIdAttr);
208 HTMLElement::removedFromDocument();
211 int HTMLImageElement::width(bool ignorePendingStylesheets) const
214 // check the attribute first for an explicit pixel value
216 int width = getAttribute(widthAttr).toInt(&ok);
220 // if the image is available, use its width
221 if (m_imageLoader.image())
222 return m_imageLoader.image()->imageSize().width();
225 if (ignorePendingStylesheets)
226 document()->updateLayoutIgnorePendingStylesheets();
228 document()->updateLayout();
230 return renderer() ? renderer()->contentWidth() : 0;
233 int HTMLImageElement::height(bool ignorePendingStylesheets) const
236 // check the attribute first for an explicit pixel value
238 int height = getAttribute(heightAttr).toInt(&ok);
242 // if the image is available, use its height
243 if (m_imageLoader.image())
244 return m_imageLoader.image()->imageSize().height();
247 if (ignorePendingStylesheets)
248 document()->updateLayoutIgnorePendingStylesheets();
250 document()->updateLayout();
252 return renderer() ? renderer()->contentHeight() : 0;
255 int HTMLImageElement::naturalWidth() const
257 if (!m_imageLoader.image())
260 return m_imageLoader.image()->imageSize().width();
263 int HTMLImageElement::naturalHeight() const
265 if (!m_imageLoader.image())
268 return m_imageLoader.image()->imageSize().height();
271 bool HTMLImageElement::isURLAttribute(Attribute* attr) const
273 return attr->name() == srcAttr
274 || attr->name() == lowsrcAttr
275 || attr->name() == longdescAttr
276 || (attr->name() == usemapAttr && attr->value().domString()[0] != '#');
279 String HTMLImageElement::name() const
281 return getAttribute(nameAttr);
284 void HTMLImageElement::setName(const String& value)
286 setAttribute(nameAttr, value);
289 String HTMLImageElement::align() const
291 return getAttribute(alignAttr);
294 void HTMLImageElement::setAlign(const String& value)
296 setAttribute(alignAttr, value);
299 String HTMLImageElement::alt() const
301 return getAttribute(altAttr);
304 void HTMLImageElement::setAlt(const String& value)
306 setAttribute(altAttr, value);
309 String HTMLImageElement::border() const
311 return getAttribute(borderAttr);
314 void HTMLImageElement::setBorder(const String& value)
316 setAttribute(borderAttr, value);
319 void HTMLImageElement::setHeight(int value)
321 setAttribute(heightAttr, String::number(value));
324 int HTMLImageElement::hspace() const
326 // ### return actual value
327 return getAttribute(hspaceAttr).toInt();
330 void HTMLImageElement::setHspace(int value)
332 setAttribute(hspaceAttr, String::number(value));
335 bool HTMLImageElement::isMap() const
337 return !getAttribute(ismapAttr).isNull();
340 void HTMLImageElement::setIsMap(bool isMap)
342 setAttribute(ismapAttr, isMap ? "" : 0);
345 String HTMLImageElement::longDesc() const
347 return document()->completeURL(getAttribute(longdescAttr));
350 void HTMLImageElement::setLongDesc(const String& value)
352 setAttribute(longdescAttr, value);
355 String HTMLImageElement::lowsrc() const
357 return document()->completeURL(getAttribute(lowsrcAttr));
360 void HTMLImageElement::setLowsrc(const String& value)
362 setAttribute(lowsrcAttr, value);
365 String HTMLImageElement::src() const
367 return document()->completeURL(getAttribute(srcAttr));
370 void HTMLImageElement::setSrc(const String& value)
372 setAttribute(srcAttr, value);
375 String HTMLImageElement::useMap() const
377 return getAttribute(usemapAttr);
380 void HTMLImageElement::setUseMap(const String& value)
382 setAttribute(usemapAttr, value);
385 int HTMLImageElement::vspace() const
387 // ### return actual vspace
388 return getAttribute(vspaceAttr).toInt();
391 void HTMLImageElement::setVspace(int value)
393 setAttribute(vspaceAttr, String::number(value));
396 void HTMLImageElement::setWidth(int value)
398 setAttribute(widthAttr, String::number(value));
401 int HTMLImageElement::x() const
403 RenderObject* r = renderer();
407 r->absolutePosition(x, y);
411 int HTMLImageElement::y() const
413 RenderObject* r = renderer();
417 r->absolutePosition(x, y);
421 bool HTMLImageElement::complete() const
423 return m_imageLoader.imageComplete();