Changeset 126200 in webkit


Ignore:
Timestamp:
Aug 21, 2012 3:49:58 PM (12 years ago)
Author:
commit-queue@webkit.org
Message:

[chromium] Add WebView methods setCompositionFromExistingText and extendSelectionAndDelete.
https://bugs.webkit.org/show_bug.cgi?id=93724

Patch by Oli Lan <olilan@chromium.org> on 2012-08-21
Reviewed by Ryosuke Niwa.

This adds two new methods to WebViewImpl.

1) setCompositionFromExistingText creates a new composition from the existing text
in the currently focused input field. The new composition is between the two offsets
provided, relative to the rootEditableElement. The current selection is left unchanged.

2) extendSelectionAndDelete extends the selection by the specified number of characters
before and after, and then deletes the selection. If the selection is just a caret, the effect
is to delete the specified number of characters before and after the current editing point.

These methods will be used e.g. by the Android port to implement IME functionality.

New tests WebViewTest.ExtendSelectionAndDelete and WebViewTest.SetCompositionFromExistingText
test the two new methods.

  • public/WebView.h:

(WebView):

  • src/WebViewImpl.cpp:

(WebKit::WebViewImpl::setComposingRegion):
(WebKit):
(WebKit::extendSelectionAndDelete):

  • src/WebViewImpl.h:

(WebViewImpl):

  • tests/WebViewTest.cpp:
