| 1 | /* |
| 2 | * Copyright (C) 2013, 2014 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2014 Igalia S.L. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "Editor.h" |
| 29 | |
| 30 | #include "Blob.h" |
| 31 | #include "CachedImage.h" |
| 32 | #include "DOMURL.h" |
| 33 | #include "DocumentFragment.h" |
| 34 | #include "Frame.h" |
| 35 | #include "HTMLEmbedElement.h" |
| 36 | #include "HTMLImageElement.h" |
| 37 | #include "HTMLInputElement.h" |
| 38 | #include "HTMLNames.h" |
| 39 | #include "HTMLObjectElement.h" |
| 40 | #include "HTMLParserIdioms.h" |
| 41 | #include "Pasteboard.h" |
| 42 | #include "RenderImage.h" |
| 43 | #include "SVGElement.h" |
| 44 | #include "SVGImageElement.h" |
| 45 | #include "SelectionData.h" |
| 46 | #include "Settings.h" |
| 47 | #include "XLinkNames.h" |
| 48 | #include "markup.h" |
| 49 | #include <cairo.h> |
| 50 | |
| 51 | namespace WebCore { |
| 52 | |
| 53 | static RefPtr<DocumentFragment> createFragmentFromPasteboardData(Pasteboard& pasteboard, Frame& frame, Range& range, bool allowPlainText, bool& chosePlainText) |
| 54 | { |
| 55 | chosePlainText = false; |
| 56 | |
| 57 | if (!pasteboard.hasData()) |
| 58 | return nullptr; |
| 59 | |
| 60 | const auto& selection = pasteboard.selectionData(); |
| 61 | if (selection.hasImage()) { |
| 62 | Vector<uint8_t> buffer; |
| 63 | auto status = cairo_surface_write_to_png_stream(selection.image()->nativeImage().get(), [](void* output, const unsigned char* data, unsigned size) { |
| 64 | if (!reinterpret_cast<Vector<uint8_t>*>(output)->tryAppend(data, size)) |
| 65 | return CAIRO_STATUS_WRITE_ERROR; |
| 66 | return CAIRO_STATUS_SUCCESS; |
| 67 | }, &buffer); |
| 68 | if (status == CAIRO_STATUS_SUCCESS) { |
| 69 | auto blob = Blob::create(WTFMove(buffer), "image/png" ); |
| 70 | if (!frame.document()) |
| 71 | return nullptr; |
| 72 | return createFragmentForImageAndURL(*frame.document(), DOMURL::createObjectURL(*frame.document(), blob)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (selection.hasMarkup() && frame.document()) |
| 77 | return createFragmentFromMarkup(*frame.document(), selection.markup(), emptyString(), DisallowScriptingAndPluginContent); |
| 78 | |
| 79 | if (!allowPlainText) |
| 80 | return nullptr; |
| 81 | |
| 82 | if (selection.hasText()) { |
| 83 | chosePlainText = true; |
| 84 | return createFragmentFromText(range, selection.text()); |
| 85 | } |
| 86 | |
| 87 | return nullptr; |
| 88 | } |
| 89 | |
| 90 | void Editor::pasteWithPasteboard(Pasteboard* pasteboard, OptionSet<PasteOption> options) |
| 91 | { |
| 92 | RefPtr<Range> range = selectedRange(); |
| 93 | if (!range) |
| 94 | return; |
| 95 | |
| 96 | bool chosePlainText; |
| 97 | RefPtr<DocumentFragment> fragment = createFragmentFromPasteboardData(*pasteboard, m_frame, *range, options.contains(PasteOption::AllowPlainText), chosePlainText); |
| 98 | |
| 99 | if (fragment && options.contains(PasteOption::AsQuotation)) |
| 100 | quoteFragmentForPasting(*fragment); |
| 101 | |
| 102 | if (fragment && shouldInsertFragment(*fragment, range.get(), EditorInsertAction::Pasted)) |
| 103 | pasteAsFragment(fragment.releaseNonNull(), canSmartReplaceWithPasteboard(*pasteboard), chosePlainText, options.contains(PasteOption::IgnoreMailBlockquote) ? MailBlockquoteHandling::IgnoreBlockquote : MailBlockquoteHandling::RespectBlockquote); |
| 104 | } |
| 105 | |
| 106 | static const AtomicString& elementURL(Element& element) |
| 107 | { |
| 108 | if (is<HTMLImageElement>(element) || is<HTMLInputElement>(element)) |
| 109 | return element.attributeWithoutSynchronization(HTMLNames::srcAttr); |
| 110 | if (is<SVGImageElement>(element)) |
| 111 | return element.attributeWithoutSynchronization(XLinkNames::hrefAttr); |
| 112 | if (is<HTMLEmbedElement>(element) || is<HTMLObjectElement>(element)) |
| 113 | return element.imageSourceURL(); |
| 114 | return nullAtom(); |
| 115 | } |
| 116 | |
| 117 | static bool getImageForElement(Element& element, RefPtr<Image>& image) |
| 118 | { |
| 119 | auto* renderer = element.renderer(); |
| 120 | if (!is<RenderImage>(renderer)) |
| 121 | return false; |
| 122 | |
| 123 | CachedImage* cachedImage = downcast<RenderImage>(*renderer).cachedImage(); |
| 124 | if (!cachedImage || cachedImage->errorOccurred()) |
| 125 | return false; |
| 126 | |
| 127 | image = cachedImage->imageForRenderer(renderer); |
| 128 | return image; |
| 129 | } |
| 130 | |
| 131 | void Editor::writeImageToPasteboard(Pasteboard& pasteboard, Element& imageElement, const URL&, const String& title) |
| 132 | { |
| 133 | PasteboardImage pasteboardImage; |
| 134 | |
| 135 | if (!getImageForElement(imageElement, pasteboardImage.image)) |
| 136 | return; |
| 137 | ASSERT(pasteboardImage.image); |
| 138 | |
| 139 | pasteboardImage.url.url = imageElement.document().completeURL(stripLeadingAndTrailingHTMLSpaces(elementURL(imageElement))); |
| 140 | pasteboardImage.url.title = title; |
| 141 | pasteboardImage.url.markup = serializeFragment(imageElement, SerializedNodes::SubtreeIncludingNode, nullptr, ResolveURLs::Yes); |
| 142 | pasteboard.write(pasteboardImage); |
| 143 | } |
| 144 | |
| 145 | void Editor::writeSelectionToPasteboard(Pasteboard& pasteboard) |
| 146 | { |
| 147 | PasteboardWebContent pasteboardContent; |
| 148 | pasteboardContent.canSmartCopyOrDelete = canSmartCopyOrDelete(); |
| 149 | pasteboardContent.text = selectedTextForDataTransfer(); |
| 150 | pasteboardContent.markup = serializePreservingVisualAppearance(m_frame.selection().selection(), ResolveURLs::YesExcludingLocalFileURLsForPrivacy, |
| 151 | m_frame.settings().selectionAcrossShadowBoundariesEnabled() ? SerializeComposedTree::Yes : SerializeComposedTree::No); |
| 152 | pasteboard.write(pasteboardContent); |
| 153 | } |
| 154 | |
| 155 | RefPtr<DocumentFragment> Editor::webContentFromPasteboard(Pasteboard& pasteboard, Range& context, bool allowPlainText, bool& chosePlainText) |
| 156 | { |
| 157 | return createFragmentFromPasteboardData(pasteboard, m_frame, context, allowPlainText, chosePlainText); |
| 158 | } |
| 159 | |
| 160 | } // namespace WebCore |
| 161 | |