~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebKit2/WebProcess/WebCoreSupport/mac/WebEditorClientMac.mm

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2006, 2010, 2011 Apple Computer, Inc.  All rights reserved.
 
3
 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 *
 
9
 * 1.  Redistributions of source code must retain the above copyright
 
10
 *     notice, this list of conditions and the following disclaimer. 
 
11
 * 2.  Redistributions in binary form must reproduce the above copyright
 
12
 *     notice, this list of conditions and the following disclaimer in the
 
13
 *     documentation and/or other materials provided with the distribution. 
 
14
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 
15
 *     its contributors may be used to endorse or promote products derived
 
16
 *     from this software without specific prior written permission. 
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
 
 
30
#import "config.h"
 
31
#import "WebEditorClient.h"
 
32
 
 
33
#import "WebCoreArgumentCoders.h"
 
34
#import "WebPage.h"
 
35
#import "WebFrame.h"
 
36
#import "WebPageProxyMessages.h"
 
37
#import "WebProcess.h"
 
38
#import <WebCore/ArchiveResource.h>
 
39
#import <WebCore/DocumentFragment.h>
 
40
#import <WebCore/DOMDocumentFragmentInternal.h>
 
41
#import <WebCore/DOMDocumentInternal.h>
 
42
#import <WebCore/FocusController.h>
 
43
#import <WebCore/Frame.h>
 
44
#import <WebCore/KeyboardEvent.h>
 
45
#import <WebCore/NotImplemented.h>
 
46
#import <WebCore/Page.h>
 
47
#import <WebKit/WebResource.h>
 
48
#import <WebKit/WebNSURLExtras.h>
 
49
 
 
50
using namespace WebCore;
 
51
 
 
52
@interface NSAttributedString (WebNSAttributedStringDetails)
 
53
- (DOMDocumentFragment*)_documentFromRange:(NSRange)range document:(DOMDocument*)document documentAttributes:(NSDictionary *)dict subresources:(NSArray **)subresources;
 
54
@end
 
55
 
 
56
@interface WebResource (WebResourceInternal)
 
57
- (WebCore::ArchiveResource*)_coreResource;
 
58
@end
 
59
 
 
60
namespace WebKit {
 
61
    
 
62
void WebEditorClient::handleKeyboardEvent(KeyboardEvent* event)
 
63
{
 
64
    if (m_page->handleEditingKeyboardEvent(event, false))
 
65
        event->setDefaultHandled();
 
66
}
 
67
 
 
68
void WebEditorClient::handleInputMethodKeydown(KeyboardEvent* event)
 
69
{
 
70
    if (m_page->handleEditingKeyboardEvent(event, true))
 
71
        event->setDefaultHandled();
 
72
}
 
73
    
 
74
NSString *WebEditorClient::userVisibleString(NSURL *url)
 
75
{
 
76
    return [url _web_userVisibleString];
 
77
}
 
78
 
 
79
NSURL *WebEditorClient::canonicalizeURL(NSURL *url)
 
80
{
 
81
    return [url _webkit_canonicalize];
 
82
}
 
83
 
 
84
NSURL *WebEditorClient::canonicalizeURLString(NSString *URLString)
 
85
{
 
86
    NSURL *URL = nil;
 
87
    if ([URLString _webkit_looksLikeAbsoluteURL])
 
88
        URL = [[NSURL _web_URLWithUserTypedString:URLString] _webkit_canonicalize];
 
89
    return URL;
 
90
}
 
91
    
 
92
static NSArray *createExcludedElementsForAttributedStringConversion()
 
93
{
 
94
    NSArray *elements = [[NSArray alloc] initWithObjects: 
 
95
        // Omit style since we want style to be inline so the fragment can be easily inserted.
 
96
        @"style", 
 
97
        // Omit xml so the result is not XHTML.
 
98
        @"xml", 
 
99
        // Omit tags that will get stripped when converted to a fragment anyway.
 
100
        @"doctype", @"html", @"head", @"body", 
 
101
        // Omit deprecated tags.
 
102
        @"applet", @"basefont", @"center", @"dir", @"font", @"isindex", @"menu", @"s", @"strike", @"u", 
 
103
        // Omit object so no file attachments are part of the fragment.
 
104
        @"object", nil];
 
105
    CFRetain(elements);
 
106
    return elements;
 
107
}
 
108
 
 
109
DocumentFragment* WebEditorClient::documentFragmentFromAttributedString(NSAttributedString *string, Vector<RefPtr<ArchiveResource> >& resources)
 
110
{
 
111
    static NSArray *excludedElements = createExcludedElementsForAttributedStringConversion();
 
112
    
 
113
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: excludedElements,
 
114
        NSExcludedElementsDocumentAttribute, nil, @"WebResourceHandler", nil];
 
115
    
 
116
    NSArray *subResources;
 
117
    Document* document = m_page->mainFrame() ? m_page->mainFrame()->document() : 0;
 
118
    DOMDocumentFragment* fragment = [string _documentFromRange:NSMakeRange(0, [string length])
 
119
                                                      document:kit(document)
 
120
                                            documentAttributes:dictionary
 
121
                                                  subresources:&subResources];
 
122
    for (WebResource* resource in subResources)
 
123
        resources.append([resource _coreResource]);
 
124
    
 
125
    [dictionary release];
 
126
    return core(fragment);
 
127
}
 
