Introduce CustomUndoStep.h and CustomUndoStep.cpp
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 23 Jan 2019 06:10:45 +0000 (06:10 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 23 Jan 2019 06:10:45 +0000 (06:10 +0000)
https://bugs.webkit.org/show_bug.cgi?id=193704
<rdar://problem/44807048>

Reviewed by Ryosuke Niwa.

This patch is more work in progress towards supporting `UndoManager.addItem()`. Here, we introduce a helper
class, CustomUndoStep, that holds a weak reference to a script-defined UndoItem. See below for more details.

No change in behavior.

* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:
* editing/CustomUndoStep.cpp:
(WebCore::CustomUndoStep::CustomUndoStep):

Subclass UndoStep.

(WebCore::CustomUndoStep::unapply):
(WebCore::CustomUndoStep::reapply):

If possible, invoke the UndoItem's undo and redo handlers.

(WebCore::CustomUndoStep::isValid const):
* editing/CustomUndoStep.h:
* editing/EditingStyle.cpp:
* editing/InsertEditableImageCommand.cpp:
(WebCore::InsertEditableImageCommand::doApply):

Unified build fixes.

* page/UndoItem.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@240328 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/Sources.txt
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/editing/CustomUndoStep.cpp [new file with mode: 0644]
Source/WebCore/editing/CustomUndoStep.h [new file with mode: 0644]
Source/WebCore/editing/EditingStyle.cpp
Source/WebCore/editing/InsertEditableImageCommand.cpp
Source/WebCore/page/UndoItem.h

index f1739ce..57e0d64 100644 (file)
@@ -1,3 +1,38 @@
+2019-01-22  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Introduce CustomUndoStep.h and CustomUndoStep.cpp
+        https://bugs.webkit.org/show_bug.cgi?id=193704
+        <rdar://problem/44807048>
+
+        Reviewed by Ryosuke Niwa.
+
+        This patch is more work in progress towards supporting `UndoManager.addItem()`. Here, we introduce a helper
+        class, CustomUndoStep, that holds a weak reference to a script-defined UndoItem. See below for more details.
+
+        No change in behavior.
+
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        * editing/CustomUndoStep.cpp:
+        (WebCore::CustomUndoStep::CustomUndoStep):
+
+        Subclass UndoStep.
+
+        (WebCore::CustomUndoStep::unapply):
+        (WebCore::CustomUndoStep::reapply):
+
+        If possible, invoke the UndoItem's undo and redo handlers.
+
+        (WebCore::CustomUndoStep::isValid const):
+        * editing/CustomUndoStep.h:
+        * editing/EditingStyle.cpp:
+        * editing/InsertEditableImageCommand.cpp:
+        (WebCore::InsertEditableImageCommand::doApply):
+
+        Unified build fixes.
+
+        * page/UndoItem.h:
+
 2019-01-22  Simon Fraser  <simon.fraser@apple.com>
 
         Adding a child to a ScrollingStateNode needs to trigger a tree state commit
index 7c48444..d2adb0e 100644 (file)
@@ -952,6 +952,7 @@ editing/BreakBlockquoteCommand.cpp
 editing/ChangeListTypeCommand.cpp
 editing/CompositeEditCommand.cpp
 editing/CreateLinkCommand.cpp
+editing/CustomUndoStep.cpp
 editing/DeleteFromTextNodeCommand.cpp
 editing/DeleteSelectionCommand.cpp
 editing/DictationAlternative.cpp
index d1851eb..dab4df5 100644 (file)
                F4D9817E2195FBF6008230FC /* ChangeListTypeCommand.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ChangeListTypeCommand.cpp; sourceTree = "<group>"; };
                F4E1965A21F2395000285078 /* JSUndoItemCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSUndoItemCustom.cpp; sourceTree = "<group>"; };
                F4E1965F21F26E4E00285078 /* UndoItem.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = UndoItem.cpp; sourceTree = "<group>"; };
+               F4E1966121F27D3C00285078 /* CustomUndoStep.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomUndoStep.h; sourceTree = "<group>"; };
+               F4E1966221F27D3D00285078 /* CustomUndoStep.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CustomUndoStep.cpp; sourceTree = "<group>"; };
                F4E57EDA213F3F5F004EA98E /* FontAttributeChanges.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FontAttributeChanges.h; sourceTree = "<group>"; };
                F4E57EDF213F434A004EA98E /* WebCoreNSFontManagerExtras.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebCoreNSFontManagerExtras.h; sourceTree = "<group>"; };
                F4E57EE0213F434A004EA98E /* WebCoreNSFontManagerExtras.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WebCoreNSFontManagerExtras.mm; sourceTree = "<group>"; };
                                2DD5A7261EBEE47D009BA597 /* CompositionUnderline.h */,
                                D0B0556709C6700100307E43 /* CreateLinkCommand.cpp */,
                                D0B0556609C6700100307E43 /* CreateLinkCommand.h */,
+                               F4E1966221F27D3D00285078 /* CustomUndoStep.cpp */,
+                               F4E1966121F27D3C00285078 /* CustomUndoStep.h */,
                                93309D8F099E64910056E581 /* DeleteFromTextNodeCommand.cpp */,
                                93309D90099E64910056E581 /* DeleteFromTextNodeCommand.h */,
                                93309D91099E64910056E581 /* DeleteSelectionCommand.cpp */,
diff --git a/Source/WebCore/editing/CustomUndoStep.cpp b/Source/WebCore/editing/CustomUndoStep.cpp
new file mode 100644 (file)
index 0000000..917f2aa
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "CustomUndoStep.h"
+
+#include "Document.h"
+#include "UndoItem.h"
+#include "UndoManager.h"
+#include "VoidCallback.h"
+
+namespace WebCore {
+
+CustomUndoStep::CustomUndoStep(UndoItem& item)
+    : m_undoItem(makeWeakPtr(item))
+{
+}
+
+void CustomUndoStep::unapply()
+{
+    if (!isValid())
+        return;
+
+    // FIXME: It's currently unclear how input events should be dispatched when unapplying or reapplying custom
+    // edit commands. Should the page be allowed to specify a target in the DOM for undo and redo?
+    Ref<UndoItem> protectedUndoItem(*m_undoItem);
+    protectedUndoItem->document()->updateLayoutIgnorePendingStylesheets();
+    protectedUndoItem->undoHandler().handleEvent();
+}
+
+void CustomUndoStep::reapply()
+{
+    if (!isValid())
+        return;
+
+    Ref<UndoItem> protectedUndoItem(*m_undoItem);
+    protectedUndoItem->document()->updateLayoutIgnorePendingStylesheets();
+    protectedUndoItem->redoHandler().handleEvent();
+}
+
+bool CustomUndoStep::isValid() const
+{
+    return m_undoItem && m_undoItem->isValid();
+}
+
+} // namespace WebCore
diff --git a/Source/WebCore/editing/CustomUndoStep.h b/Source/WebCore/editing/CustomUndoStep.h
new file mode 100644 (file)
index 0000000..fbce454
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include "UndoStep.h"
+#include <wtf/Ref.h>
+#include <wtf/WeakPtr.h>
+
+namespace WebCore {
+
+class Document;
+class UndoItem;
+
+class CustomUndoStep final : public UndoStep {
+public:
+    static Ref<CustomUndoStep> create(UndoItem& item)
+    {
+        return adoptRef(*new CustomUndoStep(item));
+    }
+
+private:
+    CustomUndoStep(UndoItem&);
+
+    void unapply() final;
+    void reapply() final;
+    EditAction editingAction() const final { return EditAction::Unspecified; }
+
+    bool isValid() const;
+
+    WeakPtr<UndoItem> m_undoItem;
+};
+
+} // namespace WebCore
index b38c150..6bbf014 100644 (file)
@@ -45,6 +45,7 @@
 #include "Node.h"
 #include "NodeTraversal.h"
 #include "QualifiedName.h"
+#include "RenderElement.h"
 #include "RenderStyle.h"
 #include "StyleFontSizeFunctions.h"
 #include "StyleProperties.h"
index 55d1184..d9afa56 100644 (file)
@@ -49,10 +49,10 @@ void InsertEditableImageCommand::doApply()
         return;
 
     m_imageElement = HTMLImageElement::create(document());
-    m_imageElement->setAttributeWithoutSynchronization(x_apple_editable_imageAttr, emptyAtom());
-    m_imageElement->setAttributeWithoutSynchronization(widthAttr, AtomicString("100%", AtomicString::ConstructFromLiteral));
-    m_imageElement->setAttributeWithoutSynchronization(heightAttr, AtomicString("300px", AtomicString::ConstructFromLiteral));
-    m_imageElement->setAttributeWithoutSynchronization(styleAttr, AtomicString("display: block", AtomicString::ConstructFromLiteral));
+    m_imageElement->setAttributeWithoutSynchronization(HTMLNames::x_apple_editable_imageAttr, emptyAtom());
+    m_imageElement->setAttributeWithoutSynchronization(HTMLNames::widthAttr, AtomicString("100%", AtomicString::ConstructFromLiteral));
+    m_imageElement->setAttributeWithoutSynchronization(HTMLNames::heightAttr, AtomicString("300px", AtomicString::ConstructFromLiteral));
+    m_imageElement->setAttributeWithoutSynchronization(HTMLNames::styleAttr, AtomicString("display: block", AtomicString::ConstructFromLiteral));
 
     insertNodeAt(*m_imageElement, endingSelection().start());
     setEndingSelection(visiblePositionAfterNode(*m_imageElement));
index 29c102d..326284a 100644 (file)
@@ -36,7 +36,7 @@ namespace WebCore {
 class Document;
 class UndoManager;
 
-class UndoItem : public RefCounted<UndoItem> {
+class UndoItem : public RefCounted<UndoItem>, public CanMakeWeakPtr<UndoItem> {
     WTF_MAKE_ISO_ALLOCATED(UndoItem);
 public:
     struct Init {