2 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "WMLImageElement.h"
26 #include "Attribute.h"
27 #include "CSSPropertyNames.h"
28 #include "CSSValueKeywords.h"
29 #include "HTMLElement.h"
30 #include "HTMLNames.h"
31 #include "RenderImage.h"
33 #include "WMLVariables.h"
37 using namespace WMLNames;
39 WMLImageElement::WMLImageElement(const QualifiedName& tagName, Document* doc)
40 : WMLElement(tagName, doc)
42 , m_useFallbackAttribute(false)
46 WMLImageElement::~WMLImageElement()
50 bool WMLImageElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
52 if (attrName == HTMLNames::widthAttr ||
53 attrName == HTMLNames::heightAttr ||
54 attrName == HTMLNames::vspaceAttr ||
55 attrName == HTMLNames::hspaceAttr) {
60 if (attrName == HTMLNames::alignAttr) {
65 return WMLElement::mapToEntry(attrName, result);
68 void WMLImageElement::parseMappedAttribute(Attribute* attr)
70 const QualifiedName& attrName = attr->name();
72 if (attrName == HTMLNames::altAttr) {
73 if (renderer() && renderer()->isImage())
74 toRenderImage(renderer())->updateAltText();
75 } else if (attrName == HTMLNames::srcAttr || attrName == localsrcAttr)
76 m_imageLoader.updateFromElementIgnoringPreviousError();
77 else if (attrName == HTMLNames::widthAttr)
78 addCSSLength(attr, CSSPropertyWidth, attr->value());
79 else if (attrName == HTMLNames::heightAttr)
80 addCSSLength(attr, CSSPropertyHeight, attr->value());
81 else if (attrName == HTMLNames::vspaceAttr) {
82 addCSSLength(attr, CSSPropertyMarginTop, attr->value());
83 addCSSLength(attr, CSSPropertyMarginBottom, attr->value());
84 } else if (attrName == HTMLNames::hspaceAttr) {
85 addCSSLength(attr, CSSPropertyMarginLeft, attr->value());
86 addCSSLength(attr, CSSPropertyMarginRight, attr->value());
87 } else if (attrName == HTMLNames::alignAttr)
88 HTMLElement::addHTMLAlignmentToStyledElement(this, attr);
90 WMLElement::parseMappedAttribute(attr);
93 void WMLImageElement::attach()
97 if (renderer() && renderer()->isImage() && m_imageLoader.haveFiredBeforeLoadEvent()) {
98 RenderImage* imageObj = toRenderImage(renderer());
99 if (imageObj->hasImage())
101 imageObj->setCachedImage(m_imageLoader.image());
103 // If we have no image at all because we have no src attribute, set
104 // image height and width for the alt text instead.
105 if (!m_imageLoader.image() && !imageObj->cachedImage())
106 imageObj->setImageSizeForAltText();
110 RenderObject* WMLImageElement::createRenderer(RenderArena* arena, RenderStyle*)
112 return new (arena) RenderImage(this);
115 void WMLImageElement::insertedIntoDocument()
117 // If we have been inserted from a renderer-less document,
118 // our loader may have not fetched the image, so do it now.
119 if (!m_imageLoader.image())
120 m_imageLoader.updateFromElement();
122 WMLElement::insertedIntoDocument();
125 bool WMLImageElement::isURLAttribute(Attribute* attr) const
127 return attr->name() == HTMLNames::srcAttr
128 || attr->name() == localsrcAttr;
131 const QualifiedName& WMLImageElement::imageSourceAttributeName() const
133 // Spec: Any 'localsrc' parameter specified takes precedence over the
134 // image specified in the src parameter.
135 if (hasAttribute(localsrcAttr) && !m_useFallbackAttribute)
138 return HTMLNames::srcAttr;
141 String WMLImageElement::altText() const
143 return substituteVariableReferences(getAttribute(HTMLNames::altAttr), document());