Fix an assertion failure in CalendarPickerElement::hostInput().
[WebKit-https.git] / Source / WebCore / html / shadow / CalendarPickerElement.cpp
1 /*
2  * Copyright (C) 2012 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 "CalendarPickerElement.h"
33
34 #if ENABLE(CALENDAR_PICKER)
35
36 #include "CalendarPicker.h"
37 #include "Chrome.h"
38 #include "ChromeClient.h"
39 #include "DateComponents.h"
40 #include "DocumentWriter.h"
41 #include "Event.h"
42 #include "FrameView.h"
43 #include "HTMLInputElement.h"
44 #include "HTMLNames.h"
45 #include "Language.h"
46 #include "LocalizedDate.h"
47 #include "LocalizedStrings.h"
48 #include "Page.h"
49 #include "RenderDetailsMarker.h"
50 #include "RenderTheme.h"
51 #include <wtf/text/StringBuilder.h>
52
53 using namespace WTF::Unicode;
54
55 namespace WebCore {
56
57 using namespace HTMLNames;
58
59 inline CalendarPickerElement::CalendarPickerElement(Document* document)
60     : HTMLDivElement(divTag, document)
61     , m_popup(0)
62 {
63     setShadowPseudoId("-webkit-calendar-picker-indicator");
64 }
65
66 PassRefPtr<CalendarPickerElement> CalendarPickerElement::create(Document* document)
67 {
68     return adoptRef(new CalendarPickerElement(document));
69 }
70
71 CalendarPickerElement::~CalendarPickerElement()
72 {
73     closePopup();
74     ASSERT(!m_popup);
75 }
76
77 RenderObject* CalendarPickerElement::createRenderer(RenderArena* arena, RenderStyle*)
78 {
79     return new (arena) RenderDetailsMarker(this);
80 }
81
82 inline HTMLInputElement* CalendarPickerElement::hostInput()
83 {
84     ASSERT(shadowAncestorNode());
85     ASSERT(shadowAncestorNode()->hasTagName(inputTag));
86     return static_cast<HTMLInputElement*>(shadowAncestorNode());
87 }
88
89 void CalendarPickerElement::defaultEventHandler(Event* event)
90 {
91     if (!renderer())
92         return;
93     HTMLInputElement* input = hostInput();
94     if (input->readOnly() || input->disabled())
95         return;
96
97     if (event->type() == eventNames().clickEvent) {
98         openPopup();
99         event->setDefaultHandled();
100     }
101
102     if (!event->defaultHandled())
103         HTMLDivElement::defaultEventHandler(event);
104 }
105
106 void CalendarPickerElement::openPopup()
107 {
108     if (m_popup)
109         return;
110     if (!document()->page())
111         return;
112     Chrome* chrome = document()->page()->chrome();
113     if (!chrome)
114         return;
115     if (!document()->view())
116         return;
117     IntRect elementRectInRootView = document()->view()->contentsToRootView(hostInput()->getPixelSnappedRect());
118     m_popup = chrome->client()->openPagePopup(this, elementRectInRootView);
119 }
120
121 void CalendarPickerElement::closePopup()
122 {
123     if (!m_popup)
124         return;
125     if (!document()->page())
126         return;
127     Chrome* chrome = document()->page()->chrome();
128     if (!chrome)
129         return;
130     chrome->client()->closePagePopup(m_popup);
131 }
132
133 void CalendarPickerElement::detach()
134 {
135     closePopup();
136     HTMLDivElement::detach();
137 }
138
139 IntSize CalendarPickerElement::contentSize()
140 {
141     return IntSize(100, 100);
142 }
143
144 #define addLiteral(literal, writer)    writer.addData(literal, sizeof(literal) - 1)
145
146 static inline void addString(const String& str, DocumentWriter& writer)
147 {
148     CString str8 = str.utf8();
149     writer.addData(str8.data(), str8.length());
150 }
151
152 static void addJavaScriptString(const String& str, DocumentWriter& writer)
153 {
154     addLiteral("\"", writer);
155     StringBuilder builder;
156     builder.reserveCapacity(str.length());
157     for (unsigned i = 0; i < str.length(); ++i) {
158         if (str[i] == '\\' || str[i] == '"')
159             builder.append('\\');
160         builder.append(str[i]);
161     }
162     addString(builder.toString(), writer);
163     addLiteral("\"", writer);
164 }
165
166 static void addProperty(const char* name, const String& value, DocumentWriter& writer)
167 {
168     writer.addData(name, strlen(name));
169     addLiteral(": ", writer);
170     addJavaScriptString(value, writer);
171     addLiteral(",\n", writer);
172 }
173
174 static void addProperty(const char* name, unsigned value, DocumentWriter& writer)
175 {
176     writer.addData(name, strlen(name));
177     addLiteral(": ", writer);
178     addString(String::number(value), writer);
179     addLiteral(",\n", writer);
180 }
181
182 static void addProperty(const char* name, bool value, DocumentWriter& writer)
183 {
184     writer.addData(name, strlen(name));
185     addLiteral(": ", writer);
186     if (value)
187         addLiteral("true", writer);
188     else
189         addLiteral("false", writer);
190     addLiteral(",\n", writer);
191 }
192
193 static void addProperty(const char* name, const Vector<String>& values, DocumentWriter& writer)
194 {
195     writer.addData(name, strlen(name));
196     addLiteral(": [", writer);
197     for (unsigned i = 0; i < values.size(); ++i) {
198         if (i)
199             addLiteral(",", writer);
200         addJavaScriptString(values[i], writer);
201     }
202     addLiteral("],\n", writer);
203 }
204
205 void CalendarPickerElement::writeDocument(DocumentWriter& writer)
206 {
207     HTMLInputElement* input = hostInput();
208     DateComponents date;
209     date.setMillisecondsSinceEpochForDate(input->minimum());
210     String minString = date.toString();
211     date.setMillisecondsSinceEpochForDate(input->maximum());
212     String maxString = date.toString();
213     Decimal step;
214     String stepString = input->fastGetAttribute(stepAttr);
215     if (stepString.isEmpty() || !input->getAllowedValueStep(&step))
216         stepString = "1";
217
218     addLiteral("<!DOCTYPE html><head><meta charset='UTF-8'><style>\n", writer);
219     writer.addData(calendarPickerCss, sizeof(calendarPickerCss));
220     if (document()->page()) {
221         CString extraStyle = document()->page()->theme()->extraCalendarPickerStyleSheet();
222         if (extraStyle.length())
223             writer.addData(extraStyle.data(), extraStyle.length());
224     }
225     addLiteral("</style></head><body><div id=main>Loading...</div><script>\n"
226                "window.dialogArguments = {\n", writer);
227     addProperty("min", minString, writer);
228     addProperty("max", maxString, writer);
229     addProperty("step", stepString, writer);
230     addProperty("required", input->required(), writer);
231     addProperty("currentValue", input->value(), writer);
232     addProperty("locale", defaultLanguage(), writer);
233     addProperty("todayLabel", calendarTodayText(), writer);
234     addProperty("clearLabel", calendarClearText(), writer);
235     addProperty("weekStartDay", firstDayOfWeek(), writer);
236     addProperty("monthLabels", monthLabels(), writer);
237     addProperty("dayLabels", weekDayShortLabels(), writer);
238     Direction dir = direction(monthLabels()[0][0]);
239     addProperty("isRTL", dir == RightToLeft || dir == RightToLeftArabic, writer);
240     addLiteral("}\n", writer);
241
242     writer.addData(calendarPickerJs, sizeof(calendarPickerJs));
243     addLiteral("</script></body>\n", writer);
244 }
245
246 void CalendarPickerElement::setValueAndClosePopup(int numValue, const String& stringValue)
247 {
248     ASSERT(m_popup);
249     closePopup();
250     if (numValue >= 0)
251         hostInput()->setValue(stringValue, DispatchChangeEvent);
252 }
253
254 void CalendarPickerElement::didClosePopup()
255 {
256     m_popup = 0;
257 }
258
259 }
260
261 #endif