Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/lexical-html/src/import/coreImportRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
IS_SUBSCRIPT,
IS_SUPERSCRIPT,
IS_UNDERLINE,
isAppleInterchangeNewline,
isBlockDomNode,
isDOMTextNode,
isLastChildInBlockNode,
Expand Down Expand Up @@ -453,7 +454,8 @@ const LineBreakRule = /* @__PURE__ */ defineImportRule({
// would otherwise survive as a LineBreakNode and tack an extra blank
// line onto the imported content.
$import: (_ctx, el) =>
isOnlyChildInBlockNode(el) || isLastChildInBlockNode(el)
isOnlyChildInBlockNode(el) ||
(isAppleInterchangeNewline(el) && isLastChildInBlockNode(el))
? []
: [$createLineBreakNode()],
match: sel.tag('br'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,9 @@ describe('LexicalEventHelpers', () => {
},
{
expectedHTML:
'<p class="editor-paragraph" dir="auto"><span data-lexical-text="true">1</span></p><p class="editor-paragraph" dir="auto"><span data-lexical-text="true">2</span></p><p class="editor-paragraph" dir="auto"><span data-lexical-text="true">3</span></p>',
'<p class="editor-paragraph" dir="auto"><span data-lexical-text="true">1</span></p><p class="editor-paragraph" dir="auto"><span data-lexical-text="true">2</span><br><br data-lexical-managed-linebreak="true"></p><p class="editor-paragraph" dir="auto"><span data-lexical-text="true">3</span></p>',
inputs: [pasteHTML('1<p>2<br /></p>3')],
name: 'last br in a block node is ignored',
name: 'last br in a block node is preserved',
},
];

Expand Down
1 change: 1 addition & 0 deletions packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ export type {SerializedLineBreakNode} from './nodes/LexicalLineBreakNode';
export {
$createLineBreakNode,
$isLineBreakNode,
isAppleInterchangeNewline,
isLastChildInBlockNode,
isOnlyChildInBlockNode,
LineBreakNode,
Expand Down
14 changes: 13 additions & 1 deletion packages/lexical/src/nodes/LexicalLineBreakNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
$applyNodeReplacement,
isBlockDomNode,
isDOMTextNode,
isHTMLElement,
} from '../LexicalUtils';

export type SerializedLineBreakNode = SerializedLexicalNode;
Expand Down Expand Up @@ -58,7 +59,10 @@ export class LineBreakNode extends LexicalNode {
static importDOM(): DOMConversionMap | null {
return {
br: (node: Node) => {
if (isOnlyChildInBlockNode(node) || isLastChildInBlockNode(node)) {
if (
isOnlyChildInBlockNode(node) ||
(isAppleInterchangeNewline(node) && isLastChildInBlockNode(node))
) {
return null;
}
return {
Expand Down Expand Up @@ -151,6 +155,14 @@ export function isLastChildInBlockNode(node: Node): boolean {
return false;
}

export function isAppleInterchangeNewline(node: Node): boolean {
return (
node.nodeName === 'BR' &&
isHTMLElement(node) &&
node.getAttribute('class') === 'Apple-interchange-newline'
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this supposed to do? my notes:

What it's supposed to do: it's a round-trip aid for Apple's own apps. When you copy a chunk of content that ends at a line/block boundary, WebKit tacks on this trailing
to preserve the notion of "there was a newline/break at the end here" so that pasting back into a WebKit-based editor reconstructs the spacing faithfully. The class name literally means "a newline inserted for interchange (clipboard transfer) purposes." It's a transport artifact, not authored content.

Why editors special-case it: because it's a clipboard artifact rather than real content, rich-text editors generally want to strip or ignore it on paste — otherwise every Safari paste tacks an extra blank line onto your document. So a lot of editors (and sanitizers like DOMPurify configs, Google Docs, Slack's composer, etc.) explicitly look for class="Apple-interchange-newline" and drop it. That's exactly what #8750 is trying to leverage: "this specific
is a known clipboard artifact, so it's safe to ignore on import."


function isWhitespaceDomTextNode(node: Node): boolean {
return isDOMTextNode(node) && /^( |\t|\r?\n)+$/.test(node.textContent || '');
}
Loading