Title: [226242] trunk/Source/WebCore
Revision
226242
Author
[email protected]
Date
2017-12-21 14:32:14 -0800 (Thu, 21 Dec 2017)

Log Message

Minor cleanup in WebContentReaderCocoa after r226213
https://bugs.webkit.org/show_bug.cgi?id=181104

Reviewed by Wenson Hsieh.

Deployed early exists in the case of attachment replacements to make the code easier to read.

* editing/cocoa/WebContentReaderCocoa.mm:
(WebCore::createFragmentAndAddResources):
(WebCore::markupForFragmentInDocument): Extracted out of sanitizeMarkupWithArchive.
(WebCore::sanitizeMarkupWithArchive):
(WebCore::WebContentReader::readImage): Simplified the return logic.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (226241 => 226242)


--- trunk/Source/WebCore/ChangeLog	2017-12-21 22:14:26 UTC (rev 226241)
+++ trunk/Source/WebCore/ChangeLog	2017-12-21 22:32:14 UTC (rev 226242)
@@ -1,3 +1,18 @@
+2017-12-21  Ryosuke Niwa  <[email protected]>
+
+        Minor cleanup in WebContentReaderCocoa after r226213
+        https://bugs.webkit.org/show_bug.cgi?id=181104
+
+        Reviewed by Wenson Hsieh.
+
+        Deployed early exists in the case of attachment replacements to make the code easier to read.
+
+        * editing/cocoa/WebContentReaderCocoa.mm:
+        (WebCore::createFragmentAndAddResources):
+        (WebCore::markupForFragmentInDocument): Extracted out of sanitizeMarkupWithArchive.
+        (WebCore::sanitizeMarkupWithArchive):
+        (WebCore::WebContentReader::readImage): Simplified the return logic.
+
 2017-12-21  Zalan Bujtas  <[email protected]>
 
         [RenderTreeBuilder] Move RenderRubyAsBlock::addChild mutation to a RenderTreeBuilder

Modified: trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm (226241 => 226242)


--- trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm	2017-12-21 22:14:26 UTC (rev 226241)
+++ trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm	2017-12-21 22:32:14 UTC (rev 226242)
@@ -303,19 +303,19 @@
         return WTFMove(fragmentAndResources.fragment);
     }
 
-    if (shouldReplaceRichContentWithAttachments())
+    if (shouldReplaceRichContentWithAttachments()) {
         replaceRichContentWithAttachments(*fragmentAndResources.fragment, fragmentAndResources.resources);
-    else {
-        HashMap<AtomicString, AtomicString> blobURLMap;
-        for (const Ref<ArchiveResource>& subresource : fragmentAndResources.resources) {
-            auto blob = Blob::create(subresource->data(), subresource->mimeType());
-            String blobURL = DOMURL::createObjectURL(document, blob);
-            blobURLMap.set(subresource->url().string(), blobURL);
-        }
+        return WTFMove(fragmentAndResources.fragment);
+    }
 
-        replaceSubresourceURLs(*fragmentAndResources.fragment, WTFMove(blobURLMap));
+    HashMap<AtomicString, AtomicString> blobURLMap;
+    for (const Ref<ArchiveResource>& subresource : fragmentAndResources.resources) {
+        auto blob = Blob::create(subresource->data(), subresource->mimeType());
+        String blobURL = DOMURL::createObjectURL(document, blob);
+        blobURLMap.set(subresource->url().string(), blobURL);
     }
 
+    replaceSubresourceURLs(*fragmentAndResources.fragment, WTFMove(blobURLMap));
     return WTFMove(fragmentAndResources.fragment);
 }
 
@@ -342,6 +342,17 @@
     return MarkupAndArchive { String::fromUTF8(mainResource->data().data(), mainResource->data().size()), mainResource.releaseNonNull(), archive.releaseNonNull() };
 }
 
