Title: [280323] trunk/Source/WebCore
Revision
280323
Author
[email protected]
Date
2021-07-26 16:54:23 -0700 (Mon, 26 Jul 2021)

Log Message

Deploy smart pointers in ApplyBlockElementCommand, IndentOutdentCommand and InsertListCommand
https://bugs.webkit.org/show_bug.cgi?id=228304

Reviewed by Chris Dumez.

Use RefPtr in more places instead of raw pointers.

* editing/ApplyBlockElementCommand.cpp:
(WebCore::ApplyBlockElementCommand::formatSelection):
(WebCore::isNewLineAtPosition):
(WebCore::ApplyBlockElementCommand::rangeForParagraphSplittingTextNodesIfNeeded):
(WebCore::ApplyBlockElementCommand::endOfNextParagraphSplittingTextNodesIfNeeded):
* editing/IndentOutdentCommand.cpp:
(WebCore::IndentOutdentCommand::tryIndentingAsListItem):
(WebCore::IndentOutdentCommand::indentIntoBlockquote):
(WebCore::IndentOutdentCommand::outdentParagraph):
* editing/InsertListCommand.cpp:
(WebCore::InsertListCommand::mergeWithNeighboringLists):
(WebCore::InsertListCommand::unlistifyParagraph):
(WebCore::adjacentEnclosingList):
(WebCore::InsertListCommand::listifyParagraph):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (280322 => 280323)


--- trunk/Source/WebCore/ChangeLog	2021-07-26 23:51:35 UTC (rev 280322)
+++ trunk/Source/WebCore/ChangeLog	2021-07-26 23:54:23 UTC (rev 280323)
@@ -1,3 +1,27 @@
+2021-07-26  Ryosuke Niwa  <[email protected]>
+
+        Deploy smart pointers in ApplyBlockElementCommand, IndentOutdentCommand and InsertListCommand
+        https://bugs.webkit.org/show_bug.cgi?id=228304
+
+        Reviewed by Chris Dumez.
+
+        Use RefPtr in more places instead of raw pointers.
+
+        * editing/ApplyBlockElementCommand.cpp:
+        (WebCore::ApplyBlockElementCommand::formatSelection):
+        (WebCore::isNewLineAtPosition):
+        (WebCore::ApplyBlockElementCommand::rangeForParagraphSplittingTextNodesIfNeeded):
+        (WebCore::ApplyBlockElementCommand::endOfNextParagraphSplittingTextNodesIfNeeded):
+        * editing/IndentOutdentCommand.cpp:
+        (WebCore::IndentOutdentCommand::tryIndentingAsListItem):
+        (WebCore::IndentOutdentCommand::indentIntoBlockquote):
+        (WebCore::IndentOutdentCommand::outdentParagraph):
+        * editing/InsertListCommand.cpp:
+        (WebCore::InsertListCommand::mergeWithNeighboringLists):
+        (WebCore::InsertListCommand::unlistifyParagraph):
+        (WebCore::adjacentEnclosingList):
+        (WebCore::InsertListCommand::listifyParagraph):
+
 2021-07-26  Kate Cheney  <[email protected]>
 
         ServiceWorkerRegistration.unregister method fails in WKWebView

Modified: trunk/Source/WebCore/editing/ApplyBlockElementCommand.cpp (280322 => 280323)


--- trunk/Source/WebCore/editing/ApplyBlockElementCommand.cpp	2021-07-26 23:51:35 UTC (rev 280322)
+++ trunk/Source/WebCore/editing/ApplyBlockElementCommand.cpp	2021-07-26 23:54:23 UTC (rev 280323)
@@ -147,7 +147,7 @@
         }
 
         Position afterEnd = end.next();
-        Node* enclosingCell = enclosingNodeOfType(start, &isTableCell);
+        RefPtr enclosingCell = enclosingNodeOfType(start, &isTableCell);
         VisiblePosition endOfNextParagraph = endOfNextParagraphSplittingTextNodesIfNeeded(endOfCurrentParagraph, start, end);
 
         formatRange(start, end, m_endOfLastParagraph, blockquoteForNextIndent);
