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

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

Issue 1878473002: ASSERT -> DCHECK in core/editing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compilation. Created 4 years, 8 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) 2005, 2006, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009, 2010, 2011 Google Inc. All rights reserved. 3 * Copyright (C) 2009, 2010, 2011 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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 , m_hasInterchangeNewlineAtStart(false) 151 , m_hasInterchangeNewlineAtStart(false)
152 , m_hasInterchangeNewlineAtEnd(false) 152 , m_hasInterchangeNewlineAtEnd(false)
153 { 153 {
154 if (!m_document) 154 if (!m_document)
155 return; 155 return;
156 if (!m_fragment || !m_fragment->hasChildren()) 156 if (!m_fragment || !m_fragment->hasChildren())
157 return; 157 return;
158 158
159 TRACE_EVENT0("blink", "ReplacementFragment constructor"); 159 TRACE_EVENT0("blink", "ReplacementFragment constructor");
160 RawPtr<Element> editableRoot = selection.rootEditableElement(); 160 RawPtr<Element> editableRoot = selection.rootEditableElement();
161 ASSERT(editableRoot); 161 DCHECK(editableRoot);
162 if (!editableRoot) 162 if (!editableRoot)
163 return; 163 return;
164 164
165 Element* shadowAncestorElement; 165 Element* shadowAncestorElement;
166 if (editableRoot->isInShadowTree()) 166 if (editableRoot->isInShadowTree())
167 shadowAncestorElement = editableRoot->shadowHost(); 167 shadowAncestorElement = editableRoot->shadowHost();
168 else 168 else
169 shadowAncestorElement = editableRoot.get(); 169 shadowAncestorElement = editableRoot.get();
170 170
171 if (!editableRoot->getAttributeEventListener(EventTypeNames::webkitBeforeTex tInserted) 171 if (!editableRoot->getAttributeEventListener(EventTypeNames::webkitBeforeTex tInserted)
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 ContainerNode* parent = refNode->nonShadowBoundaryParentNode(); 277 ContainerNode* parent = refNode->nonShadowBoundaryParentNode();
278 if (!parent) 278 if (!parent)
279 return; 279 return;
280 280
281 parent->insertBefore(node, refNode); 281 parent->insertBefore(node, refNode);
282 } 282 }
283 283
284 RawPtr<HTMLElement> ReplacementFragment::insertFragmentForTestRendering(Element* rootEditableElement) 284 RawPtr<HTMLElement> ReplacementFragment::insertFragmentForTestRendering(Element* rootEditableElement)
285 { 285 {
286 TRACE_EVENT0("blink", "ReplacementFragment::insertFragmentForTestRendering") ; 286 TRACE_EVENT0("blink", "ReplacementFragment::insertFragmentForTestRendering") ;
287 ASSERT(m_document); 287 DCHECK(m_document);
288 RawPtr<HTMLElement> holder = createDefaultParagraphElement(*m_document.get() ); 288 RawPtr<HTMLElement> holder = createDefaultParagraphElement(*m_document.get() );
289 289
290 holder->appendChild(m_fragment); 290 holder->appendChild(m_fragment);
291 rootEditableElement->appendChild(holder.get()); 291 rootEditableElement->appendChild(holder.get());
292 m_document->updateLayoutIgnorePendingStylesheets(); 292 m_document->updateLayoutIgnorePendingStylesheets();
293 293
294 return holder.release(); 294 return holder.release();
295 } 295 }
296 296
297 void ReplacementFragment::restoreAndRemoveTestRenderingNodesToFragment(Element* holder) 297 void ReplacementFragment::restoreAndRemoveTestRenderingNodesToFragment(Element* holder)
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 continue; 521 continue;
522 522
523 Element* element = toElement(node); 523 Element* element = toElement(node);
524 524
525 const StylePropertySet* inlineStyle = element->inlineStyle(); 525 const StylePropertySet* inlineStyle = element->inlineStyle();
526 RawPtr<EditingStyle> newInlineStyle = EditingStyle::create(inlineStyle); 526 RawPtr<EditingStyle> newInlineStyle = EditingStyle::create(inlineStyle);
527 if (inlineStyle) { 527 if (inlineStyle) {
528 if (element->isHTMLElement()) { 528 if (element->isHTMLElement()) {
529 Vector<QualifiedName> attributes; 529 Vector<QualifiedName> attributes;
530 HTMLElement* htmlElement = toHTMLElement(element); 530 HTMLElement* htmlElement = toHTMLElement(element);
531 ASSERT(htmlElement); 531 DCHECK(htmlElement);
532 532
533 if (newInlineStyle->conflictsWithImplicitStyleOfElement(htmlElem ent)) { 533 if (newInlineStyle->conflictsWithImplicitStyleOfElement(htmlElem ent)) {
534 // e.g. <b style="font-weight: normal;"> is converted to <sp an style="font-weight: normal;"> 534 // e.g. <b style="font-weight: normal;"> is converted to <sp an style="font-weight: normal;">
535 element = replaceElementWithSpanPreservingChildrenAndAttribu tes(htmlElement); 535 element = replaceElementWithSpanPreservingChildrenAndAttribu tes(htmlElement);
536 inlineStyle = element->inlineStyle(); 536 inlineStyle = element->inlineStyle();
537 insertedNodes.didReplaceNode(*htmlElement, *element); 537 insertedNodes.didReplaceNode(*htmlElement, *element);
538 } else if (newInlineStyle->extractConflictingImplicitStyleOfAttr ibutes(htmlElement, EditingStyle::PreserveWritingDirection, 0, attributes, 538 } else if (newInlineStyle->extractConflictingImplicitStyleOfAttr ibutes(htmlElement, EditingStyle::PreserveWritingDirection, 0, attributes,
539 EditingStyle::DoNotExtractMatchingStyle)) { 539 EditingStyle::DoNotExtractMatchingStyle)) {
540 // e.g. <font size="3" style="font-size: 20px;"> is converte d to <font style="font-size: 20px;"> 540 // e.g. <font size="3" style="font-size: 20px;"> is converte d to <font style="font-size: 20px;">
541 for (size_t i = 0; i < attributes.size(); i++) 541 for (size_t i = 0; i < attributes.size(); i++)
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
970 static inline HTMLElement* elementToSplitToAvoidPastingIntoInlineElementsWithSty le(const Position& insertionPos) 970 static inline HTMLElement* elementToSplitToAvoidPastingIntoInlineElementsWithSty le(const Position& insertionPos)
971 { 971 {
972 Element* containingBlock = enclosingBlock(insertionPos.computeContainerNode( )); 972 Element* containingBlock = enclosingBlock(insertionPos.computeContainerNode( ));
973 return toHTMLElement(highestEnclosingNodeOfType(insertionPos, isInlineHTMLEl ementWithStyle, CannotCrossEditingBoundary, containingBlock)); 973 return toHTMLElement(highestEnclosingNodeOfType(insertionPos, isInlineHTMLEl ementWithStyle, CannotCrossEditingBoundary, containingBlock));
974 } 974 }
975 975
976 void ReplaceSelectionCommand::doApply(EditingState* editingState) 976 void ReplaceSelectionCommand::doApply(EditingState* editingState)
977 { 977 {
978 TRACE_EVENT0("blink", "ReplaceSelectionCommand::doApply"); 978 TRACE_EVENT0("blink", "ReplaceSelectionCommand::doApply");
979 const VisibleSelection selection = endingSelection(); 979 const VisibleSelection selection = endingSelection();
980 ASSERT(selection.isCaretOrRange()); 980 DCHECK(selection.isCaretOrRange());
981 ASSERT(selection.start().anchorNode()); 981 DCHECK(selection.start().anchorNode());
982 if (!selection.isNonOrphanedCaretOrRange() || !selection.start().anchorNode( )) 982 if (!selection.isNonOrphanedCaretOrRange() || !selection.start().anchorNode( ))
983 return; 983 return;
984 984
985 if (!selection.rootEditableElement()) 985 if (!selection.rootEditableElement())
986 return; 986 return;
987 987
988 ReplacementFragment fragment(&document(), m_documentFragment.get(), selectio n); 988 ReplacementFragment fragment(&document(), m_documentFragment.get(), selectio n);
989 bool trivialReplaceResult = performTrivialReplace(fragment, editingState); 989 bool trivialReplaceResult = performTrivialReplace(fragment, editingState);
990 if (editingState->isAborted()) 990 if (editingState->isAborted())
991 return; 991 return;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 if (fragment.hasInterchangeNewlineAtStart()) { 1037 if (fragment.hasInterchangeNewlineAtStart()) {
1038 VisiblePosition startAfterDelete = endingSelection().visibleStart(); 1038 VisiblePosition startAfterDelete = endingSelection().visibleStart();
1039 if (isEndOfParagraph(startAfterDelete) && !isStartOfParagraph(startA fterDelete) && !isEndOfEditableOrNonEditableContent(startAfterDelete)) 1039 if (isEndOfParagraph(startAfterDelete) && !isStartOfParagraph(startA fterDelete) && !isEndOfEditableOrNonEditableContent(startAfterDelete))
1040 setEndingSelection(nextPositionOf(startAfterDelete)); 1040 setEndingSelection(nextPositionOf(startAfterDelete));
1041 else 1041 else
1042 insertParagraphSeparator(editingState); 1042 insertParagraphSeparator(editingState);
1043 if (editingState->isAborted()) 1043 if (editingState->isAborted())
1044 return; 1044 return;
1045 } 1045 }
1046 } else { 1046 } else {
1047 ASSERT(selection.isCaret()); 1047 DCHECK(selection.isCaret());
1048 if (fragment.hasInterchangeNewlineAtStart()) { 1048 if (fragment.hasInterchangeNewlineAtStart()) {
1049 const VisiblePosition next = nextPositionOf(visibleStart, CannotCros sEditingBoundary); 1049 const VisiblePosition next = nextPositionOf(visibleStart, CannotCros sEditingBoundary);
1050 if (isEndOfParagraph(visibleStart) && !isStartOfParagraph(visibleSta rt) && next.isNotNull()) 1050 if (isEndOfParagraph(visibleStart) && !isStartOfParagraph(visibleSta rt) && next.isNotNull())
1051 setEndingSelection(next); 1051 setEndingSelection(next);
1052 else 1052 else
1053 insertParagraphSeparator(editingState); 1053 insertParagraphSeparator(editingState);
1054 if (editingState->isAborted()) 1054 if (editingState->isAborted())
1055 return; 1055 return;
1056 } 1056 }
1057 // We split the current paragraph in two to avoid nesting the blocks fro m the fragment inside the current block. 1057 // We split the current paragraph in two to avoid nesting the blocks fro m the fragment inside the current block.
(...skipping 12 matching lines...) Expand all
1070 Position insertionPos = endingSelection().start(); 1070 Position insertionPos = endingSelection().start();
1071 // We don't want any of the pasted content to end up nested in a Mail blockq uote, so first break 1071 // We don't want any of the pasted content to end up nested in a Mail blockq uote, so first break
1072 // out of any surrounding Mail blockquotes. Unless we're inserting in a tabl e, in which case 1072 // out of any surrounding Mail blockquotes. Unless we're inserting in a tabl e, in which case
1073 // breaking the blockquote will prevent the content from actually being inse rted in the table. 1073 // breaking the blockquote will prevent the content from actually being inse rted in the table.
1074 if (enclosingNodeOfType(insertionPos, isMailHTMLBlockquoteElement, CanCrossE ditingBoundary) && m_preventNesting && !(enclosingNodeOfType(insertionPos, &isTa bleStructureNode))) { 1074 if (enclosingNodeOfType(insertionPos, isMailHTMLBlockquoteElement, CanCrossE ditingBoundary) && m_preventNesting && !(enclosingNodeOfType(insertionPos, &isTa bleStructureNode))) {
1075 applyCommandToComposite(BreakBlockquoteCommand::create(document()), edit ingState); 1075 applyCommandToComposite(BreakBlockquoteCommand::create(document()), edit ingState);
1076 if (editingState->isAborted()) 1076 if (editingState->isAborted())
1077 return; 1077 return;
1078 // This will leave a br between the split. 1078 // This will leave a br between the split.
1079 Node* br = endingSelection().start().anchorNode(); 1079 Node* br = endingSelection().start().anchorNode();
1080 ASSERT(isHTMLBRElement(br)); 1080 DCHECK(isHTMLBRElement(br));
1081 // Insert content between the two blockquotes, but remove the br (since it was just a placeholder). 1081 // Insert content between the two blockquotes, but remove the br (since it was just a placeholder).
1082 insertionPos = positionInParentBeforeNode(*br); 1082 insertionPos = positionInParentBeforeNode(*br);
1083 removeNode(br, editingState); 1083 removeNode(br, editingState);
1084 if (editingState->isAborted()) 1084 if (editingState->isAborted())
1085 return; 1085 return;
1086 } 1086 }
1087 1087
1088 // Inserting content could cause whitespace to collapse, e.g. inserting <div >foo</div> into hello^ world. 1088 // Inserting content could cause whitespace to collapse, e.g. inserting <div >foo</div> into hello^ world.
1089 prepareWhitespaceAtPositionForSplit(insertionPos); 1089 prepareWhitespaceAtPositionForSplit(insertionPos);
1090 1090
1091 // If the downstream node has been removed there's no point in continuing. 1091 // If the downstream node has been removed there's no point in continuing.
1092 if (!mostForwardCaretPosition(insertionPos).anchorNode()) 1092 if (!mostForwardCaretPosition(insertionPos).anchorNode())
1093 return; 1093 return;
1094 1094
1095 // NOTE: This would be an incorrect usage of downstream() if downstream() we re changed to mean the last position after 1095 // NOTE: This would be an incorrect usage of downstream() if downstream() we re changed to mean the last position after
1096 // p that maps to the same visible position as p (since in the case where a br is at the end of a block and collapsed 1096 // p that maps to the same visible position as p (since in the case where a br is at the end of a block and collapsed
1097 // away, there are positions after the br which map to the same visible posi tion as [br, 0]). 1097 // away, there are positions after the br which map to the same visible posi tion as [br, 0]).
1098 HTMLBRElement* endBR = isHTMLBRElement(*mostForwardCaretPosition(insertionPo s).anchorNode()) ? toHTMLBRElement(mostForwardCaretPosition(insertionPos).anchor Node()) : 0; 1098 HTMLBRElement* endBR = isHTMLBRElement(*mostForwardCaretPosition(insertionPo s).anchorNode()) ? toHTMLBRElement(mostForwardCaretPosition(insertionPos).anchor Node()) : 0;
1099 VisiblePosition originalVisPosBeforeEndBR; 1099 VisiblePosition originalVisPosBeforeEndBR;
1100 if (endBR) 1100 if (endBR)
1101 originalVisPosBeforeEndBR = previousPositionOf(createVisiblePosition(pos itionBeforeNode(endBR))); 1101 originalVisPosBeforeEndBR = previousPositionOf(createVisiblePosition(pos itionBeforeNode(endBR)));
1102 1102
1103 RawPtr<Element> enclosingBlockOfInsertionPos = enclosingBlock(insertionPos.a nchorNode()); 1103 RawPtr<Element> enclosingBlockOfInsertionPos = enclosingBlock(insertionPos.a nchorNode());
1104 1104
1105 // Adjust insertionPos to prevent nesting. 1105 // Adjust insertionPos to prevent nesting.
1106 // If the start was in a Mail blockquote, we will have already handled adjus ting insertionPos above. 1106 // If the start was in a Mail blockquote, we will have already handled adjus ting insertionPos above.
1107 if (m_preventNesting && enclosingBlockOfInsertionPos && !isTableCell(enclosi ngBlockOfInsertionPos.get()) && !startIsInsideMailBlockquote) { 1107 if (m_preventNesting && enclosingBlockOfInsertionPos && !isTableCell(enclosi ngBlockOfInsertionPos.get()) && !startIsInsideMailBlockquote) {
1108 ASSERT(enclosingBlockOfInsertionPos != currentRoot); 1108 DCHECK_NE(enclosingBlockOfInsertionPos, currentRoot);
1109 VisiblePosition visibleInsertionPos = createVisiblePosition(insertionPos ); 1109 VisiblePosition visibleInsertionPos = createVisiblePosition(insertionPos );
1110 if (isEndOfBlock(visibleInsertionPos) && !(isStartOfBlock(visibleInserti onPos) && fragment.hasInterchangeNewlineAtEnd())) 1110 if (isEndOfBlock(visibleInsertionPos) && !(isStartOfBlock(visibleInserti onPos) && fragment.hasInterchangeNewlineAtEnd()))
1111 insertionPos = positionInParentAfterNode(*enclosingBlockOfInsertionP os); 1111 insertionPos = positionInParentAfterNode(*enclosingBlockOfInsertionP os);
1112 else if (isStartOfBlock(visibleInsertionPos)) 1112 else if (isStartOfBlock(visibleInsertionPos))
1113 insertionPos = positionInParentBeforeNode(*enclosingBlockOfInsertion Pos); 1113 insertionPos = positionInParentBeforeNode(*enclosingBlockOfInsertion Pos);
1114 } 1114 }
1115 1115
1116 // Paste at start or end of link goes outside of link. 1116 // Paste at start or end of link goes outside of link.
1117 insertionPos = positionAvoidingSpecialElementBoundary(insertionPos, editingS tate); 1117 insertionPos = positionAvoidingSpecialElementBoundary(insertionPos, editingS tate);
1118 if (editingState->isAborted()) 1118 if (editingState->isAborted())
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1170 // 3) Merge the start of the added content with the content before the posit ion being pasted into. 1170 // 3) Merge the start of the added content with the content before the posit ion being pasted into.
1171 // 4) Do one of the following: a) expand the last br if the fragment ends wi th one and it collapsed, 1171 // 4) Do one of the following: a) expand the last br if the fragment ends wi th one and it collapsed,
1172 // b) merge the last paragraph of the incoming fragment with the paragraph t hat contained the 1172 // b) merge the last paragraph of the incoming fragment with the paragraph t hat contained the
1173 // end of the selection that was pasted into, or c) handle an interchange ne wline at the end of the 1173 // end of the selection that was pasted into, or c) handle an interchange ne wline at the end of the
1174 // incoming fragment. 1174 // incoming fragment.
1175 // 5) Add spaces for smart replace. 1175 // 5) Add spaces for smart replace.
1176 // 6) Select the replacement if requested, and match style if requested. 1176 // 6) Select the replacement if requested, and match style if requested.
1177 1177
1178 InsertedNodes insertedNodes; 1178 InsertedNodes insertedNodes;
1179 RawPtr<Node> refNode = fragment.firstChild(); 1179 RawPtr<Node> refNode = fragment.firstChild();
1180 ASSERT(refNode); 1180 DCHECK(refNode);
1181 RawPtr<Node> node = refNode->nextSibling(); 1181 RawPtr<Node> node = refNode->nextSibling();
1182 1182
1183 fragment.removeNode(refNode); 1183 fragment.removeNode(refNode);
1184 1184
1185 Element* blockStart = enclosingBlock(insertionPos.anchorNode()); 1185 Element* blockStart = enclosingBlock(insertionPos.anchorNode());
1186 if ((isHTMLListElement(refNode.get()) || (isLegacyAppleHTMLSpanElement(refNo de.get()) && isHTMLListElement(refNode->firstChild()))) 1186 if ((isHTMLListElement(refNode.get()) || (isLegacyAppleHTMLSpanElement(refNo de.get()) && isHTMLListElement(refNode->firstChild())))
1187 && blockStart && blockStart->layoutObject()->isListItem() && blockStart- >parentNode()->hasEditableStyle()) { 1187 && blockStart && blockStart->layoutObject()->isListItem() && blockStart- >parentNode()->hasEditableStyle()) {
1188 refNode = insertAsListItems(toHTMLElement(refNode), blockStart, insertio nPos, insertedNodes, editingState); 1188 refNode = insertAsListItems(toHTMLElement(refNode), blockStart, insertio nPos, insertedNodes, editingState);
1189 if (editingState->isAborted()) 1189 if (editingState->isAborted())
1190 return; 1190 return;
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
1487 Position start = positionAtStartOfInsertedContent().deepEquivalent(); 1487 Position start = positionAtStartOfInsertedContent().deepEquivalent();
1488 Position end = positionAtEndOfInsertedContent().deepEquivalent(); 1488 Position end = positionAtEndOfInsertedContent().deepEquivalent();
1489 1489
1490 // Mutation events may have deleted start or end 1490 // Mutation events may have deleted start or end
1491 if (start.isNotNull() && !start.isOrphan() && end.isNotNull() && !end.isOrph an()) { 1491 if (start.isNotNull() && !start.isOrphan() && end.isNotNull() && !end.isOrph an()) {
1492 // FIXME (11475): Remove this and require that the creator of the fragme nt to use nbsps. 1492 // FIXME (11475): Remove this and require that the creator of the fragme nt to use nbsps.
1493 rebalanceWhitespaceAt(start); 1493 rebalanceWhitespaceAt(start);
1494 rebalanceWhitespaceAt(end); 1494 rebalanceWhitespaceAt(end);
1495 1495
1496 if (m_matchStyle) { 1496 if (m_matchStyle) {
1497 ASSERT(m_insertionStyle); 1497 DCHECK(m_insertionStyle);
1498 applyStyle(m_insertionStyle.get(), start, end, editingState); 1498 applyStyle(m_insertionStyle.get(), start, end, editingState);
1499 if (editingState->isAborted()) 1499 if (editingState->isAborted())
1500 return; 1500 return;
1501 } 1501 }
1502 1502
1503 if (lastPositionToSelect.isNotNull()) 1503 if (lastPositionToSelect.isNotNull())
1504 end = lastPositionToSelect; 1504 end = lastPositionToSelect;
1505 1505
1506 mergeTextNodesAroundPosition(start, end, editingState); 1506 mergeTextNodesAroundPosition(start, end, editingState);
1507 if (editingState->isAborted()) 1507 if (editingState->isAborted())
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
1714 visitor->trace(m_startOfInsertedContent); 1714 visitor->trace(m_startOfInsertedContent);
1715 visitor->trace(m_endOfInsertedContent); 1715 visitor->trace(m_endOfInsertedContent);
1716 visitor->trace(m_insertionStyle); 1716 visitor->trace(m_insertionStyle);
1717 visitor->trace(m_documentFragment); 1717 visitor->trace(m_documentFragment);
1718 visitor->trace(m_startOfInsertedRange); 1718 visitor->trace(m_startOfInsertedRange);
1719 visitor->trace(m_endOfInsertedRange); 1719 visitor->trace(m_endOfInsertedRange);
1720 CompositeEditCommand::trace(visitor); 1720 CompositeEditCommand::trace(visitor);
1721 } 1721 }
1722 1722
1723 } // namespace blink 1723 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698