2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "ValidationMessage.h"
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"
43 #include "RenderBlock.h"
44 #include "RenderObject.h"
46 #include "ShadowRoot.h"
48 #include <wtf/PassOwnPtr.h>
52 using namespace HTMLNames;
54 ALWAYS_INLINE ValidationMessage::ValidationMessage(FormAssociatedElement* element)
59 ValidationMessage::~ValidationMessage()
64 PassOwnPtr<ValidationMessage> ValidationMessage::create(FormAssociatedElement* element)
66 return adoptPtr(new ValidationMessage(element));
69 void ValidationMessage::setMessage(const String& message)
71 // Don't modify the DOM tree in this context.
72 // If so, an assertion in Node::isFocusable() fails.
73 ASSERT(!message.isEmpty());
76 m_timer = adoptPtr(new Timer<ValidationMessage>(this, &ValidationMessage::buildBubbleTree));
78 m_timer = adoptPtr(new Timer<ValidationMessage>(this, &ValidationMessage::setMessageDOMAndStartTimer));
79 m_timer->startOneShot(0);
82 void ValidationMessage::setMessageDOMAndStartTimer(Timer<ValidationMessage>*)
84 ASSERT(m_messageHeading);
85 ASSERT(m_messageBody);
86 m_messageHeading->removeAllChildren();
87 m_messageBody->removeAllChildren();
89 m_message.split('\n', lines);
90 Document* doc = m_messageHeading->document();
91 for (unsigned i = 0; i < lines.size(); ++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);
97 m_messageHeading->setInnerText(lines[i], ASSERT_NO_EXCEPTION);
100 int magnification = doc->page() ? doc->page()->settings()->validationMessageTimerMaginification() : -1;
101 if (magnification <= 0)
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));
109 static void adjustBubblePosition(const LayoutRect& hostRect, HTMLElement* bubble)
112 if (hostRect.isEmpty())
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();
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);
130 void ValidationMessage::buildBubbleTree(Timer<ValidationMessage>*)
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);
142 adjustBubblePosition(host->getRect(), m_bubble.get());
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);
150 m_bubble->appendChild(clipper.release(), ec);
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);
169 setMessageDOMAndStartTimer();
171 // FIXME: Use transition to show the bubble.
174 void ValidationMessage::requestToHideMessage()
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);
181 void ValidationMessage::deleteBubbleTree(Timer<ValidationMessage>*)
184 m_messageHeading = 0;
186 HTMLElement* host = toHTMLElement(m_element);
188 host->shadowRoot()->removeChild(m_bubble.get(), ec);
191 m_message = String();
194 } // namespace WebCore