Feature: Lint and agent rules for shadow dom & iframe realm safety
Description
Follow-up to Shadow DOM support (#8694). Maintaining support for Shadow DOM and editors that cross iframe boundaries will require continued scrutiny to make sure that the correct helper functions and editor.getRootElement() relative window objects are used instead of the global objects.
Some of these things we can probably manage with eslint rules, the less trivial ones we will simply have to document and reference in AGENTS.md to help us catch them in code review and prevent agents from generating new violations.
Some of the patterns to look for (not comprehensive):
- window (getDefaultView)
- window.getSelection() (getDOMSelection)
- selection.getRangeAt(0) (getDOMSelectionRange)
- document (getRootOwnerDocument)
- document.activeElement (getActiveElementDeep)
- event.target (getComposedEventTarget)
- element.parentElement (getParentElement)
- selection.anchorNode (getDOMSelectionPoints)
Linking to packages/lexical-website/docs/concepts/shadow-dom.md will probably make sense for AGENTS.md as that covers most of this.
Impact
Will avoid regressions relevant to iframes and Shadow DOM.
I was looking into this issue and had a couple of questions before starting.
I added a no-restricted-globals override for window and document in packages/**/src/** (excluding tests/benchmarks) and an AGENTS.md section with the pattern table (8 rows mapping globals to their shadow-DOM/iframe-safe helpers, linked to the shadow-dom.md doc).
Running the rule against the current codebase turned up 333 violations. Non-library packages (playground 199, devtools 21, website 7) can be excluded from the rule since they operate on the host page intentionally, which leaves 106 in library code. Breaking those down:
- ~30 are legitimate globals — SSR guards (
typeof window === 'undefined'), feature-detection probes inenvironment.ts, thegetRootOwnerDocument/getDOMSelectionfallback definitions themselves,exportDOMserialization,window.opentop-level navigation - ~22 are
document.createElementinsidecreateDOMmethods where the node receiveseditorbut doesn't use it for document access. Fixing these needs a pattern decision (threadeditorthrough, use$getEditor(), or add a helper like$getDocument()) - ~15 are fixable now using existing context (
dom.ownerDocument,element.ownerDocument.defaultView,activeEditorin reconciler scope, etc.)
Would it be better to set the rule to warn for now and address the existing violations incrementally in follow-up PRs, or do a full migration in one shot? I haven't worked on this type of lint-rule-plus-migration PR before so I wanted to check what you'd prefer.
IMO something like $getDocument() makes the most sense, in some use cases (primarily headless) it can be useful to call createDOM and have document point at something other than the global window.document when the editor doesn't have a root element (withDOM does this in a very brute force sort of way). Maybe we could have an lexical-eslint-plugin rule for createDOM/updateDOM/exportDOM that can autofix those document references.
As for warn vs. error I think people and agents have a tendency to ignore warnings so it's usually better to just enforce things. It can make review a bit easier if the mechanical transformation is in a different commit from the more meaningful changes to the config/linters/etc.