[IMP] web_editor: allow pasting HTML tables

Previously, when a clipboardData contained both files and html content,
we prioritised files. This is correct when the original content is an
image (in which case the clipboard html may contain an image tag or be
empty, depending on the source). This is however incorrect in at least
one known case: copying a table from a spreadsheet app, as those usually
generate an image of the table alongside the actual html table. In that
case, we would rather keep the table.

This commit differentiates the specific case where the HTML represents a
table from other cases. In the former, priority is now given to the HTML
content. In the latter, we keep the files as we did before.

task-3165757

closes odoo/odoo#119862

X-original-commit: 021f2f028b382fe15ca75928d1b30a4eb48322df
Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
This commit is contained in:
Stanislas Gueniffey
2023-04-27 07:06:08 +02:00
parent f1566d4d2d
commit 7c0d82a3d9
@@ -4378,18 +4378,25 @@ export class OdooEditor extends EventTarget {
if (fragment.hasChildNodes()) {
this._applyCommand('insert', fragment);
}
} else if (files.length && targetSupportsHtmlContent) {
this.addImagesFiles(files).then(html => {
const imageNodes = this._applyCommand('insert', this._prepareClipboardData(html));
if (imageNodes && this.options.dropImageAsAttachment) {
// Mark images as having to be saved as attachments.
for (const imageNode of imageNodes) {
imageNode.classList.add('o_b64_image_to_save');
} else if ((files.length || clipboardHtml) && targetSupportsHtmlContent) {
const clipboardElem = this._prepareClipboardData(clipboardHtml);
// When copy pasting a table from the outside, a picture of the
// table can be included in the clipboard as an image file. In that
// particular case the html table is given a higher priority than
// the clipboard picture.
if (files.length && !clipboardElem.querySelector('table')) {
this.addImagesFiles(files).then(html => {
const imageNodes = this._applyCommand('insert', this._prepareClipboardData(html));
if (imageNodes && this.options.dropImageAsAttachment) {
// Mark images as having to be saved as attachments.
for (const imageNode of imageNodes) {
imageNode.classList.add('o_b64_image_to_save');
}
}
}
});
} else if (clipboardHtml && targetSupportsHtmlContent) {
this._applyCommand('insert', this._prepareClipboardData(clipboardHtml));
});
} else {
this._applyCommand('insert', clipboardElem);
}
} else {
const text = ev.clipboardData.getData('text/plain');
const selectionIsInsideALink = !!closestElement(sel.anchorNode, 'a');