1 /**
2 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
3 *
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.
8 *
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.
13 *
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.
18 *
19 */
20
21 #include "config.h"
22
23 #if ENABLE(WML)
24 #include "WMLImageElement.h"
25
26 #include "CSSPropertyNames.h"
27 #include "CSSValueKeywords.h"
28 #include "HTMLElement.h"
29 #include "HTMLNames.h"
30 #include "MappedAttribute.h"
31 #include "RenderImage.h"
32 #include "WMLNames.h"
33 #include "WMLVariables.h"
34
35 namespace WebCore {
36
37 using namespace WMLNames;
38
WMLImageElement(const QualifiedName & tagName,Document * doc)39 WMLImageElement::WMLImageElement(const QualifiedName& tagName, Document* doc)
40 : WMLElement(tagName, doc)
41 , m_imageLoader(this)
42 , m_useFallbackAttribute(false)
43 {
44 }
45
~WMLImageElement()46 WMLImageElement::~WMLImageElement()
47 {
48 }
49
mapToEntry(const QualifiedName & attrName,MappedAttributeEntry & result) const50 bool WMLImageElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
51 {
52 if (attrName == HTMLNames::widthAttr ||
53 attrName == HTMLNames::heightAttr ||
54 attrName == HTMLNames::vspaceAttr ||
55 attrName == HTMLNames::hspaceAttr) {
56 result = eUniversal;
57 return false;
58 }
59
60 if (attrName == HTMLNames::alignAttr) {
61 result = eReplaced;
62 return false;
63 }
64
65 return WMLElement::mapToEntry(attrName, result);
66 }
67
parseMappedAttribute(MappedAttribute * attr)68 void WMLImageElement::parseMappedAttribute(MappedAttribute* attr)
69 {
70 const QualifiedName& attrName = attr->name();
71
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);
89 else
90 WMLElement::parseMappedAttribute(attr);
91 }
92
attach()93 void WMLImageElement::attach()
94 {
95 WMLElement::attach();
96
97 if (renderer() && renderer()->isImage()) {
98 RenderImage* imageObj = toRenderImage(renderer());
99 if (imageObj->hasImage())
100 return;
101
102 imageObj->setCachedImage(m_imageLoader.image());
103
104 // If we have no image at all because we have no src attribute, set
105 // image height and width for the alt text instead.
106 if (!m_imageLoader.image() && !imageObj->cachedImage())
107 imageObj->setImageSizeForAltText();
108 }
109 }
110
createRenderer(RenderArena * arena,RenderStyle *)111 RenderObject* WMLImageElement::createRenderer(RenderArena* arena, RenderStyle*)
112 {
113 return new (arena) RenderImage(this);
114 }
115
insertedIntoDocument()116 void WMLImageElement::insertedIntoDocument()
117 {
118 // If we have been inserted from a renderer-less document,
119 // our loader may have not fetched the image, so do it now.
120 if (!m_imageLoader.image())
121 m_imageLoader.updateFromElement();
122
123 WMLElement::insertedIntoDocument();
124 }
125
isURLAttribute(Attribute * attr) const126 bool WMLImageElement::isURLAttribute(Attribute* attr) const
127 {
128 return attr->name() == HTMLNames::srcAttr
129 || attr->name() == localsrcAttr;
130 }
131
imageSourceAttributeName() const132 const QualifiedName& WMLImageElement::imageSourceAttributeName() const
133 {
134 // Spec: Any 'localsrc' parameter specified takes precedence over the
135 // image specified in the src parameter.
136 if (hasAttribute(localsrcAttr) && !m_useFallbackAttribute)
137 return localsrcAttr;
138
139 return HTMLNames::srcAttr;
140 }
141
altText() const142 String WMLImageElement::altText() const
143 {
144 return substituteVariableReferences(getAttribute(HTMLNames::altAttr), document());
145 }
146
147 }
148
149 #endif
150