128
 
 
129
void WebEditorClient::setInsertionPasteboard(const String&)
 
130
{
 
131
    // This is used only by Mail, no need to implement it now.
 
132
    notImplemented();
 
133
}
 
134
 
 
135
 
 
136
static void changeWordCase(WebPage* page, SEL selector)
 
137
{
 
138
    Frame* frame = page->corePage()->focusController()->focusedOrMainFrame();
 
139
    if (!frame->editor()->canEdit())
 
140
        return;
 
141
 
 
142
    frame->editor()->command("selectWord").execute();
 
143
 
 
144
    NSString *selectedString = frame->displayStringModifiedByEncoding(frame->editor()->selectedText());
 
145
    page->replaceSelectionWithText(frame, [selectedString performSelector:selector]);
 
146
}
 
147
 
 
148
#if USE(APPKIT)
 
149
void WebEditorClient::uppercaseWord()
 
150
{
 
151
    changeWordCase(m_page, @selector(uppercaseString));
 
152
}
 
153
 
 
154
void WebEditorClient::lowercaseWord()
 
155
{
 
156
    changeWordCase(m_page, @selector(lowercaseString));
 
157
}
 
158
 
 
159
void WebEditorClient::capitalizeWord()
 
160
{
 
161
    changeWordCase(m_page, @selector(capitalizedString));
 
162
}
 
163
#endif
 
164
 
 
165
#if USE(AUTOMATIC_TEXT_REPLACEMENT)
 
166
void WebEditorClient::showSubstitutionsPanel(bool)
 
167
{
 
168
    notImplemented();
 
169
}
 
170
 
 
171
bool WebEditorClient::substitutionsPanelIsShowing()
 
172
{
 
173
    bool isShowing;
 
174
    m_page->sendSync(Messages::WebPageProxy::SubstitutionsPanelIsShowing(), Messages::WebPageProxy::SubstitutionsPanelIsShowing::Reply(isShowing));
 
175
    return isShowing;
 
176
}
 
177
 
 
178
void WebEditorClient::toggleSmartInsertDelete()
 
179
{
 
180
    // This is handled in the UI process.
 
181
    ASSERT_NOT_REACHED();
 
182
}
 
183
 
 
184
bool WebEditorClient::isAutomaticQuoteSubstitutionEnabled()
 
185
{
 
186
    return WebProcess::shared().textCheckerState().isAutomaticQuoteSubstitutionEnabled;
 
187
}
 
188
 
 
189
void WebEditorClient::toggleAutomaticQuoteSubstitution()
 
190
{
 
191
    // This is handled in the UI process.
 
192
    ASSERT_NOT_REACHED();
 
193
}
 
194
 
 
195
bool WebEditorClient::isAutomaticLinkDetectionEnabled()
 
196
{
 
197
    return WebProcess::shared().textCheckerState().isAutomaticLinkDetectionEnabled;
 
198
}
 
199
 
 
200
void WebEditorClient::toggleAutomaticLinkDetection()
 
201
{
 
202
    // This is handled in the UI process.
 
203
    ASSERT_NOT_REACHED();
 
204
}
 
205
 
 
206
bool WebEditorClient::isAutomaticDashSubstitutionEnabled()
 
207
{
 
208
    return WebProcess::shared().textCheckerState().isAutomaticDashSubstitutionEnabled;
 
209
}
 
210
 
 
211
void WebEditorClient::toggleAutomaticDashSubstitution()
 
212
{
 
213
    // This is handled in the UI process.
 
214
    ASSERT_NOT_REACHED();
 
215
}
 
216
 
 
217
bool WebEditorClient::isAutomaticTextReplacementEnabled()
 
218
{
 
219
    return WebProcess::shared().textCheckerState().isAutomaticTextReplacementEnabled;
 
220
}
 
221
 
 
222
void WebEditorClient::toggleAutomaticTextReplacement()
 
223
{
 
224
    // This is handled in the UI process.
 
225
    ASSERT_NOT_REACHED();
 
226
}
 
227
 
 
228
bool WebEditorClient::isAutomaticSpellingCorrectionEnabled()
 
229
{
 
230
    return WebProcess::shared().textCheckerState().isAutomaticSpellingCorrectionEnabled;
 
231
}
 
232
 
 
233
void WebEditorClient::toggleAutomaticSpellingCorrection()
 
234
{
 
235
    notImplemented();
 
236
}
 
237
#endif // USE(AUTOMATIC_TEXT_REPLACEMENT)
 
238
 
 
239
void WebEditorClient::checkTextOfParagraph(const UChar* text, int length, WebCore::TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results)
 
240
{
 
241
    // FIXME: It would be nice if we wouldn't have to copy the text here.
 
242
    m_page->sendSync(Messages::WebPageProxy::CheckTextOfParagraph(String(text, length), checkingTypes), Messages::WebPageProxy::CheckTextOfParagraph::Reply(results));
 
243
}
 
244
 
 
245
} // namespace WebKit