Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "ValidationMessage.h"
     33 
     34 #include "CSSPropertyNames.h"
     35 #include "CSSStyleSelector.h"
     36 #include "CSSValueKeywords.h"
     37 #include "FormAssociatedElement.h"
     38 #include "HTMLBRElement.h"
     39 #include "HTMLNames.h"
     40 #include "Page.h"
     41 #include "RenderObject.h"
     42 #include "Settings.h"
     43 #include "ShadowRoot.h"
     44 #include "Text.h"
     45 #include <wtf/PassOwnPtr.h>
     46 
     47 namespace WebCore {
     48 
     49 using namespace HTMLNames;
     50 
     51 ALWAYS_INLINE ValidationMessage::ValidationMessage(FormAssociatedElement* element)
     52     : m_element(element)
     53 {
     54 }
     55 
     56 ValidationMessage::~ValidationMessage()
     57 {
     58     deleteBubbleTree();
     59 }
     60 
     61 PassOwnPtr<ValidationMessage> ValidationMessage::create(FormAssociatedElement* element)
     62 {
     63     return adoptPtr(new ValidationMessage(element));
     64 }
     65 
     66 void ValidationMessage::setMessage(const String& message)
     67 {
     68     // Don't modify the DOM tree in this context.
     69     // If so, an assertion in Node::isFocusable() fails.
     70     ASSERT(!message.isEmpty());
     71     m_message = message;
     72     if (!m_bubble)
     73         m_timer.set(new Timer<ValidationMessage>(this, &ValidationMessage::buildBubbleTree));
     74     else
     75         m_timer.set(new Timer<ValidationMessage>(this, &ValidationMessage::setMessageDOMAndStartTimer));
     76     m_timer->startOneShot(0);
     77 }
     78 
     79 void ValidationMessage::setMessageDOMAndStartTimer(Timer<ValidationMessage>*)
     80 {
     81     ASSERT(m_bubbleMessage);
     82     m_bubbleMessage->removeAllChildren();
     83     Vector<String> lines;
     84     m_message.split('\n', lines);
     85     Document* doc = m_bubbleMessage->document();
     86     ExceptionCode ec = 0;
     87     for (unsigned i = 0; i < lines.size(); ++i) {
     88         if (i) {
     89             m_bubbleMessage->appendChild(HTMLBRElement::create(doc), ec);
     90             m_bubbleMessage->appendChild(Text::create(doc, lines[i]), ec);
     91         } else {
     92             RefPtr<HTMLElement> bold = HTMLElement::create(bTag, doc);
     93             bold->setInnerText(lines[i], ec);
     94             m_bubbleMessage->appendChild(bold.release(), ec);
     95         }
     96     }
     97 
     98     int magnification = doc->page() ? doc->page()->settings()->validationMessageTimerMaginification() : -1;
     99     if (magnification <= 0)
    100         m_timer.clear();
    101     else {
    102         m_timer.set(new Timer<ValidationMessage>(this, &ValidationMessage::deleteBubbleTree));
    103         m_timer->startOneShot(max(5.0, static_cast<double>(m_message.length()) * magnification / 1000));
    104     }
    105 }
    106 
    107 class ElementWithPseudoId : public HTMLElement {
    108 public:
    109     static PassRefPtr<HTMLElement> create(Document* doc, const AtomicString& pseudoName)
    110     {
    111         return adoptRef(new ElementWithPseudoId(doc, pseudoName));
    112     }
    113 
    114 protected:
    115     ElementWithPseudoId(Document* doc, const AtomicString& pseudoName)
    116         : HTMLElement(divTag, doc)
    117         , m_pseudoName(pseudoName) { };
    118     virtual const AtomicString& shadowPseudoId() const { return m_pseudoName; }
    119 
    120 private:
    121     AtomicString m_pseudoName;
    122 };
    123 
    124 void ValidationMessage::buildBubbleTree(Timer<ValidationMessage>*)
    125 {
    126     HTMLElement* host = toHTMLElement(m_element);
    127     Document* doc = host->document();
    128     m_bubble = ElementWithPseudoId::create(doc, "-webkit-validation-bubble");
    129     // Need to force position:absolute because RenderMenuList doesn't assume it
    130     // contains non-absolute or non-fixed renderers as children.
    131     m_bubble->getInlineStyleDecl()->setProperty(CSSPropertyPosition, CSSValueAbsolute);
    132     ExceptionCode ec = 0;
    133     host->ensureShadowRoot()->appendChild(m_bubble.get(), ec);
    134 
    135     RefPtr<HTMLElement> clipper = ElementWithPseudoId::create(doc, "-webkit-validation-bubble-arrow-clipper");
    136     clipper->appendChild(ElementWithPseudoId::create(doc, "-webkit-validation-bubble-arrow"), ec);
    137     m_bubble->appendChild(clipper.release(), ec);
    138     m_bubbleMessage = ElementWithPseudoId::create(doc, "-webkit-validation-bubble-message");
    139     m_bubble->appendChild(m_bubbleMessage, ec);
    140 
    141     setMessageDOMAndStartTimer();
    142 
    143     // FIXME: Use transition to show the bubble.
    144 
    145     // We don't need to adjust the bubble location. The default position is enough.
    146 }
    147 
    148 void ValidationMessage::requestToHideMessage()
    149 {
    150     // We must not modify the DOM tree in this context by the same reason as setMessage().
    151     m_timer.set(new Timer<ValidationMessage>(this, &ValidationMessage::deleteBubbleTree));
    152     m_timer->startOneShot(0);
    153 }
    154 
    155 void ValidationMessage::deleteBubbleTree(Timer<ValidationMessage>*)
    156 {
    157     if (m_bubble) {
    158         m_bubbleMessage = 0;
    159         HTMLElement* host = toHTMLElement(m_element);
    160         ExceptionCode ec;
    161         host->shadowRoot()->removeChild(m_bubble.get(), ec);
    162         m_bubble = 0;
    163     }
    164     m_message = String();
    165 }
    166 
    167 } // namespace WebCore
    168