OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. |
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) | 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 11 matching lines...) Expand all Loading... |
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 */ | 25 */ |
26 | 26 |
27 #include "config.h" | 27 #include "config.h" |
28 #include "core/editing/InputMethodController.h" | 28 #include "core/editing/InputMethodController.h" |
29 | 29 |
30 #include "core/dom/Document.h" | 30 #include "core/dom/Document.h" |
31 #include "core/dom/Element.h" | 31 #include "core/dom/Element.h" |
32 #include "core/dom/Range.h" | |
33 #include "core/dom/Text.h" | 32 #include "core/dom/Text.h" |
| 33 #include "core/editing/EditingUtilities.h" |
34 #include "core/editing/Editor.h" | 34 #include "core/editing/Editor.h" |
35 #include "core/editing/commands/TypingCommand.h" | 35 #include "core/editing/commands/TypingCommand.h" |
| 36 #include "core/editing/markers/DocumentMarkerController.h" |
36 #include "core/events/CompositionEvent.h" | 37 #include "core/events/CompositionEvent.h" |
37 #include "core/frame/LocalFrame.h" | 38 #include "core/frame/LocalFrame.h" |
38 #include "core/html/HTMLTextAreaElement.h" | 39 #include "core/html/HTMLTextAreaElement.h" |
39 #include "core/input/EventHandler.h" | 40 #include "core/input/EventHandler.h" |
40 #include "core/layout/LayoutObject.h" | 41 #include "core/layout/LayoutObject.h" |
| 42 #include "core/layout/LayoutTheme.h" |
41 #include "core/page/ChromeClient.h" | 43 #include "core/page/ChromeClient.h" |
42 | 44 |
43 namespace blink { | 45 namespace blink { |
44 | 46 |
45 InputMethodController::SelectionOffsetsScope::SelectionOffsetsScope(InputMethodC
ontroller* inputMethodController) | 47 InputMethodController::SelectionOffsetsScope::SelectionOffsetsScope(InputMethodC
ontroller* inputMethodController) |
46 : m_inputMethodController(inputMethodController) | 48 : m_inputMethodController(inputMethodController) |
47 , m_offsets(inputMethodController->getSelectionOffsets()) | 49 , m_offsets(inputMethodController->getSelectionOffsets()) |
48 { | 50 { |
49 } | 51 } |
50 | 52 |
51 InputMethodController::SelectionOffsetsScope::~SelectionOffsetsScope() | 53 InputMethodController::SelectionOffsetsScope::~SelectionOffsetsScope() |
52 { | 54 { |
53 m_inputMethodController->setSelectionOffsets(m_offsets); | 55 m_inputMethodController->setSelectionOffsets(m_offsets); |
54 } | 56 } |
55 | 57 |
56 // ---------------------------- | 58 // ---------------------------- |
57 | 59 |
58 PassOwnPtrWillBeRawPtr<InputMethodController> InputMethodController::create(Loca
lFrame& frame) | 60 PassOwnPtrWillBeRawPtr<InputMethodController> InputMethodController::create(Loca
lFrame& frame) |
59 { | 61 { |
60 return adoptPtrWillBeNoop(new InputMethodController(frame)); | 62 return adoptPtrWillBeNoop(new InputMethodController(frame)); |
61 } | 63 } |
62 | 64 |
63 InputMethodController::InputMethodController(LocalFrame& frame) | 65 InputMethodController::InputMethodController(LocalFrame& frame) |
64 : m_frame(&frame) | 66 : m_frame(&frame) |
65 , m_compositionStart(0) | 67 , m_isDirty(false) |
66 , m_compositionEnd(0) | 68 , m_hasComposition(false) |
67 { | 69 { |
68 } | 70 } |
69 | 71 |
70 InputMethodController::~InputMethodController() | 72 InputMethodController::~InputMethodController() |
71 { | 73 { |
72 } | 74 } |
73 | 75 |
74 bool InputMethodController::hasComposition() const | 76 bool InputMethodController::hasComposition() const |
75 { | 77 { |
76 return m_compositionNode && m_compositionNode->isContentEditable(); | 78 return m_hasComposition; |
77 } | 79 } |
78 | 80 |
79 inline Editor& InputMethodController::editor() const | 81 inline Editor& InputMethodController::editor() const |
80 { | 82 { |
81 return frame().editor(); | 83 return frame().editor(); |
82 } | 84 } |
83 | 85 |
84 void InputMethodController::clear() | 86 void InputMethodController::clear() |
85 { | 87 { |
86 m_compositionNode = nullptr; | 88 m_hasComposition = false; |
87 m_customCompositionUnderlines.clear(); | 89 if (m_compositionRange) { |
| 90 m_compositionRange->setStart(frame().document(), 0); |
| 91 m_compositionRange->collapse(true); |
| 92 } |
| 93 frame().document()->markers().removeMarkers(DocumentMarker::Composition); |
| 94 m_isDirty = false; |
| 95 } |
| 96 |
| 97 void InputMethodController::documentDetached() |
| 98 { |
| 99 clear(); |
| 100 m_compositionRange = nullptr; |
88 } | 101 } |
89 | 102 |
90 bool InputMethodController::insertTextForConfirmedComposition(const String& text
) | 103 bool InputMethodController::insertTextForConfirmedComposition(const String& text
) |
91 { | 104 { |
92 return frame().eventHandler().handleTextInputEvent(text, 0, TextEventInputCo
mposition); | 105 return frame().eventHandler().handleTextInputEvent(text, 0, TextEventInputCo
mposition); |
93 } | 106 } |
94 | 107 |
95 void InputMethodController::selectComposition() const | 108 void InputMethodController::selectComposition() const |
96 { | 109 { |
97 const EphemeralRange range = compositionEphemeralRange(); | 110 const EphemeralRange range = compositionEphemeralRange(); |
98 if (range.isNull()) | 111 if (range.isNull()) |
99 return; | 112 return; |
100 | 113 |
101 // The composition can start inside a composed character sequence, so we hav
e to override checks. | 114 // The composition can start inside a composed character sequence, so we hav
e to override checks. |
102 // See <http://bugs.webkit.org/show_bug.cgi?id=15781> | 115 // See <http://bugs.webkit.org/show_bug.cgi?id=15781> |
103 VisibleSelection selection; | 116 VisibleSelection selection; |
104 selection.setWithoutValidation(range.startPosition(), range.endPosition()); | 117 selection.setWithoutValidation(range.startPosition(), range.endPosition()); |
105 frame().selection().setSelection(selection, 0); | 118 frame().selection().setSelection(selection, 0); |
106 } | 119 } |
107 | 120 |
108 bool InputMethodController::confirmComposition() | 121 bool InputMethodController::confirmComposition() |
109 { | 122 { |
110 if (!hasComposition()) | 123 if (!hasComposition()) |
111 return false; | 124 return false; |
112 return finishComposition(m_compositionNode->data().substring(m_compositionSt
art, m_compositionEnd - m_compositionStart), ConfirmComposition); | 125 return confirmComposition(plainText(compositionEphemeralRange())); |
113 } | 126 } |
114 | 127 |
115 bool InputMethodController::confirmComposition(const String& text) | 128 bool InputMethodController::confirmComposition(const String& text) |
116 { | 129 { |
117 return finishComposition(text, ConfirmComposition); | 130 return finishComposition(text, ConfirmComposition); |
118 } | 131 } |
119 | 132 |
120 bool InputMethodController::confirmCompositionOrInsertText(const String& text, C
onfirmCompositionBehavior confirmBehavior) | 133 bool InputMethodController::confirmCompositionOrInsertText(const String& text, C
onfirmCompositionBehavior confirmBehavior) |
121 { | 134 { |
122 if (!hasComposition()) { | 135 if (!hasComposition()) { |
(...skipping 19 matching lines...) Expand all Loading... |
142 { | 155 { |
143 finishComposition(emptyString(), CancelComposition); | 156 finishComposition(emptyString(), CancelComposition); |
144 } | 157 } |
145 | 158 |
146 void InputMethodController::cancelCompositionIfSelectionIsInvalid() | 159 void InputMethodController::cancelCompositionIfSelectionIsInvalid() |
147 { | 160 { |
148 if (!hasComposition() || editor().preventRevealSelection()) | 161 if (!hasComposition() || editor().preventRevealSelection()) |
149 return; | 162 return; |
150 | 163 |
151 // Check if selection start and selection end are valid. | 164 // Check if selection start and selection end are valid. |
152 Position start = frame().selection().start(); | 165 FrameSelection& selection = frame().selection(); |
153 Position end = frame().selection().end(); | 166 if (!selection.isNone() && !m_compositionRange->collapsed()) { |
154 if (start.computeContainerNode() == m_compositionNode | 167 if (selection.start().compareTo(m_compositionRange->startPosition()) >=
0 |
155 && end.computeContainerNode() == m_compositionNode | 168 && selection.end().compareTo(m_compositionRange->endPosition()) <= 0
) |
156 && static_cast<unsigned>(start.computeOffsetInContainerNode()) >= m_comp
ositionStart | 169 return; |
157 && static_cast<unsigned>(end.computeOffsetInContainerNode()) <= m_compos
itionEnd) | 170 } |
158 return; | |
159 | 171 |
160 cancelComposition(); | 172 cancelComposition(); |
161 frame().chromeClient().didCancelCompositionOnSelectionChange(); | 173 frame().chromeClient().didCancelCompositionOnSelectionChange(); |
162 } | 174 } |
163 | 175 |
164 bool InputMethodController::finishComposition(const String& text, FinishComposit
ionMode mode) | 176 bool InputMethodController::finishComposition(const String& text, FinishComposit
ionMode mode) |
165 { | 177 { |
166 if (!hasComposition()) | 178 if (!hasComposition()) |
167 return false; | 179 return false; |
168 | 180 |
(...skipping 10 matching lines...) Expand all Loading... |
179 return false; | 191 return false; |
180 | 192 |
181 // Dispatch a compositionend event to the focused node. | 193 // Dispatch a compositionend event to the focused node. |
182 // We should send this event before sending a TextEvent as written in Sectio
n 6.2.2 and 6.2.3 of | 194 // We should send this event before sending a TextEvent as written in Sectio
n 6.2.2 and 6.2.3 of |
183 // the DOM Event specification. | 195 // the DOM Event specification. |
184 if (Element* target = frame().document()->focusedElement()) { | 196 if (Element* target = frame().document()->focusedElement()) { |
185 RefPtrWillBeRawPtr<CompositionEvent> event = CompositionEvent::create(Ev
entTypeNames::compositionend, frame().domWindow(), text); | 197 RefPtrWillBeRawPtr<CompositionEvent> event = CompositionEvent::create(Ev
entTypeNames::compositionend, frame().domWindow(), text); |
186 target->dispatchEvent(event); | 198 target->dispatchEvent(event); |
187 } | 199 } |
188 | 200 |
| 201 bool dirty = m_isDirty || plainText(compositionEphemeralRange()) != text; |
| 202 |
189 // If text is empty, then delete the old composition here. If text is non-em
pty, InsertTextCommand::input | 203 // If text is empty, then delete the old composition here. If text is non-em
pty, InsertTextCommand::input |
190 // will delete the old composition with an optimized replace operation. | 204 // will delete the old composition with an optimized replace operation. |
191 if (text.isEmpty() && mode != CancelComposition) { | 205 if (text.isEmpty() && mode != CancelComposition && dirty) { |
192 ASSERT(frame().document()); | 206 ASSERT(frame().document()); |
193 TypingCommand::deleteSelection(*frame().document(), 0); | 207 TypingCommand::deleteSelection(*frame().document(), 0); |
194 } | 208 } |
195 | 209 |
196 m_compositionNode = nullptr; | 210 clear(); |
197 m_customCompositionUnderlines.clear(); | |
198 | 211 |
199 insertTextForConfirmedComposition(text); | 212 if (dirty) |
| 213 insertTextForConfirmedComposition(text); |
200 | 214 |
201 if (mode == CancelComposition) { | 215 if (mode == CancelComposition) { |
202 // An open typing command that disagrees about current selection would c
ause issues with typing later on. | 216 // An open typing command that disagrees about current selection would c
ause issues with typing later on. |
203 TypingCommand::closeTyping(m_frame); | 217 TypingCommand::closeTyping(m_frame); |
204 } | 218 } |
205 | 219 |
206 return true; | 220 return true; |
207 } | 221 } |
208 | 222 |
209 void InputMethodController::setComposition(const String& text, const Vector<Comp
ositionUnderline>& underlines, unsigned selectionStart, unsigned selectionEnd) | 223 void InputMethodController::setComposition(const String& text, const Vector<Comp
ositionUnderline>& underlines, unsigned selectionStart, unsigned selectionEnd) |
(...skipping 10 matching lines...) Expand all Loading... |
220 if (frame().selection().isNone()) | 234 if (frame().selection().isNone()) |
221 return; | 235 return; |
222 | 236 |
223 if (Element* target = frame().document()->focusedElement()) { | 237 if (Element* target = frame().document()->focusedElement()) { |
224 // Dispatch an appropriate composition event to the focused node. | 238 // Dispatch an appropriate composition event to the focused node. |
225 // We check the composition status and choose an appropriate composition
event since this | 239 // We check the composition status and choose an appropriate composition
event since this |
226 // function is used for three purposes: | 240 // function is used for three purposes: |
227 // 1. Starting a new composition. | 241 // 1. Starting a new composition. |
228 // Send a compositionstart and a compositionupdate event when this fu
nction creates | 242 // Send a compositionstart and a compositionupdate event when this fu
nction creates |
229 // a new composition node, i.e. | 243 // a new composition node, i.e. |
230 // m_compositionNode == 0 && !text.isEmpty(). | 244 // !hasComposition() && !text.isEmpty(). |
231 // Sending a compositionupdate event at this time ensures that at lea
st one | 245 // Sending a compositionupdate event at this time ensures that at lea
st one |
232 // compositionupdate event is dispatched. | 246 // compositionupdate event is dispatched. |
233 // 2. Updating the existing composition node. | 247 // 2. Updating the existing composition node. |
234 // Send a compositionupdate event when this function updates the exis
ting composition | 248 // Send a compositionupdate event when this function updates the exis
ting composition |
235 // node, i.e. m_compositionNode != 0 && !text.isEmpty(). | 249 // node, i.e. hasComposition() && !text.isEmpty(). |
236 // 3. Canceling the ongoing composition. | 250 // 3. Canceling the ongoing composition. |
237 // Send a compositionend event when function deletes the existing com
position node, i.e. | 251 // Send a compositionend event when function deletes the existing com
position node, i.e. |
238 // m_compositionNode != 0 && test.isEmpty(). | 252 // !hasComposition() && test.isEmpty(). |
239 RefPtrWillBeRawPtr<CompositionEvent> event = nullptr; | 253 RefPtrWillBeRawPtr<CompositionEvent> event = nullptr; |
240 if (!hasComposition()) { | 254 if (!hasComposition()) { |
241 // We should send a compositionstart event only when the given text
is not empty because this | 255 // We should send a compositionstart event only when the given text
is not empty because this |
242 // function doesn't create a composition node when the text is empty
. | 256 // function doesn't create a composition node when the text is empty
. |
243 if (!text.isEmpty()) { | 257 if (!text.isEmpty()) { |
244 target->dispatchEvent(CompositionEvent::create(EventTypeNames::c
ompositionstart, frame().domWindow(), frame().selectedText())); | 258 target->dispatchEvent(CompositionEvent::create(EventTypeNames::c
ompositionstart, frame().domWindow(), frame().selectedText())); |
245 event = CompositionEvent::create(EventTypeNames::compositionupda
te, frame().domWindow(), text); | 259 event = CompositionEvent::create(EventTypeNames::compositionupda
te, frame().domWindow(), text); |
246 } | 260 } |
247 } else { | 261 } else { |
248 if (!text.isEmpty()) | 262 if (!text.isEmpty()) |
249 event = CompositionEvent::create(EventTypeNames::compositionupda
te, frame().domWindow(), text); | 263 event = CompositionEvent::create(EventTypeNames::compositionupda
te, frame().domWindow(), text); |
250 else | 264 else |
251 event = CompositionEvent::create(EventTypeNames::compositionend,
frame().domWindow(), text); | 265 event = CompositionEvent::create(EventTypeNames::compositionend,
frame().domWindow(), text); |
252 } | 266 } |
253 if (event.get()) | 267 if (event.get()) |
254 target->dispatchEvent(event); | 268 target->dispatchEvent(event); |
255 } | 269 } |
256 | 270 |
257 // If text is empty, then delete the old composition here. If text is non-em
pty, InsertTextCommand::input | 271 // If text is empty, then delete the old composition here. If text is non-em
pty, InsertTextCommand::input |
258 // will delete the old composition with an optimized replace operation. | 272 // will delete the old composition with an optimized replace operation. |
259 if (text.isEmpty()) { | 273 if (text.isEmpty()) { |
260 ASSERT(frame().document()); | 274 ASSERT(frame().document()); |
261 TypingCommand::deleteSelection(*frame().document(), TypingCommand::Preve
ntSpellChecking); | 275 TypingCommand::deleteSelection(*frame().document(), TypingCommand::Preve
ntSpellChecking); |
262 } | 276 } |
263 | 277 |
264 m_compositionNode = nullptr; | 278 clear(); |
265 m_customCompositionUnderlines.clear(); | |
266 | 279 |
267 if (text.isEmpty()) | 280 if (text.isEmpty()) |
268 return; | 281 return; |
269 ASSERT(frame().document()); | 282 ASSERT(frame().document()); |
270 TypingCommand::insertText(*frame().document(), text, TypingCommand::SelectIn
sertedText | TypingCommand::PreventSpellChecking, TypingCommand::TextComposition
Update); | 283 TypingCommand::insertText(*frame().document(), text, TypingCommand::SelectIn
sertedText | TypingCommand::PreventSpellChecking, TypingCommand::TextComposition
Update); |
271 | 284 |
272 // Find out what node has the composition now. | 285 // Find out what node has the composition now. |
273 Position base = mostForwardCaretPosition(frame().selection().base()); | 286 Position base = mostForwardCaretPosition(frame().selection().base()); |
274 Node* baseNode = base.anchorNode(); | 287 Node* baseNode = base.anchorNode(); |
275 if (!baseNode || !baseNode->isTextNode()) | 288 if (!baseNode || !baseNode->isTextNode()) |
276 return; | 289 return; |
277 | 290 |
278 Position extent = frame().selection().extent(); | 291 Position extent = frame().selection().extent(); |
279 Node* extentNode = extent.anchorNode(); | 292 Node* extentNode = extent.anchorNode(); |
280 if (baseNode != extentNode) | 293 if (baseNode != extentNode) |
281 return; | 294 return; |
282 | 295 |
283 unsigned extentOffset = extent.computeOffsetInContainerNode(); | 296 unsigned extentOffset = extent.computeOffsetInContainerNode(); |
284 unsigned baseOffset = base.computeOffsetInContainerNode(); | 297 unsigned baseOffset = base.computeOffsetInContainerNode(); |
285 if (baseOffset + text.length() != extentOffset) | 298 if (baseOffset + text.length() != extentOffset) |
286 return; | 299 return; |
287 | 300 |
288 m_compositionNode = toText(baseNode); | 301 m_isDirty = true; |
289 m_compositionStart = baseOffset; | 302 m_hasComposition = true; |
290 m_compositionEnd = extentOffset; | 303 if (!m_compositionRange) |
291 m_customCompositionUnderlines = underlines; | 304 m_compositionRange = Range::create(baseNode->document()); |
292 for (auto& underline : m_customCompositionUnderlines) { | 305 m_compositionRange->setStart(baseNode, baseOffset); |
293 underline.startOffset += baseOffset; | 306 m_compositionRange->setEnd(baseNode, extentOffset); |
294 underline.endOffset += baseOffset; | 307 |
295 } | |
296 if (baseNode->layoutObject()) | 308 if (baseNode->layoutObject()) |
297 baseNode->layoutObject()->setShouldDoFullPaintInvalidation(); | 309 baseNode->layoutObject()->setShouldDoFullPaintInvalidation(); |
298 | 310 |
299 unsigned start = std::min(baseOffset + selectionStart, extentOffset); | 311 unsigned start = std::min(baseOffset + selectionStart, extentOffset); |
300 unsigned end = std::min(std::max(start, baseOffset + selectionEnd), extentOf
fset); | 312 unsigned end = std::min(std::max(start, baseOffset + selectionEnd), extentOf
fset); |
301 RefPtrWillBeRawPtr<Range> selectedRange = Range::create(baseNode->document()
, baseNode, start, baseNode, end); | 313 RefPtrWillBeRawPtr<Range> selectedRange = Range::create(baseNode->document()
, baseNode, start, baseNode, end); |
302 frame().selection().setSelectedRange(selectedRange.get(), TextAffinity::Down
stream, FrameSelection::NonDirectional, NotUserTriggered); | 314 frame().selection().setSelectedRange(selectedRange.get(), TextAffinity::Down
stream, FrameSelection::NonDirectional, NotUserTriggered); |
| 315 |
| 316 if (underlines.isEmpty()) { |
| 317 frame().document()->markers().addCompositionMarker(m_compositionRange->s
tartPosition(), m_compositionRange->endPosition(), Color::black, false, LayoutTh
eme::theme().platformDefaultCompositionBackgroundColor()); |
| 318 return; |
| 319 } |
| 320 for (const auto& underline : underlines) { |
| 321 unsigned underlineStart = baseOffset + underline.startOffset; |
| 322 unsigned underlineEnd = baseOffset + underline.endOffset; |
| 323 EphemeralRange ephemeralLineRange = EphemeralRange(Position(baseNode, un
derlineStart), Position(baseNode, underlineEnd)); |
| 324 if (ephemeralLineRange.isNull()) |
| 325 continue; |
| 326 frame().document()->markers().addCompositionMarker(ephemeralLineRange.st
artPosition(), ephemeralLineRange.endPosition(), underline.color, underline.thic
k, underline.backgroundColor); |
| 327 } |
303 } | 328 } |
304 | 329 |
305 void InputMethodController::setCompositionFromExistingText(const Vector<Composit
ionUnderline>& underlines, unsigned compositionStart, unsigned compositionEnd) | 330 void InputMethodController::setCompositionFromExistingText(const Vector<Composit
ionUnderline>& underlines, unsigned compositionStart, unsigned compositionEnd) |
306 { | 331 { |
307 Element* editable = frame().selection().rootEditableElement(); | 332 Element* editable = frame().selection().rootEditableElement(); |
308 Position base = mostForwardCaretPosition(frame().selection().base()); | 333 if (!editable) |
309 Node* baseNode = base.anchorNode(); | 334 return; |
310 if (baseNode && editable->firstChild() == baseNode && editable->lastChild()
== baseNode && baseNode->isTextNode()) { | |
311 m_compositionNode = nullptr; | |
312 m_customCompositionUnderlines.clear(); | |
313 | 335 |
314 if (!base.isOffsetInAnchor()) | 336 const EphemeralRange range = PlainTextRange(compositionStart, compositionEnd
).createRange(*editable); |
315 return; | 337 if (range.isNull()) |
316 if (baseNode != frame().selection().extent().anchorNode()) | 338 return; |
317 return; | |
318 | 339 |
319 m_compositionNode = toText(baseNode); | 340 const Position start = range.startPosition(); |
320 const EphemeralRange range = PlainTextRange(compositionStart, compositio
nEnd).createRange(*editable); | 341 if (editableRootForPosition(start) != editable) |
321 if (range.isNull()) | 342 return; |
322 return; | |
323 | 343 |
324 m_compositionStart = range.startPosition().computeOffsetInContainerNode(
); | 344 const Position end = range.endPosition(); |
325 m_compositionEnd = range.endPosition().computeOffsetInContainerNode(); | 345 if (editableRootForPosition(end) != editable) |
326 m_customCompositionUnderlines = underlines; | |
327 size_t numUnderlines = m_customCompositionUnderlines.size(); | |
328 for (size_t i = 0; i < numUnderlines; ++i) { | |
329 m_customCompositionUnderlines[i].startOffset += m_compositionStart; | |
330 m_customCompositionUnderlines[i].endOffset += m_compositionStart; | |
331 } | |
332 if (baseNode->layoutObject()) | |
333 baseNode->layoutObject()->setShouldDoFullPaintInvalidation(); | |
334 return; | 346 return; |
| 347 |
| 348 clear(); |
| 349 |
| 350 for (const auto& underline : underlines) { |
| 351 unsigned underlineStart = compositionStart + underline.startOffset; |
| 352 unsigned underlineEnd = compositionStart + underline.endOffset; |
| 353 EphemeralRange ephemeralLineRange = PlainTextRange(underlineStart, under
lineEnd).createRange(*editable); |
| 354 if (ephemeralLineRange.isNull()) |
| 355 continue; |
| 356 frame().document()->markers().addCompositionMarker(ephemeralLineRange.st
artPosition(), ephemeralLineRange.endPosition(), underline.color, underline.thic
k, underline.backgroundColor); |
335 } | 357 } |
336 | 358 |
337 Editor::RevealSelectionScope revealSelectionScope(&editor()); | 359 m_hasComposition = true; |
338 SelectionOffsetsScope selectionOffsetsScope(this); | 360 if (!m_compositionRange) |
339 setSelectionOffsets(PlainTextRange(compositionStart, compositionEnd)); | 361 m_compositionRange = Range::create(range.document()); |
340 setComposition(frame().selectedText(), underlines, 0, 0); | 362 m_compositionRange->setStart(range.startPosition()); |
| 363 m_compositionRange->setEnd(range.endPosition()); |
341 } | 364 } |
342 | 365 |
343 EphemeralRange InputMethodController::compositionEphemeralRange() const | 366 EphemeralRange InputMethodController::compositionEphemeralRange() const |
344 { | 367 { |
345 if (!hasComposition()) | 368 if (!hasComposition()) |
346 return EphemeralRange(); | 369 return EphemeralRange(); |
347 unsigned length = m_compositionNode->length(); | 370 return EphemeralRange(m_compositionRange.get()); |
348 unsigned start = std::min(m_compositionStart, length); | |
349 unsigned end = std::min(std::max(start, m_compositionEnd), length); | |
350 if (start >= end) | |
351 return EphemeralRange(); | |
352 return EphemeralRange(Position(m_compositionNode.get(), start), Position(m_c
ompositionNode.get(), end)); | |
353 } | 371 } |
354 | 372 |
355 PassRefPtrWillBeRawPtr<Range> InputMethodController::compositionRange() const | 373 PassRefPtrWillBeRawPtr<Range> InputMethodController::compositionRange() const |
356 { | 374 { |
357 return createRange(compositionEphemeralRange()); | 375 return hasComposition() ? m_compositionRange : nullptr; |
358 } | 376 } |
359 | 377 |
360 PlainTextRange InputMethodController::getSelectionOffsets() const | 378 PlainTextRange InputMethodController::getSelectionOffsets() const |
361 { | 379 { |
362 RefPtrWillBeRawPtr<Range> range = firstRangeOf(frame().selection().selection
()); | 380 RefPtrWillBeRawPtr<Range> range = firstRangeOf(frame().selection().selection
()); |
363 if (!range) | 381 if (!range) |
364 return PlainTextRange(); | 382 return PlainTextRange(); |
365 ContainerNode* editable = frame().selection().rootEditableElementOrTreeScope
RootNode(); | 383 ContainerNode* editable = frame().selection().rootEditableElementOrTreeScope
RootNode(); |
366 ASSERT(editable); | 384 ASSERT(editable); |
367 return PlainTextRange::create(*editable, *range.get()); | 385 return PlainTextRange::create(*editable, *range.get()); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 if (before == 0) | 433 if (before == 0) |
416 break; | 434 break; |
417 ++before; | 435 ++before; |
418 } while (frame().selection().start() == frame().selection().end() && before
<= static_cast<int>(selectionOffsets.start())); | 436 } while (frame().selection().start() == frame().selection().end() && before
<= static_cast<int>(selectionOffsets.start())); |
419 TypingCommand::deleteSelection(*frame().document()); | 437 TypingCommand::deleteSelection(*frame().document()); |
420 } | 438 } |
421 | 439 |
422 DEFINE_TRACE(InputMethodController) | 440 DEFINE_TRACE(InputMethodController) |
423 { | 441 { |
424 visitor->trace(m_frame); | 442 visitor->trace(m_frame); |
425 visitor->trace(m_compositionNode); | 443 visitor->trace(m_compositionRange); |
426 } | 444 } |
427 | 445 |
428 } // namespace blink | 446 } // namespace blink |
OLD | NEW |