Skip to content

Commit 69cca59

Browse files
livecodeianpeter-b
authored andcommitted
[[ Bug 17622 ]] Encode/decode html text transferred on the clipboard
(cherry picked from commit 09f72bd)
1 parent 812e27b commit 69cca59

14 files changed

Lines changed: 294 additions & 12 deletions

engine/src/clipboard.cpp

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,13 @@ bool MCClipboard::AddLiveCodeStyledText(MCDataRef p_pickled_text)
372372
// This type is optional as it may not be a faithful representation
373373
MCAutoDataRef t_html(ConvertStyledTextToHTML(p_pickled_text));
374374
if (*t_html != NULL)
375-
t_success = t_item->AddRepresentation(t_type_string, *t_html);
375+
{
376+
MCAutoDataRef t_encoded;
377+
t_encoded = m_clipboard->EncodeHTMLFragmentForTransfer(*t_html);
378+
t_success = *t_encoded != nil;
379+
if (t_success)
380+
t_success = t_item->AddRepresentation(t_type_string, *t_encoded);
381+
}
376382
}
377383

378384
// Also attempt to add as plain text, so we have a fall-back
@@ -459,6 +465,12 @@ bool MCClipboard::AddHTML(MCDataRef p_html)
459465
if (t_item == NULL)
460466
return false;
461467

468+
// Encode the HTML in the required format for the clipboard
469+
MCAutoDataRef t_encoded;
470+
t_encoded = m_clipboard->EncodeHTMLFragmentForTransfer(p_html);
471+
if (*t_encoded == nil)
472+
return false;
473+
462474
// Add the data to the clipboard with the correct type
463475
MCStringRef t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeHTML);
464476
if (t_type_string == NULL)
@@ -849,13 +861,19 @@ bool MCClipboard::CopyAsLiveCodeStyledText(MCDataRef& r_pickled_text) const
849861
MCAutoDataRef t_html;
850862
if (CopyAsData(kMCRawClipboardKnownTypeHTML, &t_html))
851863
{
852-
// Convert to LiveCode styled text
853-
MCDataRef t_pickled_text = ConvertHTMLToStyledText(*t_html);
854-
if (t_pickled_text != NULL)
855-
{
856-
r_pickled_text = t_pickled_text;
857-
return true;
858-
}
864+
MCAutoDataRef t_decodedhtml;
865+
t_decodedhtml.Reset(m_clipboard->DecodeTransferredHTML(*t_html));
866+
867+
if (*t_decodedhtml != nil)
868+
{
869+
// Convert to LiveCode styled text
870+
MCDataRef t_pickled_text = ConvertHTMLToStyledText(*t_decodedhtml);
871+
if (t_pickled_text != NULL)
872+
{
873+
r_pickled_text = t_pickled_text;
874+
return true;
875+
}
876+
}
859877
}
860878

861879
// Finally, try plain text.
@@ -935,7 +953,17 @@ bool MCClipboard::CopyAsRTF(MCDataRef& r_rtf_data) const
935953

936954
bool MCClipboard::CopyAsHTML(MCDataRef& r_html_data) const
937955
{
938-
return CopyAsData(kMCRawClipboardKnownTypeHTML, r_html_data);
956+
MCAutoDataRef t_data;
957+
if (!CopyAsData(kMCRawClipboardKnownTypeHTML, &t_data))
958+
return false;
959+
960+
MCDataRef t_decoded = nil;
961+
t_decoded = m_clipboard->DecodeTransferredHTML(*t_data);
962+
if (t_decoded == nil)
963+
return false;
964+
965+
r_html_data = t_decoded;
966+
return true;
939967
}
940968

941969
bool MCClipboard::CopyAsPNG(MCDataRef& r_png) const

engine/src/em-clipboard.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,14 @@ MCStringRef MCEmscriptenRawClipboard::DecodeTransferredFileList(MCDataRef p_data
109109
{
110110
return NULL;
111111
}
112+
113+
MCDataRef MCEmscriptenRawClipboard::EncodeHTMLFragmentForTransfer(MCDataRef p_html) const
114+
{
115+
return NULL;
116+
}
117+
118+
MCDataRef MCEmscriptenRawClipboard::DecodeTransferredHTML(MCDataRef p_html) const
119+
{
120+
return NULL;
121+
}
122+

engine/src/em-clipboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class MCEmscriptenRawClipboard :
4444
virtual MCStringRef GetKnownTypeString(MCRawClipboardKnownType p_type) const;
4545
virtual MCDataRef EncodeFileListForTransfer(MCStringRef p_file_list) const;
4646
virtual MCStringRef DecodeTransferredFileList(MCDataRef p_data) const;
47+
virtual MCDataRef EncodeHTMLFragmentForTransfer(MCDataRef p_html) const;
48+
virtual MCDataRef DecodeTransferredHTML(MCDataRef p_html) const;
4749
};
4850

