TextInputController.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2010 Google Inc. All rights reserved.
  3. * Copyright (C) 2011 Igalia S.L.
  4. * Copyright (C) 2012 Samsung Electronics
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  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.
  15. *
  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.
  27. */
  28. #include "config.h"
  29. #include "TextInputController.h"
  30. #include "DumpRenderTree.h"
  31. #include "DumpRenderTreeChrome.h"
  32. #include "WebCoreSupport/DumpRenderTreeSupportEfl.h"
  33. #include <JavaScriptCore/JSObjectRef.h>
  34. #include <JavaScriptCore/JSRetainPtr.h>
  35. #include <JavaScriptCore/JSStringRef.h>
  36. #include <JavaScriptCore/OpaqueJSString.h>
  37. static JSValueRef setMarkedTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  38. {
  39. if (!browser->mainView() || argumentCount < 3)
  40. return JSValueMakeUndefined(context);
  41. JSStringRef string = JSValueToStringCopy(context, arguments[0], exception);
  42. size_t bufferSize = JSStringGetMaximumUTF8CStringSize(string);
  43. char* text = new char[bufferSize];
  44. JSStringGetUTF8CString(string, text, bufferSize);
  45. JSStringRelease(string);
  46. int start = static_cast<int>(JSValueToNumber(context, arguments[1], exception));
  47. int length = static_cast<int>(JSValueToNumber(context, arguments[2], exception));
  48. DumpRenderTreeSupportEfl::setComposition(browser->mainView(), text, start, length);
  49. delete[] text;
  50. return JSValueMakeUndefined(context);
  51. }
  52. static JSValueRef hasMarkedTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  53. {
  54. if (!browser->mainView())
  55. return JSValueMakeUndefined(context);
  56. return JSValueMakeBoolean(context, DumpRenderTreeSupportEfl::hasComposition(browser->mainView()));
  57. }
  58. static JSValueRef markedRangeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  59. {
  60. if (!browser->mainView())
  61. return JSValueMakeUndefined(context);
  62. int start, length;
  63. if (!DumpRenderTreeSupportEfl::compositionRange(browser->mainView(), &start, &length))
  64. return JSValueMakeUndefined(context);
  65. JSValueRef arrayValues[2];
  66. arrayValues[0] = JSValueMakeNumber(context, start);
  67. arrayValues[1] = JSValueMakeNumber(context, length);
  68. JSObjectRef arrayObject = JSObjectMakeArray(context, 2, arrayValues, exception);
  69. return arrayObject;
  70. }
  71. static JSValueRef insertTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  72. {
  73. if (!browser->mainView() || argumentCount < 1)
  74. return JSValueMakeUndefined(context);
  75. JSStringRef string = JSValueToStringCopy(context, arguments[0], exception);
  76. size_t bufferSize = JSStringGetMaximumUTF8CStringSize(string);
  77. char* text = new char[bufferSize];
  78. JSStringGetUTF8CString(string, text, bufferSize);
  79. JSStringRelease(string);
  80. DumpRenderTreeSupportEfl::confirmComposition(browser->mainView(), text);
  81. delete[] text;
  82. return JSValueMakeUndefined(context);
  83. }
  84. static JSValueRef unmarkTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  85. {
  86. if (!browser->mainView())
  87. return JSValueMakeUndefined(context);
  88. DumpRenderTreeSupportEfl::confirmComposition(browser->mainView(), 0);
  89. return JSValueMakeUndefined(context);
  90. }
  91. static JSValueRef firstRectForCharacterRangeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  92. {
  93. if (!browser->mainView() || argumentCount < 2)
  94. return JSValueMakeUndefined(context);
  95. int location = static_cast<int>(JSValueToNumber(context, arguments[0], exception));
  96. int length = static_cast<int>(JSValueToNumber(context, arguments[1], exception));
  97. WebCore::IntRect rect = DumpRenderTreeSupportEfl::firstRectForCharacterRange(browser->mainView(), location, length);
  98. JSValueRef arrayValues[4];
  99. arrayValues[0] = JSValueMakeNumber(context, rect.x());
  100. arrayValues[1] = JSValueMakeNumber(context, rect.y());
  101. arrayValues[2] = JSValueMakeNumber(context, rect.width());
  102. arrayValues[3] = JSValueMakeNumber(context, rect.height());
  103. JSObjectRef arrayObject = JSObjectMakeArray(context, 4, arrayValues, exception);
  104. return arrayObject;
  105. }
  106. static JSValueRef selectedRangeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  107. {
  108. if (!browser->mainView())
  109. return JSValueMakeUndefined(context);
  110. int start, length;
  111. if (!DumpRenderTreeSupportEfl::selectedRange(browser->mainView(), &start, &length))
  112. return JSValueMakeUndefined(context);
  113. JSValueRef arrayValues[2];
  114. arrayValues[0] = JSValueMakeNumber(context, start);
  115. arrayValues[1] = JSValueMakeNumber(context, length);
  116. JSObjectRef arrayObject = JSObjectMakeArray(context, 2, arrayValues, exception);
  117. return arrayObject;
  118. }
  119. static JSStaticFunction staticFunctions[] = {
  120. { "setMarkedText", setMarkedTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  121. { "hasMarkedText", hasMarkedTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  122. { "markedRange", markedRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  123. { "insertText", insertTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  124. { "unmarkText", unmarkTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  125. { "firstRectForCharacterRange", firstRectForCharacterRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  126. { "selectedRange", selectedRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  127. { 0, 0, 0 }
  128. };
  129. static JSClassRef getClass(JSContextRef context)
  130. {
  131. static JSClassRef textInputControllerClass = 0;
  132. if (!textInputControllerClass) {
  133. JSClassDefinition classDefinition = {
  134. 0, 0, 0, 0, 0, 0,
  135. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  136. classDefinition.staticFunctions = staticFunctions;
  137. textInputControllerClass = JSClassCreate(&classDefinition);
  138. }
  139. return textInputControllerClass;
  140. }
  141. JSObjectRef makeTextInputController(JSContextRef context)
  142. {
  143. return JSObjectMake(context, getClass(context), 0);
  144. }