Provide Obj-C private API to simplify markup.
authorenrica@apple.com <enrica@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 6 Apr 2012 22:44:02 +0000 (22:44 +0000)
committerenrica@apple.com <enrica@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 6 Apr 2012 22:44:02 +0000 (22:44 +0000)
https://bugs.webkit.org/show_bug.cgi?id=83334
<rdar://problem/11033861>

Reviewed by Sam Weinig.

Source/WebCore:

Added test in TestWebKitAPI

* WebCore.exp.in:
* editing/Editor.cpp:
(WebCore::Editor::simplifyMarkup): Exposing the new command through the editor.
* editing/Editor.h:

Source/WebKit/mac:

* WebView/WebView.mm:
(-[WebView _simplifyMarkup:endNode:]): Added.
* WebView/WebViewPrivate.h:

Tools:

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/mac/SimplifyMarkup.mm: Added.
(-[SimplifyMarkupTest webView:didFinishLoadForFrame:]):
* TestWebKitAPI/Tests/mac/verboseMarkup.html: Added.

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

Source/WebCore/ChangeLog
Source/WebCore/WebCore.exp.in
Source/WebCore/editing/Editor.cpp
Source/WebCore/editing/Editor.h
Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebView/WebView.mm
Source/WebKit/mac/WebView/WebViewPrivate.h
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/mac/SimplifyMarkup.mm [new file with mode: 0644]
Tools/TestWebKitAPI/Tests/mac/verboseMarkup.html [new file with mode: 0644]

index 5ca8a65..98d26e4 100644 (file)
@@ -1,3 +1,18 @@
+2012-04-05  Enrica Casucci  <enrica@apple.com>
+
+        Provide Obj-C private API to simplify markup.
+        https://bugs.webkit.org/show_bug.cgi?id=83334
+        <rdar://problem/11033861>
+
+        Reviewed by Sam Weinig.
+
+        Added test in TestWebKitAPI
+
+        * WebCore.exp.in:
+        * editing/Editor.cpp:
+        (WebCore::Editor::simplifyMarkup): Exposing the new command through the editor.
+        * editing/Editor.h:
+
 2012-04-06  Benjamin Poulain  <bpoulain@apple.com>
 
         Get rid of the useless flag PREEMPT_GEOLOCATION_PERMISSION
index f203c83..d8cb614 100644 (file)
@@ -386,6 +386,7 @@ __ZN7WebCore14SecurityPolicy22generateReferrerHeaderENS0_14ReferrerPolicyERKNS_4
 __ZN7WebCore14SecurityPolicy27resetOriginAccessWhitelistsEv
 __ZN7WebCore14SecurityPolicy29addOriginAccessWhitelistEntryERKNS_14SecurityOriginERKN3WTF6StringES7_b
 __ZN7WebCore14SecurityPolicy32removeOriginAccessWhitelistEntryERKNS_14SecurityOriginERKN3WTF6StringES7_b
+__ZN7WebCore6Editor14simplifyMarkupEPNS_4NodeES2_
 __ZN7WebCore14StorageTracker12deleteOriginEPNS_14SecurityOriginE
 __ZN7WebCore14StorageTracker16deleteAllOriginsEv
 __ZN7WebCore14StorageTracker16syncLocalStorageEv
index 004b258..91982df 100644 (file)
@@ -74,6 +74,7 @@
 #include "RenderedPosition.h"
 #include "ReplaceSelectionCommand.h"
 #include "Settings.h"
+#include "SimplifyMarkupCommand.h"
 #include "Sound.h"
 #include "SpellChecker.h"
 #include "SpellingCorrectionCommand.h"
@@ -1041,6 +1042,24 @@ void Editor::performDelete()
     setStartNewKillRingSequence(false);
 }
 