4951

engine/src/lnx-clipboard.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ MCStringRef MCLinuxRawClipboard::DecodeTransferredFileList(MCDataRef p_data) con
315315
return t_paths;
316316
}
317317

318+
MCDataRef MCLinuxRawClipboard::EncodeHTMLFragmentForTransfer(MCDataRef p_html) const
319+
{
320+
return MCValueRetain(p_html);
321+
}
322+
323+
MCDataRef MCLinuxRawClipboard::DecodeTransferredHTML(MCDataRef p_html) const
324+
{
325+
return MCValueRetain(p_html);
326+
}
327+
318328
const MCLinuxRawClipboardItem* MCLinuxRawClipboard::GetSelectionItem() const
319329
{
320330
if (m_selected_item)

engine/src/lnx-clipboard.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ class MCLinuxRawClipboard :
112112
virtual bool FlushData();
113113
virtual uindex_t GetMaximumItemCount() const;
114114
virtual MCStringRef GetKnownTypeString(MCRawClipboardKnownType p_type) const;
115-
virtual MCDataRef EncodeFileListForTransfer(MCStringRef p_file_list) const;
116-
virtual MCStringRef DecodeTransferredFileList(MCDataRef p_data) const;
115+
virtual MCDataRef EncodeHTMLFragmentForTransfer(MCDataRef p_html) const;
116+
virtual MCDataRef DecodeTransferredHTML(MCDataRef p_html) const;
117117

118118
// Gets the item that was most recently pushed to the system (this is used
119119
// to respond to selection requests in order to perform copies)

engine/src/mac-clipboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class MCMacRawClipboard :
111111
virtual MCStringRef GetKnownTypeString(MCRawClipboardKnownType p_type) const;
112112
virtual MCDataRef EncodeFileListForTransfer(MCStringRef p_file_path) const;
113113
virtual MCStringRef DecodeTransferredFileList(MCDataRef p_encoded_path) const;
114+
virtual MCDataRef EncodeHTMLFragmentForTransfer(MCDataRef p_html) const;
115+
virtual MCDataRef DecodeTransferredHTML(MCDataRef p_html) const;
114116

115117
// Constructor. The NSPasteboard being wrapped is required.
116118
MCMacRawClipboard(NSPasteboard* p_pasteboard);

engine/src/mac-clipboard.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@
274274
return t_path;
275275
}
276276

277+
MCDataRef MCMacRawClipboard::EncodeHTMLFragmentForTransfer(MCDataRef p_html) const
278+
{
279+
return MCValueRetain(p_html);
280+
}
281+
282+
MCDataRef MCMacRawClipboard::DecodeTransferredHTML(MCDataRef p_html) const
283+
{
284+
return MCValueRetain(p_html);
285+
}
286+
277287
MCStringRef MCMacRawClipboard::CopyAsUTI(MCStringRef p_key)
278288
{
279289
// If the key is already in UTI form, just pass it out

engine/src/mblandroid-clipboard.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,14 @@ MCStringRef MCAndroidRawClipboard::DecodeTransferredFileList(MCDataRef p_data) c
109109
{
110110
return NULL;
111111
}
112+
113+
MCDataRef MCAndroidRawClipboard::EncodeHTMLFragmentForTransfer(MCDataRef p_html) const
114+
{
115+
return NULL;
116+
}
117+
118+
MCDataRef MCAndroidRawClipboard::DecodeTransferredHTML(MCDataRef p_html) const
119+
{
120+
return NULL;
121+
}
122+

engine/src/mblandroid-clipboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class MCAndroidRawClipboard :
4444
virtual MCStringRef GetKnownTypeString(MCRawClipboardKnownType p_type) const;
4545
virtual MCDataRef EncodeFileListForTransfer(MCStringRef p_file_list) const;
4646
virtual MCStringRef DecodeTransferredFileList(MCDataRef p_data) const;
47+
virtual MCDataRef EncodeHTMLFragmentForTransfer(MCDataRef p_html) const;
48+
virtual MCDataRef DecodeTransferredHTML(MCDataRef p_html) const;
4749
};
4850

4951

engine/src/mbliphone-clipboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public MCRawClipboard
4444
virtual MCStringRef GetKnownTypeString(MCRawClipboardKnownType p_type) const;
4545
virtual MCDataRef EncodeFileListForTransfer(MCStringRef p_file_list) const;
4646
virtual MCStringRef DecodeTransferredFileList(MCDataRef p_data) const;
47+
virtual MCDataRef EncodeHTMLFragmentForTransfer(MCDataRef p_html) const;
48+
virtual MCDataRef DecodeTransferredHTML(MCDataRef p_html) const;
4749
};
4850

4951

0 commit comments

Comments
 (0)