initial import
[vuplus_webkit] / Source / WebCore / html / ValidationMessage.cpp
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 "ExceptionCodePlaceholder.h"
38 #include "FormAssociatedElement.h"
39 #include "HTMLBRElement.h"
40 #include "HTMLDivElement.h"
41 #include "HTMLNames.h"
42 #include "Page.h"
43 #include "RenderBlock.h"
44 #include "RenderObject.h"
45 #include "Settings.h"
46 #include "ShadowRoot.h"
47 #include "Text.h"
48 #include <wtf/PassOwnPtr.h>
49
50 namespace WebCore {
51
52 using namespace HTMLNames;
53
54 ALWAYS_INLINE ValidationMessage::ValidationMessage(FormAssociatedElement* element)
55     : m_element(element)
56 {
57 }
58
59 ValidationMessage::~ValidationMessage()
60 {
61     deleteBubbleTree();
62 }
63
64 PassOwnPtr<ValidationMessage> ValidationMessage::create(FormAssociatedElement* element)
65 {
66     return adoptPtr(new ValidationMessage(element));
67 }
68
69 void ValidationMessage::setMessage(const String& message)
70 {
71     // Don't modify the DOM tree in this context.
72     // If so, an assertion in Node::isFocusable() fails.
73     ASSERT(!message.isEmpty());
74     m_message = message;
75     if (!m_bubble)
76         m_timer = adoptPtr(new Timer<ValidationMessage>(this, &ValidationMessage::buildBubbleTree));
77     else
78         m_timer = adoptPtr(new Timer<ValidationMessage>(this, &ValidationMessage::setMessageDOMAndStartTimer));
79     m_timer->startOneShot(0);
80 }
81
82 void ValidationMessage::setMessageDOMAndStartTimer(Timer<ValidationMessage>*)
83 {
84     ASSERT(m_messageHeading);
85     ASSERT(m_messageBody);
86     m_messageHeading->removeAllChildren();
87     m_messageBody->removeAllChildren();
88     Vector<String> lines;
89     m_message.split('\n', lines);
90     Document* doc = m_messageHeading->document();
91     for (unsigned i = 0; i < lines.size(); ++i) {
92         if (i) {
93             m_messageBody->appendChild(Text::create(doc, lines[i]), ASSERT_NO_EXCEPTION);
94             if (i < lines.size() - 1)
95                 m_messageBody->appendChild(HTMLBRElement::create(doc), ASSERT_NO_EXCEPTION);
96         } else
97             m_messageHeading->setInnerText(lines[i], ASSERT_NO_EXCEPTION);
98     }
99
100     int magnification = doc->page() ? doc->page()->settings()->validationMessageTimerMaginification() : -1;
101     if (magnification <= 0)
102         m_timer.clear();
103     else {
104         m_timer = adoptPtr(new Timer<ValidationMessage>(this, &ValidationMessage::deleteBubbleTree));
105         m_timer->startOneShot(max(5.0, static_cast<double>(m_message.length()) * magnification / 1000));
106     }
107 }
108
109 static void adjustBubblePosition(const LayoutRect& hostRect, HTMLElement* bubble)
110 {
111     ASSERT(bubble);
112     if (hostRect.isEmpty())
113         return;
114     double hostX = hostRect.x();
115     double hostY = hostRect.y();
116     if (RenderBox* container = bubble->renderer()->containingBlock()) {
117         FloatPoint containerLocation = container->localToAbsolute();
118         hostX -= containerLocation.x() + container->borderLeft();
119         hostY -= containerLocation.y() + container->borderTop();
120     }
121     bubble->getInlineStyleDecl()->setProperty(CSSPropertyTop, hostY + hostRect.height(), CSSPrimitiveValue::CSS_PX);
122     // The 'left' value of ::-webkit-validation-bubble-arrow.
123     const int bubbleArrowTopOffset = 32;
124     double bubbleX = hostX;
125     if (hostRect.width() / 2 < bubbleArrowTopOffset)
126         bubbleX = max(hostX + hostRect.width() / 2 - bubbleArrowTopOffset, 0.0);
127     bubble->getInlineStyleDecl()->setProperty(CSSPropertyLeft, bubbleX, CSSPrimitiveValue::CSS_PX);
128 }
129
130 void ValidationMessage::buildBubbleTree(Timer<ValidationMessage>*)
131 {
132     HTMLElement* host = toHTMLElement(m_element);
133     Document* doc = host->document();
134     m_bubble = HTMLDivElement::create(doc);
135     m_bubble->setShadowPseudoId("-webkit-validation-bubble");
136     // Need to force position:absolute because RenderMenuList doesn't assume it
137     // contains non-absolute or non-fixed renderers as children.
138     m_bubble->getInlineStyleDecl()->setProperty(CSSPropertyPosition, CSSValueAbsolute);
139     ExceptionCode ec = 0;
140     host->ensureShadowRoot()->appendChild(m_bubble.get(), ec);
141     ASSERT(!ec);
142     adjustBubblePosition(host->getRect(), m_bubble.get());
143
144     RefPtr<HTMLDivElement> clipper = HTMLDivElement::create(doc);
145     clipper->setShadowPseudoId("-webkit-validation-bubble-arrow-clipper");
146     RefPtr<HTMLDivElement> bubbleArrow = HTMLDivElement::create(doc);
147     bubbleArrow->setShadowPseudoId("-webkit-validation-bubble-arrow");
148     clipper->appendChild(bubbleArrow.release(), ec);
149     ASSERT(!ec);
150     m_bubble->appendChild(clipper.release(), ec);
151     ASSERT(!ec);
152
153     RefPtr<HTMLElement> message = HTMLDivElement::create(doc);
154     message->setShadowPseudoId("-webkit-validation-bubble-message");
155     RefPtr<HTMLElement> icon = HTMLDivElement::create(doc);
156     icon->setShadowPseudoId("-webkit-validation-bubble-icon");
157     message->appendChild(icon.release(), ASSERT_NO_EXCEPTION);
158     RefPtr<HTMLElement> textBlock = HTMLDivElement::create(doc);
159     textBlock->setShadowPseudoId("-webkit-validation-bubble-text-block");
160     m_messageHeading = HTMLDivElement::create(doc);
161     m_messageHeading->setShadowPseudoId("-webkit-validation-bubble-heading");
162     textBlock->appendChild(m_messageHeading, ASSERT_NO_EXCEPTION);
163     m_messageBody = HTMLDivElement::create(doc);
164     m_messageBody->setShadowPseudoId("-webkit-validation-bubble-body");
165     textBlock->appendChild(m_messageBody, ASSERT_NO_EXCEPTION);
166     message->appendChild(textBlock.release(), ASSERT_NO_EXCEPTION);
167     m_bubble->appendChild(message.release(), ASSERT_NO_EXCEPTION);
168
169     setMessageDOMAndStartTimer();
170
171     // FIXME: Use transition to show the bubble.
172 }
173
174 void ValidationMessage::requestToHideMessage()
175 {
176     // We must not modify the DOM tree in this context by the same reason as setMessage().
177     m_timer = adoptPtr(new Timer<ValidationMessage>(this, &ValidationMessage::deleteBubbleTree));
178     m_timer->startOneShot(0);
179 }
180
181 void ValidationMessage::deleteBubbleTree(Timer<ValidationMessage>*)
182 {
183     if (m_bubble) {
184         m_messageHeading = 0;
185         m_messageBody = 0;
186         HTMLElement* host = toHTMLElement(m_element);
187         ExceptionCode ec;
188         host->shadowRoot()->removeChild(m_bubble.get(), ec);
189         m_bubble = 0;
190     }
191     m_message = String();
192 }
193
194 } // namespace WebCore