Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
      3  * Copyright (C) 2010 Google Inc. All rights reserved.
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Library General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Library General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Library General Public License
     16  * along with this library; see the file COPYING.LIB.  If not, write to
     17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  * Boston, MA 02110-1301, USA.
     19  *
     20  */
     21 
     22 #include "config.h"
     23 #include "ImageInputType.h"
     24 
     25 #include "FormDataList.h"
     26 #include "HTMLFormElement.h"
     27 #include "HTMLImageLoader.h"
     28 #include "HTMLInputElement.h"
     29 #include "MouseEvent.h"
     30 #include "RenderImage.h"
     31 #include <wtf/PassOwnPtr.h>
     32 
     33 namespace WebCore {
     34 
     35 inline ImageInputType::ImageInputType(HTMLInputElement* element)
     36     : BaseButtonInputType(element)
     37 {
     38 }
     39 
     40 PassOwnPtr<InputType> ImageInputType::create(HTMLInputElement* element)
     41 {
     42     return adoptPtr(new ImageInputType(element));
     43 }
     44 
     45 const AtomicString& ImageInputType::formControlType() const
     46 {
     47     return InputTypeNames::image();
     48 }
     49 
     50 bool ImageInputType::isFormDataAppendable() const
     51 {
     52     return true;
     53 }
     54 
     55 bool ImageInputType::appendFormData(FormDataList& encoding, bool) const
     56 {
     57     if (!element()->isActivatedSubmit())
     58         return false;
     59     const AtomicString& name = element()->name();
     60     encoding.appendData(name.isEmpty() ? "x" : (name + ".x"), m_clickLocation.x());
     61     encoding.appendData(name.isEmpty() ? "y" : (name + ".y"), m_clickLocation.y());
     62     if (!name.isEmpty() && !element()->value().isEmpty())
     63         encoding.appendData(name, element()->value());
     64     return true;
     65 }
     66 
     67 bool ImageInputType::supportsValidation() const
     68 {
     69     return false;
     70 }
     71 
     72 void ImageInputType::handleDOMActivateEvent(Event* event)
     73 {
     74     RefPtr<HTMLInputElement> element = this->element();
     75     if (element->disabled() || !element->form())
     76         return;
     77     element->setActivatedSubmit(true);
     78     if (event->underlyingEvent() && event->underlyingEvent()->isMouseEvent()) {
     79         MouseEvent* mouseEvent = static_cast<MouseEvent*>(event->underlyingEvent());
     80         m_clickLocation = IntPoint(mouseEvent->offsetX(), mouseEvent->offsetY());
     81     } else
     82         m_clickLocation = IntPoint();
     83     element->form()->prepareForSubmission(event); // Event handlers can run.
     84     element->setActivatedSubmit(false);
     85     event->setDefaultHandled();
     86 }
     87 
     88 RenderObject* ImageInputType::createRenderer(RenderArena* arena, RenderStyle*) const
     89 {
     90     RenderImage* image = new (arena) RenderImage(element());
     91     image->setImageResource(RenderImageResource::create());
     92     return image;
     93 }
     94 
     95 void ImageInputType::altAttributeChanged()
     96 {
     97     RenderImage* image = toRenderImage(element()->renderer());
     98     if (!image)
     99         return;
    100     image->updateAltText();
    101 }
    102 
    103 void ImageInputType::srcAttributeChanged()
    104 {
    105     if (!element()->renderer())
    106         return;
    107     if (!m_imageLoader)
    108         m_imageLoader = adoptPtr(new HTMLImageLoader(element()));
    109     m_imageLoader->updateFromElementIgnoringPreviousError();
    110 }
    111 
    112 void ImageInputType::attach()
    113 {
    114     BaseButtonInputType::attach();
    115 
    116     if (!m_imageLoader)
    117         m_imageLoader = adoptPtr(new HTMLImageLoader(element()));
    118     m_imageLoader->updateFromElement();
    119 
    120     RenderImage* renderer = toRenderImage(element()->renderer());
    121     if (!renderer)
    122         return;
    123 
    124     if (!m_imageLoader->haveFiredBeforeLoadEvent())
    125         return;
    126 
    127     RenderImageResource* imageResource = renderer->imageResource();
    128     imageResource->setCachedImage(m_imageLoader->image());
    129 
    130     // If we have no image at all because we have no src attribute, set
    131     // image height and width for the alt text instead.
    132     if (!m_imageLoader->image() && !imageResource->cachedImage())
    133         renderer->setImageSizeForAltText();
    134 }
    135 
    136 void ImageInputType::willMoveToNewOwnerDocument()
    137 {
    138     BaseButtonInputType::willMoveToNewOwnerDocument();
    139     if (m_imageLoader)
    140         m_imageLoader->elementWillMoveToNewOwnerDocument();
    141 }
    142 
    143 bool ImageInputType::shouldRespectAlignAttribute()
    144 {
    145     return true;
    146 }
    147 
    148 bool ImageInputType::canBeSuccessfulSubmitButton()
    149 {
    150     return true;
    151 }
    152 
    153 bool ImageInputType::isImageButton() const
    154 {
    155     return true;
    156 }
    157 
    158 bool ImageInputType::isEnumeratable()
    159 {
    160     return false;
    161 }
    162 
    163 bool ImageInputType::shouldRespectHeightAndWidthAttributes()
    164 {
    165     return true;
    166 }
    167 
    168 } // namespace WebCore
    169