I agree with prefer-create-el as the default recommendation for ordinary plugin UI. Using Obsidian's DOM helpers generally produces clearer and more consistent plugin code.
There are, however, cases where native Document.createElement() is intentional and the target Document is part of the required semantics.
In Obsidian Excalidraw I have several such cases:
- detached
<canvas> elements used for rendering, cropping and image export;
- detached
<img> elements;
<style> elements that must belong to an iframe's contentDocument;
- elements that must belong to a popout view's
ownerDocument;
- detached containers used for parsing or offscreen rendering.
For example, this is intentionally document-scoped:
const style = iframe.contentDocument.createElement("style");
const canvas = ownerDocument.createElement("canvas");
Creating the element in another document and then moving it is not necessarily equivalent, and a global plugin stylesheet cannot style an isolated iframe document.
The current rule is therefore excellent guidance for:
document.createElement("div")
when constructing ordinary Obsidian UI, but it is overly broad for operations whose purpose is specifically to create an element owned by another Document, or to create a detached rendering object.
Possible ways to focus the rule:
- Treat explicit
ownerDocument.createElement(...) and contentDocument.createElement(...) differently from generic global-document UI creation.
- Recognize common detached rendering elements such as
canvas and img.
- Document a supported Obsidian API for creating an element in an explicitly supplied
Document, if one exists.
- Otherwise allow the normal narrowly-scoped ESLint suppression mechanism to represent these intentional cases without producing a Community Plugin scorecard warning.
I agree with
prefer-create-elas the default recommendation for ordinary plugin UI. Using Obsidian's DOM helpers generally produces clearer and more consistent plugin code.There are, however, cases where native
Document.createElement()is intentional and the targetDocumentis part of the required semantics.In Obsidian Excalidraw I have several such cases:
<canvas>elements used for rendering, cropping and image export;<img>elements;<style>elements that must belong to an iframe'scontentDocument;ownerDocument;For example, this is intentionally document-scoped:
Creating the element in another document and then moving it is not necessarily equivalent, and a global plugin stylesheet cannot style an isolated iframe document.
The current rule is therefore excellent guidance for:
when constructing ordinary Obsidian UI, but it is overly broad for operations whose purpose is specifically to create an element owned by another
Document, or to create a detached rendering object.Possible ways to focus the rule:
ownerDocument.createElement(...)andcontentDocument.createElement(...)differently from generic global-document UI creation.canvasandimg.Document, if one exists.