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 "CSSStyleSelector.h"
35 #include "FormAssociatedElement.h"
36 #include "HTMLBRElement.h"
37 #include "HTMLNames.h"
38 #include "RenderObject.h"
40 #include <wtf/PassOwnPtr.h>
44 using namespace HTMLNames;
46 ALWAYS_INLINE ValidationMessage::ValidationMessage(FormAssociatedElement* element)
51 ValidationMessage::~ValidationMessage()
56 PassOwnPtr<ValidationMessage> ValidationMessage::create(FormAssociatedElement* element)
58 return adoptPtr(new ValidationMessage(element));
61 void ValidationMessage::setMessage(const String& message)
63 // Don't modify the DOM tree in this context.
64 // If so, an assertion in Node::isFocusable() fails.
65 ASSERT(!message.isEmpty());
68 m_timer.set(new Timer<ValidationMessage>(this, &ValidationMessage::buildBubbleTree));
70 m_timer.set(new Timer<ValidationMessage>(this, &ValidationMessage::setMessageDOMAndStartTimer));
71 m_timer->startOneShot(0);
74 void ValidationMessage::setMessageDOMAndStartTimer(Timer<ValidationMessage>*)
76 ASSERT(m_bubbleMessage);
77 m_bubbleMessage->removeAllChildren();
79 m_message.split('\n', lines);
80 Document* doc = m_bubbleMessage->document();
82 for (unsigned i = 0; i < lines.size(); ++i) {
84 m_bubbleMessage->appendChild(HTMLBRElement::create(doc), ec);
85 m_bubbleMessage->appendChild(Text::create(doc, lines[i]), ec);
87 RefPtr<HTMLElement> bold = HTMLElement::create(bTag, doc);
88 bold->setInnerText(lines[i], ec);
89 m_bubbleMessage->appendChild(bold.release(), ec);
93 m_timer.set(new Timer<ValidationMessage>(this, &ValidationMessage::deleteBubbleTree));
94 m_timer->startOneShot(max(5.0, m_message.length() / 20.0));
97 class ElementWithPseudoId : public HTMLElement {
99 static PassRefPtr<HTMLElement> create(Document* doc, const AtomicString& pseudoName)
101 return adoptRef(new ElementWithPseudoId(doc, pseudoName));
105 ElementWithPseudoId(Document* doc, const AtomicString& pseudoName)
106 : HTMLElement(divTag, doc)
107 , m_pseudoName(pseudoName) { };
108 virtual AtomicString shadowPseudoId() const { return m_pseudoName; }
111 AtomicString m_pseudoName;
114 void ValidationMessage::buildBubbleTree(Timer<ValidationMessage>*)
116 HTMLElement* host = toHTMLElement(m_element);
117 Document* doc = host->document();
118 m_bubble = ElementWithPseudoId::create(doc, "-webkit-validation-bubble");
119 ExceptionCode ec = 0;
120 // FIXME: We need a way to host multiple shadow roots in a single node, or
121 // to inherit an existing shadow tree.
122 if (host->shadowRoot())
123 host->shadowRoot()->appendChild(m_bubble.get(), ec);
125 host->setShadowRoot(m_bubble);
126 // FIXME: The following attach() should be unnecessary.
130 m_bubble->appendChild(ElementWithPseudoId::create(doc, "-webkit-validation-bubble-top-outer-arrow"), ec);
131 m_bubble->appendChild(ElementWithPseudoId::create(doc, "-webkit-validation-bubble-top-inner-arrow"), ec);
132 m_bubbleMessage = ElementWithPseudoId::create(doc, "-webkit-validation-bubble-message");
133 m_bubble->appendChild(m_bubbleMessage, ec);
135 setMessageDOMAndStartTimer();
137 // FIXME: Use transition to show the bubble.
139 // We don't need to adjust the bubble location. The default position is enough.
142 void ValidationMessage::requestToHideMessage()
144 // We must not modify the DOM tree in this context by the same reason as setMessage().
145 m_timer.set(new Timer<ValidationMessage>(this, &ValidationMessage::deleteBubbleTree));
146 m_timer->startOneShot(0);
149 void ValidationMessage::deleteBubbleTree(Timer<ValidationMessage>*)
153 HTMLElement* host = toHTMLElement(m_element);
154 if (m_bubble->isShadowRoot())
155 host->setShadowRoot(0);
158 host->shadowRoot()->removeChild(m_bubble.get(), ec);
162 m_message = String();
165 } // namespace WebCore