Home | History | Annotate | Download | only in wml
      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 
     39 WMLImageElement::WMLImageElement(const QualifiedName& tagName, Document* doc)
     40     : WMLElement(tagName, doc)
     41     , m_imageLoader(this)
     42     , m_useFallbackAttribute(false)
     43 {
     44 }
     45 
     46 WMLImageElement::~WMLImageElement()
     47 {
     48 }
     49 
     50 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 
     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 
     93 void WMLImageElement::attach()
     94 {
     95     WMLElement::attach();
     96 
     97     if (renderer() && renderer()->isImage() && m_imageLoader.haveFiredBeforeLoadEvent()) {
     98         RenderImage* imageObj = toRenderImage(renderer());
     99         if (imageObj->hasImage())
    100             return;
    101         imageObj->setCachedImage(m_imageLoader.image());
    102 
    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();
    107     }
    108 }
    109 
    110 RenderObject* WMLImageElement::createRenderer(RenderArena* arena, RenderStyle*)
    111 {
    112     return new (arena) RenderImage(this);
    113 }
    114 
    115 void WMLImageElement::insertedIntoDocument()
    116 {
    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();
    121 
    122     WMLElement::insertedIntoDocument();
    123 }
    124 
    125 bool WMLImageElement::isURLAttribute(Attribute* attr) const
    126 {
    127     return attr->name() == HTMLNames::srcAttr
    128            || attr->name() == localsrcAttr;
    129 }
    130 
    131 const QualifiedName& WMLImageElement::imageSourceAttributeName() const
    132 {
    133     // Spec: Any 'localsrc' parameter specified takes precedence over the
    134     // image specified in the src parameter.
    135     if (hasAttribute(localsrcAttr) && !m_useFallbackAttribute)
    136         return localsrcAttr;
    137 
    138     return HTMLNames::srcAttr;
    139 }
    140 
    141 String WMLImageElement::altText() const
    142 {
    143     return substituteVariableReferences(getAttribute(HTMLNames::altAttr), document());
    144 }
    145 
    146 }
    147 
    148 #endif
    149