+void Editor::simplifyMarkup(Node* startNode, Node* endNode)
+{
+    if (!startNode)
+        return;
+    if (endNode) {
+        if (startNode->document() != endNode->document())
+            return;
+        // check if start node is before endNode
+        Node* node = startNode;
+        while (node && node != endNode)
+            node = node->traverseNextNode();
+        if (!node)
+            return;
+    }
+    
+    applyCommand(SimplifyMarkupCommand::create(m_frame->document(), startNode, (endNode) ? endNode->traverseNextNode() : 0));
+}
+
 void Editor::copyURL(const KURL& url, const String& title)
 {
     Pasteboard::generalPasteboard()->writeURL(url, title, m_frame);
index 45bac9f..f06f05e 100644 (file)
@@ -381,6 +381,8 @@ public:
     bool selectionStartHasMarkerFor(DocumentMarker::MarkerType, int from, int length) const;
     void updateMarkersForWordsAffectedByEditing(bool onlyHandleWordsContainingSelection);
     void deletedAutocorrectionAtPosition(const Position&, const String& originalString);
+    
+    void simplifyMarkup(Node* startNode, Node* endNode);
 
     void deviceScaleFactorChanged();
 
index 748362c..6a21684 100644 (file)
@@ -1,3 +1,15 @@
+2012-04-06  Enrica Casucci  <enrica@apple.com>
+
+        Provide Obj-C private API to simplify markup.
+        https://bugs.webkit.org/show_bug.cgi?id=83334
+        <rdar://problem/11033861>
+
+        Reviewed by Sam Weinig.
+
+        * WebView/WebView.mm:
+        (-[WebView _simplifyMarkup:endNode:]): Added.
+        * WebView/WebViewPrivate.h:
+
 2012-04-06  Tim Horton  <timothy_horton@apple.com>
 
         Add autodetection of image orientation from EXIF information
index 3ff8867..1f09d79 100644 (file)
@@ -5623,6 +5623,17 @@ FOR_EACH_RESPONDER_SELECTOR(FORWARD)
     return coreFrame->selection()->isAll(CanCrossEditingBoundary);
 }
 
+- (void)_simplifyMarkup:(DOMNode *)startNode endNode:(DOMNode *)endNode
+{
+    Frame* coreFrame = core([self mainFrame]);
+    if (!coreFrame || !startNode)
+        return;
+    Node* coreStartNode= core(startNode);
+    if (coreStartNode->document() != coreFrame->document())
+        return;
+    return coreFrame->editor()->simplifyMarkup(coreStartNode, core(endNode));    
+}
+
 @end
 
 static WebFrameView *containingFrameView(NSView *view)
index 8566545..02f7bcc 100644 (file)
@@ -697,6 +697,8 @@ Could be worth adding to the API.
 - (void)_replaceSelectionWithNode:(DOMNode *)node matchStyle:(BOOL)matchStyle;
 - (BOOL)_selectionIsCaret;
 - (BOOL)_selectionIsAll;
+- (void)_simplifyMarkup:(DOMNode *)startNode endNode:(DOMNode *)endNode;
+
 @end
 
 @interface WebView (WebViewDeviceOrientation)
index 6152920..50cbede 100644 (file)
@@ -1,3 +1,16 @@
+2012-04-06  Enrica Casucci  <enrica@apple.com>
+
+        Provide Obj-C private API to simplify markup.
+        https://bugs.webkit.org/show_bug.cgi?id=83334
+        <rdar://problem/11033861>
+
+        Reviewed by Sam Weinig.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/mac/SimplifyMarkup.mm: Added.
+        (-[SimplifyMarkupTest webView:didFinishLoadForFrame:]):
+        * TestWebKitAPI/Tests/mac/verboseMarkup.html: Added.
+
 2012-04-06  Dirk Pranke  <dpranke@chromium.org>
 
         new-run-webkit-tests: fix bugs in 'mock' drt implementation
index 54df878..d116c75 100644 (file)
                C0C5D3BE14598B6F00A802A6 /* GetBackingScaleFactor.mm in Sources */ = {isa = PBXBuildFile; fileRef = C0C5D3BC14598B6F00A802A6 /* GetBackingScaleFactor.mm */; };
                C0C5D3C61459912900A802A6 /* GetBackingScaleFactor_Bundle.mm in Sources */ = {isa = PBXBuildFile; fileRef = C0C5D3BD14598B6F00A802A6 /* GetBackingScaleFactor_Bundle.mm */; };
                C507E8A714C6545B005D6B3B /* InspectorBar.mm in Sources */ = {isa = PBXBuildFile; fileRef = C507E8A614C6545B005D6B3B /* InspectorBar.mm */; };
