Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: Source/core/editing/InputMethodController.cpp

Issue 1325563002: Avoid style clobbering in setCompositionFromExistingText. (2nd land) (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Use DocumentMarker for underlines Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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)
67 { 68 {
68 } 69 }
69 70
70 InputMethodController::~InputMethodController() 71 InputMethodController::~InputMethodController()
71 { 72 {
72 } 73 }
73 74
74 bool InputMethodController::hasComposition() const 75 bool InputMethodController::hasComposition() const
75 { 76 {
76 return m_compositionNode && m_compositionNode->isContentEditable(); 77 return m_compositionRange.get() && m_compositionRange->startContainer() && m _compositionRange->startContainer()->isContentEditable();
77 } 78 }
78 79
79 inline Editor& InputMethodController::editor() const 80 inline Editor& InputMethodController::editor() const
80 { 81 {
81 return frame().editor(); 82 return frame().editor();
82 } 83 }
83 84
84 void InputMethodController::clear() 85 void InputMethodController::clear()
85 { 86 {
86 m_compositionNode = nullptr; 87 m_compositionRange = nullptr;
87 m_customCompositionUnderlines.clear(); 88 frame().document()->markers().removeMarkers(DocumentMarker::Composition);
89 m_isDirty = false;
88 } 90 }
89 91
90 bool InputMethodController::insertTextForConfirmedComposition(const String& text ) 92 bool InputMethodController::insertTextForConfirmedComposition(const String& text )
91 { 93 {
92 return frame().eventHandler().handleTextInputEvent(text, 0, TextEventInputCo mposition); 94 return frame().eventHandler().handleTextInputEvent(text, 0, TextEventInputCo mposition);
93 } 95 }
94 96
95 void InputMethodController::selectComposition() const 97 void InputMethodController::selectComposition() const
96 { 98 {
97 const EphemeralRange range = compositionEphemeralRange(); 99 const EphemeralRange range = compositionEphemeralRange();
98 if (range.isNull()) 100 if (range.isNull())
99 return; 101 return;
100 102
101 // The composition can start inside a composed character sequence, so we hav e to override checks. 103 // 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> 104 // See <http://bugs.webkit.org/show_bug.cgi?id=15781>
103 VisibleSelection selection; 105 VisibleSelection selection;
104 selection.setWithoutValidation(range.startPosition(), range.endPosition()); 106 selection.setWithoutValidation(range.startPosition(), range.endPosition());
105 frame().selection().setSelection(selection, 0); 107 frame().selection().setSelection(selection, 0);
106 } 108 }
107 109
108 bool InputMethodController::confirmComposition() 110 bool InputMethodController::confirmComposition()
109 { 111 {
110 if (!hasComposition()) 112 if (!hasComposition())
111 return false; 113 return false;
112 return finishComposition(m_compositionNode->data().substring(m_compositionSt art, m_compositionEnd - m_compositionStart), ConfirmComposition); 114 return confirmComposition(plainText(compositionEphemeralRange()));
113 } 115 }
114 116
115 bool InputMethodController::confirmComposition(const String& text) 117 bool InputMethodController::confirmComposition(const String& text)
116 { 118 {
117 return finishComposition(text, ConfirmComposition); 119 return finishComposition(text, ConfirmComposition);
118 } 120 }
119 121
120 bool InputMethodController::confirmCompositionOrInsertText(const String& text, C onfirmCompositionBehavior confirmBehavior) 122 bool InputMethodController::confirmCompositionOrInsertText(const String& text, C onfirmCompositionBehavior confirmBehavior)
121 { 123 {
122 if (!hasComposition()) { 124 if (!hasComposition()) {
(...skipping 19 matching lines...) Expand all
142 { 144 {
143 finishComposition(emptyString(), CancelComposition); 145 finishComposition(emptyString(), CancelComposition);
144 } 146 }
145 147
146 void InputMethodController::cancelCompositionIfSelectionIsInvalid() 148 void InputMethodController::cancelCompositionIfSelectionIsInvalid()
147 { 149 {
148 if (!hasComposition() || editor().preventRevealSelection()) 150 if (!hasComposition() || editor().preventRevealSelection())
149 return; 151 return;
150 152
151 // Check if selection start and selection end are valid. 153 // Check if selection start and selection end are valid.
152 Position start = frame().selection().start(); 154 FrameSelection& selection = frame().selection();
153 Position end = frame().selection().end(); 155 if (!selection.isNone()) {
154 if (start.computeContainerNode() == m_compositionNode 156 Position start = selection.start();
155 && end.computeContainerNode() == m_compositionNode 157 Position end = selection.end();
156 && static_cast<unsigned>(start.computeOffsetInContainerNode()) >= m_comp ositionStart 158 if (start.anchorNode() && end.anchorNode()) {
157 && static_cast<unsigned>(end.computeOffsetInContainerNode()) <= m_compos itionEnd) 159 if (m_compositionRange->isPointInRange(start.computeContainerNode(), start.computeOffsetInContainerNode(), IGNORE_EXCEPTION)
158 return; 160 && m_compositionRange->isPointInRange(end.computeContainerNode() , end.computeOffsetInContainerNode(), IGNORE_EXCEPTION))
161 return;
162 }
163 }
159 164
160 cancelComposition(); 165 cancelComposition();
161 frame().chromeClient().didCancelCompositionOnSelectionChange(); 166 frame().chromeClient().didCancelCompositionOnSelectionChange();
162 } 167 }
163 168
164 bool InputMethodController::finishComposition(const String& text, FinishComposit ionMode mode) 169 bool InputMethodController::finishComposition(const String& text, FinishComposit ionMode mode)
165 { 170 {
166 if (!hasComposition()) 171 if (!hasComposition())
167 return false; 172 return false;
168 173
(...skipping 10 matching lines...) Expand all
179 return false; 184 return false;
180 185
181 // Dispatch a compositionend event to the focused node. 186 // 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 187 // 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. 188 // the DOM Event specification.
184 if (Element* target = frame().document()->focusedElement()) { 189 if (Element* target = frame().document()->focusedElement()) {
185 RefPtrWillBeRawPtr<CompositionEvent> event = CompositionEvent::create(Ev entTypeNames::compositionend, frame().domWindow(), text); 190 RefPtrWillBeRawPtr<CompositionEvent> event = CompositionEvent::create(Ev entTypeNames::compositionend, frame().domWindow(), text);
186 target->dispatchEvent(event); 191 target->dispatchEvent(event);
187 } 192 }
188 193
194 bool dirty = m_isDirty || plainText(compositionEphemeralRange()) != text;
195
189 // If text is empty, then delete the old composition here. If text is non-em pty, InsertTextCommand::input 196 // 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. 197 // will delete the old composition with an optimized replace operation.
191 if (text.isEmpty() && mode != CancelComposition) { 198 if (text.isEmpty() && mode != CancelComposition && dirty) {
192 ASSERT(frame().document()); 199 ASSERT(frame().document());
193 TypingCommand::deleteSelection(*frame().document(), 0); 200 TypingCommand::deleteSelection(*frame().document(), 0);
194 } 201 }
195 202
196 m_compositionNode = nullptr; 203 clear();
197 m_customCompositionUnderlines.clear();
198 204
199 insertTextForConfirmedComposition(text); 205 if (dirty)
206 insertTextForConfirmedComposition(text);
200 207
201 if (mode == CancelComposition) { 208 if (mode == CancelComposition) {
202 // An open typing command that disagrees about current selection would c ause issues with typing later on. 209 // An open typing command that disagrees about current selection would c ause issues with typing later on.
203 TypingCommand::closeTyping(m_frame); 210 TypingCommand::closeTyping(m_frame);
204 } 211 }
205 212
206 return true; 213 return true;
207 } 214 }
208 215
209 void InputMethodController::setComposition(const String& text, const Vector<Comp ositionUnderline>& underlines, unsigned selectionStart, unsigned selectionEnd) 216 void InputMethodController::setComposition(const String& text, const Vector<Comp ositionUnderline>& underlines, unsigned selectionStart, unsigned selectionEnd)
(...skipping 10 matching lines...) Expand all
220 if (frame().selection().isNone()) 227 if (frame().selection().isNone())
221 return; 228 return;
222 229
223 if (Element* target = frame().document()->focusedElement()) { 230 if (Element* target = frame().document()->focusedElement()) {
224 // Dispatch an appropriate composition event to the focused node. 231 // Dispatch an appropriate composition event to the focused node.
225 // We check the composition status and choose an appropriate composition event since this 232 // We check the composition status and choose an appropriate composition event since this
226 // function is used for three purposes: 233 // function is used for three purposes:
227 // 1. Starting a new composition. 234 // 1. Starting a new composition.
228 // Send a compositionstart and a compositionupdate event when this fu nction creates 235 // Send a compositionstart and a compositionupdate event when this fu nction creates
229 // a new composition node, i.e. 236 // a new composition node, i.e.
230 // m_compositionNode == 0 && !text.isEmpty(). 237 // !hasComposition() && !text.isEmpty().
231 // Sending a compositionupdate event at this time ensures that at lea st one 238 // Sending a compositionupdate event at this time ensures that at lea st one
232 // compositionupdate event is dispatched. 239 // compositionupdate event is dispatched.
233 // 2. Updating the existing composition node. 240 // 2. Updating the existing composition node.
234 // Send a compositionupdate event when this function updates the exis ting composition 241 // Send a compositionupdate event when this function updates the exis ting composition
235 // node, i.e. m_compositionNode != 0 && !text.isEmpty(). 242 // node, i.e. hasComposition() && !text.isEmpty().
236 // 3. Canceling the ongoing composition. 243 // 3. Canceling the ongoing composition.
237 // Send a compositionend event when function deletes the existing com position node, i.e. 244 // Send a compositionend event when function deletes the existing com position node, i.e.
238 // m_compositionNode != 0 && test.isEmpty(). 245 // !hasComposition() && test.isEmpty().
239 RefPtrWillBeRawPtr<CompositionEvent> event = nullptr; 246 RefPtrWillBeRawPtr<CompositionEvent> event = nullptr;
240 if (!hasComposition()) { 247 if (!hasComposition()) {
241 // We should send a compositionstart event only when the given text is not empty because this 248 // 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 . 249 // function doesn't create a composition node when the text is empty .
243 if (!text.isEmpty()) { 250 if (!text.isEmpty()) {
244 target->dispatchEvent(CompositionEvent::create(EventTypeNames::c ompositionstart, frame().domWindow(), frame().selectedText())); 251 target->dispatchEvent(CompositionEvent::create(EventTypeNames::c ompositionstart, frame().domWindow(), frame().selectedText()));
245 event = CompositionEvent::create(EventTypeNames::compositionupda te, frame().domWindow(), text); 252 event = CompositionEvent::create(EventTypeNames::compositionupda te, frame().domWindow(), text);
246 } 253 }
247 } else { 254 } else {
248 if (!text.isEmpty()) 255 if (!text.isEmpty())
249 event = CompositionEvent::create(EventTypeNames::compositionupda te, frame().domWindow(), text); 256 event = CompositionEvent::create(EventTypeNames::compositionupda te, frame().domWindow(), text);
250 else 257 else
251 event = CompositionEvent::create(EventTypeNames::compositionend, frame().domWindow(), text); 258 event = CompositionEvent::create(EventTypeNames::compositionend, frame().domWindow(), text);
252 } 259 }
253 if (event.get()) 260 if (event.get())
254 target->dispatchEvent(event); 261 target->dispatchEvent(event);
255 } 262 }
256 263
257 // If text is empty, then delete the old composition here. If text is non-em pty, InsertTextCommand::input 264 // 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. 265 // will delete the old composition with an optimized replace operation.
259 if (text.isEmpty()) { 266 if (text.isEmpty()) {
260 ASSERT(frame().document()); 267 ASSERT(frame().document());
261 TypingCommand::deleteSelection(*frame().document(), TypingCommand::Preve ntSpellChecking); 268 TypingCommand::deleteSelection(*frame().document(), TypingCommand::Preve ntSpellChecking);
262 } 269 }
263 270
264 m_compositionNode = nullptr; 271 clear();
265 m_customCompositionUnderlines.clear(); 272
273 m_isDirty = true;
266 274
267 if (text.isEmpty()) 275 if (text.isEmpty())
268 return; 276 return;
269 ASSERT(frame().document()); 277 ASSERT(frame().document());
270 TypingCommand::insertText(*frame().document(), text, TypingCommand::SelectIn sertedText | TypingCommand::PreventSpellChecking, TypingCommand::TextComposition Update); 278 TypingCommand::insertText(*frame().document(), text, TypingCommand::SelectIn sertedText | TypingCommand::PreventSpellChecking, TypingCommand::TextComposition Update);
271 279
272 // Find out what node has the composition now. 280 // Find out what node has the composition now.
273 Position base = mostForwardCaretPosition(frame().selection().base()); 281 Position base = mostForwardCaretPosition(frame().selection().base());
274 Node* baseNode = base.anchorNode(); 282 Node* baseNode = base.anchorNode();
275 if (!baseNode || !baseNode->isTextNode()) 283 if (!baseNode || !baseNode->isTextNode())
276 return; 284 return;
277 285
278 Position extent = frame().selection().extent(); 286 Position extent = frame().selection().extent();
279 Node* extentNode = extent.anchorNode(); 287 Node* extentNode = extent.anchorNode();
280 if (baseNode != extentNode) 288 if (baseNode != extentNode)
281 return; 289 return;
282 290
283 unsigned extentOffset = extent.computeOffsetInContainerNode(); 291 unsigned extentOffset = extent.computeOffsetInContainerNode();
284 unsigned baseOffset = base.computeOffsetInContainerNode(); 292 unsigned baseOffset = base.computeOffsetInContainerNode();
285 if (baseOffset + text.length() != extentOffset) 293 if (baseOffset + text.length() != extentOffset)
286 return; 294 return;
287 295
288 m_compositionNode = toText(baseNode); 296 m_compositionRange = Range::create(baseNode->document(), baseNode, baseOffse t, baseNode, extentOffset);
yosin_UTC9 2015/09/04 06:20:42 nit: Please create |Range| object at construction
289 m_compositionStart = baseOffset; 297
290 m_compositionEnd = extentOffset;
291 m_customCompositionUnderlines = underlines;
292 for (auto& underline : m_customCompositionUnderlines) {
293 underline.startOffset += baseOffset;
294 underline.endOffset += baseOffset;
295 }
296 if (baseNode->layoutObject()) 298 if (baseNode->layoutObject())
297 baseNode->layoutObject()->setShouldDoFullPaintInvalidation(); 299 baseNode->layoutObject()->setShouldDoFullPaintInvalidation();
298 300
299 unsigned start = std::min(baseOffset + selectionStart, extentOffset); 301 unsigned start = std::min(baseOffset + selectionStart, extentOffset);
300 unsigned end = std::min(std::max(start, baseOffset + selectionEnd), extentOf fset); 302 unsigned end = std::min(std::max(start, baseOffset + selectionEnd), extentOf fset);
301 RefPtrWillBeRawPtr<Range> selectedRange = Range::create(baseNode->document() , baseNode, start, baseNode, end); 303 RefPtrWillBeRawPtr<Range> selectedRange = Range::create(baseNode->document() , baseNode, start, baseNode, end);
302 frame().selection().setSelectedRange(selectedRange.get(), TextAffinity::Down stream, FrameSelection::NonDirectional, NotUserTriggered); 304 frame().selection().setSelectedRange(selectedRange.get(), TextAffinity::Down stream, FrameSelection::NonDirectional, NotUserTriggered);
305
306 if (underlines.size()) {
307 for (size_t i = 0; i < underlines.size(); ++i) {
308 unsigned underlineStart = baseOffset + underlines[i].startOffset;
309 unsigned underlineEnd = baseOffset + underlines[i].endOffset;
310 EphemeralRange ephemeralLineRange = EphemeralRange(Position(baseNode , underlineStart), Position(baseNode, underlineEnd));
311 if (ephemeralLineRange.isNull())
312 continue;
313 frame().document()->markers().addCompositionMarker(ephemeralLineRang e.startPosition(), ephemeralLineRange.endPosition(), underlines[i].color, underl ines[i].thick, underlines[i].backgroundColor);
314 }
315 } else {
316 frame().document()->markers().addCompositionMarker(m_compositionRange->s tartPosition(), m_compositionRange->endPosition(), Color::black, false, LayoutTh eme::theme().platformDefaultCompositionBackgroundColor());
317 }
318
303 } 319 }
304 320
305 void InputMethodController::setCompositionFromExistingText(const Vector<Composit ionUnderline>& underlines, unsigned compositionStart, unsigned compositionEnd) 321 void InputMethodController::setCompositionFromExistingText(const Vector<Composit ionUnderline>& underlines, unsigned compositionStart, unsigned compositionEnd)
306 { 322 {
307 Element* editable = frame().selection().rootEditableElement(); 323 Element* editable = frame().selection().rootEditableElement();
308 Position base = mostForwardCaretPosition(frame().selection().base()); 324 if (!editable)
309 Node* baseNode = base.anchorNode(); 325 return;
310 if (baseNode && editable->firstChild() == baseNode && editable->lastChild() == baseNode && baseNode->isTextNode()) {
311 m_compositionNode = nullptr;
312 m_customCompositionUnderlines.clear();
313 326
314 if (!base.isOffsetInAnchor()) 327 EphemeralRange range = PlainTextRange(compositionStart, compositionEnd).crea teRange(*editable);
yosin_UTC9 2015/09/04 06:20:42 nit: |const EphemeralRange|
315 return; 328 if (range.isNull())
316 if (baseNode != frame().selection().extent().anchorNode()) 329 return;
317 return;
318 330
319 m_compositionNode = toText(baseNode); 331 Position start = range.startPosition();
yosin_UTC9 2015/09/04 06:20:42 nit: |const Position|
320 const EphemeralRange range = PlainTextRange(compositionStart, compositio nEnd).createRange(*editable); 332 if (!start.anchorNode() || start.anchorNode()->rootEditableElement() != edit able)
321 if (range.isNull()) 333 return;
322 return;
323 334
324 m_compositionStart = range.startPosition().computeOffsetInContainerNode( ); 335 Position end = range.endPosition();
yosin_UTC9 2015/09/04 06:20:42 nit: |const Position|
325 m_compositionEnd = range.endPosition().computeOffsetInContainerNode(); 336 if (!end.anchorNode() || end.anchorNode()->rootEditableElement() != 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; 337 return;
338
339 clear();
340
341 for (size_t i = 0; i < underlines.size(); ++i) {
342 unsigned underlineStart = compositionStart + underlines[i].startOffset;
343 unsigned underlineEnd = compositionStart + underlines[i].endOffset;
344 EphemeralRange ephemeralLineRange = PlainTextRange(underlineStart, under lineEnd).createRange(*editable);
345 if (ephemeralLineRange.isNull())
346 continue;
347 frame().document()->markers().addCompositionMarker(ephemeralLineRange.st artPosition(), ephemeralLineRange.endPosition(), underlines[i].color, underlines [i].thick, underlines[i].backgroundColor);
335 } 348 }
336 349
337 Editor::RevealSelectionScope revealSelectionScope(&editor()); 350 m_compositionRange = Range::create(range.document(), range.startPosition(), range.endPosition());
yosin_UTC9 2015/09/04 06:20:42 nit: Please reuse |Range| object.
338 SelectionOffsetsScope selectionOffsetsScope(this);
339 setSelectionOffsets(PlainTextRange(compositionStart, compositionEnd));
340 setComposition(frame().selectedText(), underlines, 0, 0);
341 } 351 }
342 352
343 EphemeralRange InputMethodController::compositionEphemeralRange() const 353 EphemeralRange InputMethodController::compositionEphemeralRange() const
344 { 354 {
345 if (!hasComposition()) 355 if (!hasComposition())
346 return EphemeralRange(); 356 return EphemeralRange();
347 unsigned length = m_compositionNode->length(); 357 return EphemeralRange(m_compositionRange->startPosition(), m_compositionRang e->endPosition());
yosin_UTC9 2015/09/04 06:20:42 nit: |return EphemeralRange(m_compositionRange);|
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 } 358 }
354 359
355 PassRefPtrWillBeRawPtr<Range> InputMethodController::compositionRange() const 360 PassRefPtrWillBeRawPtr<Range> InputMethodController::compositionRange() const
356 { 361 {
357 return createRange(compositionEphemeralRange()); 362 return hasComposition() ? m_compositionRange : nullptr;
358 } 363 }
359 364
360 PlainTextRange InputMethodController::getSelectionOffsets() const 365 PlainTextRange InputMethodController::getSelectionOffsets() const
361 { 366 {
362 RefPtrWillBeRawPtr<Range> range = firstRangeOf(frame().selection().selection ()); 367 RefPtrWillBeRawPtr<Range> range = firstRangeOf(frame().selection().selection ());
363 if (!range) 368 if (!range)
364 return PlainTextRange(); 369 return PlainTextRange();
365 ContainerNode* editable = frame().selection().rootEditableElementOrTreeScope RootNode(); 370 ContainerNode* editable = frame().selection().rootEditableElementOrTreeScope RootNode();
366 ASSERT(editable); 371 ASSERT(editable);
367 return PlainTextRange::create(*editable, *range.get()); 372 return PlainTextRange::create(*editable, *range.get());
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 if (before == 0) 420 if (before == 0)
416 break; 421 break;
417 ++before; 422 ++before;
418 } while (frame().selection().start() == frame().selection().end() && before <= static_cast<int>(selectionOffsets.start())); 423 } while (frame().selection().start() == frame().selection().end() && before <= static_cast<int>(selectionOffsets.start()));
419 TypingCommand::deleteSelection(*frame().document()); 424 TypingCommand::deleteSelection(*frame().document());
420 } 425 }
421 426
422 DEFINE_TRACE(InputMethodController) 427 DEFINE_TRACE(InputMethodController)
423 { 428 {
424 visitor->trace(m_frame); 429 visitor->trace(m_frame);
425 visitor->trace(m_compositionNode); 430 visitor->trace(m_compositionRange);
426 } 431 }
427 432
428 } // namespace blink 433 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698