Skip to content

[lexical][lexical-rich-text][lexical-list] Bug Fix: Import dir attribute in importDOM#8412

Merged
etrepum merged 5 commits intofacebook:mainfrom
mayrang:feat/import-dom-dir-attribute
Apr 28, 2026
Merged

[lexical][lexical-rich-text][lexical-list] Bug Fix: Import dir attribute in importDOM#8412
etrepum merged 5 commits intofacebook:mainfrom
mayrang:feat/import-dom-dir-attribute

Conversation

@mayrang
Copy link
Copy Markdown
Contributor

@mayrang mayrang commented Apr 28, 2026

Description

Fixes #7765

The dir attribute is exported correctly via exportDOM (added in #7727), but importDOM conversion functions don't read it back. This means the text direction is lost when importing HTML — for example, RTL content pasted into the editor loses its direction.

What changed

Added dir attribute handling to all ElementNode importDOM conversion functions:

  • $convertParagraphElement (LexicalParagraphNode.ts)
  • $convertHeadingElement (lexical-rich-text)
  • $convertBlockquoteElement (lexical-rich-text)
  • $convertListNode (lexical-list)
  • $convertListItemElement (lexical-list)

Each reads element.getAttribute('dir') and calls node.setDirection(dir) when the value is 'ltr' or 'rtl'.

Test Plan

Added tests for HTML import → direction preservation:

  • Paragraph with dir="rtl" / dir="ltr" / no dir
  • Heading with dir="rtl"
  • Blockquote with dir="rtl"
  • List with dir="rtl"

…ute in importDOM

Read the dir attribute from HTML elements during importDOM and call
setDirection() on the created node. Previously, exportDOM correctly
exported the dir attribute but importDOM did not read it back, causing
text direction to be lost on HTML import.

Fixes facebook#7765
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 28, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Apr 28, 2026 8:07pm
lexical-playground Ready Ready Preview, Comment Apr 28, 2026 8:07pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 28, 2026
@etrepum
Copy link
Copy Markdown
Collaborator

etrepum commented Apr 28, 2026

tests are failing, expectations probably need to change to match the more correct behavior

…r attribute

Update test fixtures to reflect that importDOM now preserves the dir
attribute on imported elements. Affects HTMLCopyAndPaste, LexicalTabNode,
and LexicalTableNode external-source paste tests.
@mayrang
Copy link
Copy Markdown
Contributor Author

mayrang commented Apr 28, 2026

Done — fixed the expectations so dir="ltr" is preserved on imported <p>, <li>, and table cell paragraphs.

…import

Update playground e2e fixtures to reflect that importDOM now preserves
the dir attribute. Affects code block, table, BIU formatting, and
font-size paste tests where the source HTML carried dir="ltr".
Comment on lines +392 to +399
let direction: string | null = null;
editor.getEditorState().read(() => {
const firstChild = $getRoot().getFirstChild();
if ($isElementNode(firstChild)) {
direction = firstChild.getDirection();
}
});
return direction;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let direction: string | null = null;
editor.getEditorState().read(() => {
const firstChild = $getRoot().getFirstChild();
if ($isElementNode(firstChild)) {
direction = firstChild.getDirection();
}
});
return direction;
return editor.read(() => {
const firstChild = $getRoot().getFirstChild();
return $isElementNode(firstChild) ? firstChild.getDirection() : null;
});

).toBe('rtl');
});

test('list with dir="rtl"', () => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this looks like it could be an importAndGetDirection test

also these could be simplified a bit further by using a data structure to build the tests https://vitest.dev/api/test.html#test-for

Comment on lines +650 to +653
const dir = domNode.getAttribute('dir');
if (dir === 'ltr' || dir === 'rtl') {
node.setDirection(dir);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This seems like a useful helper to export from lexical since it's used across several places, e.g.

export function $setDirectionFromDOM<T extends ElementNode>(node: T, domNode: HTMLElement): T {
  const dir = domNode.getAttribute('dir');
  return dir === 'ltr' || dir === 'rtl' ? node.setDirection(dir) : node;
}

…tract $setDirectionFromDOM helper

Extract the dir attribute import pattern into $setDirectionFromDOM in
LexicalUtils and reuse it in ParagraphNode, ListItemNode, ListNode, and
the heading/blockquote converters in lexical-rich-text. Refactor the
LexicalHtml dir-import tests to use editor.read(cb) returning directly
and consolidate the cases into a single test.for table.
@mayrang
Copy link
Copy Markdown
Contributor Author

mayrang commented Apr 28, 2026

Thanks for the pointers — all three applied:

  • Extracted $setDirectionFromDOM<T> in LexicalUtils (exported from lexical), used in ParagraphNode, ListItemNode, ListNode, and the heading/blockquote converters.
  • Switched the test helper to editor.read(cb) returning the direction directly.
  • Folded the cases (including the list one) into a single test.for table.

elementNode.setIndent(indent);
}

export function $setDirectionFromDOM<T extends ElementNode>(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's add some API docs for this new export, otherwise I think everything here looks great

@mayrang
Copy link
Copy Markdown
Contributor Author

mayrang commented Apr 28, 2026

Done — added JSDoc for $setDirectionFromDOM.

@etrepum etrepum added this pull request to the merge queue Apr 28, 2026
Merged via the queue into facebook:main with commit a4bb1e4 Apr 28, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: ElementNode importDOM should import the dir attribute

2 participants