+               C540F776152E4DA000A40C8C /* SimplifyMarkup.mm in Sources */ = {isa = PBXBuildFile; fileRef = C540F775152E4DA000A40C8C /* SimplifyMarkup.mm */; };
+               C540F784152E5A9A00A40C8C /* verboseMarkup.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = C540F783152E5A7800A40C8C /* verboseMarkup.html */; };
                E490296814E2E3A4002BEDD1 /* TypingStyleCrash.mm in Sources */ = {isa = PBXBuildFile; fileRef = E490296714E2E3A4002BEDD1 /* TypingStyleCrash.mm */; };
                F3FC3EE313678B7300126A65 /* libgtest.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F3FC3EE213678B7300126A65 /* libgtest.a */; };
                F6F3F29113342FEB00A6BF19 /* CookieManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F3F29013342FEB00A6BF19 /* CookieManager.cpp */; };
                                BC2D006412AA04CE00E732A3 /* file-with-anchor.html in Copy Resources */,
                                37DC6791140D7D7600ABCCDB /* DOMRangeOfString.html in Copy Resources */,
                                1ADBEFE3130C6AA100D61D19 /* simple-accelerated-compositing.html in Copy Resources */,
+                               C540F784152E5A9A00A40C8C /* verboseMarkup.html in Copy Resources */,
                        );
                        name = "Copy Resources";
                        runOnlyForDeploymentPostprocessing = 0;
                C0C5D3BC14598B6F00A802A6 /* GetBackingScaleFactor.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = GetBackingScaleFactor.mm; sourceTree = "<group>"; };
                C0C5D3BD14598B6F00A802A6 /* GetBackingScaleFactor_Bundle.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = GetBackingScaleFactor_Bundle.mm; sourceTree = "<group>"; };
                C507E8A614C6545B005D6B3B /* InspectorBar.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = InspectorBar.mm; sourceTree = "<group>"; };
+               C540F775152E4DA000A40C8C /* SimplifyMarkup.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SimplifyMarkup.mm; sourceTree = "<group>"; };
+               C540F783152E5A7800A40C8C /* verboseMarkup.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = verboseMarkup.html; sourceTree = "<group>"; };
                E490296714E2E3A4002BEDD1 /* TypingStyleCrash.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TypingStyleCrash.mm; sourceTree = "<group>"; };
                F3FC3EE213678B7300126A65 /* libgtest.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libgtest.a; sourceTree = BUILT_PRODUCTS_DIR; };
                F6F3F29013342FEB00A6BF19 /* CookieManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CookieManager.cpp; sourceTree = "<group>"; };
                                C507E8A614C6545B005D6B3B /* InspectorBar.mm */,
                                517E7DFB15110EA600D0B008 /* MemoryCachePruneWithinResourceLoadDelegate.mm */,
                                3722C8681461E03E00C45D00 /* RenderedImageFromDOMRange.mm */,
+                               C540F775152E4DA000A40C8C /* SimplifyMarkup.mm */,
                                3799AD3914120A43005EB0C6 /* StringByEvaluatingJavaScriptFromString.mm */,
                                37A6895D148A9B50005100FA /* SubresourceErrorCrash.mm */,
                                E490296714E2E3A4002BEDD1 /* TypingStyleCrash.mm */,
                                37DC678F140D7D3A00ABCCDB /* DOMRangeOfString.html */,
                                C07E6CB113FD738A0038B22B /* devicePixelRatio.html */,
                                517E7E031511187500D0B008 /* MemoryCachePruneWithinResourceLoadDelegate.html */,
+                               C540F783152E5A7800A40C8C /* verboseMarkup.html */,
                        );
                        name = Resources;
                        sourceTree = "<group>";
                                517E7DFC15110EA600D0B008 /* MemoryCachePruneWithinResourceLoadDelegate.mm in Sources */,
                                51FBBB4D1513D4E900822738 /* WebViewCanPasteURL.mm in Sources */,
                                5142B2711517C88B00C32B19 /* ContextMenuCanCopyURL.mm in Sources */,
