Use enum class in createMarkup arguments
[WebKit-https.git] / Source / WebCore / editing / cocoa / EditorCocoa.mm
1 /*
2  * Copyright (C) 2006-2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "Editor.h"
28
29 #import "ArchiveResource.h"
30 #import "CSSValueList.h"
31 #import "CSSValuePool.h"
32 #import "CachedResourceLoader.h"
33 #import "ColorMac.h"
34 #import "DocumentFragment.h"
35 #import "DocumentLoader.h"
36 #import "Editing.h"
37 #import "EditingStyle.h"
38 #import "EditorClient.h"
39 #import "FontAttributes.h"
40 #import "FontCascade.h"
41 #import "Frame.h"
42 #import "FrameLoader.h"
43 #import "FrameSelection.h"
44 #import "HTMLAttachmentElement.h"
45 #import "HTMLConverter.h"
46 #import "HTMLImageElement.h"
47 #import "HTMLSpanElement.h"
48 #import "LegacyWebArchive.h"
49 #import "Pasteboard.h"
50 #import "RenderElement.h"
51 #import "RenderStyle.h"
52 #import "Text.h"
53 #import "WebContentReader.h"
54 #import "WebCoreNSURLExtras.h"
55 #import "markup.h"
56 #import <pal/spi/cocoa/NSAttributedStringSPI.h>
57 #import <pal/spi/cocoa/NSKeyedArchiverSPI.h>
58 #import <wtf/BlockObjCExceptions.h>
59
60 namespace WebCore {
61
62 void Editor::platformFontAttributesAtSelectionStart(FontAttributes& attributes, const RenderStyle& style) const
63 {
64     if (auto ctFont = style.fontCascade().primaryFont().getCTFont())
65         attributes.font = (__bridge id)ctFont;
66 }
67
68 static RefPtr<SharedBuffer> archivedDataForAttributedString(NSAttributedString *attributedString)
69 {
70     if (!attributedString.length)
71         return nullptr;
72
73     return SharedBuffer::create(securelyArchivedDataWithRootObject(attributedString));
74 }
75
76 String Editor::selectionInHTMLFormat()
77 {
78     if (auto range = selectedRange())
79         return createMarkup(*range, nullptr, AnnotateForInterchange::Yes, ConvertBlocksToInlines::No, ResolveURLs::YesExcludingLocalFileURLsForPrivacy);
80     return { };
81 }
82
83 #if ENABLE(ATTACHMENT_ELEMENT)
84
85 void Editor::getPasteboardTypesAndDataForAttachment(Element& element, Vector<String>& outTypes, Vector<RefPtr<SharedBuffer>>& outData)
86 {
87     auto& document = element.document();
88     auto elementRange = Range::create(document, { &element, Position::PositionIsBeforeAnchor }, { &element, Position::PositionIsAfterAnchor });
89     client()->getClientPasteboardDataForRange(elementRange.ptr(), outTypes, outData);
90
91     outTypes.append(PasteboardCustomData::cocoaType());
92     outData.append(PasteboardCustomData { document.originIdentifierForPasteboard(), { }, { }, { } }.createSharedBuffer());
93
94     if (auto archive = LegacyWebArchive::create(elementRange.ptr())) {
95         if (auto webArchiveData = archive->rawDataRepresentation()) {
96             outTypes.append(WebArchivePboardType);
97             outData.append(SharedBuffer::create(webArchiveData.get()));
98         }
99     }
100 }
101
102 #endif
103
104 void Editor::writeSelectionToPasteboard(Pasteboard& pasteboard)
105 {
106     NSAttributedString *attributedString = attributedStringFromRange(*selectedRange());
107
108     PasteboardWebContent content;
109     content.contentOrigin = m_frame.document()->originIdentifierForPasteboard();
110     content.canSmartCopyOrDelete = canSmartCopyOrDelete();
111     content.dataInWebArchiveFormat = selectionInWebArchiveFormat();
112     content.dataInRTFDFormat = attributedString.containsAttachments ? dataInRTFDFormat(attributedString) : nullptr;
113     content.dataInRTFFormat = dataInRTFFormat(attributedString);
114     content.dataInAttributedStringFormat = archivedDataForAttributedString(attributedString);
115     content.dataInHTMLFormat = selectionInHTMLFormat();
116     content.dataInStringFormat = stringSelectionForPasteboardWithImageAltText();
117     client()->getClientPasteboardDataForRange(selectedRange().get(), content.clientTypes, content.clientData);
118
119     pasteboard.write(content);
120 }
121
122 void Editor::writeSelection(PasteboardWriterData& pasteboardWriterData)
123 {
124     NSAttributedString *attributedString = attributedStringFromRange(*selectedRange());
125
126     PasteboardWriterData::WebContent webContent;
127     webContent.contentOrigin = m_frame.document()->originIdentifierForPasteboard();
128     webContent.canSmartCopyOrDelete = canSmartCopyOrDelete();
129     webContent.dataInWebArchiveFormat = selectionInWebArchiveFormat();
130     webContent.dataInRTFDFormat = attributedString.containsAttachments ? dataInRTFDFormat(attributedString) : nullptr;
131     webContent.dataInRTFFormat = dataInRTFFormat(attributedString);
132     webContent.dataInAttributedStringFormat = archivedDataForAttributedString(attributedString);
133     webContent.dataInHTMLFormat = selectionInHTMLFormat();
134     webContent.dataInStringFormat = stringSelectionForPasteboardWithImageAltText();
135     client()->getClientPasteboardDataForRange(selectedRange().get(), webContent.clientTypes, webContent.clientData);
136
137     pasteboardWriterData.setWebContent(WTFMove(webContent));
138 }
139
140 RefPtr<SharedBuffer> Editor::selectionInWebArchiveFormat()
141 {
142     RefPtr<LegacyWebArchive> archive = LegacyWebArchive::createFromSelection(&m_frame);
143     if (!archive)
144         return nullptr;
145     return SharedBuffer::create(archive->rawDataRepresentation().get());
146 }
147
148 // FIXME: Makes no sense that selectedTextForDataTransfer always includes alt text, but stringSelectionForPasteboard does not.
149 // This was left in a bad state when selectedTextForDataTransfer was added. Need to look over clients and fix this.
150 String Editor::stringSelectionForPasteboard()
151 {
152     if (!canCopy())
153         return emptyString();
154     String text = selectedText();
155     text.replace(noBreakSpace, ' ');
156     return text;
157 }
158
159 String Editor::stringSelectionForPasteboardWithImageAltText()
160 {
161     if (!canCopy())
162         return emptyString();
163     String text = selectedTextForDataTransfer();
164     text.replace(noBreakSpace, ' ');
165     return text;
166 }
167
168 void Editor::replaceSelectionWithAttributedString(NSAttributedString *attributedString, MailBlockquoteHandling mailBlockquoteHandling)
169 {
170     if (m_frame.selection().isNone())
171         return;
172
173     if (m_frame.selection().selection().isContentRichlyEditable()) {
174         if (auto fragment = createFragmentAndAddResources(m_frame, attributedString)) {
175             if (shouldInsertFragment(*fragment, selectedRange().get(), EditorInsertAction::Pasted))
176                 pasteAsFragment(fragment.releaseNonNull(), false, false, mailBlockquoteHandling);
177         }
178     } else {
179         String text = attributedString.string;
180         if (shouldInsertText(text, selectedRange().get(), EditorInsertAction::Pasted))
181             pasteAsPlainText(text, false);
182     }
183 }
184
185 String Editor::userVisibleString(const URL& url)
186 {
187     return WebCore::userVisibleString(url);
188 }
189
190 RefPtr<SharedBuffer> Editor::dataInRTFDFormat(NSAttributedString *string)
191 {
192     NSUInteger length = string.length;
193     if (!length)
194         return nullptr;
195
196     BEGIN_BLOCK_OBJC_EXCEPTIONS;
197     return SharedBuffer::create([string RTFDFromRange:NSMakeRange(0, length) documentAttributes:@{ }]);
198     END_BLOCK_OBJC_EXCEPTIONS;
199
200     return nullptr;
201 }
202
203 RefPtr<SharedBuffer> Editor::dataInRTFFormat(NSAttributedString *string)
204 {
205     NSUInteger length = string.length;
206     if (!length)
207         return nullptr;
208
209     BEGIN_BLOCK_OBJC_EXCEPTIONS;
210     return SharedBuffer::create([string RTFFromRange:NSMakeRange(0, length) documentAttributes:@{ }]);
211     END_BLOCK_OBJC_EXCEPTIONS;
212
213     return nullptr;
214 }
215
216 // FIXME: Should give this function a name that makes it clear it adds resources to the document loader as a side effect.
217 // Or refactor so it does not do that.
218 RefPtr<DocumentFragment> Editor::webContentFromPasteboard(Pasteboard& pasteboard, Range& context, bool allowPlainText, bool& chosePlainText)
219 {
220     WebContentReader reader(m_frame, context, allowPlainText);
221     pasteboard.read(reader);
222     chosePlainText = reader.madeFragmentFromPlainText;
223     return WTFMove(reader.fragment);
224 }
225
226 }