Skip to content

Commit

Permalink
Bug 1920646 - part 1: Make HTMLEditor paste/drop things as plaintex…
Browse files Browse the repository at this point in the history
…t when `contenteditable=plaintext-only` r=m_kato

Chrome sets `beforeinput.data` instead of `beforeinput.dataTransfer`, but
Input Events Level 2 spec defines that browsers should set `dataTransfer` when
**contenteditable** [1].  Therefore, the new WPT expects `dataTransfer`.

However, it's unclear that the `dataTransfer` should have `text/html` or only
`text/plain`.  From web apps point of view, `text/html` data may make them
serialize the rich text format to plaintext without any dependencies of browsers
and OS.  On the other hand, they cannot distinguish whether the user tries to
paste with or without formatting when `contenteditable=true`.  Therefore, I
filed a spec issue for this.  We need to be back later about this issue.

1. https://w3c.github.io/input-events/#overview
2. w3c/input-events#162

Differential Revision: https://phabricator.services.mozilla.com/D223908
  • Loading branch information
masayuki-nakano committed Oct 2, 2024
1 parent 518105e commit 7e9bbe2
Show file tree
Hide file tree
Showing 8 changed files with 723 additions and 127 deletions.
24 changes: 15 additions & 9 deletions editor/libeditor/HTMLEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3142,9 +3142,10 @@ class HTMLEditor final : public EditorBase,
*
* @param aClipboardType nsIClipboard::kGlobalClipboard or
* nsIClipboard::kSelectionClipboard.
* @param aEditingHost The editing host.
*/
MOZ_CAN_RUN_SCRIPT nsresult
PasteInternal(nsIClipboard::ClipboardType aClipboardType);
MOZ_CAN_RUN_SCRIPT nsresult PasteInternal(
nsIClipboard::ClipboardType aClipboardType, const Element& aEditingHost);

[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult
InsertWithQuotationsAsSubAction(const nsAString& aQuotedText) final;
Expand Down Expand Up @@ -3198,8 +3199,8 @@ class HTMLEditor final : public EditorBase,
* with <span _moz_quote="true">, and each chunk not starting with ">" is
* inserted as normal text.
*/
MOZ_CAN_RUN_SCRIPT nsresult
InsertTextWithQuotationsInternal(const nsAString& aStringToInsert);
MOZ_CAN_RUN_SCRIPT nsresult InsertTextWithQuotationsInternal(
const nsAString& aStringToInsert, const Element& aEditingHost);

/**
* ReplaceContainerWithTransactionInternal() is implementation of
Expand Down Expand Up @@ -3722,20 +3723,23 @@ class HTMLEditor final : public EditorBase,
[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult SetSelectionAtDocumentStart();

// Methods for handling plaintext quotations
MOZ_CAN_RUN_SCRIPT nsresult
PasteAsPlaintextQuotation(nsIClipboard::ClipboardType aSelectionType);
MOZ_CAN_RUN_SCRIPT nsresult PasteAsPlaintextQuotation(
nsIClipboard::ClipboardType aSelectionType, const Element& aEditingHost);

enum class AddCites { No, Yes };
/**
* Insert a string as quoted text, replacing the selected text (if any).
* @param aQuotedText The string to insert.
* @param aAddCites Whether to prepend extra ">" to each line
* (usually true, unless those characters
* have already been added.)
* @param aEditingHost The editing host.
* @return aNodeInserted The node spanning the insertion, if applicable.
* If aAddCites is false, this will be null.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult InsertAsPlaintextQuotation(
const nsAString& aQuotedText, bool aAddCites, nsINode** aNodeInserted);
const nsAString& aQuotedText, AddCites aAddCites,
const Element& aEditingHost, nsINode** aNodeInserted = nullptr);

/**
* InsertObject() inserts given object at aPointToInsert.
Expand All @@ -3749,7 +3753,8 @@ class HTMLEditor final : public EditorBase,
DeleteSelectedContent aDeleteSelectedContent);

class HTMLTransferablePreparer;
nsresult PrepareHTMLTransferable(nsITransferable** aTransferable) const;
nsresult PrepareHTMLTransferable(nsITransferable** aTransferable,
const Element* aEditingHost) const;

enum class HavePrivateHTMLFlavor { No, Yes };
MOZ_CAN_RUN_SCRIPT nsresult InsertFromTransferableAtSelection(
Expand All @@ -3765,7 +3770,8 @@ class HTMLEditor final : public EditorBase,
MOZ_CAN_RUN_SCRIPT nsresult InsertFromDataTransfer(
const dom::DataTransfer* aDataTransfer, uint32_t aIndex,
nsIPrincipal* aSourcePrincipal, const EditorDOMPoint& aDroppedAt,
DeleteSelectedContent aDeleteSelectedContent);
DeleteSelectedContent aDeleteSelectedContent,
const Element& aEditingHost);

static HavePrivateHTMLFlavor ClipboardHasPrivateHTMLFlavor(
nsIClipboard* clipboard);
Expand Down
Loading

0 comments on commit 7e9bbe2

Please sign in to comment.