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

Side by Side Diff: third_party/WebKit/Source/core/editing/commands/ApplyBlockElementCommand.cpp

Issue 2450213002: Utilize EditCommand::setEndingSelection() taking SelectionInDOMTree (Closed)
Patch Set: 2016-10-27T18:27:07 Created 4 years, 1 month 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, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * Copyright (C) 2010 Google Inc. All rights reserved.
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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 if (visibleEnd.deepEquivalent() != visibleStart.deepEquivalent() && 78 if (visibleEnd.deepEquivalent() != visibleStart.deepEquivalent() &&
79 isStartOfParagraph(visibleEnd)) { 79 isStartOfParagraph(visibleEnd)) {
80 const Position& newEnd = 80 const Position& newEnd =
81 previousPositionOf(visibleEnd, CannotCrossEditingBoundary) 81 previousPositionOf(visibleEnd, CannotCrossEditingBoundary)
82 .deepEquivalent(); 82 .deepEquivalent();
83 SelectionInDOMTree::Builder builder; 83 SelectionInDOMTree::Builder builder;
84 builder.collapse(visibleStart.toPositionWithAffinity()); 84 builder.collapse(visibleStart.toPositionWithAffinity());
85 if (newEnd.isNotNull()) 85 if (newEnd.isNotNull())
86 builder.extend(newEnd); 86 builder.extend(newEnd);
87 builder.setIsDirectional(endingSelection().isDirectional()); 87 builder.setIsDirectional(endingSelection().isDirectional());
88 const VisibleSelection newSelection = 88 const VisibleSelection newSelection =
Xiaocheng 2016/10/28 02:25:26 We can remove |newSelection|, as |builder| builds
89 createVisibleSelection(builder.build()); 89 createVisibleSelection(builder.build());
90 if (newSelection.isNone()) 90 if (newSelection.isNone())
91 return; 91 return;
92 setEndingSelection(newSelection); 92 setEndingSelection(builder.build());
93 } 93 }
94 94
95 VisibleSelection selection = 95 VisibleSelection selection =
96 selectionForParagraphIteration(endingSelection()); 96 selectionForParagraphIteration(endingSelection());
97 VisiblePosition startOfSelection = selection.visibleStart(); 97 VisiblePosition startOfSelection = selection.visibleStart();
98 VisiblePosition endOfSelection = selection.visibleEnd(); 98 VisiblePosition endOfSelection = selection.visibleEnd();
99 DCHECK(!startOfSelection.isNull()); 99 DCHECK(!startOfSelection.isNull());
100 DCHECK(!endOfSelection.isNull()); 100 DCHECK(!endOfSelection.isNull());
101 ContainerNode* startScope = nullptr; 101 ContainerNode* startScope = nullptr;
102 int startIndex = indexForVisiblePosition(startOfSelection, startScope); 102 int startIndex = indexForVisiblePosition(startOfSelection, startScope);
103 ContainerNode* endScope = nullptr; 103 ContainerNode* endScope = nullptr;
104 int endIndex = indexForVisiblePosition(endOfSelection, endScope); 104 int endIndex = indexForVisiblePosition(endOfSelection, endScope);
105 105
106 formatSelection(startOfSelection, endOfSelection, editingState); 106 formatSelection(startOfSelection, endOfSelection, editingState);
107 if (editingState->isAborted()) 107 if (editingState->isAborted())
108 return; 108 return;
109 109
110 document().updateStyleAndLayoutIgnorePendingStylesheets(); 110 document().updateStyleAndLayoutIgnorePendingStylesheets();
111 111
112 DCHECK_EQ(startScope, endScope); 112 DCHECK_EQ(startScope, endScope);
113 DCHECK_GE(startIndex, 0); 113 DCHECK_GE(startIndex, 0);
114 DCHECK_LE(startIndex, endIndex); 114 DCHECK_LE(startIndex, endIndex);
115 if (startScope == endScope && startIndex >= 0 && startIndex <= endIndex) { 115 if (startScope == endScope && startIndex >= 0 && startIndex <= endIndex) {
116 VisiblePosition start(visiblePositionForIndex(startIndex, startScope)); 116 VisiblePosition start(visiblePositionForIndex(startIndex, startScope));
117 VisiblePosition end(visiblePositionForIndex(endIndex, endScope)); 117 VisiblePosition end(visiblePositionForIndex(endIndex, endScope));
118 if (start.isNotNull() && end.isNotNull()) { 118 if (start.isNotNull() && end.isNotNull()) {
119 setEndingSelection(createVisibleSelection( 119 setEndingSelection(
120 SelectionInDOMTree::Builder() 120 SelectionInDOMTree::Builder()
121 .collapse(start.toPositionWithAffinity()) 121 .collapse(start.toPositionWithAffinity())
122 .extend(end.deepEquivalent()) 122 .extend(end.deepEquivalent())
123 .setIsDirectional(endingSelection().isDirectional()) 123 .setIsDirectional(endingSelection().isDirectional())
124 .build())); 124 .build());
125 } 125 }
126 } 126 }
127 } 127 }
128 128
129 static bool isAtUnsplittableElement(const Position& pos) { 129 static bool isAtUnsplittableElement(const Position& pos) {
130 Node* node = pos.anchorNode(); 130 Node* node = pos.anchorNode();
131 return node == rootEditableElementOf(pos) || 131 return node == rootEditableElementOf(pos) ||
132 node == enclosingNodeOfType(pos, &isTableCell); 132 node == enclosingNodeOfType(pos, &isTableCell);
133 } 133 }
134 134
135 void ApplyBlockElementCommand::formatSelection( 135 void ApplyBlockElementCommand::formatSelection(
136 const VisiblePosition& startOfSelection, 136 const VisiblePosition& startOfSelection,
137 const VisiblePosition& endOfSelection, 137 const VisiblePosition& endOfSelection,
138 EditingState* editingState) { 138 EditingState* editingState) {
139 // Special case empty unsplittable elements because there's nothing to split 139 // Special case empty unsplittable elements because there's nothing to split
140 // and there's nothing to move. 140 // and there's nothing to move.
141 Position start = mostForwardCaretPosition(startOfSelection.deepEquivalent()); 141 Position start = mostForwardCaretPosition(startOfSelection.deepEquivalent());
142 if (isAtUnsplittableElement(start)) { 142 if (isAtUnsplittableElement(start)) {
143 HTMLElement* blockquote = createBlockElement(); 143 HTMLElement* blockquote = createBlockElement();
144 insertNodeAt(blockquote, start, editingState); 144 insertNodeAt(blockquote, start, editingState);
145 if (editingState->isAborted()) 145 if (editingState->isAborted())
146 return; 146 return;
147 HTMLBRElement* placeholder = HTMLBRElement::create(document()); 147 HTMLBRElement* placeholder = HTMLBRElement::create(document());
148 appendNode(placeholder, blockquote, editingState); 148 appendNode(placeholder, blockquote, editingState);
149 if (editingState->isAborted()) 149 if (editingState->isAborted())
150 return; 150 return;
151 document().updateStyleAndLayoutIgnorePendingStylesheets(); 151 setEndingSelection(SelectionInDOMTree::Builder()
152 setEndingSelection(createVisibleSelection( 152 .collapse(Position::beforeNode(placeholder))
153 SelectionInDOMTree::Builder() 153 .setIsDirectional(endingSelection().isDirectional())
154 .collapse(Position::beforeNode(placeholder)) 154 .build());
155 .setIsDirectional(endingSelection().isDirectional())
156 .build()));
157 return; 155 return;
158 } 156 }
159 157
160 HTMLElement* blockquoteForNextIndent = nullptr; 158 HTMLElement* blockquoteForNextIndent = nullptr;
161 VisiblePosition endOfCurrentParagraph = endOfParagraph(startOfSelection); 159 VisiblePosition endOfCurrentParagraph = endOfParagraph(startOfSelection);
162 VisiblePosition endOfLastParagraph = endOfParagraph(endOfSelection); 160 VisiblePosition endOfLastParagraph = endOfParagraph(endOfSelection);
163 Position endAfterSelection = 161 Position endAfterSelection =
164 endOfParagraph(nextPositionOf(endOfLastParagraph)).deepEquivalent(); 162 endOfParagraph(nextPositionOf(endOfLastParagraph)).deepEquivalent();
165 m_endOfLastParagraph = endOfLastParagraph.deepEquivalent(); 163 m_endOfLastParagraph = endOfLastParagraph.deepEquivalent();
166 164
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 element->setAttribute(styleAttr, m_inlineStyle); 392 element->setAttribute(styleAttr, m_inlineStyle);
395 return element; 393 return element;
396 } 394 }
397 395
398 DEFINE_TRACE(ApplyBlockElementCommand) { 396 DEFINE_TRACE(ApplyBlockElementCommand) {
399 visitor->trace(m_endOfLastParagraph); 397 visitor->trace(m_endOfLastParagraph);
400 CompositeEditCommand::trace(visitor); 398 CompositeEditCommand::trace(visitor);
401 } 399 }
402 400
403 } // namespace blink 401 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698