Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006, 2007, 2008, 2011 Apple 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 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "core/editing/Editor.h" |
| 29 | |
| 30 | #include "CSSPropertyNames.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 31 | #include "HTMLNames.h" |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 32 | #include "bindings/v8/ExceptionStatePlaceholder.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 33 | #include "core/accessibility/AXObjectCache.h" |
| 34 | #include "core/css/CSSComputedStyleDeclaration.h" |
| 35 | #include "core/css/StylePropertySet.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 36 | #include "core/dom/Clipboard.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 37 | #include "core/dom/DocumentFragment.h" |
| 38 | #include "core/dom/DocumentMarkerController.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 39 | #include "core/dom/NodeList.h" |
| 40 | #include "core/dom/NodeTraversal.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 41 | #include "core/dom/ParserContentPolicy.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 42 | #include "core/dom/Text.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 43 | #include "core/editing/ApplyStyleCommand.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 44 | #include "core/editing/DeleteSelectionCommand.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 45 | #include "core/editing/IndentOutdentCommand.h" |
Ben Murdoch | 3c9e4ae | 2013-08-12 14:20:44 +0100 | [diff] [blame] | 46 | #include "core/editing/InputMethodController.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 47 | #include "core/editing/InsertListCommand.h" |
| 48 | #include "core/editing/ModifySelectionListLevel.h" |
| 49 | #include "core/editing/RemoveFormatCommand.h" |
| 50 | #include "core/editing/RenderedPosition.h" |
| 51 | #include "core/editing/ReplaceSelectionCommand.h" |
| 52 | #include "core/editing/SimplifyMarkupCommand.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 53 | #include "core/editing/SpellChecker.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 54 | #include "core/editing/TextIterator.h" |
| 55 | #include "core/editing/TypingCommand.h" |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 56 | #include "core/editing/UndoStack.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 57 | #include "core/editing/VisibleUnits.h" |
| 58 | #include "core/editing/htmlediting.h" |
| 59 | #include "core/editing/markup.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 60 | #include "core/events/ClipboardEvent.h" |
| 61 | #include "core/events/KeyboardEvent.h" |
| 62 | #include "core/events/ScopedEventQueue.h" |
| 63 | #include "core/events/TextEvent.h" |
| 64 | #include "core/events/ThreadLocalEventNames.h" |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 65 | #include "core/fetch/ResourceFetcher.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 66 | #include "core/html/HTMLImageElement.h" |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 67 | #include "core/html/HTMLInputElement.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 68 | #include "core/html/HTMLTextAreaElement.h" |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 69 | #include "core/loader/EmptyClients.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 70 | #include "core/page/EditorClient.h" |
| 71 | #include "core/page/EventHandler.h" |
| 72 | #include "core/page/FocusController.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 73 | #include "core/frame/Frame.h" |
| 74 | #include "core/frame/FrameView.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 75 | #include "core/page/Page.h" |
| 76 | #include "core/page/Settings.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 77 | #include "core/platform/Pasteboard.h" |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 78 | #include "core/platform/chromium/ChromiumDataObject.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 79 | #include "core/rendering/HitTestResult.h" |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 80 | #include "platform/KillRing.h" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 81 | #include "wtf/unicode/CharacterNames.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 82 | |
| 83 | namespace WebCore { |
| 84 | |
| 85 | using namespace std; |
| 86 | using namespace HTMLNames; |
| 87 | using namespace WTF; |
| 88 | using namespace Unicode; |
| 89 | |
Ben Murdoch | 3c9e4ae | 2013-08-12 14:20:44 +0100 | [diff] [blame] | 90 | Editor::RevealSelectionScope::RevealSelectionScope(Editor* editor) |
| 91 | : m_editor(editor) |
| 92 | { |
| 93 | ++m_editor->m_preventRevealSelection; |
| 94 | } |
| 95 | |
| 96 | Editor::RevealSelectionScope::~RevealSelectionScope() |
| 97 | { |
| 98 | ASSERT(m_editor->m_preventRevealSelection); |
| 99 | --m_editor->m_preventRevealSelection; |
| 100 | if (!m_editor->m_preventRevealSelection) |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 101 | m_editor->m_frame.selection().revealSelection(ScrollAlignment::alignToEdgeIfNeeded, RevealExtent); |
Ben Murdoch | 3c9e4ae | 2013-08-12 14:20:44 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 104 | // When an event handler has moved the selection outside of a text control |
| 105 | // we should use the target control's selection for this editing operation. |
| 106 | VisibleSelection Editor::selectionForCommand(Event* event) |
| 107 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 108 | VisibleSelection selection = m_frame.selection().selection(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 109 | if (!event) |
| 110 | return selection; |
| 111 | // If the target is a text control, and the current selection is outside of its shadow tree, |
| 112 | // then use the saved selection for that text control. |
| 113 | HTMLTextFormControlElement* textFormControlOfSelectionStart = enclosingTextFormControl(selection.start()); |
| 114 | HTMLTextFormControlElement* textFromControlOfTarget = isHTMLTextFormControlElement(event->target()->toNode()) ? toHTMLTextFormControlElement(event->target()->toNode()) : 0; |
| 115 | if (textFromControlOfTarget && (selection.start().isNull() || textFromControlOfTarget != textFormControlOfSelectionStart)) { |
| 116 | if (RefPtr<Range> range = textFromControlOfTarget->selection()) |
| 117 | return VisibleSelection(range.get(), DOWNSTREAM, selection.isDirectional()); |
| 118 | } |
| 119 | return selection; |
| 120 | } |
| 121 | |
| 122 | // Function considers Mac editing behavior a fallback when Page or Settings is not available. |
| 123 | EditingBehavior Editor::behavior() const |
| 124 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 125 | if (!m_frame.settings()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 126 | return EditingBehavior(EditingMacBehavior); |
| 127 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 128 | return EditingBehavior(m_frame.settings()->editingBehaviorType()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 131 | static EditorClient& emptyEditorClient() |
| 132 | { |
| 133 | DEFINE_STATIC_LOCAL(EmptyEditorClient, client, ()); |
| 134 | return client; |
| 135 | } |
| 136 | |
| 137 | EditorClient& Editor::client() const |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 138 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 139 | if (Page* page = m_frame.page()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 140 | return page->editorClient(); |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 141 | return emptyEditorClient(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 144 | UndoStack* Editor::undoStack() const |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 145 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 146 | if (Page* page = m_frame.page()) |
| 147 | return &page->undoStack(); |
| 148 | return 0; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 151 | bool Editor::handleTextEvent(TextEvent* event) |
| 152 | { |
| 153 | // Default event handling for Drag and Drop will be handled by DragController |
| 154 | // so we leave the event for it. |
| 155 | if (event->isDrop()) |
| 156 | return false; |
| 157 | |
| 158 | if (event->isPaste()) { |
| 159 | if (event->pastingFragment()) |
| 160 | replaceSelectionWithFragment(event->pastingFragment(), false, event->shouldSmartReplace(), event->shouldMatchStyle()); |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 161 | else |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 162 | replaceSelectionWithText(event->data(), false, event->shouldSmartReplace()); |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | String data = event->data(); |
| 167 | if (data == "\n") { |
| 168 | if (event->isLineBreak()) |
| 169 | return insertLineBreak(); |
| 170 | return insertParagraphSeparator(); |
| 171 | } |
| 172 | |
| 173 | return insertTextWithoutSendingTextEvent(data, false, event); |
| 174 | } |
| 175 | |
| 176 | bool Editor::canEdit() const |
| 177 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 178 | return m_frame.selection().rootEditableElement(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | bool Editor::canEditRichly() const |
| 182 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 183 | return m_frame.selection().isContentRichlyEditable(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 186 | // WinIE uses onbeforecut and onbeforepaste to enables the cut and paste menu items. They |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 187 | // also send onbeforecopy, apparently for symmetry, but it doesn't affect the menu items. |
| 188 | // We need to use onbeforecopy as a real menu enabler because we allow elements that are not |
| 189 | // normally selectable to implement copy/paste (like divs, or a document body). |
| 190 | |
| 191 | bool Editor::canDHTMLCut() |
| 192 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 193 | return !m_frame.selection().isInPasswordField() && !dispatchCPPEvent(EventTypeNames::beforecut, ClipboardNumb); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | bool Editor::canDHTMLCopy() |
| 197 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 198 | return !m_frame.selection().isInPasswordField() && !dispatchCPPEvent(EventTypeNames::beforecopy, ClipboardNumb); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | bool Editor::canDHTMLPaste() |
| 202 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 203 | return !dispatchCPPEvent(EventTypeNames::beforepaste, ClipboardNumb); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | bool Editor::canCut() const |
| 207 | { |
| 208 | return canCopy() && canDelete(); |
| 209 | } |
| 210 | |
| 211 | static HTMLImageElement* imageElementFromImageDocument(Document* document) |
| 212 | { |
| 213 | if (!document) |
| 214 | return 0; |
| 215 | if (!document->isImageDocument()) |
| 216 | return 0; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 217 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 218 | HTMLElement* body = document->body(); |
| 219 | if (!body) |
| 220 | return 0; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 221 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 222 | Node* node = body->firstChild(); |
| 223 | if (!node) |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 224 | return 0; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 225 | if (!node->hasTagName(imgTag)) |
| 226 | return 0; |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 227 | return toHTMLImageElement(node); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | bool Editor::canCopy() const |
| 231 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 232 | if (imageElementFromImageDocument(m_frame.document())) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 233 | return true; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 234 | FrameSelection& selection = m_frame.selection(); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 235 | return selection.isRange() && !selection.isInPasswordField(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | bool Editor::canPaste() const |
| 239 | { |
| 240 | return canEdit(); |
| 241 | } |
| 242 | |
| 243 | bool Editor::canDelete() const |
| 244 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 245 | FrameSelection& selection = m_frame.selection(); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 246 | return selection.isRange() && selection.rootEditableElement(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | bool Editor::canDeleteRange(Range* range) const |
| 250 | { |
| 251 | Node* startContainer = range->startContainer(); |
| 252 | Node* endContainer = range->endContainer(); |
| 253 | if (!startContainer || !endContainer) |
| 254 | return false; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 255 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 256 | if (!startContainer->rendererIsEditable() || !endContainer->rendererIsEditable()) |
| 257 | return false; |
| 258 | |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 259 | if (range->collapsed(IGNORE_EXCEPTION)) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 260 | VisiblePosition start(range->startPosition(), DOWNSTREAM); |
| 261 | VisiblePosition previous = start.previous(); |
| 262 | // FIXME: We sometimes allow deletions at the start of editable roots, like when the caret is in an empty list item. |
| 263 | if (previous.isNull() || previous.deepEquivalent().deprecatedNode()->rootEditableElement() != startContainer->rootEditableElement()) |
| 264 | return false; |
| 265 | } |
| 266 | return true; |
| 267 | } |
| 268 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 269 | bool Editor::smartInsertDeleteEnabled() const |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 270 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 271 | if (Settings* settings = m_frame.settings()) |
| 272 | return settings->smartInsertDeleteEnabled(); |
| 273 | return false; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 274 | } |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 275 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 276 | bool Editor::canSmartCopyOrDelete() const |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 277 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 278 | return smartInsertDeleteEnabled() && m_frame.selection().granularity() == WordGranularity; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 281 | bool Editor::isSelectTrailingWhitespaceEnabled() const |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 282 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 283 | if (Settings* settings = m_frame.settings()) |
| 284 | return settings->selectTrailingWhitespaceEnabled(); |
| 285 | return false; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | bool Editor::deleteWithDirection(SelectionDirection direction, TextGranularity granularity, bool killRing, bool isTypingAction) |
| 289 | { |
| 290 | if (!canEdit()) |
| 291 | return false; |
| 292 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 293 | if (m_frame.selection().isRange()) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 294 | if (isTypingAction) { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 295 | ASSERT(m_frame.document()); |
| 296 | TypingCommand::deleteKeyPressed(*m_frame.document(), canSmartCopyOrDelete() ? TypingCommand::SmartDelete : 0, granularity); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 297 | revealSelectionAfterEditingOperation(); |
| 298 | } else { |
| 299 | if (killRing) |
| 300 | addToKillRing(selectedRange().get(), false); |
| 301 | deleteSelectionWithSmartDelete(canSmartCopyOrDelete()); |
| 302 | // Implicitly calls revealSelectionAfterEditingOperation(). |
| 303 | } |
| 304 | } else { |
| 305 | TypingCommand::Options options = 0; |
| 306 | if (canSmartCopyOrDelete()) |
| 307 | options |= TypingCommand::SmartDelete; |
| 308 | if (killRing) |
| 309 | options |= TypingCommand::KillRing; |
| 310 | switch (direction) { |
| 311 | case DirectionForward: |
| 312 | case DirectionRight: |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 313 | ASSERT(m_frame.document()); |
| 314 | TypingCommand::forwardDeleteKeyPressed(*m_frame.document(), options, granularity); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 315 | break; |
| 316 | case DirectionBackward: |
| 317 | case DirectionLeft: |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 318 | ASSERT(m_frame.document()); |
| 319 | TypingCommand::deleteKeyPressed(*m_frame.document(), options, granularity); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | revealSelectionAfterEditingOperation(); |
| 323 | } |
| 324 | |
| 325 | // FIXME: We should to move this down into deleteKeyPressed. |
| 326 | // clear the "start new kill ring sequence" setting, because it was set to true |
| 327 | // when the selection was updated by deleting the range |
| 328 | if (killRing) |
| 329 | setStartNewKillRingSequence(false); |
| 330 | |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | void Editor::deleteSelectionWithSmartDelete(bool smartDelete) |
| 335 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 336 | if (m_frame.selection().isNone()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 337 | return; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 338 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 339 | ASSERT(m_frame.document()); |
| 340 | DeleteSelectionCommand::create(*m_frame.document(), smartDelete)->apply(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void Editor::pasteAsPlainText(const String& pastingText, bool smartReplace) |
| 344 | { |
| 345 | Node* target = findEventTargetFromSelection(); |
| 346 | if (!target) |
| 347 | return; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 348 | target->dispatchEvent(TextEvent::createForPlainTextPaste(m_frame.domWindow(), pastingText, smartReplace), IGNORE_EXCEPTION); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void Editor::pasteAsFragment(PassRefPtr<DocumentFragment> pastingFragment, bool smartReplace, bool matchStyle) |
| 352 | { |
| 353 | Node* target = findEventTargetFromSelection(); |
| 354 | if (!target) |
| 355 | return; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 356 | target->dispatchEvent(TextEvent::createForFragmentPaste(m_frame.domWindow(), pastingFragment, smartReplace, matchStyle), IGNORE_EXCEPTION); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 359 | bool Editor::tryDHTMLCopy() |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 360 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 361 | if (m_frame.selection().isInPasswordField()) |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 362 | return false; |
| 363 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 364 | return !dispatchCPPEvent(EventTypeNames::copy, ClipboardWritable); |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | bool Editor::tryDHTMLCut() |
| 368 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 369 | if (m_frame.selection().isInPasswordField()) |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 370 | return false; |
| 371 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 372 | return !dispatchCPPEvent(EventTypeNames::cut, ClipboardWritable); |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | bool Editor::tryDHTMLPaste(PasteMode pasteMode) |
| 376 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 377 | return !dispatchCPPEvent(EventTypeNames::paste, ClipboardReadable, pasteMode); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void Editor::pasteAsPlainTextWithPasteboard(Pasteboard* pasteboard) |
| 381 | { |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 382 | String text = pasteboard->plainText(); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 383 | pasteAsPlainText(text, canSmartReplaceWithPasteboard(pasteboard)); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 386 | void Editor::pasteWithPasteboard(Pasteboard* pasteboard) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 387 | { |
| 388 | RefPtr<Range> range = selectedRange(); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 389 | RefPtr<DocumentFragment> fragment; |
| 390 | bool chosePlainText = false; |
| 391 | |
| 392 | if (pasteboard->isHTMLAvailable()) { |
| 393 | unsigned fragmentStart = 0; |
| 394 | unsigned fragmentEnd = 0; |
| 395 | KURL url; |
| 396 | String markup = pasteboard->readHTML(url, fragmentStart, fragmentEnd); |
| 397 | if (!markup.isEmpty()) { |
| 398 | ASSERT(m_frame.document()); |
| 399 | fragment = createFragmentFromMarkupWithContext(*m_frame.document(), markup, fragmentStart, fragmentEnd, url, DisallowScriptingAndPluginContent); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (!fragment) { |
| 404 | String text = pasteboard->plainText(); |
| 405 | if (!text.isEmpty()) { |
| 406 | chosePlainText = true; |
| 407 | fragment = createFragmentFromText(range.get(), text); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (fragment) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 412 | pasteAsFragment(fragment, canSmartReplaceWithPasteboard(pasteboard), chosePlainText); |
| 413 | } |
| 414 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 415 | void Editor::writeSelectionToPasteboard(Pasteboard* pasteboard, Range* selectedRange, const String& plainText) |
| 416 | { |
| 417 | String html = createMarkup(selectedRange, 0, AnnotateForInterchange, false, ResolveNonLocalURLs); |
| 418 | KURL url = selectedRange->startContainer()->document().url(); |
| 419 | pasteboard->writeHTML(html, url, plainText, canSmartCopyOrDelete()); |
| 420 | } |
| 421 | |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 422 | // Returns whether caller should continue with "the default processing", which is the same as |
| 423 | // the event handler NOT setting the return value to false |
| 424 | bool Editor::dispatchCPPEvent(const AtomicString &eventType, ClipboardAccessPolicy policy, PasteMode pasteMode) |
| 425 | { |
| 426 | Node* target = findEventTargetFromSelection(); |
| 427 | if (!target) |
| 428 | return true; |
| 429 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 430 | RefPtr<Clipboard> clipboard = Clipboard::create( |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 431 | Clipboard::CopyAndPaste, |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 432 | policy, |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 433 | policy == ClipboardWritable |
| 434 | ? ChromiumDataObject::create() |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 435 | : ChromiumDataObject::createFromPasteboard(pasteMode)); |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 436 | |
| 437 | RefPtr<Event> evt = ClipboardEvent::create(eventType, true, true, clipboard); |
| 438 | target->dispatchEvent(evt, IGNORE_EXCEPTION); |
| 439 | bool noDefaultProcessing = evt->defaultPrevented(); |
| 440 | if (noDefaultProcessing && policy == ClipboardWritable) { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 441 | RefPtr<ChromiumDataObject> dataObject = clipboard->dataObject(); |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 442 | Pasteboard::generalPasteboard()->writeDataObject(dataObject.release()); |
| 443 | } |
| 444 | |
| 445 | // invalidate clipboard here for security |
| 446 | clipboard->setAccessPolicy(ClipboardNumb); |
| 447 | |
| 448 | return !noDefaultProcessing; |
| 449 | } |
| 450 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 451 | bool Editor::canSmartReplaceWithPasteboard(Pasteboard* pasteboard) |
| 452 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 453 | return smartInsertDeleteEnabled() && pasteboard->canSmartReplace(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 456 | void Editor::replaceSelectionWithFragment(PassRefPtr<DocumentFragment> fragment, bool selectReplacement, bool smartReplace, bool matchStyle) |
| 457 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 458 | if (m_frame.selection().isNone() || !m_frame.selection().isContentEditable() || !fragment) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 459 | return; |
| 460 | |
| 461 | ReplaceSelectionCommand::CommandOptions options = ReplaceSelectionCommand::PreventNesting | ReplaceSelectionCommand::SanitizeFragment; |
| 462 | if (selectReplacement) |
| 463 | options |= ReplaceSelectionCommand::SelectReplacement; |
| 464 | if (smartReplace) |
| 465 | options |= ReplaceSelectionCommand::SmartReplace; |
| 466 | if (matchStyle) |
| 467 | options |= ReplaceSelectionCommand::MatchStyle; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 468 | ASSERT(m_frame.document()); |
| 469 | ReplaceSelectionCommand::create(*m_frame.document(), fragment, options, EditActionPaste)->apply(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 470 | revealSelectionAfterEditingOperation(); |
| 471 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 472 | if (m_frame.selection().isInPasswordField() || !spellChecker().isContinuousSpellCheckingEnabled()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 473 | return; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 474 | spellChecker().chunkAndMarkAllMisspellingsAndBadGrammar(m_frame.selection().rootEditableElement()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void Editor::replaceSelectionWithText(const String& text, bool selectReplacement, bool smartReplace) |
| 478 | { |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 479 | replaceSelectionWithFragment(createFragmentFromText(selectedRange().get(), text), selectReplacement, smartReplace, true); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | PassRefPtr<Range> Editor::selectedRange() |
| 483 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 484 | return m_frame.selection().toNormalizedRange(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | bool Editor::shouldDeleteRange(Range* range) const |
| 488 | { |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 489 | if (!range || range->collapsed(IGNORE_EXCEPTION)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 490 | return false; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 491 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 492 | return canDeleteRange(range); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void Editor::notifyComponentsOnChangedSelection(const VisibleSelection& oldSelection, FrameSelection::SetSelectionOptions options) |
| 496 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 497 | client().respondToChangedSelection(m_frame.selection().selectionType()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 498 | setStartNewKillRingSequence(true); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | void Editor::respondToChangedContents(const VisibleSelection& endingSelection) |
| 502 | { |
| 503 | if (AXObjectCache::accessibilityEnabled()) { |
| 504 | Node* node = endingSelection.start().deprecatedNode(); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 505 | if (AXObjectCache* cache = m_frame.document()->existingAXObjectCache()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 506 | cache->postNotification(node, AXObjectCache::AXValueChanged, false); |
| 507 | } |
| 508 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 509 | spellChecker().updateMarkersForWordsAffectedByEditing(true); |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 510 | client().respondToChangedContents(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 513 | TriState Editor::selectionUnorderedListState() const |
| 514 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 515 | if (m_frame.selection().isCaret()) { |
| 516 | if (enclosingNodeWithTag(m_frame.selection().selection().start(), ulTag)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 517 | return TrueTriState; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 518 | } else if (m_frame.selection().isRange()) { |
| 519 | Node* startNode = enclosingNodeWithTag(m_frame.selection().selection().start(), ulTag); |
| 520 | Node* endNode = enclosingNodeWithTag(m_frame.selection().selection().end(), ulTag); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 521 | if (startNode && endNode && startNode == endNode) |
| 522 | return TrueTriState; |
| 523 | } |
| 524 | |
| 525 | return FalseTriState; |
| 526 | } |
| 527 | |
| 528 | TriState Editor::selectionOrderedListState() const |
| 529 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 530 | if (m_frame.selection().isCaret()) { |
| 531 | if (enclosingNodeWithTag(m_frame.selection().selection().start(), olTag)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 532 | return TrueTriState; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 533 | } else if (m_frame.selection().isRange()) { |
| 534 | Node* startNode = enclosingNodeWithTag(m_frame.selection().selection().start(), olTag); |
| 535 | Node* endNode = enclosingNodeWithTag(m_frame.selection().selection().end(), olTag); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 536 | if (startNode && endNode && startNode == endNode) |
| 537 | return TrueTriState; |
| 538 | } |
| 539 | |
| 540 | return FalseTriState; |
| 541 | } |
| 542 | |
| 543 | PassRefPtr<Node> Editor::insertOrderedList() |
| 544 | { |
| 545 | if (!canEditRichly()) |
| 546 | return 0; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 547 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 548 | ASSERT(m_frame.document()); |
| 549 | RefPtr<Node> newList = InsertListCommand::insertList(*m_frame.document(), InsertListCommand::OrderedList); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 550 | revealSelectionAfterEditingOperation(); |
| 551 | return newList; |
| 552 | } |
| 553 | |
| 554 | PassRefPtr<Node> Editor::insertUnorderedList() |
| 555 | { |
| 556 | if (!canEditRichly()) |
| 557 | return 0; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 558 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 559 | ASSERT(m_frame.document()); |
| 560 | RefPtr<Node> newList = InsertListCommand::insertList(*m_frame.document(), InsertListCommand::UnorderedList); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 561 | revealSelectionAfterEditingOperation(); |
| 562 | return newList; |
| 563 | } |
| 564 | |
| 565 | bool Editor::canIncreaseSelectionListLevel() |
| 566 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 567 | ASSERT(m_frame.document()); |
| 568 | return canEditRichly() && IncreaseSelectionListLevelCommand::canIncreaseSelectionListLevel(*m_frame.document()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | bool Editor::canDecreaseSelectionListLevel() |
| 572 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 573 | ASSERT(m_frame.document()); |
| 574 | return canEditRichly() && DecreaseSelectionListLevelCommand::canDecreaseSelectionListLevel(*m_frame.document()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | PassRefPtr<Node> Editor::increaseSelectionListLevel() |
| 578 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 579 | if (!canEditRichly() || m_frame.selection().isNone()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 580 | return 0; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 581 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 582 | ASSERT(m_frame.document()); |
| 583 | RefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevel(*m_frame.document()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 584 | revealSelectionAfterEditingOperation(); |
| 585 | return newList; |
| 586 | } |
| 587 | |
| 588 | PassRefPtr<Node> Editor::increaseSelectionListLevelOrdered() |
| 589 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 590 | if (!canEditRichly() || m_frame.selection().isNone()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 591 | return 0; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 592 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 593 | ASSERT(m_frame.document()); |
| 594 | RefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevelOrdered(*m_frame.document()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 595 | revealSelectionAfterEditingOperation(); |
| 596 | return newList.release(); |
| 597 | } |
| 598 | |
| 599 | PassRefPtr<Node> Editor::increaseSelectionListLevelUnordered() |
| 600 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 601 | if (!canEditRichly() || m_frame.selection().isNone()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 602 | return 0; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 603 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 604 | ASSERT(m_frame.document()); |
| 605 | RefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevelUnordered(*m_frame.document()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 606 | revealSelectionAfterEditingOperation(); |
| 607 | return newList.release(); |
| 608 | } |
| 609 | |
| 610 | void Editor::decreaseSelectionListLevel() |
| 611 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 612 | if (!canEditRichly() || m_frame.selection().isNone()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 613 | return; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 614 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 615 | ASSERT(m_frame.document()); |
| 616 | DecreaseSelectionListLevelCommand::decreaseSelectionListLevel(*m_frame.document()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 617 | revealSelectionAfterEditingOperation(); |
| 618 | } |
| 619 | |
| 620 | void Editor::removeFormattingAndStyle() |
| 621 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 622 | ASSERT(m_frame.document()); |
| 623 | RemoveFormatCommand::create(*m_frame.document())->apply(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 624 | } |
| 625 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 626 | void Editor::clearLastEditCommand() |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 627 | { |
| 628 | m_lastEditCommand.clear(); |
| 629 | } |
| 630 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 631 | Node* Editor::findEventTargetFrom(const VisibleSelection& selection) const |
| 632 | { |
| 633 | Node* target = selection.start().element(); |
| 634 | if (!target) |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 635 | target = m_frame.document()->body(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 636 | |
| 637 | return target; |
| 638 | } |
| 639 | |
| 640 | Node* Editor::findEventTargetFromSelection() const |
| 641 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 642 | return findEventTargetFrom(m_frame.selection().selection()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | void Editor::applyStyle(StylePropertySet* style, EditAction editingAction) |
| 646 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 647 | switch (m_frame.selection().selectionType()) { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 648 | case NoSelection: |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 649 | // do nothing |
| 650 | break; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 651 | case CaretSelection: |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 652 | computeAndSetTypingStyle(style, editingAction); |
| 653 | break; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 654 | case RangeSelection: |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 655 | if (style) { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 656 | ASSERT(m_frame.document()); |
| 657 | ApplyStyleCommand::create(*m_frame.document(), EditingStyle::create(style).get(), editingAction)->apply(); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 658 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | } |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 662 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 663 | void Editor::applyParagraphStyle(StylePropertySet* style, EditAction editingAction) |
| 664 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 665 | if (m_frame.selection().isNone() || !style) |
| 666 | return; |
| 667 | ASSERT(m_frame.document()); |
| 668 | ApplyStyleCommand::create(*m_frame.document(), EditingStyle::create(style).get(), editingAction, ApplyStyleCommand::ForceBlockProperties)->apply(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | void Editor::applyStyleToSelection(StylePropertySet* style, EditAction editingAction) |
| 672 | { |
| 673 | if (!style || style->isEmpty() || !canEditRichly()) |
| 674 | return; |
| 675 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 676 | applyStyle(style, editingAction); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void Editor::applyParagraphStyleToSelection(StylePropertySet* style, EditAction editingAction) |
| 680 | { |
| 681 | if (!style || style->isEmpty() || !canEditRichly()) |
| 682 | return; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 683 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 684 | applyParagraphStyle(style, editingAction); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | bool Editor::selectionStartHasStyle(CSSPropertyID propertyID, const String& value) const |
| 688 | { |
| 689 | return EditingStyle::create(propertyID, value)->triStateOfStyle( |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 690 | EditingStyle::styleAtSelectionStart(m_frame.selection().selection(), propertyID == CSSPropertyBackgroundColor).get()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | TriState Editor::selectionHasStyle(CSSPropertyID propertyID, const String& value) const |
| 694 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 695 | return EditingStyle::create(propertyID, value)->triStateOfStyle(m_frame.selection().selection()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | String Editor::selectionStartCSSPropertyValue(CSSPropertyID propertyID) |
| 699 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 700 | RefPtr<EditingStyle> selectionStyle = EditingStyle::styleAtSelectionStart(m_frame.selection().selection(), |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 701 | propertyID == CSSPropertyBackgroundColor); |
| 702 | if (!selectionStyle || !selectionStyle->style()) |
| 703 | return String(); |
| 704 | |
| 705 | if (propertyID == CSSPropertyFontSize) |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 706 | return String::number(selectionStyle->legacyFontSize(m_frame.document())); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 707 | return selectionStyle->style()->getPropertyValue(propertyID); |
| 708 | } |
| 709 | |
| 710 | void Editor::indent() |
| 711 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 712 | ASSERT(m_frame.document()); |
| 713 | IndentOutdentCommand::create(*m_frame.document(), IndentOutdentCommand::Indent)->apply(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | void Editor::outdent() |
| 717 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 718 | ASSERT(m_frame.document()); |
| 719 | IndentOutdentCommand::create(*m_frame.document(), IndentOutdentCommand::Outdent)->apply(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 720 | } |
| 721 | |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 722 | static void dispatchEditableContentChangedEvents(PassRefPtr<Element> startRoot, PassRefPtr<Element> endRoot) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 723 | { |
| 724 | if (startRoot) |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 725 | startRoot->dispatchEvent(Event::create(EventTypeNames::webkitEditableContentChanged), IGNORE_EXCEPTION); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 726 | if (endRoot && endRoot != startRoot) |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 727 | endRoot->dispatchEvent(Event::create(EventTypeNames::webkitEditableContentChanged), IGNORE_EXCEPTION); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | void Editor::appliedEditing(PassRefPtr<CompositeEditCommand> cmd) |
| 731 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 732 | EventQueueScope scope; |
| 733 | m_frame.document()->updateLayout(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 734 | |
| 735 | EditCommandComposition* composition = cmd->composition(); |
| 736 | ASSERT(composition); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 737 | dispatchEditableContentChangedEvents(composition->startingRootEditableElement(), composition->endingRootEditableElement()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 738 | VisibleSelection newSelection(cmd->endingSelection()); |
| 739 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 740 | // Don't clear the typing style with this selection change. We do those things elsewhere if necessary. |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 741 | changeSelectionAfterCommand(newSelection, 0); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 742 | |
| 743 | if (!cmd->preservesTypingStyle()) |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 744 | m_frame.selection().clearTypingStyle(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 745 | |
| 746 | // Command will be equal to last edit command only in the case of typing |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 747 | if (m_lastEditCommand.get() == cmd) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 748 | ASSERT(cmd->isTypingCommand()); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 749 | } else { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 750 | // Only register a new undo command if the command passed in is |
| 751 | // different from the last command |
| 752 | m_lastEditCommand = cmd; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 753 | if (UndoStack* undoStack = this->undoStack()) |
| 754 | undoStack->registerUndoStep(m_lastEditCommand->ensureComposition()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | respondToChangedContents(newSelection); |
| 758 | } |
| 759 | |
| 760 | void Editor::unappliedEditing(PassRefPtr<EditCommandComposition> cmd) |
| 761 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 762 | EventQueueScope scope; |
| 763 | m_frame.document()->updateLayout(); |
| 764 | |
| 765 | dispatchEditableContentChangedEvents(cmd->startingRootEditableElement(), cmd->endingRootEditableElement()); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 766 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 767 | VisibleSelection newSelection(cmd->startingSelection()); |
| 768 | changeSelectionAfterCommand(newSelection, FrameSelection::CloseTyping | FrameSelection::ClearTypingStyle); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 769 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 770 | m_lastEditCommand = 0; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 771 | if (UndoStack* undoStack = this->undoStack()) |
| 772 | undoStack->registerRedoStep(cmd); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 773 | respondToChangedContents(newSelection); |
| 774 | } |
| 775 | |
| 776 | void Editor::reappliedEditing(PassRefPtr<EditCommandComposition> cmd) |
| 777 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 778 | EventQueueScope scope; |
| 779 | m_frame.document()->updateLayout(); |
| 780 | |
| 781 | dispatchEditableContentChangedEvents(cmd->startingRootEditableElement(), cmd->endingRootEditableElement()); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 782 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 783 | VisibleSelection newSelection(cmd->endingSelection()); |
| 784 | changeSelectionAfterCommand(newSelection, FrameSelection::CloseTyping | FrameSelection::ClearTypingStyle); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 785 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 786 | m_lastEditCommand = 0; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 787 | if (UndoStack* undoStack = this->undoStack()) |
| 788 | undoStack->registerUndoStep(cmd); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 789 | respondToChangedContents(newSelection); |
| 790 | } |
| 791 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 792 | PassOwnPtr<Editor> Editor::create(Frame& frame) |
| 793 | { |
| 794 | return adoptPtr(new Editor(frame)); |
| 795 | } |
| 796 | |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame] | 797 | Editor::Editor(Frame& frame) |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 798 | : m_frame(frame) |
Ben Murdoch | 3c9e4ae | 2013-08-12 14:20:44 +0100 | [diff] [blame] | 799 | , m_preventRevealSelection(0) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 800 | , m_shouldStartNewKillRingSequence(false) |
| 801 | // This is off by default, since most editors want this behavior (this matches IE but not FF). |
| 802 | , m_shouldStyleWithCSS(false) |
| 803 | , m_killRing(adoptPtr(new KillRing)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 804 | , m_areMarkedTextMatchesHighlighted(false) |
| 805 | , m_defaultParagraphSeparator(EditorParagraphSeparatorIsDiv) |
| 806 | , m_overwriteModeEnabled(false) |
| 807 | { |
| 808 | } |
| 809 | |
| 810 | Editor::~Editor() |
| 811 | { |
| 812 | } |
| 813 | |
| 814 | void Editor::clear() |
| 815 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 816 | m_frame.inputMethodController().clear(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 817 | m_shouldStyleWithCSS = false; |
| 818 | m_defaultParagraphSeparator = EditorParagraphSeparatorIsDiv; |
| 819 | } |
| 820 | |
| 821 | bool Editor::insertText(const String& text, Event* triggeringEvent) |
| 822 | { |
Torne (Richard Coles) | f79f16f | 2013-10-31 11:16:44 +0000 | [diff] [blame] | 823 | return m_frame.eventHandler().handleTextInputEvent(text, triggeringEvent); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 824 | } |
| 825 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 826 | bool Editor::insertTextWithoutSendingTextEvent(const String& text, bool selectInsertedText, TextEvent* triggeringEvent) |
| 827 | { |
| 828 | if (text.isEmpty()) |
| 829 | return false; |
| 830 | |
| 831 | VisibleSelection selection = selectionForCommand(triggeringEvent); |
| 832 | if (!selection.isContentEditable()) |
| 833 | return false; |
| 834 | RefPtr<Range> range = selection.toNormalizedRange(); |
| 835 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 836 | spellChecker().updateMarkersForWordsAffectedByEditing(isSpaceOrNewline(text[0])); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 837 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 838 | // Get the selection to use for the event that triggered this insertText. |
| 839 | // If the event handler changed the selection, we may want to use a different selection |
| 840 | // that is contained in the event target. |
| 841 | selection = selectionForCommand(triggeringEvent); |
| 842 | if (selection.isContentEditable()) { |
| 843 | if (Node* selectionStart = selection.start().deprecatedNode()) { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 844 | RefPtr<Document> document(selectionStart->document()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 845 | |
| 846 | // Insert the text |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 847 | TypingCommand::Options options = 0; |
| 848 | if (selectInsertedText) |
| 849 | options |= TypingCommand::SelectInsertedText; |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 850 | TypingCommand::insertText(*document.get(), text, selection, options, triggeringEvent && triggeringEvent->isComposition() ? TypingCommand::TextCompositionConfirm : TypingCommand::TextCompositionNone); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 851 | |
| 852 | // Reveal the current selection |
Ben Murdoch | 3c9e4ae | 2013-08-12 14:20:44 +0100 | [diff] [blame] | 853 | if (Frame* editedFrame = document->frame()) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 854 | if (Page* page = editedFrame->page()) |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 855 | page->focusController().focusedOrMainFrame()->selection().revealSelection(ScrollAlignment::alignCenterIfNeeded); |
Ben Murdoch | 3c9e4ae | 2013-08-12 14:20:44 +0100 | [diff] [blame] | 856 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
| 860 | return true; |
| 861 | } |
| 862 | |
| 863 | bool Editor::insertLineBreak() |
| 864 | { |
| 865 | if (!canEdit()) |
| 866 | return false; |
| 867 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 868 | VisiblePosition caret = m_frame.selection().selection().visibleStart(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 869 | bool alignToEdge = isEndOfEditableOrNonEditableContent(caret); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 870 | ASSERT(m_frame.document()); |
| 871 | TypingCommand::insertLineBreak(*m_frame.document(), 0); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 872 | revealSelectionAfterEditingOperation(alignToEdge ? ScrollAlignment::alignToEdgeIfNeeded : ScrollAlignment::alignCenterIfNeeded); |
| 873 | |
| 874 | return true; |
| 875 | } |
| 876 | |
| 877 | bool Editor::insertParagraphSeparator() |
| 878 | { |
| 879 | if (!canEdit()) |
| 880 | return false; |
| 881 | |
| 882 | if (!canEditRichly()) |
| 883 | return insertLineBreak(); |
| 884 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 885 | VisiblePosition caret = m_frame.selection().selection().visibleStart(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 886 | bool alignToEdge = isEndOfEditableOrNonEditableContent(caret); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 887 | ASSERT(m_frame.document()); |
| 888 | TypingCommand::insertParagraphSeparator(*m_frame.document(), 0); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 889 | revealSelectionAfterEditingOperation(alignToEdge ? ScrollAlignment::alignToEdgeIfNeeded : ScrollAlignment::alignCenterIfNeeded); |
| 890 | |
| 891 | return true; |
| 892 | } |
| 893 | |
| 894 | void Editor::cut() |
| 895 | { |
| 896 | if (tryDHTMLCut()) |
| 897 | return; // DHTML did the whole operation |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 898 | if (!canCut()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 899 | return; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 900 | RefPtr<Range> selection = selectedRange(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 901 | if (shouldDeleteRange(selection.get())) { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 902 | spellChecker().updateMarkersForWordsAffectedByEditing(true); |
| 903 | String plainText = m_frame.selectedTextForClipboard(); |
| 904 | if (enclosingTextFormControl(m_frame.selection().start())) { |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 905 | Pasteboard::generalPasteboard()->writePlainText(plainText, |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 906 | canSmartCopyOrDelete() ? Pasteboard::CanSmartReplace : Pasteboard::CannotSmartReplace); |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 907 | } else { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 908 | writeSelectionToPasteboard(Pasteboard::generalPasteboard(), selection.get(), plainText); |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 909 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 910 | deleteSelectionWithSmartDelete(canSmartCopyOrDelete()); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | void Editor::copy() |
| 915 | { |
| 916 | if (tryDHTMLCopy()) |
| 917 | return; // DHTML did the whole operation |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 918 | if (!canCopy()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 919 | return; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 920 | if (enclosingTextFormControl(m_frame.selection().start())) { |
| 921 | Pasteboard::generalPasteboard()->writePlainText(m_frame.selectedTextForClipboard(), |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 922 | canSmartCopyOrDelete() ? Pasteboard::CanSmartReplace : Pasteboard::CannotSmartReplace); |
| 923 | } else { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 924 | Document* document = m_frame.document(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 925 | if (HTMLImageElement* imageElement = imageElementFromImageDocument(document)) |
| 926 | Pasteboard::generalPasteboard()->writeImage(imageElement, document->url(), document->title()); |
| 927 | else |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 928 | writeSelectionToPasteboard(Pasteboard::generalPasteboard(), selectedRange().get(), m_frame.selectedTextForClipboard()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 929 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | void Editor::paste() |
| 933 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 934 | ASSERT(m_frame.document()); |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 935 | if (tryDHTMLPaste(AllMimeTypes)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 936 | return; // DHTML did the whole operation |
| 937 | if (!canPaste()) |
| 938 | return; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 939 | spellChecker().updateMarkersForWordsAffectedByEditing(false); |
| 940 | ResourceFetcher* loader = m_frame.document()->fetcher(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 941 | ResourceCacheValidationSuppressor validationSuppressor(loader); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 942 | if (m_frame.selection().isContentRichlyEditable()) |
| 943 | pasteWithPasteboard(Pasteboard::generalPasteboard()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 944 | else |
| 945 | pasteAsPlainTextWithPasteboard(Pasteboard::generalPasteboard()); |
| 946 | } |
| 947 | |
| 948 | void Editor::pasteAsPlainText() |
| 949 | { |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 950 | if (tryDHTMLPaste(PlainTextOnly)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 951 | return; |
| 952 | if (!canPaste()) |
| 953 | return; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 954 | spellChecker().updateMarkersForWordsAffectedByEditing(false); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 955 | pasteAsPlainTextWithPasteboard(Pasteboard::generalPasteboard()); |
| 956 | } |
| 957 | |
| 958 | void Editor::performDelete() |
| 959 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 960 | if (!canDelete()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 961 | return; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 962 | addToKillRing(selectedRange().get(), false); |
| 963 | deleteSelectionWithSmartDelete(canSmartCopyOrDelete()); |
| 964 | |
| 965 | // clear the "start new kill ring sequence" setting, because it was set to true |
| 966 | // when the selection was updated by deleting the range |
| 967 | setStartNewKillRingSequence(false); |
| 968 | } |
| 969 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 970 | void Editor::copyImage(const HitTestResult& result) |
| 971 | { |
| 972 | KURL url = result.absoluteLinkURL(); |
| 973 | if (url.isEmpty()) |
| 974 | url = result.absoluteImageURL(); |
| 975 | |
| 976 | Pasteboard::generalPasteboard()->writeImage(result.innerNonSharedNode(), url, result.altDisplayString()); |
| 977 | } |
| 978 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 979 | void Editor::clearUndoRedoOperations() |
| 980 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 981 | if (UndoStack* undoStack = this->undoStack()) |
| 982 | undoStack->clearUndoRedoOperations(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | bool Editor::canUndo() |
| 986 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 987 | if (UndoStack* undoStack = this->undoStack()) |
| 988 | return undoStack->canUndo(); |
| 989 | return false; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | void Editor::undo() |
| 993 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 994 | if (UndoStack* undoStack = this->undoStack()) |
| 995 | undoStack->undo(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | bool Editor::canRedo() |
| 999 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 1000 | if (UndoStack* undoStack = this->undoStack()) |
| 1001 | return undoStack->canRedo(); |
| 1002 | return false; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | void Editor::redo() |
| 1006 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 1007 | if (UndoStack* undoStack = this->undoStack()) |
| 1008 | undoStack->redo(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1009 | } |
| 1010 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1011 | void Editor::setBaseWritingDirection(WritingDirection direction) |
| 1012 | { |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame] | 1013 | Node* focusedElement = frame().document()->focusedElement(); |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 1014 | if (focusedElement && isHTMLTextFormControlElement(focusedElement)) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1015 | if (direction == NaturalWritingDirection) |
| 1016 | return; |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 1017 | toHTMLElement(focusedElement)->setAttribute(dirAttr, direction == LeftToRightWritingDirection ? "ltr" : "rtl"); |
| 1018 | focusedElement->dispatchInputEvent(); |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame] | 1019 | frame().document()->updateStyleIfNeeded(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1020 | return; |
| 1021 | } |
| 1022 | |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 1023 | RefPtr<MutableStylePropertySet> style = MutableStylePropertySet::create(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1024 | style->setProperty(CSSPropertyDirection, direction == LeftToRightWritingDirection ? "ltr" : direction == RightToLeftWritingDirection ? "rtl" : "inherit", false); |
| 1025 | applyParagraphStyleToSelection(style.get(), EditActionSetWritingDirection); |
| 1026 | } |
| 1027 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1028 | void Editor::revealSelectionAfterEditingOperation(const ScrollAlignment& alignment, RevealExtentOption revealExtentOption) |
| 1029 | { |
Ben Murdoch | 3c9e4ae | 2013-08-12 14:20:44 +0100 | [diff] [blame] | 1030 | if (m_preventRevealSelection) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1031 | return; |
| 1032 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1033 | m_frame.selection().revealSelection(alignment, revealExtentOption); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1036 | void Editor::transpose() |
| 1037 | { |
| 1038 | if (!canEdit()) |
| 1039 | return; |
| 1040 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1041 | VisibleSelection selection = m_frame.selection().selection(); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 1042 | if (!selection.isCaret()) |
| 1043 | return; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1044 | |
| 1045 | // Make a selection that goes back one character and forward two characters. |
| 1046 | VisiblePosition caret = selection.visibleStart(); |
| 1047 | VisiblePosition next = isEndOfParagraph(caret) ? caret : caret.next(); |
| 1048 | VisiblePosition previous = next.previous(); |
| 1049 | if (next == previous) |
| 1050 | return; |
| 1051 | previous = previous.previous(); |
| 1052 | if (!inSameParagraph(next, previous)) |
| 1053 | return; |
| 1054 | RefPtr<Range> range = makeRange(previous, next); |
| 1055 | if (!range) |
| 1056 | return; |
| 1057 | VisibleSelection newSelection(range.get(), DOWNSTREAM); |
| 1058 | |
| 1059 | // Transpose the two characters. |
| 1060 | String text = plainText(range.get()); |
| 1061 | if (text.length() != 2) |
| 1062 | return; |
| 1063 | String transposed = text.right(1) + text.left(1); |
| 1064 | |
| 1065 | // Select the two characters. |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1066 | if (newSelection != m_frame.selection().selection()) |
| 1067 | m_frame.selection().setSelection(newSelection); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1068 | |
| 1069 | // Insert the transposed characters. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1070 | replaceSelectionWithText(transposed, false, false); |
| 1071 | } |
| 1072 | |
| 1073 | void Editor::addToKillRing(Range* range, bool prepend) |
| 1074 | { |
| 1075 | if (m_shouldStartNewKillRingSequence) |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 1076 | killRing().startNewSequence(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1077 | |
| 1078 | String text = plainText(range); |
| 1079 | if (prepend) |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 1080 | killRing().prepend(text); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1081 | else |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 1082 | killRing().append(text); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1083 | m_shouldStartNewKillRingSequence = false; |
| 1084 | } |
| 1085 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1086 | void Editor::changeSelectionAfterCommand(const VisibleSelection& newSelection, FrameSelection::SetSelectionOptions options) |
| 1087 | { |
| 1088 | // If the new selection is orphaned, then don't update the selection. |
| 1089 | if (newSelection.start().isOrphan() || newSelection.end().isOrphan()) |
| 1090 | return; |
| 1091 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1092 | // See <rdar://problem/5729315> Some shouldChangeSelectedDOMRange contain Ranges for selections that are no longer valid |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1093 | bool selectionDidNotChangeDOMPosition = newSelection == m_frame.selection().selection(); |
| 1094 | m_frame.selection().setSelection(newSelection, options); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1095 | |
| 1096 | // Some editing operations change the selection visually without affecting its position within the DOM. |
| 1097 | // For example when you press return in the following (the caret is marked by ^): |
| 1098 | // <div contentEditable="true"><div>^Hello</div></div> |
| 1099 | // WebCore inserts <div><br></div> *before* the current block, which correctly moves the paragraph down but which doesn't |
| 1100 | // change the caret's DOM position (["hello", 0]). In these situations the above FrameSelection::setSelection call |
| 1101 | // does not call EditorClient::respondToChangedSelection(), which, on the Mac, sends selection change notifications and |
| 1102 | // starts a new kill ring sequence, but we want to do these things (matches AppKit). |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 1103 | if (selectionDidNotChangeDOMPosition) |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 1104 | client().respondToChangedSelection(m_frame.selection().selectionType()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1107 | IntRect Editor::firstRectForRange(Range* range) const |
| 1108 | { |
| 1109 | LayoutUnit extraWidthToEndOfLine = 0; |
| 1110 | ASSERT(range->startContainer()); |
| 1111 | ASSERT(range->endContainer()); |
| 1112 | |
| 1113 | IntRect startCaretRect = RenderedPosition(VisiblePosition(range->startPosition()).deepEquivalent(), DOWNSTREAM).absoluteRect(&extraWidthToEndOfLine); |
| 1114 | if (startCaretRect == LayoutRect()) |
| 1115 | return IntRect(); |
| 1116 | |
| 1117 | IntRect endCaretRect = RenderedPosition(VisiblePosition(range->endPosition()).deepEquivalent(), UPSTREAM).absoluteRect(); |
| 1118 | if (endCaretRect == LayoutRect()) |
| 1119 | return IntRect(); |
| 1120 | |
| 1121 | if (startCaretRect.y() == endCaretRect.y()) { |
| 1122 | // start and end are on the same line |
| 1123 | return IntRect(min(startCaretRect.x(), endCaretRect.x()), |
| 1124 | startCaretRect.y(), |
| 1125 | abs(endCaretRect.x() - startCaretRect.x()), |
| 1126 | max(startCaretRect.height(), endCaretRect.height())); |
| 1127 | } |
| 1128 | |
| 1129 | // start and end aren't on the same line, so go from start to the end of its line |
| 1130 | return IntRect(startCaretRect.x(), |
| 1131 | startCaretRect.y(), |
| 1132 | startCaretRect.width() + extraWidthToEndOfLine, |
| 1133 | startCaretRect.height()); |
| 1134 | } |
| 1135 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1136 | void Editor::computeAndSetTypingStyle(StylePropertySet* style, EditAction editingAction) |
| 1137 | { |
| 1138 | if (!style || style->isEmpty()) { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1139 | m_frame.selection().clearTypingStyle(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | // Calculate the current typing style. |
| 1144 | RefPtr<EditingStyle> typingStyle; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1145 | if (m_frame.selection().typingStyle()) { |
| 1146 | typingStyle = m_frame.selection().typingStyle()->copy(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1147 | typingStyle->overrideWithStyle(style); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1148 | } else { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1149 | typingStyle = EditingStyle::create(style); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1150 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1151 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1152 | typingStyle->prepareToApplyAt(m_frame.selection().selection().visibleStart().deepEquivalent(), EditingStyle::PreserveWritingDirection); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1153 | |
| 1154 | // Handle block styles, substracting these from the typing style. |
| 1155 | RefPtr<EditingStyle> blockStyle = typingStyle->extractAndRemoveBlockProperties(); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 1156 | if (!blockStyle->isEmpty()) { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1157 | ASSERT(m_frame.document()); |
| 1158 | ApplyStyleCommand::create(*m_frame.document(), blockStyle.get(), editingAction)->apply(); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 1159 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1160 | |
| 1161 | // Set the remaining style as the typing style. |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1162 | m_frame.selection().setTypingStyle(typingStyle); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1163 | } |
| 1164 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1165 | bool Editor::findString(const String& target, bool forward, bool caseFlag, bool wrapFlag, bool startInSelection) |
| 1166 | { |
| 1167 | FindOptions options = (forward ? 0 : Backwards) | (caseFlag ? 0 : CaseInsensitive) | (wrapFlag ? WrapAround : 0) | (startInSelection ? StartInSelection : 0); |
| 1168 | return findString(target, options); |
| 1169 | } |
| 1170 | |
| 1171 | bool Editor::findString(const String& target, FindOptions options) |
| 1172 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1173 | VisibleSelection selection = m_frame.selection().selection(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1174 | |
| 1175 | RefPtr<Range> resultRange = rangeOfString(target, selection.firstRange().get(), options); |
| 1176 | |
| 1177 | if (!resultRange) |
| 1178 | return false; |
| 1179 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1180 | m_frame.selection().setSelection(VisibleSelection(resultRange.get(), DOWNSTREAM)); |
| 1181 | m_frame.selection().revealSelection(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1182 | return true; |
| 1183 | } |
| 1184 | |
| 1185 | PassRefPtr<Range> Editor::findStringAndScrollToVisible(const String& target, Range* previousMatch, FindOptions options) |
| 1186 | { |
| 1187 | RefPtr<Range> nextMatch = rangeOfString(target, previousMatch, options); |
| 1188 | if (!nextMatch) |
| 1189 | return 0; |
| 1190 | |
| 1191 | nextMatch->firstNode()->renderer()->scrollRectToVisible(nextMatch->boundingBox(), |
| 1192 | ScrollAlignment::alignCenterIfNeeded, ScrollAlignment::alignCenterIfNeeded); |
| 1193 | |
| 1194 | return nextMatch.release(); |
| 1195 | } |
| 1196 | |
| 1197 | PassRefPtr<Range> Editor::rangeOfString(const String& target, Range* referenceRange, FindOptions options) |
| 1198 | { |
| 1199 | if (target.isEmpty()) |
| 1200 | return 0; |
| 1201 | |
| 1202 | // Start from an edge of the reference range, if there's a reference range that's not in shadow content. Which edge |
| 1203 | // is used depends on whether we're searching forward or backward, and whether startInSelection is set. |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1204 | RefPtr<Range> searchRange(rangeOfContents(m_frame.document())); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1205 | |
| 1206 | bool forward = !(options & Backwards); |
| 1207 | bool startInReferenceRange = referenceRange && (options & StartInSelection); |
| 1208 | if (referenceRange) { |
| 1209 | if (forward) |
| 1210 | searchRange->setStart(startInReferenceRange ? referenceRange->startPosition() : referenceRange->endPosition()); |
| 1211 | else |
| 1212 | searchRange->setEnd(startInReferenceRange ? referenceRange->endPosition() : referenceRange->startPosition()); |
| 1213 | } |
| 1214 | |
| 1215 | RefPtr<Node> shadowTreeRoot = referenceRange && referenceRange->startContainer() ? referenceRange->startContainer()->nonBoundaryShadowTreeRootNode() : 0; |
| 1216 | if (shadowTreeRoot) { |
| 1217 | if (forward) |
| 1218 | searchRange->setEnd(shadowTreeRoot.get(), shadowTreeRoot->childNodeCount()); |
| 1219 | else |
| 1220 | searchRange->setStart(shadowTreeRoot.get(), 0); |
| 1221 | } |
| 1222 | |
| 1223 | RefPtr<Range> resultRange(findPlainText(searchRange.get(), target, options)); |
| 1224 | // If we started in the reference range and the found range exactly matches the reference range, find again. |
| 1225 | // Build a selection with the found range to remove collapsed whitespace. |
| 1226 | // Compare ranges instead of selection objects to ignore the way that the current selection was made. |
| 1227 | if (startInReferenceRange && areRangesEqual(VisibleSelection(resultRange.get()).toNormalizedRange().get(), referenceRange)) { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1228 | searchRange = rangeOfContents(m_frame.document()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1229 | if (forward) |
| 1230 | searchRange->setStart(referenceRange->endPosition()); |
| 1231 | else |
| 1232 | searchRange->setEnd(referenceRange->startPosition()); |
| 1233 | |
| 1234 | if (shadowTreeRoot) { |
| 1235 | if (forward) |
| 1236 | searchRange->setEnd(shadowTreeRoot.get(), shadowTreeRoot->childNodeCount()); |
| 1237 | else |
| 1238 | searchRange->setStart(shadowTreeRoot.get(), 0); |
| 1239 | } |
| 1240 | |
| 1241 | resultRange = findPlainText(searchRange.get(), target, options); |
| 1242 | } |
| 1243 | |
| 1244 | // If nothing was found in the shadow tree, search in main content following the shadow tree. |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 1245 | if (resultRange->collapsed(ASSERT_NO_EXCEPTION) && shadowTreeRoot) { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1246 | searchRange = rangeOfContents(m_frame.document()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1247 | if (forward) |
| 1248 | searchRange->setStartAfter(shadowTreeRoot->shadowHost()); |
| 1249 | else |
| 1250 | searchRange->setEndBefore(shadowTreeRoot->shadowHost()); |
| 1251 | |
| 1252 | resultRange = findPlainText(searchRange.get(), target, options); |
| 1253 | } |
| 1254 | |
| 1255 | // If we didn't find anything and we're wrapping, search again in the entire document (this will |
| 1256 | // redundantly re-search the area already searched in some cases). |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 1257 | if (resultRange->collapsed(ASSERT_NO_EXCEPTION) && options & WrapAround) { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1258 | searchRange = rangeOfContents(m_frame.document()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1259 | resultRange = findPlainText(searchRange.get(), target, options); |
| 1260 | // We used to return false here if we ended up with the same range that we started with |
| 1261 | // (e.g., the reference range was already the only instance of this text). But we decided that |
| 1262 | // this should be a success case instead, so we'll just fall through in that case. |
| 1263 | } |
| 1264 | |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 1265 | return resultRange->collapsed(ASSERT_NO_EXCEPTION) ? 0 : resultRange.release(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | void Editor::setMarkedTextMatchesAreHighlighted(bool flag) |
| 1269 | { |
| 1270 | if (flag == m_areMarkedTextMatchesHighlighted) |
| 1271 | return; |
| 1272 | |
| 1273 | m_areMarkedTextMatchesHighlighted = flag; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1274 | m_frame.document()->markers()->repaintMarkers(DocumentMarker::TextMatch); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | void Editor::respondToChangedSelection(const VisibleSelection& oldSelection, FrameSelection::SetSelectionOptions options) |
| 1278 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1279 | spellChecker().respondToChangedSelection(oldSelection, options); |
| 1280 | m_frame.inputMethodController().cancelCompositionIfSelectionIsInvalid(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1281 | notifyComponentsOnChangedSelection(oldSelection, options); |
| 1282 | } |
| 1283 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1284 | SpellChecker& Editor::spellChecker() const |
Ben Murdoch | 3c9e4ae | 2013-08-12 14:20:44 +0100 | [diff] [blame] | 1285 | { |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1286 | return m_frame.spellChecker(); |
Ben Murdoch | 3c9e4ae | 2013-08-12 14:20:44 +0100 | [diff] [blame] | 1287 | } |
| 1288 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1289 | void Editor::toggleOverwriteModeEnabled() |
| 1290 | { |
| 1291 | m_overwriteModeEnabled = !m_overwriteModeEnabled; |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 1292 | frame().selection().setShouldShowBlockCursor(m_overwriteModeEnabled); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1293 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1294 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1295 | } // namespace WebCore |