Location:
trunk/Source/WebKit/chromium
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/chromium/ChangeLog

    r126199 r126200  
     12012-08-21  Oli Lan  <olilan@chromium.org>
     2
     3        [chromium] Add WebView methods setCompositionFromExistingText and extendSelectionAndDelete.
     4        https://bugs.webkit.org/show_bug.cgi?id=93724
     5
     6        Reviewed by Ryosuke Niwa.
     7
     8        This adds two new methods to WebViewImpl.
     9
     10        1) setCompositionFromExistingText creates a new composition from the existing text
     11        in the currently focused input field. The new composition is between the two offsets
     12        provided, relative to the rootEditableElement. The current selection is left unchanged.
     13       
     14        2) extendSelectionAndDelete extends the selection by the specified number of characters
     15        before and after, and then deletes the selection. If the selection is just a caret, the effect
     16        is to delete the specified number of characters before and after the current editing point.
     17
     18        These methods will be used e.g. by the Android port to implement IME functionality.
     19
     20        New tests WebViewTest.ExtendSelectionAndDelete and WebViewTest.SetCompositionFromExistingText
     21        test the two new methods.       
     22
     23        * public/WebView.h:
     24        (WebView):
     25        * src/WebViewImpl.cpp:
     26        (WebKit::WebViewImpl::setComposingRegion):
     27        (WebKit):
     28        (WebKit::extendSelectionAndDelete):
     29        * src/WebViewImpl.h:
     30        (WebViewImpl):
     31        * tests/WebViewTest.cpp:
     32
    1332012-08-21  Alec Flett  <alecflett@chromium.org>
    234
  • trunk/Source/WebKit/chromium/public/WebView.h

    r125738 r126200  
    461461
    462462    virtual bool setEditableSelectionOffsets(int start, int end) = 0;
     463    virtual bool setCompositionFromExistingText(int compositionStart, int compositionEnd, const WebVector<WebCompositionUnderline>& underlines) = 0;
     464    virtual void extendSelectionAndDelete(int before, int after) = 0;
    463465
    464466    virtual bool isSelectionEditable() const = 0;
  • trunk/Source/WebKit/chromium/src/WebViewImpl.cpp

    r126174 r126200  
    22152215}
    22162216
     2217bool WebViewImpl::setCompositionFromExistingText(int compositionStart, int compositionEnd, const WebVector<WebCompositionUnderline>& underlines)
     2218{
     2219    const Frame* focused = focusedWebCoreFrame();
     2220    if (!focused)
     2221        return false;
     2222
     2223    Editor* editor = focused->editor();
     2224    if (!editor || !editor->canEdit())
     2225        return false;
     2226
     2227    editor->cancelComposition();
     2228
     2229    if (compositionStart == compositionEnd)
     2230        return true;
     2231
     2232    size_t location;
     2233    size_t length;
     2234    caretOrSelectionRange(&location, &length);
     2235    editor->setIgnoreCompositionSelectionChange(true);
     2236    editor->setSelectionOffsets(compositionStart, compositionEnd);
     2237    String text = editor->selectedText();
     2238    focused->document()->execCommand("delete", true);
     2239    editor->setComposition(text, CompositionUnderlineVectorBuilder(underlines), 0, 0);
     2240    editor->setSelectionOffsets(location, location + length);
     2241    editor->setIgnoreCompositionSelectionChange(false);
     2242
     2243    return true;
     2244}
     2245
     2246void WebViewImpl::extendSelectionAndDelete(int before, int after)
     2247{
     2248    const Frame* focused = focusedWebCoreFrame();
     2249    if (!focused)
     2250        return;
     2251
     2252    Editor* editor = focused->editor();
     2253    if (!editor || !editor->canEdit())
     2254        return;
     2255
     2256    FrameSelection* selection = focused->selection();
     2257    if (!selection)
     2258        return;
     2259
     2260    size_t location;
     2261    size_t length;
     2262    RefPtr<Range> range = selection->selection().firstRange();
     2263    if (range && TextIterator::getLocationAndLengthFromRange(selection->rootEditableElement(), range.get(), location, length)) {
     2264        editor->setSelectionOffsets(max(static_cast<int>(location) - before, 0), location + length + after);
     2265        focused->document()->execCommand("delete", true);
     2266    }
     2267}
     2268
    22172269bool WebViewImpl::isSelectionEditable() const
    22182270{
  • trunk/Source/WebKit/chromium/src/WebViewImpl.h

    r126174 r126200  
    163163    virtual WebTextInputType textInputType();
    164164    virtual bool setEditableSelectionOffsets(int start, int end);
     165    virtual bool setCompositionFromExistingText(int compositionStart, int compositionEnd, const WebVector<WebCompositionUnderline>& underlines);
     166    virtual void extendSelectionAndDelete(int before, int after);
    165167    virtual bool isSelectionEditable() const;
    166168    virtual WebColor backgroundColor() const;
  • trunk/Source/WebKit/chromium/tests/WebViewTest.cpp

    r123965 r126200  
    380380}
    381381
    382 }
     382TEST_F(WebViewTest, ExtendSelectionAndDelete)
     383{
     384    URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8("input_field_populated.html"));
     385    WebView* webView = FrameTestHelpers::createWebViewAndLoad(m_baseURL + "input_field_populated.html");
     386    webView->setInitialFocus(false);
     387    webView->setEditableSelectionOffsets(10, 10);
     388    webView->extendSelectionAndDelete(5, 8);
     389    WebTextInputInfo info = webView->textInputInfo();
     390    EXPECT_EQ("01234ijklmnopqrstuvwxyz", std::string(info.value.utf8().data()));
     391    EXPECT_EQ(5, info.selectionStart);
     392    EXPECT_EQ(5, info.selectionEnd);
     393    webView->extendSelectionAndDelete(10, 0);
     394    info = webView->textInputInfo();
     395    EXPECT_EQ("ijklmnopqrstuvwxyz", std::string(info.value.utf8().data()));
     396    webView->close();
     397}
     398
     399TEST_F(WebViewTest, SetCompositionFromExistingText)
     400{
     401    URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8("input_field_populated.html"));
     402    WebView* webView = FrameTestHelpers::createWebViewAndLoad(m_baseURL + "input_field_populated.html");
     403    webView->setInitialFocus(false);
     404    WebVector<WebCompositionUnderline> emptyUnderlines;
     405    webView->setEditableSelectionOffsets(4, 10);
     406    webView->setCompositionFromExistingText(8, 12, emptyUnderlines);
     407    WebTextInputInfo info = webView->textInputInfo();
     408    EXPECT_EQ(4, info.selectionStart);
     409    EXPECT_EQ(10, info.selectionEnd);
     410    EXPECT_EQ(8, info.compositionStart);
     411    EXPECT_EQ(12, info.compositionEnd);
     412    webView->setCompositionFromExistingText(0, 0, emptyUnderlines);
     413    info = webView->textInputInfo();
     414    EXPECT_EQ(4, info.selectionStart);
     415    EXPECT_EQ(10, info.selectionEnd);
     416    EXPECT_EQ(-1, info.compositionStart);
     417    EXPECT_EQ(-1, info.compositionEnd);
     418    webView->close();
     419}
     420
     421}
Note: See TracChangeset for help on using the changeset viewer.