+                               C540F776152E4DA000A40C8C /* SimplifyMarkup.mm in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
diff --git a/Tools/TestWebKitAPI/Tests/mac/SimplifyMarkup.mm b/Tools/TestWebKitAPI/Tests/mac/SimplifyMarkup.mm
new file mode 100644 (file)
index 0000000..70ec47d
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2012 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 "PlatformUtilities.h"
+#include "PlatformWebView.h"
+#include <wtf/RetainPtr.h>
+
+#import <WebKit/DOM.h>
+#import <WebKit/WebViewPrivate.h>
+
+@interface SimplifyMarkupTest : NSObject {
+}
+@end
+
+static bool didFinishLoad;
+
+@implementation SimplifyMarkupTest
+
+- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
+{
+    didFinishLoad = true;
+}
+@end
+
+namespace TestWebKitAPI {
+
+TEST(WebKit1, SimplifyMarkupTest)
+{
+    RetainPtr<WebView> webView1(AdoptNS, [[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]);
+    RetainPtr<WebView> webView2(AdoptNS, [[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]);
+    RetainPtr<SimplifyMarkupTest> testController(AdoptNS, [SimplifyMarkupTest new]);
+    
+    webView1.get().frameLoadDelegate = testController.get();
+    [[webView1.get() mainFrame] loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"verboseMarkup" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]];
+    
+    Util::run(&didFinishLoad);
+    didFinishLoad = false;
+    webView2.get().frameLoadDelegate = testController.get();
+    [[webView2.get() mainFrame] loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"verboseMarkup" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]];
+    
+    Util::run(&didFinishLoad);
+    didFinishLoad = false;
+
+    DOMDocument *document1 = webView1.get().mainFrameDocument;
+    NSString* markupBefore = [[document1 body] innerHTML];
+    DOMDocument *document2 = webView2.get().mainFrameDocument;
+    
+    // If start is after end, nothing is done
+    DOMNode *start = [document1 getElementById:@"test2"];
+    DOMNode *end = [document1 getElementById:@"test1"];
+
+    [webView1.get() _simplifyMarkup:[document1 body] endNode:end];
+    NSString* markupAfter = [[document1 body] innerHTML];
+
+    EXPECT_WK_STREQ(markupBefore, markupAfter);
+    EXPECT_EQ([markupBefore length], [markupAfter length]);
+
+    // If the two nodes are not in the same webView, nothing is done.
+    start = [document1 getElementById:@"test1"];
+    end = [document2 getElementById:@"test2"];
+    [webView1.get() _simplifyMarkup:start endNode:end];
+    markupAfter = [[document1 body] innerHTML];
+    
+    EXPECT_WK_STREQ(markupBefore, markupAfter);
+    EXPECT_EQ([markupBefore length], [markupAfter length]);
+
+    // If the two nodes are not in the same document, nothing is done.
+    DOMHTMLFrameElement* frame = (DOMHTMLFrameElement *)[document1 getElementById:@"test3"];
+    end = [[frame contentDocument] firstChild];
+    
+    [webView1.get() _simplifyMarkup:start endNode:end];
+    markupAfter = [[document1 body] innerHTML];
+    
+    EXPECT_WK_STREQ(markupBefore, markupAfter);
+    EXPECT_EQ([markupBefore length], [markupAfter length]);
+
+    // If the nodes are in the same webView, same document and in the right order,
+    // we should have a simplified markup.
+    [webView1.get() _simplifyMarkup:[document1 body] endNode:nil];
+    markupAfter = [[document1 body] innerHTML];
+    // We only verify that the markup has changed and that it is less verbose
+    // then the original version.
+    // The accuracy of the operation is tested by the DRT tests already.
+    EXPECT_GT([markupBefore length], [markupAfter length]);
+}
+
+} // namespace TestWebKitAPI
diff --git a/Tools/TestWebKitAPI/Tests/mac/verboseMarkup.html b/Tools/TestWebKitAPI/Tests/mac/verboseMarkup.html
new file mode 100644 (file)
index 0000000..c8a2448
--- /dev/null
@@ -0,0 +1,19 @@
+<html>
+    <body contenteditable="true">
+        <div>Hello</div>
+        <div id="test1"><b><i>Hello</i></b></div>
+        <div><b><i><span style="font-weight: normal"><b><i>Hello</i></b></span></i></b></div>
+        <div><div><div>Hello</div></div></div>
+        <div><b><div><i>Hello</i></div></b></div>
+        <div><div style="text-align: center;"><b>Hello</b></div></div>
+        <div id="test2"><b><i><span style="font-weight: normal"><b><i>hello</i></b></span></i></b></div><div><b><i><span style="font-weight: normal"><b><i>world</i></b></span></i></b></div>
+        <div><b><i><span style="font-weight: normal;"><b><i>hello1</i></b><b><i> hello2</i></b></span></i></b></div>
+        <div><i style="margin: 10px;"><b><i style="margin: 10px;">hello</i></b></i></div>
+        <div><b><i><span style="font-weight: normal"><b><i>Hello <!-- comment -->world</i></b></span></i></b></div>
+        <div><b><i><span style="font-weight: normal">plain text<b><i>bold italic text</i></b></span></i></b></div>
+        <iframe id="test3" src="data:text/html,
+            <div>This is another document.</div>
+            ">
+        </iframe>
+     </body>
+</html>