2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2011 Igalia S.L.
4 * Copyright (C) 2012 Samsung Electronics
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "TextInputController.h"
32 #include "DumpRenderTree.h"
33 #include "DumpRenderTreeChrome.h"
34 #include "WebCoreSupport/DumpRenderTreeSupportEfl.h"
35 #include <JavaScriptCore/JSObjectRef.h>
36 #include <JavaScriptCore/JSRetainPtr.h>
37 #include <JavaScriptCore/JSStringRef.h>
38 #include <JavaScriptCore/OpaqueJSString.h>
40 static JSValueRef setMarkedTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
42 if (!browser->mainView() || argumentCount < 3)
43 return JSValueMakeUndefined(context);
45 JSStringRef string = JSValueToStringCopy(context, arguments[0], exception);
47 size_t bufferSize = JSStringGetMaximumUTF8CStringSize(string);
48 char* text = new char[bufferSize];
49 JSStringGetUTF8CString(string, text, bufferSize);
50 JSStringRelease(string);
52 int start = static_cast<int>(JSValueToNumber(context, arguments[1], exception));
53 int length = static_cast<int>(JSValueToNumber(context, arguments[2], exception));
55 DumpRenderTreeSupportEfl::setComposition(browser->mainView(), text, start, length);
58 return JSValueMakeUndefined(context);
61 static JSValueRef hasMarkedTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
63 if (!browser->mainView())
64 return JSValueMakeUndefined(context);
66 return JSValueMakeBoolean(context, DumpRenderTreeSupportEfl::hasComposition(browser->mainView()));
69 static JSValueRef markedRangeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
71 if (!browser->mainView())
72 return JSValueMakeUndefined(context);
75 if (!DumpRenderTreeSupportEfl::compositionRange(browser->mainView(), &start, &length))
76 return JSValueMakeUndefined(context);
78 JSValueRef arrayValues[2];
79 arrayValues[0] = JSValueMakeNumber(context, start);
80 arrayValues[1] = JSValueMakeNumber(context, length);
81 JSObjectRef arrayObject = JSObjectMakeArray(context, 2, arrayValues, exception);
85 static JSValueRef insertTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
87 if (!browser->mainView() || argumentCount < 1)
88 return JSValueMakeUndefined(context);
90 JSStringRef string = JSValueToStringCopy(context, arguments[0], exception);
92 size_t bufferSize = JSStringGetMaximumUTF8CStringSize(string);
93 char* text = new char[bufferSize];
94 JSStringGetUTF8CString(string, text, bufferSize);
95 JSStringRelease(string);
97 DumpRenderTreeSupportEfl::confirmComposition(browser->mainView(), text);
100 return JSValueMakeUndefined(context);
103 static JSValueRef unmarkTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
105 if (!browser->mainView())
106 return JSValueMakeUndefined(context);
108 DumpRenderTreeSupportEfl::confirmComposition(browser->mainView(), 0);
109 return JSValueMakeUndefined(context);
112 static JSValueRef firstRectForCharacterRangeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
114 if (!browser->mainView() || argumentCount < 2)
115 return JSValueMakeUndefined(context);
117 int location = static_cast<int>(JSValueToNumber(context, arguments[0], exception));
118 int length = static_cast<int>(JSValueToNumber(context, arguments[1], exception));
120 WebCore::IntRect rect = DumpRenderTreeSupportEfl::firstRectForCharacterRange(browser->mainView(), location, length);
122 JSValueRef arrayValues[4];
123 arrayValues[0] = JSValueMakeNumber(context, rect.x());
124 arrayValues[1] = JSValueMakeNumber(context, rect.y());
125 arrayValues[2] = JSValueMakeNumber(context, rect.width());
126 arrayValues[3] = JSValueMakeNumber(context, rect.height());
127 JSObjectRef arrayObject = JSObjectMakeArray(context, 4, arrayValues, exception);
131 static JSValueRef selectedRangeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
133 if (!browser->mainView())
134 return JSValueMakeUndefined(context);
137 if (!DumpRenderTreeSupportEfl::selectedRange(browser->mainView(), &start, &length))
138 return JSValueMakeUndefined(context);
140 JSValueRef arrayValues[2];
141 arrayValues[0] = JSValueMakeNumber(context, start);
142 arrayValues[1] = JSValueMakeNumber(context, length);
143 JSObjectRef arrayObject = JSObjectMakeArray(context, 2, arrayValues, exception);
147 static JSStaticFunction staticFunctions[] = {
148 { "setMarkedText", setMarkedTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
149 { "hasMarkedText", hasMarkedTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
150 { "markedRange", markedRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
151 { "insertText", insertTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
152 { "unmarkText", unmarkTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
153 { "firstRectForCharacterRange", firstRectForCharacterRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
154 { "selectedRange", selectedRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
158 static JSClassRef getClass(JSContextRef context)
160 static JSClassRef textInputControllerClass = 0;
162 if (!textInputControllerClass) {
163 JSClassDefinition classDefinition = {
165 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
166 classDefinition.staticFunctions = staticFunctions;
168 textInputControllerClass = JSClassCreate(&classDefinition);
171 return textInputControllerClass;
174 JSObjectRef makeTextInputController(JSContextRef context)
176 return JSObjectMake(context, getClass(context), 0);