@@ -174,7 +174,7 @@
 
 static bool isNewLineAtPosition(const Position& position)
 {
-    Node* textNode = position.containerNode();
+    RefPtr textNode = position.containerNode();
     unsigned offset = position.offsetInContainerNode();
     if (!is<Text>(textNode) || offset >= downcast<Text>(*textNode).length())
         return false;
@@ -206,25 +206,28 @@
     if (auto* startStyle = renderStyleOfEnclosingTextNode(start)) {
         isStartAndEndOnSameNode = renderStyleOfEnclosingTextNode(end) && start.containerNode() == end.containerNode();
         bool isStartAndEndOfLastParagraphOnSameNode = renderStyleOfEnclosingTextNode(m_endOfLastParagraph) && start.containerNode() == m_endOfLastParagraph.containerNode();
+        bool preservesNewLine = startStyle->preserveNewline();
+        bool collapsesWhiteSpace = startStyle->collapseWhiteSpace();
+        startStyle = nullptr;
 
         // Avoid obtanining the start of next paragraph for start
-        if (startStyle->preserveNewline() && isNewLineAtPosition(start) && !isNewLineAtPosition(start.previous()) && start.offsetInContainerNode() > 0)
+        if (preservesNewLine && isNewLineAtPosition(start) && !isNewLineAtPosition(start.previous()) && start.offsetInContainerNode() > 0)
             start = startOfParagraph(end.previous()).deepEquivalent();
 
         // If start is in the middle of a text node, split.
-        if (!startStyle->collapseWhiteSpace() && start.offsetInContainerNode() > 0) {
+        if (!collapsesWhiteSpace && start.offsetInContainerNode() > 0) {
             int startOffset = start.offsetInContainerNode();
-            Text* startText = start.containerText();
+            RefPtr startText = start.containerText();
             ASSERT(startText);
             splitTextNode(*startText, startOffset);
-            start = firstPositionInNode(startText);
+            start = firstPositionInNode(startText.get());
             if (isStartAndEndOnSameNode) {
                 ASSERT(end.offsetInContainerNode() >= startOffset);
-                end = Position(startText, end.offsetInContainerNode() - startOffset);
+                end = Position(startText.get(), end.offsetInContainerNode() - startOffset);
             }
             if (isStartAndEndOfLastParagraphOnSameNode) {
                 ASSERT(m_endOfLastParagraph.offsetInContainerNode() >= startOffset);
-                m_endOfLastParagraph = Position(startText, m_endOfLastParagraph.offsetInContainerNode() - startOffset);
+                m_endOfLastParagraph = Position(startText.get(), m_endOfLastParagraph.offsetInContainerNode() - startOffset);
             }
         }
     }
@@ -233,7 +236,12 @@
         bool isEndAndEndOfLastParagraphOnSameNode = renderStyleOfEnclosingTextNode(m_endOfLastParagraph) && end.deprecatedNode() == m_endOfLastParagraph.deprecatedNode();
         // Include \n at the end of line if we're at an empty paragraph
         unsigned endOffset = end.offsetInContainerNode();
-        if (endStyle->preserveNewline() && start == end && endOffset < end.containerNode()->length()) {
+        bool preservesNewLine = endStyle->preserveNewline();
+        bool collapseWhiteSpace = endStyle->collapseWhiteSpace();
+        auto userModify = endStyle->userModify();
+        endStyle = nullptr;
+
+        if (preservesNewLine && start == end && endOffset < end.containerNode()->length()) {
             if (!isNewLineAtPosition(end.previous()) && isNewLineAtPosition(end))
                 end = Position(end.containerText(), ++endOffset);
             if (isEndAndEndOfLastParagraphOnSameNode && end.offsetInContainerNode() >= m_endOfLastParagraph.offsetInContainerNode())
@@ -241,7 +249,7 @@
         }
 
         // If end is in the middle of a text node and the text node is editable, split.
-        if (endStyle->userModify() != UserModify::ReadOnly && !endStyle->collapseWhiteSpace() && endOffset && endOffset < end.containerNode()->length()) {
+        if (userModify != UserModify::ReadOnly && !collapseWhiteSpace && endOffset && endOffset < end.containerNode()->length()) {
             RefPtr<Text> endContainer = end.containerText();
             splitTextNode(*endContainer, endOffset);
             if (is<Text>(endContainer) && !endContainer->previousSibling()) {
@@ -266,12 +274,15 @@
 {
     VisiblePosition endOfNextParagraph = endOfParagraph(endOfCurrentParagraph.next());
     Position position = endOfNextParagraph.deepEquivalent();
+
     auto* style = renderStyleOfEnclosingTextNode(position);
     if (!style)
         return endOfNextParagraph;
+    bool preserveNewLine = style->preserveNewline();
+    style = nullptr;
 
     RefPtr<Text> text = position.containerText();
-    if (!style->preserveNewline() || !position.offsetInContainerNode() || !isNewLineAtPosition(firstPositionInNode(text.get())))
+    if (!preserveNewLine || !position.offsetInContainerNode() || !isNewLineAtPosition(firstPositionInNode(text.get())))
         return endOfNextParagraph;
 
     // \n at the beginning of the text node immediately following the current paragraph is trimmed by moveParagraphWithClones.

Modified: trunk/Source/WebCore/editing/IndentOutdentCommand.cpp (280322 => 280323)


--- trunk/Source/WebCore/editing/IndentOutdentCommand.cpp	2021-07-26 23:51:35 UTC (rev 280322)
+++ trunk/Source/WebCore/editing/IndentOutdentCommand.cpp	2021-07-26 23:54:23 UTC (rev 280323)
@@ -58,13 +58,13 @@
 bool IndentOutdentCommand::tryIndentingAsListItem(const Position& start, const Position& end)
 {
     // If our selection is not inside a list, bail out.
-    Node* lastNodeInSelectedParagraph = start.deprecatedNode();
-    RefPtr<Element> listNode = enclosingList(lastNodeInSelectedParagraph);
+    RefPtr lastNodeInSelectedParagraph = start.deprecatedNode();
+    RefPtr<Element> listNode = enclosingList(lastNodeInSelectedParagraph.get());
     if (!listNode)
         return false;
 
     // Find the block that we want to indent.  If it's not a list item (e.g., a div inside a list item), we bail out.
-    RefPtr<Element> selectedListItem = enclosingBlock(lastNodeInSelectedParagraph);
+    RefPtr<Element> selectedListItem = enclosingBlock(lastNodeInSelectedParagraph.get());
 
     if (!selectedListItem || !selectedListItem->hasTagName(liTag))
         return false;
@@ -92,14 +92,14 @@
 
 void IndentOutdentCommand::indentIntoBlockquote(const Position& start, const Position& end, RefPtr<Element>& targetBlockquote)
 {
-    Node* enclosingCell = enclosingNodeOfType(start, &isTableCell);
-    Node* nodeToSplitTo;
-    if (enclosingCell)
-        nodeToSplitTo = enclosingCell;
-    else if (enclosingList(start.containerNode()))
-        nodeToSplitTo = enclosingBlock(start.containerNode());
-    else
-        nodeToSplitTo = editableRootForPosition(start);
+    RefPtr enclosingCell = enclosingNodeOfType(start, &isTableCell);
+    auto nodeToSplitTo = [&]() -> RefPtr<Node> {
+        if (enclosingCell)
+            return enclosingCell;
+        if (enclosingList(start.containerNode()))
+            return enclosingBlock(start.containerNode());
+        return editableRootForPosition(start);
+    }();
 
     if (!nodeToSplitTo)
         return;
@@ -132,7 +132,7 @@
     VisiblePosition visibleStartOfParagraph = startOfParagraph(endingSelection().visibleStart());
     VisiblePosition visibleEndOfParagraph = endOfParagraph(visibleStartOfParagraph);
 
-    auto* enclosingNode = downcast<HTMLElement>(enclosingNodeOfType(visibleStartOfParagraph.deepEquivalent(), &isListOrIndentBlockquote));
+    RefPtr enclosingNode = downcast<HTMLElement>(enclosingNodeOfType(visibleStartOfParagraph.deepEquivalent(), &isListOrIndentBlockquote));
     if (!enclosingNode || !enclosingNode->parentNode()->hasEditableStyle()) // We can't outdent if there is no place to go!
         return;
 
@@ -147,16 +147,16 @@
     }
     
     // The selection is inside a blockquote i.e. enclosingNode is a blockquote
-    VisiblePosition positionInEnclosingBlock = VisiblePosition(firstPositionInNode(enclosingNode));
+    VisiblePosition positionInEnclosingBlock = VisiblePosition(firstPositionInNode(enclosingNode.get()));
     // If the blockquote is inline, the start of the enclosing block coincides with
     // positionInEnclosingBlock.
     VisiblePosition startOfEnclosingBlock = (enclosingNode->renderer() && enclosingNode->renderer()->isInline()) ? positionInEnclosingBlock : startOfBlock(positionInEnclosingBlock);
-    VisiblePosition lastPositionInEnclosingBlock = VisiblePosition(lastPositionInNode(enclosingNode));
+    VisiblePosition lastPositionInEnclosingBlock = VisiblePosition(lastPositionInNode(enclosingNode.get()));
     VisiblePosition endOfEnclosingBlock = endOfBlock(lastPositionInEnclosingBlock);
     if (visibleStartOfParagraph == startOfEnclosingBlock &&
         visibleEndOfParagraph == endOfEnclosingBlock) {
         // The blockquote doesn't contain anything outside the paragraph, so it can be totally removed.
-        Node* splitPoint = enclosingNode->nextSibling();
+        RefPtr splitPoint = enclosingNode->nextSibling();
         removeNodePreservingChildren(*enclosingNode);
         // outdentRegion() assumes it is operating on the first paragraph of an enclosing blockquote, but if there are multiply nested blockquotes and we've
         // just removed one, then this assumption isn't true. By splitting the next containing blockquote after this node, we keep this assumption true
@@ -180,14 +180,14 @@
         return;
     }
 
-    auto* startOfParagraphNode = visibleStartOfParagraph.deepEquivalent().deprecatedNode();
-    auto* enclosingBlockFlow = enclosingBlock(startOfParagraphNode);
+    RefPtr startOfParagraphNode = visibleStartOfParagraph.deepEquivalent().deprecatedNode();
+    RefPtr enclosingBlockFlow = enclosingBlock(startOfParagraphNode.get());
     RefPtr<Node> splitBlockquoteNode = enclosingNode;
     if (enclosingBlockFlow != enclosingNode)
         splitBlockquoteNode = splitTreeToNode(*startOfParagraphNode, *enclosingNode, true);
     else {
         // We split the blockquote at where we start outdenting.
-        auto* highestInlineNode = highestEnclosingNodeOfType(visibleStartOfParagraph.deepEquivalent(), isInline, CannotCrossEditingBoundary, enclosingBlockFlow);
+        RefPtr highestInlineNode = highestEnclosingNodeOfType(visibleStartOfParagraph.deepEquivalent(), isInline, CannotCrossEditingBoundary, enclosingBlockFlow.get());
         splitElement(*enclosingNode, highestInlineNode ? *highestInlineNode : *visibleStartOfParagraph.deepEquivalent().deprecatedNode());
     }
     auto placeholder = HTMLBRElement::create(document());

Modified: trunk/Source/WebCore/editing/InsertListCommand.cpp (280322 => 280323)


--- trunk/Source/WebCore/editing/InsertListCommand.cpp	2021-07-26 23:51:35 UTC (rev 280322)
+++ trunk/Source/WebCore/editing/InsertListCommand.cpp	2021-07-26 23:54:23 UTC (rev 280323)
@@ -74,11 +74,11 @@
 Ref<HTMLElement> InsertListCommand::mergeWithNeighboringLists(HTMLElement& list)
 {
     Ref<HTMLElement> protectedList = list;
-    Element* previousList = list.previousElementSibling();
-    if (canMergeLists(previousList, &list))
+    RefPtr previousList = list.previousElementSibling();
+    if (canMergeLists(previousList.get(), &list))
         mergeIdenticalElements(*previousList, list);
 
-    Element* sibling = ElementTraversal::nextSibling(list);
+    RefPtr sibling = ElementTraversal::nextSibling(list);
     if (!is<HTMLElement>(sibling))
         return protectedList;
 
@@ -277,8 +277,8 @@
 
 void InsertListCommand::unlistifyParagraph(const VisiblePosition& originalStart, HTMLElement* listNode, Node* listChildNode)
 {
-    Node* nextListChild;
-    Node* previousListChild;
+    RefPtr<Node> nextListChild;
+    RefPtr<Node> previousListChild;
     VisiblePosition start;
     VisiblePosition end;
 
@@ -339,21 +339,21 @@
     moveParagraphs(start, end, insertionPoint, true);
 }
 
-static Element* adjacentEnclosingList(const VisiblePosition& pos, const VisiblePosition& adjacentPos, const QualifiedName& listTag)
+static RefPtr<Element> adjacentEnclosingList(const VisiblePosition& pos, const VisiblePosition& adjacentPos, const QualifiedName& listTag)
 {
-    Element* listNode = outermostEnclosingList(adjacentPos.deepEquivalent().deprecatedNode());
+    RefPtr<Element> listNode = outermostEnclosingList(adjacentPos.deepEquivalent().deprecatedNode());
 
     if (!listNode)
-        return 0;
+        return nullptr;
 
-    Node* previousCell = enclosingTableCell(pos.deepEquivalent());
-    Node* currentCell = enclosingTableCell(adjacentPos.deepEquivalent());
+    RefPtr previousCell = enclosingTableCell(pos.deepEquivalent());
+    RefPtr currentCell = enclosingTableCell(adjacentPos.deepEquivalent());
 
     if (!listNode->hasTagName(listTag)
         || listNode->contains(pos.deepEquivalent().deprecatedNode())
         || previousCell != currentCell
-        || enclosingList(listNode) != enclosingList(pos.deepEquivalent().deprecatedNode()))
-        return 0;
+        || enclosingList(listNode.get()) != enclosingList(pos.deepEquivalent().deprecatedNode()))
+        return nullptr;
 
     return listNode;
 }
@@ -372,13 +372,13 @@
     appendNode(placeholder.copyRef(), listItemElement.copyRef());
 
     // Place list item into adjoining lists.
-    Element* previousList = adjacentEnclosingList(start.deepEquivalent(), start.previous(CannotCrossEditingBoundary), listTag);
-    Element* nextList = adjacentEnclosingList(start.deepEquivalent(), end.next(CannotCrossEditingBoundary), listTag);
+    auto previousList = adjacentEnclosingList(start.deepEquivalent(), start.previous(CannotCrossEditingBoundary), listTag);
+    auto nextList = adjacentEnclosingList(start.deepEquivalent(), end.next(CannotCrossEditingBoundary), listTag);
     RefPtr<HTMLElement> listElement;
     if (previousList)
         appendNode(WTFMove(listItemElement), *previousList);
     else if (nextList)
-        insertNodeAt(WTFMove(listItemElement), positionBeforeNode(nextList));
+        insertNodeAt(WTFMove(listItemElement), positionBeforeNode(nextList.get()));
     else {
         // Create the list.
         listElement = createHTMLElement(document(), listTag);
@@ -400,9 +400,9 @@
         // clean markup when inline elements are pushed down as far as possible.
         Position insertionPos(start.deepEquivalent().upstream());
         // Also avoid the containing list item.
-        Node* listChild = enclosingListChild(insertionPos.deprecatedNode());
+        RefPtr listChild = enclosingListChild(insertionPos.deprecatedNode());
         if (listChild && listChild->hasTagName(liTag))
-            insertionPos = positionInParentBeforeNode(listChild);
+            insertionPos = positionInParentBeforeNode(listChild.get());
 
         if (!isEditablePosition(insertionPos))
             return 0;
@@ -425,7 +425,7 @@
     if (listElement)
         return mergeWithNeighboringLists(*listElement);
 
-    if (canMergeLists(previousList, nextList))
+    if (canMergeLists(previousList.get(), nextList.get()))
         mergeIdenticalElements(*previousList, *nextList);
 
     return listElement;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to