2 * Copyright (C) 2012 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 "CalendarPickerElement.h"
34 #if ENABLE(CALENDAR_PICKER)
36 #include "CalendarPicker.h"
38 #include "ChromeClient.h"
39 #include "DateComponents.h"
40 #include "DocumentWriter.h"
42 #include "FrameView.h"
43 #include "HTMLInputElement.h"
44 #include "HTMLNames.h"
46 #include "LocalizedDate.h"
47 #include "LocalizedStrings.h"
49 #include "RenderDetailsMarker.h"
50 #include "RenderTheme.h"
51 #include <wtf/text/StringBuilder.h>
53 using namespace WTF::Unicode;
57 using namespace HTMLNames;
59 inline CalendarPickerElement::CalendarPickerElement(Document* document)
60 : HTMLDivElement(divTag, document)
63 setShadowPseudoId("-webkit-calendar-picker-indicator");
66 PassRefPtr<CalendarPickerElement> CalendarPickerElement::create(Document* document)
68 return adoptRef(new CalendarPickerElement(document));
71 RenderObject* CalendarPickerElement::createRenderer(RenderArena* arena, RenderStyle*)
73 return new (arena) RenderDetailsMarker(this);
76 inline HTMLInputElement* CalendarPickerElement::hostInput()
78 ASSERT(shadowAncestorNode());
79 ASSERT(shadowAncestorNode()->hasTagName(inputTag));
80 return static_cast<HTMLInputElement*>(shadowAncestorNode());
83 void CalendarPickerElement::defaultEventHandler(Event* event)
85 HTMLInputElement* input = hostInput();
86 if (input->readOnly() || input->disabled())
89 if (event->type() == eventNames().clickEvent) {
91 event->setDefaultHandled();
94 if (!event->defaultHandled())
95 HTMLDivElement::defaultEventHandler(event);
98 void CalendarPickerElement::openPopup()
102 if (!document()->page())
104 Chrome* chrome = document()->page()->chrome();
107 if (!document()->view())
109 IntRect elementRectInRootView = document()->view()->contentsToRootView(hostInput()->getPixelSnappedRect());
110 m_popup = chrome->client()->openPagePopup(this, elementRectInRootView);
113 void CalendarPickerElement::closePopup()
117 if (!document()->page())
119 Chrome* chrome = document()->page()->chrome();
122 chrome->client()->closePagePopup(m_popup);
125 void CalendarPickerElement::detach()
128 HTMLDivElement::detach();
131 IntSize CalendarPickerElement::contentSize()
133 return IntSize(100, 100);
136 #define addLiteral(literal, writer) writer.addData(literal, sizeof(literal) - 1)
138 static inline void addString(const String& str, DocumentWriter& writer)
140 CString str8 = str.utf8();
141 writer.addData(str8.data(), str8.length());
144 static void addJavaScriptString(const String& str, DocumentWriter& writer)
146 addLiteral("\"", writer);
147 StringBuilder builder;
148 builder.reserveCapacity(str.length());
149 for (unsigned i = 0; i < str.length(); ++i) {
150 if (str[i] == '\\' || str[i] == '"')
151 builder.append('\\');
152 builder.append(str[i]);
154 addString(builder.toString(), writer);
155 addLiteral("\"", writer);
158 static void addProperty(const char* name, const String& value, DocumentWriter& writer)
160 writer.addData(name, strlen(name));
161 addLiteral(": ", writer);
162 addJavaScriptString(value, writer);
163 addLiteral(",\n", writer);
166 static void addProperty(const char* name, unsigned value, DocumentWriter& writer)
168 writer.addData(name, strlen(name));
169 addLiteral(": ", writer);
170 addString(String::number(value), writer);
171 addLiteral(",\n", writer);
174 static void addProperty(const char* name, bool value, DocumentWriter& writer)
176 writer.addData(name, strlen(name));
177 addLiteral(": ", writer);
179 addLiteral("true", writer);
181 addLiteral("false", writer);
182 addLiteral(",\n", writer);
185 static void addProperty(const char* name, const Vector<String>& values, DocumentWriter& writer)
187 writer.addData(name, strlen(name));
188 addLiteral(": [", writer);
189 for (unsigned i = 0; i < values.size(); ++i) {
191 addLiteral(",", writer);
192 addJavaScriptString(values[i], writer);
194 addLiteral("],\n", writer);
197 void CalendarPickerElement::writeDocument(DocumentWriter& writer)
199 HTMLInputElement* input = hostInput();
201 date.setMillisecondsSinceEpochForDate(input->minimum());
202 String minString = date.toString();
203 date.setMillisecondsSinceEpochForDate(input->maximum());
204 String maxString = date.toString();
206 String stepString = input->fastGetAttribute(stepAttr);
207 if (stepString.isEmpty() || !input->getAllowedValueStep(&step))
210 addLiteral("<!DOCTYPE html><head><meta charset='UTF-8'><style>\n", writer);
211 writer.addData(calendarPickerCss, sizeof(calendarPickerCss));
212 if (document()->page()) {
213 CString extraStyle = document()->page()->theme()->extraCalendarPickerStyleSheet();
214 if (extraStyle.length())
215 writer.addData(extraStyle.data(), extraStyle.length());
217 addLiteral("</style></head><body><div id=main>Loading...</div><script>\n"
218 "window.dialogArguments = {\n", writer);
219 addProperty("min", minString, writer);
220 addProperty("max", maxString, writer);
221 addProperty("step", stepString, writer);
222 addProperty("required", input->required(), writer);
223 addProperty("currentValue", input->value(), writer);
224 addProperty("locale", defaultLanguage(), writer);
225 addProperty("todayLabel", calendarTodayText(), writer);
226 addProperty("clearLabel", calendarClearText(), writer);
227 addProperty("weekStartDay", firstDayOfWeek(), writer);
228 addProperty("monthLabels", monthLabels(), writer);
229 addProperty("dayLabels", weekDayShortLabels(), writer);
230 Direction dir = direction(monthLabels()[0][0]);
231 addProperty("isRTL", dir == RightToLeft || dir == RightToLeftArabic, writer);
232 addLiteral("}\n", writer);
234 writer.addData(calendarPickerJs, sizeof(calendarPickerJs));
235 addLiteral("</script></body>\n", writer);
238 void CalendarPickerElement::setValueAndClosePopup(int numValue, const String& stringValue)
243 hostInput()->setValue(stringValue, DispatchChangeEvent);
246 void CalendarPickerElement::didClosePopup()