2
* Copyright (C) 2006, 2010, 2011 Apple Computer, Inc. All rights reserved.
3
* Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
31
#import "WebEditorClient.h"
33
#import "WebCoreArgumentCoders.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>
50
using namespace WebCore;
52
@interface NSAttributedString (WebNSAttributedStringDetails)
53
- (DOMDocumentFragment*)_documentFromRange:(NSRange)range document:(DOMDocument*)document documentAttributes:(NSDictionary *)dict subresources:(NSArray **)subresources;
56
@interface WebResource (WebResourceInternal)
57
- (WebCore::ArchiveResource*)_coreResource;
62
void WebEditorClient::handleKeyboardEvent(KeyboardEvent* event)
64
if (m_page->handleEditingKeyboardEvent(event, false))
65
event->setDefaultHandled();
68
void WebEditorClient::handleInputMethodKeydown(KeyboardEvent* event)
70
if (m_page->handleEditingKeyboardEvent(event, true))
71
event->setDefaultHandled();
74
NSString *WebEditorClient::userVisibleString(NSURL *url)
76
return [url _web_userVisibleString];
79
NSURL *WebEditorClient::canonicalizeURL(NSURL *url)
81
return [url _webkit_canonicalize];
84
NSURL *WebEditorClient::canonicalizeURLString(NSString *URLString)
87
if ([URLString _webkit_looksLikeAbsoluteURL])
88
URL = [[NSURL _web_URLWithUserTypedString:URLString] _webkit_canonicalize];
92
static NSArray *createExcludedElementsForAttributedStringConversion()
94
NSArray *elements = [[NSArray alloc] initWithObjects:
95
// Omit style since we want style to be inline so the fragment can be easily inserted.
97
// Omit xml so the result is not XHTML.
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.
109
DocumentFragment* WebEditorClient::documentFragmentFromAttributedString(NSAttributedString *string, Vector<RefPtr<ArchiveResource> >& resources)
111
static NSArray *excludedElements = createExcludedElementsForAttributedStringConversion();
113
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: excludedElements,
114
NSExcludedElementsDocumentAttribute, nil, @"WebResourceHandler", nil];
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]);
125
[dictionary release];
126
return core(fragment);
129
void WebEditorClient::setInsertionPasteboard(const String&)
131
// This is used only by Mail, no need to implement it now.
136
static void changeWordCase(WebPage* page, SEL selector)
138
Frame* frame = page->corePage()->focusController()->focusedOrMainFrame();
139
if (!frame->editor()->canEdit())
142
frame->editor()->command("selectWord").execute();
144
NSString *selectedString = frame->displayStringModifiedByEncoding(frame->editor()->selectedText());
145
page->replaceSelectionWithText(frame, [selectedString performSelector:selector]);
149
void WebEditorClient::uppercaseWord()
151
changeWordCase(m_page, @selector(uppercaseString));
154
void WebEditorClient::lowercaseWord()
156
changeWordCase(m_page, @selector(lowercaseString));
159
void WebEditorClient::capitalizeWord()
161
changeWordCase(m_page, @selector(capitalizedString));
165
#if USE(AUTOMATIC_TEXT_REPLACEMENT)
166
void WebEditorClient::showSubstitutionsPanel(bool)
171
bool WebEditorClient::substitutionsPanelIsShowing()
174
m_page->sendSync(Messages::WebPageProxy::SubstitutionsPanelIsShowing(), Messages::WebPageProxy::SubstitutionsPanelIsShowing::Reply(isShowing));
178
void WebEditorClient::toggleSmartInsertDelete()
180
// This is handled in the UI process.
181
ASSERT_NOT_REACHED();
184
bool WebEditorClient::isAutomaticQuoteSubstitutionEnabled()
186
return WebProcess::shared().textCheckerState().isAutomaticQuoteSubstitutionEnabled;
189
void WebEditorClient::toggleAutomaticQuoteSubstitution()
191
// This is handled in the UI process.
192
ASSERT_NOT_REACHED();
195
bool WebEditorClient::isAutomaticLinkDetectionEnabled()
197
return WebProcess::shared().textCheckerState().isAutomaticLinkDetectionEnabled;
200
void WebEditorClient::toggleAutomaticLinkDetection()
202
// This is handled in the UI process.
203
ASSERT_NOT_REACHED();
206
bool WebEditorClient::isAutomaticDashSubstitutionEnabled()
208
return WebProcess::shared().textCheckerState().isAutomaticDashSubstitutionEnabled;
211
void WebEditorClient::toggleAutomaticDashSubstitution()
213
// This is handled in the UI process.
214
ASSERT_NOT_REACHED();
217
bool WebEditorClient::isAutomaticTextReplacementEnabled()
219
return WebProcess::shared().textCheckerState().isAutomaticTextReplacementEnabled;
222
void WebEditorClient::toggleAutomaticTextReplacement()
224
// This is handled in the UI process.
225
ASSERT_NOT_REACHED();
228
bool WebEditorClient::isAutomaticSpellingCorrectionEnabled()
230
return WebProcess::shared().textCheckerState().isAutomaticSpellingCorrectionEnabled;
233
void WebEditorClient::toggleAutomaticSpellingCorrection()
237
#endif // USE(AUTOMATIC_TEXT_REPLACEMENT)
239
void WebEditorClient::checkTextOfParagraph(const UChar* text, int length, WebCore::TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results)
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));
245
} // namespace WebKit