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

Side by Side Diff: Source/core/editing/commands/CompositeEditCommand.cpp

Issue 1302353002: Get rid of redundant member function PositionAlgorithm<Strategy>::{upstream,downstream} (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: 2015-08-22T04:51:20 Rebase Created 5 years, 4 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, 2007, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 return; 693 return;
694 Text* textNode = toText(node); 694 Text* textNode = toText(node);
695 695
696 if (textNode->length() == 0) 696 if (textNode->length() == 0)
697 return; 697 return;
698 LayoutText* layoutText = textNode->layoutObject(); 698 LayoutText* layoutText = textNode->layoutObject();
699 if (layoutText && !layoutText->style()->collapseWhiteSpace()) 699 if (layoutText && !layoutText->style()->collapseWhiteSpace())
700 return; 700 return;
701 701
702 // Delete collapsed whitespace so that inserting nbsps doesn't uncollapse it . 702 // Delete collapsed whitespace so that inserting nbsps doesn't uncollapse it .
703 Position upstreamPos = position.upstream(); 703 Position upstreamPos = mostBackwardCaretPosition(position);
704 deleteInsignificantText(upstreamPos, position.downstream()); 704 deleteInsignificantText(upstreamPos, mostForwardCaretPosition(position));
705 position = upstreamPos.downstream(); 705 position = mostForwardCaretPosition(upstreamPos);
706 706
707 VisiblePosition visiblePos(position); 707 VisiblePosition visiblePos(position);
708 VisiblePosition previousVisiblePos(visiblePos.previous()); 708 VisiblePosition previousVisiblePos(visiblePos.previous());
709 replaceCollapsibleWhitespaceWithNonBreakingSpaceIfNeeded(previousVisiblePos) ; 709 replaceCollapsibleWhitespaceWithNonBreakingSpaceIfNeeded(previousVisiblePos) ;
710 replaceCollapsibleWhitespaceWithNonBreakingSpaceIfNeeded(visiblePos); 710 replaceCollapsibleWhitespaceWithNonBreakingSpaceIfNeeded(visiblePos);
711 } 711 }
712 712
713 void CompositeEditCommand::replaceCollapsibleWhitespaceWithNonBreakingSpaceIfNee ded(const VisiblePosition& visiblePosition) 713 void CompositeEditCommand::replaceCollapsibleWhitespaceWithNonBreakingSpaceIfNee ded(const VisiblePosition& visiblePosition)
714 { 714 {
715 if (!isCollapsibleWhitespace(visiblePosition.characterAfter())) 715 if (!isCollapsibleWhitespace(visiblePosition.characterAfter()))
716 return; 716 return;
717 Position pos = visiblePosition.deepEquivalent().downstream(); 717 Position pos = mostForwardCaretPosition(visiblePosition.deepEquivalent());
718 if (!pos.computeContainerNode() || !pos.computeContainerNode()->isTextNode() ) 718 if (!pos.computeContainerNode() || !pos.computeContainerNode()->isTextNode() )
719 return; 719 return;
720 replaceTextInNodePreservingMarkers(toText(pos.computeContainerNode()), pos.o ffsetInContainerNode(), 1, nonBreakingSpaceString()); 720 replaceTextInNodePreservingMarkers(toText(pos.computeContainerNode()), pos.o ffsetInContainerNode(), 1, nonBreakingSpaceString());
721 } 721 }
722 722
723 void CompositeEditCommand::rebalanceWhitespace() 723 void CompositeEditCommand::rebalanceWhitespace()
724 { 724 {
725 VisibleSelection selection = endingSelection(); 725 VisibleSelection selection = endingSelection();
726 if (selection.isNone()) 726 if (selection.isNone())
727 return; 727 return;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 for (const auto& node : nodes) { 831 for (const auto& node : nodes) {
832 Text* textNode = node.get(); 832 Text* textNode = node.get();
833 int startOffset = textNode == start.anchorNode() ? start.computeOffsetIn ContainerNode() : 0; 833 int startOffset = textNode == start.anchorNode() ? start.computeOffsetIn ContainerNode() : 0;
834 int endOffset = textNode == end.anchorNode() ? end.computeOffsetInContai nerNode() : static_cast<int>(textNode->length()); 834 int endOffset = textNode == end.anchorNode() ? end.computeOffsetInContai nerNode() : static_cast<int>(textNode->length());
835 deleteInsignificantText(textNode, startOffset, endOffset); 835 deleteInsignificantText(textNode, startOffset, endOffset);
836 } 836 }
837 } 837 }
838 838
839 void CompositeEditCommand::deleteInsignificantTextDownstream(const Position& pos ) 839 void CompositeEditCommand::deleteInsignificantTextDownstream(const Position& pos )
840 { 840 {
841 Position end = VisiblePosition(pos, VP_DEFAULT_AFFINITY).next().deepEquivale nt().downstream(); 841 Position end = mostForwardCaretPosition(VisiblePosition(pos, VP_DEFAULT_AFFI NITY).next().deepEquivalent());
842 deleteInsignificantText(pos, end); 842 deleteInsignificantText(pos, end);
843 } 843 }
844 844
845 PassRefPtrWillBeRawPtr<HTMLBRElement> CompositeEditCommand::appendBlockPlacehold er(PassRefPtrWillBeRawPtr<Element> container) 845 PassRefPtrWillBeRawPtr<HTMLBRElement> CompositeEditCommand::appendBlockPlacehold er(PassRefPtrWillBeRawPtr<Element> container)
846 { 846 {
847 if (!container) 847 if (!container)
848 return nullptr; 848 return nullptr;
849 849
850 document().updateLayoutIgnorePendingStylesheets(); 850 document().updateLayoutIgnorePendingStylesheets();
851 851
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
919 ASSERT(isEditablePosition(pos, ContentIsEditable, DoNotUpdateStyle)); 919 ASSERT(isEditablePosition(pos, ContentIsEditable, DoNotUpdateStyle));
920 920
921 // It's strange that this function is responsible for verifying that pos has not been invalidated 921 // It's strange that this function is responsible for verifying that pos has not been invalidated
922 // by an earlier call to this function. The caller, applyBlockStyle, should do this. 922 // by an earlier call to this function. The caller, applyBlockStyle, should do this.
923 VisiblePosition visiblePos(pos, VP_DEFAULT_AFFINITY); 923 VisiblePosition visiblePos(pos, VP_DEFAULT_AFFINITY);
924 VisiblePosition visibleParagraphStart(startOfParagraph(visiblePos)); 924 VisiblePosition visibleParagraphStart(startOfParagraph(visiblePos));
925 VisiblePosition visibleParagraphEnd = endOfParagraph(visiblePos); 925 VisiblePosition visibleParagraphEnd = endOfParagraph(visiblePos);
926 VisiblePosition next = visibleParagraphEnd.next(); 926 VisiblePosition next = visibleParagraphEnd.next();
927 VisiblePosition visibleEnd = next.isNotNull() ? next : visibleParagraphEnd; 927 VisiblePosition visibleEnd = next.isNotNull() ? next : visibleParagraphEnd;
928 928
929 Position upstreamStart = visibleParagraphStart.deepEquivalent().upstream(); 929 Position upstreamStart = mostBackwardCaretPosition(visibleParagraphStart.dee pEquivalent());
930 Position upstreamEnd = visibleEnd.deepEquivalent().upstream(); 930 Position upstreamEnd = mostBackwardCaretPosition(visibleEnd.deepEquivalent() );
931 931
932 // If there are no VisiblePositions in the same block as pos then 932 // If there are no VisiblePositions in the same block as pos then
933 // upstreamStart will be outside the paragraph 933 // upstreamStart will be outside the paragraph
934 if (comparePositions(pos, upstreamStart) < 0) 934 if (comparePositions(pos, upstreamStart) < 0)
935 return nullptr; 935 return nullptr;
936 936
937 // Perform some checks to see if we need to perform work in this function. 937 // Perform some checks to see if we need to perform work in this function.
938 if (isBlock(upstreamStart.anchorNode())) { 938 if (isBlock(upstreamStart.anchorNode())) {
939 // If the block is the root editable element, always move content to a n ew block, 939 // If the block is the root editable element, always move content to a n ew block,
940 // since it is illegal to modify attributes on the root editable element for editing. 940 // since it is illegal to modify attributes on the root editable element for editing.
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
1077 // before and after the table/list collapse onto one line. 1077 // before and after the table/list collapse onto one line.
1078 // Deleting a paragraph will leave a placeholder. Remove it (and prune 1078 // Deleting a paragraph will leave a placeholder. Remove it (and prune
1079 // empty or unrendered parents). 1079 // empty or unrendered parents).
1080 1080
1081 void CompositeEditCommand::cleanupAfterDeletion(VisiblePosition destination) 1081 void CompositeEditCommand::cleanupAfterDeletion(VisiblePosition destination)
1082 { 1082 {
1083 VisiblePosition caretAfterDelete = endingSelection().visibleStart(); 1083 VisiblePosition caretAfterDelete = endingSelection().visibleStart();
1084 Node* destinationNode = destination.deepEquivalent().anchorNode(); 1084 Node* destinationNode = destination.deepEquivalent().anchorNode();
1085 if (caretAfterDelete.deepEquivalent() != destination.deepEquivalent() && isS tartOfParagraph(caretAfterDelete) && isEndOfParagraph(caretAfterDelete)) { 1085 if (caretAfterDelete.deepEquivalent() != destination.deepEquivalent() && isS tartOfParagraph(caretAfterDelete) && isEndOfParagraph(caretAfterDelete)) {
1086 // Note: We want the rightmost candidate. 1086 // Note: We want the rightmost candidate.
1087 Position position = caretAfterDelete.deepEquivalent().downstream(); 1087 Position position = mostForwardCaretPosition(caretAfterDelete.deepEquiva lent());
1088 Node* node = position.anchorNode(); 1088 Node* node = position.anchorNode();
1089 1089
1090 // Bail if we'd remove an ancestor of our destination. 1090 // Bail if we'd remove an ancestor of our destination.
1091 if (destinationNode && destinationNode->isDescendantOf(node)) 1091 if (destinationNode && destinationNode->isDescendantOf(node))
1092 return; 1092 return;
1093 1093
1094 // Normally deletion will leave a br as a placeholder. 1094 // Normally deletion will leave a br as a placeholder.
1095 if (isHTMLBRElement(*node)) { 1095 if (isHTMLBRElement(*node)) {
1096 removeNodeAndPruneAncestors(node, destinationNode); 1096 removeNodeAndPruneAncestors(node, destinationNode);
1097 1097
(...skipping 30 matching lines...) Expand all
1128 void CompositeEditCommand::moveParagraphWithClones(const VisiblePosition& startO fParagraphToMove, const VisiblePosition& endOfParagraphToMove, HTMLElement* bloc kElement, Node* outerNode) 1128 void CompositeEditCommand::moveParagraphWithClones(const VisiblePosition& startO fParagraphToMove, const VisiblePosition& endOfParagraphToMove, HTMLElement* bloc kElement, Node* outerNode)
1129 { 1129 {
1130 ASSERT(outerNode); 1130 ASSERT(outerNode);
1131 ASSERT(blockElement); 1131 ASSERT(blockElement);
1132 1132
1133 VisiblePosition beforeParagraph = startOfParagraphToMove.previous(); 1133 VisiblePosition beforeParagraph = startOfParagraphToMove.previous();
1134 VisiblePosition afterParagraph(endOfParagraphToMove.next()); 1134 VisiblePosition afterParagraph(endOfParagraphToMove.next());
1135 1135
1136 // We upstream() the end and downstream() the start so that we don't include collapsed whitespace in the move. 1136 // We upstream() the end and downstream() the start so that we don't include collapsed whitespace in the move.
1137 // When we paste a fragment, spaces after the end and before the start are t reated as though they were rendered. 1137 // When we paste a fragment, spaces after the end and before the start are t reated as though they were rendered.
1138 Position start = startOfParagraphToMove.deepEquivalent().downstream(); 1138 Position start = mostForwardCaretPosition(startOfParagraphToMove.deepEquival ent());
1139 Position end = startOfParagraphToMove.deepEquivalent() == endOfParagraphToMo ve.deepEquivalent() ? start : endOfParagraphToMove.deepEquivalent().upstream(); 1139 Position end = startOfParagraphToMove.deepEquivalent() == endOfParagraphToMo ve.deepEquivalent() ? start : mostBackwardCaretPosition(endOfParagraphToMove.dee pEquivalent());
1140 if (comparePositions(start, end) > 0) 1140 if (comparePositions(start, end) > 0)
1141 end = start; 1141 end = start;
1142 1142
1143 cloneParagraphUnderNewElement(start, end, outerNode, blockElement); 1143 cloneParagraphUnderNewElement(start, end, outerNode, blockElement);
1144 1144
1145 setEndingSelection(VisibleSelection(start, end)); 1145 setEndingSelection(VisibleSelection(start, end));
1146 deleteSelection(false, false, false); 1146 deleteSelection(false, false, false);
1147 1147
1148 // There are bugs in deletion when it removes a fully selected table/list. 1148 // There are bugs in deletion when it removes a fully selected table/list.
1149 // It expands and removes the entire table/list, but will let content 1149 // It expands and removes the entire table/list, but will let content
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 if (endInParagraph) 1203 if (endInParagraph)
1204 endIndex = TextIterator::rangeLength(startOfParagraphToMove.toPa rentAnchoredPosition(), visibleEnd.toParentAnchoredPosition(), true); 1204 endIndex = TextIterator::rangeLength(startOfParagraphToMove.toPa rentAnchoredPosition(), visibleEnd.toParentAnchoredPosition(), true);
1205 } 1205 }
1206 } 1206 }
1207 1207
1208 VisiblePosition beforeParagraph = startOfParagraphToMove.previous(CannotCros sEditingBoundary); 1208 VisiblePosition beforeParagraph = startOfParagraphToMove.previous(CannotCros sEditingBoundary);
1209 VisiblePosition afterParagraph(endOfParagraphToMove.next(CannotCrossEditingB oundary)); 1209 VisiblePosition afterParagraph(endOfParagraphToMove.next(CannotCrossEditingB oundary));
1210 1210
1211 // We upstream() the end and downstream() the start so that we don't include collapsed whitespace in the move. 1211 // We upstream() the end and downstream() the start so that we don't include collapsed whitespace in the move.
1212 // When we paste a fragment, spaces after the end and before the start are t reated as though they were rendered. 1212 // When we paste a fragment, spaces after the end and before the start are t reated as though they were rendered.
1213 Position start = startOfParagraphToMove.deepEquivalent().downstream(); 1213 Position start = mostForwardCaretPosition(startOfParagraphToMove.deepEquival ent());
1214 Position end = endOfParagraphToMove.deepEquivalent().upstream(); 1214 Position end = mostBackwardCaretPosition(endOfParagraphToMove.deepEquivalent ());
1215 1215
1216 // FIXME: This is an inefficient way to preserve style on nodes in the parag raph to move. It 1216 // FIXME: This is an inefficient way to preserve style on nodes in the parag raph to move. It
1217 // shouldn't matter though, since moved paragraphs will usually be quite sma ll. 1217 // shouldn't matter though, since moved paragraphs will usually be quite sma ll.
1218 RefPtrWillBeRawPtr<DocumentFragment> fragment = startOfParagraphToMove.deepE quivalent() != endOfParagraphToMove.deepEquivalent() ? 1218 RefPtrWillBeRawPtr<DocumentFragment> fragment = startOfParagraphToMove.deepE quivalent() != endOfParagraphToMove.deepEquivalent() ?
1219 createFragmentFromMarkup(document(), createMarkup(start.parentAnchoredEq uivalent(), end.parentAnchoredEquivalent(), DoNotAnnotateForInterchange, Convert BlocksToInlines::Convert, DoNotResolveURLs, constrainingAncestor), "") : nullptr ; 1219 createFragmentFromMarkup(document(), createMarkup(start.parentAnchoredEq uivalent(), end.parentAnchoredEquivalent(), DoNotAnnotateForInterchange, Convert BlocksToInlines::Convert, DoNotResolveURLs, constrainingAncestor), "") : nullptr ;
1220 1220
1221 // A non-empty paragraph's style is moved when we copy and move it. We don' t move 1221 // A non-empty paragraph's style is moved when we copy and move it. We don' t move
1222 // anything if we're given an empty paragraph, but an empty paragraph can ha ve style 1222 // anything if we're given an empty paragraph, but an empty paragraph can ha ve style
1223 // too, <div><b><br></b></div> for example. Save it so that we can preserve it later. 1223 // too, <div><b><br></b></div> for example. Save it so that we can preserve it later.
1224 RefPtrWillBeRawPtr<EditingStyle> styleInEmptyParagraph = nullptr; 1224 RefPtrWillBeRawPtr<EditingStyle> styleInEmptyParagraph = nullptr;
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
1385 // If the br we inserted collapsed, for example foo<br><blockquote>...</bloc kquote>, insert 1385 // If the br we inserted collapsed, for example foo<br><blockquote>...</bloc kquote>, insert
1386 // a second one. 1386 // a second one.
1387 if (!isStartOfParagraph(atBR)) 1387 if (!isStartOfParagraph(atBR))
1388 insertNodeBefore(createBreakElement(document()), br); 1388 insertNodeBefore(createBreakElement(document()), br);
1389 setEndingSelection(VisibleSelection(atBR, endingSelection().isDirectional()) ); 1389 setEndingSelection(VisibleSelection(atBR, endingSelection().isDirectional()) );
1390 1390
1391 // If this is an empty paragraph there must be a line break here. 1391 // If this is an empty paragraph there must be a line break here.
1392 if (!lineBreakExistsAtVisiblePosition(caret)) 1392 if (!lineBreakExistsAtVisiblePosition(caret))
1393 return false; 1393 return false;
1394 1394
1395 Position caretPos(caret.deepEquivalent().downstream()); 1395 Position caretPos(mostForwardCaretPosition(caret.deepEquivalent()));
1396 // A line break is either a br or a preserved newline. 1396 // A line break is either a br or a preserved newline.
1397 ASSERT(isHTMLBRElement(caretPos.anchorNode()) || (caretPos.anchorNode()->isT extNode() && caretPos.anchorNode()->layoutObject()->style()->preserveNewline())) ; 1397 ASSERT(isHTMLBRElement(caretPos.anchorNode()) || (caretPos.anchorNode()->isT extNode() && caretPos.anchorNode()->layoutObject()->style()->preserveNewline())) ;
1398 1398
1399 if (isHTMLBRElement(*caretPos.anchorNode())) { 1399 if (isHTMLBRElement(*caretPos.anchorNode())) {
1400 removeNodeAndPruneAncestors(caretPos.anchorNode()); 1400 removeNodeAndPruneAncestors(caretPos.anchorNode());
1401 } else if (caretPos.anchorNode()->isTextNode()) { 1401 } else if (caretPos.anchorNode()->isTextNode()) {
1402 ASSERT(caretPos.computeOffsetInContainerNode() == 0); 1402 ASSERT(caretPos.computeOffsetInContainerNode() == 0);
1403 Text* textNode = toText(caretPos.anchorNode()); 1403 Text* textNode = toText(caretPos.anchorNode());
1404 ContainerNode* parentNode = textNode->parentNode(); 1404 ContainerNode* parentNode = textNode->parentNode();
1405 // The preserved newline must be the first thing in the node, since othe rwise the previous 1405 // The preserved newline must be the first thing in the node, since othe rwise the previous
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1437 // Make sure anchors are pushed down before avoiding them so that we don't 1437 // Make sure anchors are pushed down before avoiding them so that we don't
1438 // also avoid structural elements like lists and blocks (5142012). 1438 // also avoid structural elements like lists and blocks (5142012).
1439 if (original.anchorNode() != enclosingAnchor && original.anchorNode( )->parentNode() != enclosingAnchor) { 1439 if (original.anchorNode() != enclosingAnchor && original.anchorNode( )->parentNode() != enclosingAnchor) {
1440 pushAnchorElementDown(enclosingAnchor); 1440 pushAnchorElementDown(enclosingAnchor);
1441 enclosingAnchor = enclosingAnchorElement(original); 1441 enclosingAnchor = enclosingAnchorElement(original);
1442 if (!enclosingAnchor) 1442 if (!enclosingAnchor)
1443 return original; 1443 return original;
1444 } 1444 }
1445 // Don't insert outside an anchor if doing so would skip over a line break. It would 1445 // Don't insert outside an anchor if doing so would skip over a line break. It would
1446 // probably be safe to move the line break so that we could still av oid the anchor here. 1446 // probably be safe to move the line break so that we could still av oid the anchor here.
1447 Position downstream(visiblePos.deepEquivalent().downstream()); 1447 Position downstream(mostForwardCaretPosition(visiblePos.deepEquivale nt()));
1448 if (lineBreakExistsAtVisiblePosition(visiblePos) && downstream.ancho rNode()->isDescendantOf(enclosingAnchor)) 1448 if (lineBreakExistsAtVisiblePosition(visiblePos) && downstream.ancho rNode()->isDescendantOf(enclosingAnchor))
1449 return original; 1449 return original;
1450 1450
1451 result = positionInParentAfterNode(*enclosingAnchor); 1451 result = positionInParentAfterNode(*enclosingAnchor);
1452 } 1452 }
1453 // If visually just before an anchor, insert *outside* the anchor unless it's the first 1453 // If visually just before an anchor, insert *outside* the anchor unless it's the first
1454 // VisiblePosition in a paragraph, to match NSTextView. 1454 // VisiblePosition in a paragraph, to match NSTextView.
1455 if (visiblePos.deepEquivalent() == firstInAnchor.deepEquivalent()) { 1455 if (visiblePos.deepEquivalent() == firstInAnchor.deepEquivalent()) {
1456 // Make sure anchors are pushed down before avoiding them so that we don't 1456 // Make sure anchors are pushed down before avoiding them so that we don't
1457 // also avoid structural elements like lists and blocks (5142012). 1457 // also avoid structural elements like lists and blocks (5142012).
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1502 } 1502 }
1503 1503
1504 DEFINE_TRACE(CompositeEditCommand) 1504 DEFINE_TRACE(CompositeEditCommand)
1505 { 1505 {
1506 visitor->trace(m_commands); 1506 visitor->trace(m_commands);
1507 visitor->trace(m_composition); 1507 visitor->trace(m_composition);
1508 EditCommand::trace(visitor); 1508 EditCommand::trace(visitor);
1509 } 1509 }
1510 1510
1511 } // namespace blink 1511 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/editing/commands/BreakBlockquoteCommand.cpp ('k') | Source/core/editing/commands/DeleteSelectionCommand.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698