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

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: Rebase. 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 Element* editableRoot = selection.rootEditableElement(); 160 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; 169 shadowAncestorElement = editableRoot;
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 HTMLElement* ReplacementFragment::insertFragmentForTestRendering(Element* rootEd itableElement) 284 HTMLElement* ReplacementFragment::insertFragmentForTestRendering(Element* rootEd itableElement)
285 { 285 {
286 TRACE_EVENT0("blink", "ReplacementFragment::insertFragmentForTestRendering") ; 286 TRACE_EVENT0("blink", "ReplacementFragment::insertFragmentForTestRendering") ;
287 ASSERT(m_document); 287 DCHECK(m_document);
288 HTMLElement* holder = createDefaultParagraphElement(*m_document.get()); 288 HTMLElement* holder = createDefaultParagraphElement(*m_document.get());
289 289
290 holder->appendChild(m_fragment); 290 holder->appendChild(m_fragment);
291 rootEditableElement->appendChild(holder); 291 rootEditableElement->appendChild(holder);
292 m_document->updateLayoutIgnorePendingStylesheets(); 292 m_document->updateLayoutIgnorePendingStylesheets();
293 293
294 return holder; 294 return holder;
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 EditingStyle* newInlineStyle = EditingStyle::create(inlineStyle); 526 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 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
967 static inline HTMLElement* elementToSplitToAvoidPastingIntoInlineElementsWithSty le(const Position& insertionPos) 967 static inline HTMLElement* elementToSplitToAvoidPastingIntoInlineElementsWithSty le(const Position& insertionPos)
968 { 968 {
969 Element* containingBlock = enclosingBlock(insertionPos.computeContainerNode( )); 969 Element* containingBlock = enclosingBlock(insertionPos.computeContainerNode( ));
970 return toHTMLElement(highestEnclosingNodeOfType(insertionPos, isInlineHTMLEl ementWithStyle, CannotCrossEditingBoundary, containingBlock)); 970 return toHTMLElement(highestEnclosingNodeOfType(insertionPos, isInlineHTMLEl ementWithStyle, CannotCrossEditingBoundary, containingBlock));
971 } 971 }
972 972
973 void ReplaceSelectionCommand::doApply(EditingState* editingState) 973 void ReplaceSelectionCommand::doApply(EditingState* editingState)
974 { 974 {
975 TRACE_EVENT0("blink", "ReplaceSelectionCommand::doApply"); 975 TRACE_EVENT0("blink", "ReplaceSelectionCommand::doApply");
976 const VisibleSelection selection = endingSelection(); 976 const VisibleSelection selection = endingSelection();
977 ASSERT(selection.isCaretOrRange()); 977 DCHECK(selection.isCaretOrRange());
978 ASSERT(selection.start().anchorNode()); 978 DCHECK(selection.start().anchorNode());
979 if (!selection.isNonOrphanedCaretOrRange() || !selection.start().anchorNode( )) 979 if (!selection.isNonOrphanedCaretOrRange() || !selection.start().anchorNode( ))
980 return; 980 return;
981 981
982 if (!selection.rootEditableElement()) 982 if (!selection.rootEditableElement())
983 return; 983 return;
984 984
985 ReplacementFragment fragment(&document(), m_documentFragment.get(), selectio n); 985 ReplacementFragment fragment(&document(), m_documentFragment.get(), selectio n);
986 bool trivialReplaceResult = performTrivialReplace(fragment, editingState); 986 bool trivialReplaceResult = performTrivialReplace(fragment, editingState);
987 if (editingState->isAborted()) 987 if (editingState->isAborted())
988 return; 988 return;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 if (fragment.hasInterchangeNewlineAtStart()) { 1034 if (fragment.hasInterchangeNewlineAtStart()) {
1035 VisiblePosition startAfterDelete = endingSelection().visibleStart(); 1035 VisiblePosition startAfterDelete = endingSelection().visibleStart();
1036 if (isEndOfParagraph(startAfterDelete) && !isStartOfParagraph(startA fterDelete) && !isEndOfEditableOrNonEditableContent(startAfterDelete)) 1036 if (isEndOfParagraph(startAfterDelete) && !isStartOfParagraph(startA fterDelete) && !isEndOfEditableOrNonEditableContent(startAfterDelete))
1037 setEndingSelection(nextPositionOf(startAfterDelete)); 1037 setEndingSelection(nextPositionOf(startAfterDelete));
1038 else 1038 else
1039 insertParagraphSeparator(editingState); 1039 insertParagraphSeparator(editingState);
1040 if (editingState->isAborted()) 1040 if (editingState->isAborted())
1041 return; 1041 return;
1042 } 1042 }
1043 } else { 1043 } else {
1044 ASSERT(selection.isCaret()); 1044 DCHECK(selection.isCaret());
1045 if (fragment.hasInterchangeNewlineAtStart()) { 1045 if (fragment.hasInterchangeNewlineAtStart()) {
1046 const VisiblePosition next = nextPositionOf(visibleStart, CannotCros sEditingBoundary); 1046 const VisiblePosition next = nextPositionOf(visibleStart, CannotCros sEditingBoundary);
1047 if (isEndOfParagraph(visibleStart) && !isStartOfParagraph(visibleSta rt) && next.isNotNull()) 1047 if (isEndOfParagraph(visibleStart) && !isStartOfParagraph(visibleSta rt) && next.isNotNull())
1048 setEndingSelection(next); 1048 setEndingSelection(next);
1049 else 1049 else
1050 insertParagraphSeparator(editingState); 1050 insertParagraphSeparator(editingState);
1051 if (editingState->isAborted()) 1051 if (editingState->isAborted())
1052 return; 1052 return;
1053 } 1053 }
1054 // We split the current paragraph in two to avoid nesting the blocks fro m the fragment inside the current block. 1054 // 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
1067 Position insertionPos = endingSelection().start(); 1067 Position insertionPos = endingSelection().start();
1068 // We don't want any of the pasted content to end up nested in a Mail blockq uote, so first break 1068 // We don't want any of the pasted content to end up nested in a Mail blockq uote, so first break
1069 // out of any surrounding Mail blockquotes. Unless we're inserting in a tabl e, in which case 1069 // out of any surrounding Mail blockquotes. Unless we're inserting in a tabl e, in which case
1070 // breaking the blockquote will prevent the content from actually being inse rted in the table. 1070 // breaking the blockquote will prevent the content from actually being inse rted in the table.
1071 if (enclosingNodeOfType(insertionPos, isMailHTMLBlockquoteElement, CanCrossE ditingBoundary) && m_preventNesting && !(enclosingNodeOfType(insertionPos, &isTa bleStructureNode))) { 1071 if (enclosingNodeOfType(insertionPos, isMailHTMLBlockquoteElement, CanCrossE ditingBoundary) && m_preventNesting && !(enclosingNodeOfType(insertionPos, &isTa bleStructureNode))) {
1072 applyCommandToComposite(BreakBlockquoteCommand::create(document()), edit ingState); 1072 applyCommandToComposite(BreakBlockquoteCommand::create(document()), edit ingState);
1073 if (editingState->isAborted()) 1073 if (editingState->isAborted())
1074 return; 1074 return;
1075 // This will leave a br between the split. 1075 // This will leave a br between the split.
1076 Node* br = endingSelection().start().anchorNode(); 1076 Node* br = endingSelection().start().anchorNode();
1077 ASSERT(isHTMLBRElement(br)); 1077 DCHECK(isHTMLBRElement(br));
yosin_UTC9 2016/04/14 04:35:03 How about adding |<< br|?
1078 // Insert content between the two blockquotes, but remove the br (since it was just a placeholder). 1078 // Insert content between the two blockquotes, but remove the br (since it was just a placeholder).
1079 insertionPos = positionInParentBeforeNode(*br); 1079 insertionPos = positionInParentBeforeNode(*br);
1080 removeNode(br, editingState); 1080 removeNode(br, editingState);
1081 if (editingState->isAborted()) 1081 if (editingState->isAborted())
1082 return; 1082 return;
1083 } 1083 }
1084 1084
1085 // Inserting content could cause whitespace to collapse, e.g. inserting <div >foo</div> into hello^ world. 1085 // Inserting content could cause whitespace to collapse, e.g. inserting <div >foo</div> into hello^ world.
1086 prepareWhitespaceAtPositionForSplit(insertionPos); 1086 prepareWhitespaceAtPositionForSplit(insertionPos);
1087 1087
1088 // If the downstream node has been removed there's no point in continuing. 1088 // If the downstream node has been removed there's no point in continuing.
1089 if (!mostForwardCaretPosition(insertionPos).anchorNode()) 1089 if (!mostForwardCaretPosition(insertionPos).anchorNode())
1090 return; 1090 return;
1091 1091
1092 // NOTE: This would be an incorrect usage of downstream() if downstream() we re changed to mean the last position after 1092 // NOTE: This would be an incorrect usage of downstream() if downstream() we re changed to mean the last position after
1093 // 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 1093 // 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
1094 // away, there are positions after the br which map to the same visible posi tion as [br, 0]). 1094 // away, there are positions after the br which map to the same visible posi tion as [br, 0]).
1095 HTMLBRElement* endBR = isHTMLBRElement(*mostForwardCaretPosition(insertionPo s).anchorNode()) ? toHTMLBRElement(mostForwardCaretPosition(insertionPos).anchor Node()) : 0; 1095 HTMLBRElement* endBR = isHTMLBRElement(*mostForwardCaretPosition(insertionPo s).anchorNode()) ? toHTMLBRElement(mostForwardCaretPosition(insertionPos).anchor Node()) : 0;
1096 VisiblePosition originalVisPosBeforeEndBR; 1096 VisiblePosition originalVisPosBeforeEndBR;
1097 if (endBR) 1097 if (endBR)
1098 originalVisPosBeforeEndBR = previousPositionOf(createVisiblePosition(pos itionBeforeNode(endBR))); 1098 originalVisPosBeforeEndBR = previousPositionOf(createVisiblePosition(pos itionBeforeNode(endBR)));
1099 1099
1100 Element* enclosingBlockOfInsertionPos = enclosingBlock(insertionPos.anchorNo de()); 1100 Element* enclosingBlockOfInsertionPos = enclosingBlock(insertionPos.anchorNo de());
1101 1101
1102 // Adjust insertionPos to prevent nesting. 1102 // Adjust insertionPos to prevent nesting.
1103 // If the start was in a Mail blockquote, we will have already handled adjus ting insertionPos above. 1103 // If the start was in a Mail blockquote, we will have already handled adjus ting insertionPos above.
1104 if (m_preventNesting && enclosingBlockOfInsertionPos && !isTableCell(enclosi ngBlockOfInsertionPos) && !startIsInsideMailBlockquote) { 1104 if (m_preventNesting && enclosingBlockOfInsertionPos && !isTableCell(enclosi ngBlockOfInsertionPos) && !startIsInsideMailBlockquote) {
1105 ASSERT(enclosingBlockOfInsertionPos != currentRoot); 1105 DCHECK_NE(enclosingBlockOfInsertionPos, currentRoot);
1106 VisiblePosition visibleInsertionPos = createVisiblePosition(insertionPos ); 1106 VisiblePosition visibleInsertionPos = createVisiblePosition(insertionPos );
1107 if (isEndOfBlock(visibleInsertionPos) && !(isStartOfBlock(visibleInserti onPos) && fragment.hasInterchangeNewlineAtEnd())) 1107 if (isEndOfBlock(visibleInsertionPos) && !(isStartOfBlock(visibleInserti onPos) && fragment.hasInterchangeNewlineAtEnd()))
1108 insertionPos = positionInParentAfterNode(*enclosingBlockOfInsertionP os); 1108 insertionPos = positionInParentAfterNode(*enclosingBlockOfInsertionP os);
1109 else if (isStartOfBlock(visibleInsertionPos)) 1109 else if (isStartOfBlock(visibleInsertionPos))
1110 insertionPos = positionInParentBeforeNode(*enclosingBlockOfInsertion Pos); 1110 insertionPos = positionInParentBeforeNode(*enclosingBlockOfInsertion Pos);
1111 } 1111 }
1112 1112
1113 // Paste at start or end of link goes outside of link. 1113 // Paste at start or end of link goes outside of link.
1114 insertionPos = positionAvoidingSpecialElementBoundary(insertionPos, editingS tate); 1114 insertionPos = positionAvoidingSpecialElementBoundary(insertionPos, editingS tate);
1115 if (editingState->isAborted()) 1115 if (editingState->isAborted())
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1167 // 3) Merge the start of the added content with the content before the posit ion being pasted into. 1167 // 3) Merge the start of the added content with the content before the posit ion being pasted into.
1168 // 4) Do one of the following: a) expand the last br if the fragment ends wi th one and it collapsed, 1168 // 4) Do one of the following: a) expand the last br if the fragment ends wi th one and it collapsed,
1169 // b) merge the last paragraph of the incoming fragment with the paragraph t hat contained the 1169 // b) merge the last paragraph of the incoming fragment with the paragraph t hat contained the
1170 // end of the selection that was pasted into, or c) handle an interchange ne wline at the end of the 1170 // end of the selection that was pasted into, or c) handle an interchange ne wline at the end of the
1171 // incoming fragment. 1171 // incoming fragment.
1172 // 5) Add spaces for smart replace. 1172 // 5) Add spaces for smart replace.
1173 // 6) Select the replacement if requested, and match style if requested. 1173 // 6) Select the replacement if requested, and match style if requested.
1174 1174
1175 InsertedNodes insertedNodes; 1175 InsertedNodes insertedNodes;
1176 Node* refNode = fragment.firstChild(); 1176 Node* refNode = fragment.firstChild();
1177 ASSERT(refNode); 1177 DCHECK(refNode);
1178 Node* node = refNode->nextSibling(); 1178 Node* node = refNode->nextSibling();
1179 1179
1180 fragment.removeNode(refNode); 1180 fragment.removeNode(refNode);
1181 1181
1182 Element* blockStart = enclosingBlock(insertionPos.anchorNode()); 1182 Element* blockStart = enclosingBlock(insertionPos.anchorNode());
1183 if ((isHTMLListElement(refNode) || (isLegacyAppleHTMLSpanElement(refNode) && isHTMLListElement(refNode->firstChild()))) 1183 if ((isHTMLListElement(refNode) || (isLegacyAppleHTMLSpanElement(refNode) && isHTMLListElement(refNode->firstChild())))
1184 && blockStart && blockStart->layoutObject()->isListItem() && blockStart- >parentNode()->hasEditableStyle()) { 1184 && blockStart && blockStart->layoutObject()->isListItem() && blockStart- >parentNode()->hasEditableStyle()) {
1185 refNode = insertAsListItems(toHTMLElement(refNode), blockStart, insertio nPos, insertedNodes, editingState); 1185 refNode = insertAsListItems(toHTMLElement(refNode), blockStart, insertio nPos, insertedNodes, editingState);
1186 if (editingState->isAborted()) 1186 if (editingState->isAborted())
1187 return; 1187 return;
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
1484 Position start = positionAtStartOfInsertedContent().deepEquivalent(); 1484 Position start = positionAtStartOfInsertedContent().deepEquivalent();
1485 Position end = positionAtEndOfInsertedContent().deepEquivalent(); 1485 Position end = positionAtEndOfInsertedContent().deepEquivalent();
1486 1486
1487 // Mutation events may have deleted start or end 1487 // Mutation events may have deleted start or end
1488 if (start.isNotNull() && !start.isOrphan() && end.isNotNull() && !end.isOrph an()) { 1488 if (start.isNotNull() && !start.isOrphan() && end.isNotNull() && !end.isOrph an()) {
1489 // FIXME (11475): Remove this and require that the creator of the fragme nt to use nbsps. 1489 // FIXME (11475): Remove this and require that the creator of the fragme nt to use nbsps.
1490 rebalanceWhitespaceAt(start); 1490 rebalanceWhitespaceAt(start);
1491 rebalanceWhitespaceAt(end); 1491 rebalanceWhitespaceAt(end);
1492 1492
1493 if (m_matchStyle) { 1493 if (m_matchStyle) {
1494 ASSERT(m_insertionStyle); 1494 DCHECK(m_insertionStyle);
1495 applyStyle(m_insertionStyle.get(), start, end, editingState); 1495 applyStyle(m_insertionStyle.get(), start, end, editingState);
1496 if (editingState->isAborted()) 1496 if (editingState->isAborted())
1497 return; 1497 return;
1498 } 1498 }
1499 1499
1500 if (lastPositionToSelect.isNotNull()) 1500 if (lastPositionToSelect.isNotNull())
1501 end = lastPositionToSelect; 1501 end = lastPositionToSelect;
1502 1502
1503 mergeTextNodesAroundPosition(start, end, editingState); 1503 mergeTextNodesAroundPosition(start, end, editingState);
1504 if (editingState->isAborted()) 1504 if (editingState->isAborted())
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
1709 visitor->trace(m_startOfInsertedContent); 1709 visitor->trace(m_startOfInsertedContent);
1710 visitor->trace(m_endOfInsertedContent); 1710 visitor->trace(m_endOfInsertedContent);
1711 visitor->trace(m_insertionStyle); 1711 visitor->trace(m_insertionStyle);
1712 visitor->trace(m_documentFragment); 1712 visitor->trace(m_documentFragment);
1713 visitor->trace(m_startOfInsertedRange); 1713 visitor->trace(m_startOfInsertedRange);
1714 visitor->trace(m_endOfInsertedRange); 1714 visitor->trace(m_endOfInsertedRange);
1715 CompositeEditCommand::trace(visitor); 1715 CompositeEditCommand::trace(visitor);
1716 } 1716 }
1717 1717
1718 } // namespace blink 1718 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698