+static String markupForFragmentInDocument(Ref<DocumentFragment>&& fragment, Document& document)
+{
+    auto* bodyElement = document.body();
+    ASSERT(bodyElement);
+    bodyElement->appendChild(WTFMove(fragment));
+
+    auto range = Range::create(document);
+    range->selectNodeContents(*bodyElement);
+    return createMarkup(range.get(), nullptr, AnnotateForInterchange, false, ResolveNonLocalURLs);
+}
+
 static String sanitizeMarkupWithArchive(Document& destinationDocument, MarkupAndArchive& markupAndArchive, const std::function<bool(const String)>& canShowMIMETypeAsHTML)
 {
     auto page = createPageForSanitizingWebContent();
@@ -349,51 +360,46 @@
     ASSERT(stagingDocument);
     auto fragment = createFragmentFromMarkup(*stagingDocument, markupAndArchive.markup, markupAndArchive.mainResource->url(), DisallowScriptingAndPluginContent);
 
-    if (shouldReplaceRichContentWithAttachments())
+    if (shouldReplaceRichContentWithAttachments()) {
         replaceRichContentWithAttachments(fragment, markupAndArchive.archive->subresources());
-    else {
-        HashMap<AtomicString, AtomicString> blobURLMap;
-        for (const Ref<ArchiveResource>& subresource : markupAndArchive.archive->subresources()) {
-            auto blob = Blob::create(subresource->data(), subresource->mimeType());
-            String blobURL = DOMURL::createObjectURL(destinationDocument, blob);
-            blobURLMap.set(subresource->url().string(), blobURL);
-        }
+        return markupForFragmentInDocument(WTFMove(fragment), *stagingDocument);
+    }
 
-        auto contentOrigin = SecurityOrigin::create(markupAndArchive.mainResource->url());
-        for (const Ref<Archive>& subframeArchive : markupAndArchive.archive->subframeArchives()) {
-            RefPtr<ArchiveResource> subframeMainResource = subframeArchive->mainResource();
-            if (!subframeMainResource)
-                continue;
+    HashMap<AtomicString, AtomicString> blobURLMap;
+    for (const Ref<ArchiveResource>& subresource : markupAndArchive.archive->subresources()) {
+        auto blob = Blob::create(subresource->data(), subresource->mimeType());
+        String blobURL = DOMURL::createObjectURL(destinationDocument, blob);
+        blobURLMap.set(subresource->url().string(), blobURL);
+    }
 
-            auto type = subframeMainResource->mimeType();
-            if (!canShowMIMETypeAsHTML(type))
-                continue;
+    auto contentOrigin = SecurityOrigin::create(markupAndArchive.mainResource->url());
+    for (const Ref<Archive>& subframeArchive : markupAndArchive.archive->subframeArchives()) {
+        RefPtr<ArchiveResource> subframeMainResource = subframeArchive->mainResource();
+        if (!subframeMainResource)
+            continue;
 
-            auto subframeURL = subframeMainResource->url();
-            MarkupAndArchive subframeContent = { String::fromUTF8(subframeMainResource->data().data(), subframeMainResource->data().size()),
-                subframeMainResource.releaseNonNull(), subframeArchive.copyRef() };
-            auto subframeMarkup = sanitizeMarkupWithArchive(destinationDocument, subframeContent, canShowMIMETypeAsHTML);
+        auto type = subframeMainResource->mimeType();
+        if (!canShowMIMETypeAsHTML(type))
+            continue;
 
-            CString utf8 = subframeMarkup.utf8();
-            Vector<uint8_t> blobBuffer;
-            blobBuffer.reserveCapacity(utf8.length());
-            blobBuffer.append(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
-            auto blob = Blob::create(WTFMove(blobBuffer), type);
+        auto subframeURL = subframeMainResource->url();
+        MarkupAndArchive subframeContent = { String::fromUTF8(subframeMainResource->data().data(), subframeMainResource->data().size()),
+            subframeMainResource.releaseNonNull(), subframeArchive.copyRef() };
+        auto subframeMarkup = sanitizeMarkupWithArchive(destinationDocument, subframeContent, canShowMIMETypeAsHTML);
 
-            String subframeBlobURL = DOMURL::createObjectURL(destinationDocument, blob);
-            blobURLMap.set(subframeURL.string(), subframeBlobURL);
-        }
+        CString utf8 = subframeMarkup.utf8();
+        Vector<uint8_t> blobBuffer;
+        blobBuffer.reserveCapacity(utf8.length());
+        blobBuffer.append(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
+        auto blob = Blob::create(WTFMove(blobBuffer), type);
 
-        replaceSubresourceURLs(fragment.get(), WTFMove(blobURLMap));
+        String subframeBlobURL = DOMURL::createObjectURL(destinationDocument, blob);
+        blobURLMap.set(subframeURL.string(), subframeBlobURL);
     }
 
-    auto* bodyElement = stagingDocument->body();
-    ASSERT(bodyElement);
-    bodyElement->appendChild(fragment);
+    replaceSubresourceURLs(fragment.get(), WTFMove(blobURLMap));
 
-    auto range = Range::create(*stagingDocument);
-    range->selectNodeContents(*bodyElement);
-    return createMarkup(range.get(), nullptr, AnnotateForInterchange, false, ResolveNonLocalURLs);
+    return markupForFragmentInDocument(WTFMove(fragment), *stagingDocument);
 }
 
 bool WebContentReader::readWebArchive(SharedBuffer& buffer)
@@ -572,10 +578,7 @@
     else
         addFragment(createFragmentForImageAndURL(document, DOMURL::createObjectURL(document, blob)));
 
-    if (!fragment)
-        return false;
-
-    return true;
+    return fragment;
 }
 
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to