Title: [293237] trunk/Source
Revision
293237
Author
[email protected]
Date
2022-04-22 12:58:34 -0700 (Fri, 22 Apr 2022)

Log Message

Modernize Text::replaceWholeText() https://bugs.webkit.org/show_bug.cgi?id=239637

Reviewed by Ryosuke Niwa.

Modernize Text::replaceWholeText():
- Drop unnecessary return value.
- Ref the Text node at call sites instead of using an internal protectedThis variable.
- Use node instead of n for variable name.
- Use modern template deduction for Ref / RefPtr.

* Source/WebKitLegacy/mac/DOM/DOMText.mm:
(-[DOMText replaceWholeText:]):
* Source/WebCore/dom/Text.cpp:
(WebCore::Text::replaceWholeText):
* Source/WebCore/dom/Text.h:
* Source/WebCore/html/shadow/DateTimeFieldElement.cpp:
(WebCore::DateTimeFieldElement::updateVisibleValue):

Canonical link: https://commits.webkit.org/249898@main

Modified Paths

Diff

Modified: trunk/Source/WebCore/dom/Text.cpp (293236 => 293237)


--- trunk/Source/WebCore/dom/Text.cpp	2022-04-22 19:02:35 UTC (rev 293236)
+++ trunk/Source/WebCore/dom/Text.cpp	2022-04-22 19:58:34 UTC (rev 293237)
@@ -116,27 +116,24 @@
     return result.toString();
 }
 
-RefPtr<Text> Text::replaceWholeText(const String& newText)
+void Text::replaceWholeText(const String& newText)
 {
-    // Remove all adjacent text nodes, and replace the contents of this one.
-
     // Protect startText and endText against mutation event handlers removing the last ref
-    RefPtr<Text> startText = const_cast<Text*>(earliestLogicallyAdjacentTextNode(this));
-    RefPtr<Text> endText = const_cast<Text*>(latestLogicallyAdjacentTextNode(this));
+    RefPtr startText = const_cast<Text*>(earliestLogicallyAdjacentTextNode(this));
+    RefPtr endText = const_cast<Text*>(latestLogicallyAdjacentTextNode(this));
 
-    RefPtr<Text> protectedThis(this); // Mutation event handlers could cause our last ref to go away
-    RefPtr<ContainerNode> parent = parentNode(); // Protect against mutation handlers moving this node during traversal
-    for (RefPtr<Node> n = startText; n && n != this && n->isTextNode() && n->parentNode() == parent;) {
-        Ref<Node> nodeToRemove(n.releaseNonNull());
-        n = nodeToRemove->nextSibling();
+    RefPtr parent = parentNode(); // Protect against mutation handlers moving this node during traversal
+    for (RefPtr<Node> node = WTFMove(startText); is<Text>(node) && node != this && node->parentNode() == parent;) {
+        auto nodeToRemove = node.releaseNonNull();
+        node = nodeToRemove->nextSibling();
         parent->removeChild(nodeToRemove);
     }
 
     if (this != endText) {
-        Node* _onePastEndText_ = endText->nextSibling();
-        for (RefPtr<Node> n = nextSibling(); n && n != onePastEndText && n->isTextNode() && n->parentNode() == parent;) {
-            Ref<Node> nodeToRemove(n.releaseNonNull());
-            n = nodeToRemove->nextSibling();
+        RefPtr nodePastEndText = endText->nextSibling();
+        for (RefPtr node = nextSibling(); is<Text>(node) && node != nodePastEndText && node->parentNode() == parent;) {
+            auto nodeToRemove = node.releaseNonNull();
+            node = nodeToRemove->nextSibling();
             parent->removeChild(nodeToRemove);
         }
     }
@@ -144,11 +141,10 @@
     if (newText.isEmpty()) {
         if (parent && parentNode() == parent)
             parent->removeChild(*this);
-        return nullptr;
+        return;
     }
 
     setData(newText);
-    return protectedThis;
 }
 
 String Text::nodeName() const

Modified: trunk/Source/WebCore/dom/Text.h (293236 => 293237)


--- trunk/Source/WebCore/dom/Text.h	2022-04-22 19:02:35 UTC (rev 293236)
+++ trunk/Source/WebCore/dom/Text.h	2022-04-22 19:58:34 UTC (rev 293237)
@@ -45,7 +45,7 @@
     // DOM Level 3: http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1312295772
 
     WEBCORE_EXPORT String wholeText() const;
-    WEBCORE_EXPORT RefPtr<Text> replaceWholeText(const String&);
+    WEBCORE_EXPORT void replaceWholeText(const String&);
     
     RenderPtr<RenderText> createTextRenderer(const RenderStyle&);
     

Modified: trunk/Source/WebCore/html/shadow/DateTimeFieldElement.cpp (293236 => 293237)


--- trunk/Source/WebCore/html/shadow/DateTimeFieldElement.cpp	2022-04-22 19:02:35 UTC (rev 293236)
+++ trunk/Source/WebCore/html/shadow/DateTimeFieldElement.cpp	2022-04-22 19:58:34 UTC (rev 293237)
@@ -190,10 +190,10 @@
     if (!firstChild())
         appendChild(Text::create(document(), String { emptyString() }));
 
-    auto& textNode = downcast<Text>(*firstChild());
+    Ref textNode = downcast<Text>(*firstChild());
     String newVisibleValue = visibleValue();
-    if (textNode.wholeText() != newVisibleValue)
-        textNode.replaceWholeText(newVisibleValue);
+    if (textNode->wholeText() != newVisibleValue)
+        textNode->replaceWholeText(newVisibleValue);
 
     if (eventBehavior == DispatchInputAndChangeEvents && m_fieldOwner)
         m_fieldOwner->fieldValueChanged();

Modified: trunk/Source/WebKit/WebProcess/InjectedBundle/API/gtk/DOM/WebKitDOMDeprecated.cpp (293236 => 293237)


--- trunk/Source/WebKit/WebProcess/InjectedBundle/API/gtk/DOM/WebKitDOMDeprecated.cpp	2022-04-22 19:02:35 UTC (rev 293236)
+++ trunk/Source/WebKit/WebProcess/InjectedBundle/API/gtk/DOM/WebKitDOMDeprecated.cpp	2022-04-22 19:58:34 UTC (rev 293237)
@@ -156,7 +156,8 @@
     g_return_val_if_fail(!error || !*error, nullptr);
 
     WebCore::JSMainThreadNullState state;
-    return WebKit::kit(WebKit::core(self)->replaceWholeText(WTF::String::fromUTF8(content)).get());
+    RefPtr { WebKit::core(self) }->replaceWholeText(WTF::String::fromUTF8(content));
+    return self;
 }
 
 gboolean webkit_dom_html_input_element_get_capture(WebKitDOMHTMLInputElement* self)

Modified: trunk/Source/WebKitLegacy/mac/DOM/DOMText.mm (293236 => 293237)


--- trunk/Source/WebKitLegacy/mac/DOM/DOMText.mm	2022-04-22 19:02:35 UTC (rev 293236)
+++ trunk/Source/WebKitLegacy/mac/DOM/DOMText.mm	2022-04-22 19:58:34 UTC (rev 293237)
@@ -53,7 +53,8 @@
 - (DOMText *)replaceWholeText:(NSString *)content
 {
     WebCore::JSMainThreadNullState state;
-    return kit(IMPL->replaceWholeText(content).get());
+    RefPtr { IMPL }->replaceWholeText(content);
+    return self;
 }
 
 @end
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to