Skip to content

prefer-create-el: iframe contentDocument has no Obsidian DOM helper equivalent #196

Description

@zsviczian

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:

  1. Treat explicit ownerDocument.createElement(...) and contentDocument.createElement(...) differently from generic global-document UI creation.
  2. Recognize common detached rendering elements such as canvas and img.
  3. Document a supported Obsidian API for creating an element in an explicitly supplied Document, if one exists.
  4. Otherwise allow the normal narrowly-scoped ESLint suppression mechanism to represent these intentional cases without producing a Community Plugin scorecard warning.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions