obsidian-plugin-dev-kit 0.0.0-stage → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +223 -2
- package/bin/opdk.js +34 -0
- package/dist/build/args.d.ts +10 -0
- package/dist/build/args.js +45 -0
- package/dist/build/bundle-check.d.ts +17 -0
- package/dist/build/bundle-check.js +42 -0
- package/dist/build/bundle-file.d.ts +4 -0
- package/dist/build/bundle-file.js +29 -0
- package/dist/build/commands.d.ts +5 -0
- package/dist/build/commands.js +109 -0
- package/dist/build/config.d.ts +3 -0
- package/dist/build/config.js +103 -0
- package/dist/build/css-imports.d.ts +23 -0
- package/dist/build/css-imports.js +50 -0
- package/dist/build/css-step.d.ts +10 -0
- package/dist/build/css-step.js +27 -0
- package/dist/build/css.d.ts +24 -0
- package/dist/build/css.js +47 -0
- package/dist/build/options.d.ts +55 -0
- package/dist/build/options.js +70 -0
- package/dist/build/release.d.ts +5 -0
- package/dist/build/release.js +34 -0
- package/dist/build/report.d.ts +7 -0
- package/dist/build/report.js +16 -0
- package/dist/build/vault.d.ts +26 -0
- package/dist/build/vault.js +290 -0
- package/dist/build-info.json +1 -0
- package/dist/cli/argv.d.ts +14 -0
- package/dist/cli/argv.js +18 -0
- package/dist/cli/commands.d.ts +3 -0
- package/dist/cli/commands.js +7 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +23 -0
- package/dist/cli/process-reporter.d.ts +5 -0
- package/dist/cli/process-reporter.js +15 -0
- package/dist/cli/registry.d.ts +4 -0
- package/dist/cli/registry.js +15 -0
- package/dist/cli/reporter.d.ts +53 -0
- package/dist/cli/reporter.js +100 -0
- package/dist/cli/run.d.ts +9 -0
- package/dist/cli/run.js +61 -0
- package/dist/cli/types.d.ts +27 -0
- package/dist/cli/types.js +1 -0
- package/dist/cli/version.d.ts +13 -0
- package/dist/cli/version.js +61 -0
- package/dist/config/define-config.d.ts +3 -0
- package/dist/config/define-config.js +4 -0
- package/dist/config/load-config.d.ts +6 -0
- package/dist/config/load-config.js +70 -0
- package/dist/config/manifest.d.ts +3 -0
- package/dist/config/manifest.js +72 -0
- package/dist/config/types.d.ts +146 -0
- package/dist/config/types.js +23 -0
- package/dist/demo/args.d.ts +4 -0
- package/dist/demo/args.js +13 -0
- package/dist/demo/checks.d.ts +56 -0
- package/dist/demo/checks.js +124 -0
- package/dist/demo/commands.d.ts +3 -0
- package/dist/demo/commands.js +65 -0
- package/dist/demo/config.d.ts +57 -0
- package/dist/demo/config.js +337 -0
- package/dist/demo/ffmpeg-run.d.ts +18 -0
- package/dist/demo/ffmpeg-run.js +59 -0
- package/dist/demo/ffmpeg.d.ts +63 -0
- package/dist/demo/ffmpeg.js +111 -0
- package/dist/demo/films.d.ts +46 -0
- package/dist/demo/films.js +134 -0
- package/dist/demo/phone-layout.d.ts +38 -0
- package/dist/demo/phone-layout.js +98 -0
- package/dist/demo/phone-render.d.ts +28 -0
- package/dist/demo/phone-render.js +124 -0
- package/dist/demo/published.d.ts +16 -0
- package/dist/demo/published.js +72 -0
- package/dist/demo/run.d.ts +31 -0
- package/dist/demo/run.js +397 -0
- package/dist/demo/stills.d.ts +21 -0
- package/dist/demo/stills.js +62 -0
- package/dist/e2e/artifacts.d.ts +27 -0
- package/dist/e2e/artifacts.js +90 -0
- package/dist/e2e/cleanup.d.ts +54 -0
- package/dist/e2e/cleanup.js +109 -0
- package/dist/e2e/config.d.ts +9 -0
- package/dist/e2e/config.js +42 -0
- package/dist/e2e/create-test.d.ts +37 -0
- package/dist/e2e/create-test.js +109 -0
- package/dist/e2e/demo/clip.d.ts +21 -0
- package/dist/e2e/demo/clip.js +44 -0
- package/dist/e2e/demo/create-demo.d.ts +42 -0
- package/dist/e2e/demo/create-demo.js +439 -0
- package/dist/e2e/demo/manifest.d.ts +76 -0
- package/dist/e2e/demo/manifest.js +168 -0
- package/dist/e2e/demo/names.d.ts +18 -0
- package/dist/e2e/demo/names.js +44 -0
- package/dist/e2e/demo/options.d.ts +22 -0
- package/dist/e2e/demo/options.js +59 -0
- package/dist/e2e/demo/ownership.d.ts +27 -0
- package/dist/e2e/demo/ownership.js +65 -0
- package/dist/e2e/demo/page-scripts.d.ts +82 -0
- package/dist/e2e/demo/page-scripts.js +250 -0
- package/dist/e2e/demo/png.d.ts +8 -0
- package/dist/e2e/demo/png.js +23 -0
- package/dist/e2e/demo/presets.d.ts +43 -0
- package/dist/e2e/demo/presets.js +20 -0
- package/dist/e2e/demo/privacy.d.ts +18 -0
- package/dist/e2e/demo/privacy.js +30 -0
- package/dist/e2e/demo/still-options.d.ts +22 -0
- package/dist/e2e/demo/still-options.js +68 -0
- package/dist/e2e/demo/types.d.ts +184 -0
- package/dist/e2e/demo/types.js +1 -0
- package/dist/e2e/electron-binary.d.ts +15 -0
- package/dist/e2e/electron-binary.js +30 -0
- package/dist/e2e/fixtures.d.ts +62 -0
- package/dist/e2e/fixtures.js +68 -0
- package/dist/e2e/gate.d.ts +4 -0
- package/dist/e2e/gate.js +8 -0
- package/dist/e2e/global-setup.d.ts +11 -0
- package/dist/e2e/global-setup.js +17 -0
- package/dist/e2e/helpers/commands.d.ts +14 -0
- package/dist/e2e/helpers/commands.js +56 -0
- package/dist/e2e/helpers/create-helpers.d.ts +24 -0
- package/dist/e2e/helpers/create-helpers.js +582 -0
- package/dist/e2e/helpers/device-metrics.d.ts +21 -0
- package/dist/e2e/helpers/device-metrics.js +25 -0
- package/dist/e2e/helpers/ime.d.ts +13 -0
- package/dist/e2e/helpers/ime.js +19 -0
- package/dist/e2e/helpers/layout-page-scripts.d.ts +103 -0
- package/dist/e2e/helpers/layout-page-scripts.js +224 -0
- package/dist/e2e/helpers/layout.d.ts +83 -0
- package/dist/e2e/helpers/layout.js +148 -0
- package/dist/e2e/helpers/mobile.d.ts +31 -0
- package/dist/e2e/helpers/mobile.js +26 -0
- package/dist/e2e/helpers/page-scripts.d.ts +194 -0
- package/dist/e2e/helpers/page-scripts.js +467 -0
- package/dist/e2e/helpers/settings.d.ts +8 -0
- package/dist/e2e/helpers/settings.js +16 -0
- package/dist/e2e/helpers/settle.d.ts +28 -0
- package/dist/e2e/helpers/settle.js +33 -0
- package/dist/e2e/helpers/types.d.ts +193 -0
- package/dist/e2e/helpers/types.js +1 -0
- package/dist/e2e/helpers/view-mode.d.ts +23 -0
- package/dist/e2e/helpers/view-mode.js +35 -0
- package/dist/e2e/index.d.ts +14 -0
- package/dist/e2e/index.js +4 -0
- package/dist/e2e/internals.d.ts +155 -0
- package/dist/e2e/internals.js +8 -0
- package/dist/e2e/obsidian-path.d.ts +17 -0
- package/dist/e2e/obsidian-path.js +31 -0
- package/dist/e2e/options.d.ts +123 -0
- package/dist/e2e/options.js +244 -0
- package/dist/e2e/page-scripts.d.ts +45 -0
- package/dist/e2e/page-scripts.js +53 -0
- package/dist/e2e/peers.d.ts +9 -0
- package/dist/e2e/peers.js +19 -0
- package/dist/e2e/plugins.d.ts +32 -0
- package/dist/e2e/plugins.js +119 -0
- package/dist/e2e/process-tree.d.ts +33 -0
- package/dist/e2e/process-tree.js +133 -0
- package/dist/e2e/readiness.d.ts +23 -0
- package/dist/e2e/readiness.js +63 -0
- package/dist/e2e/seed.d.ts +34 -0
- package/dist/e2e/seed.js +122 -0
- package/dist/e2e/session.d.ts +71 -0
- package/dist/e2e/session.js +266 -0
- package/dist/e2e/vault.d.ts +52 -0
- package/dist/e2e/vault.js +146 -0
- package/dist/e2e/version.d.ts +25 -0
- package/dist/e2e/version.js +40 -0
- package/dist/eslint/index.d.ts +2 -0
- package/dist/eslint/index.js +1 -0
- package/dist/eslint/obsidian-plugin.d.ts +49 -0
- package/dist/eslint/obsidian-plugin.js +115 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/dist/kit/args.d.ts +7 -0
- package/dist/kit/args.js +21 -0
- package/dist/kit/commands.d.ts +3 -0
- package/dist/kit/commands.js +52 -0
- package/dist/kit/files.d.ts +3 -0
- package/dist/kit/files.js +24 -0
- package/dist/kit/pins.d.ts +89 -0
- package/dist/kit/pins.js +178 -0
- package/dist/kit/remote.d.ts +13 -0
- package/dist/kit/remote.js +33 -0
- package/dist/kit/runners.d.ts +11 -0
- package/dist/kit/runners.js +53 -0
- package/dist/kit/terminal-text.d.ts +6 -0
- package/dist/kit/terminal-text.js +20 -0
- package/dist/kit/upgrade.d.ts +26 -0
- package/dist/kit/upgrade.js +162 -0
- package/dist/peers/resolve.d.ts +8 -0
- package/dist/peers/resolve.js +28 -0
- package/dist/peers/versions.d.ts +30 -0
- package/dist/peers/versions.js +32 -0
- package/dist/release/api-check.d.ts +27 -0
- package/dist/release/api-check.js +119 -0
- package/dist/release/args.d.ts +40 -0
- package/dist/release/args.js +120 -0
- package/dist/release/assets.d.ts +21 -0
- package/dist/release/assets.js +35 -0
- package/dist/release/build-check.d.ts +43 -0
- package/dist/release/build-check.js +184 -0
- package/dist/release/build-script.d.ts +12 -0
- package/dist/release/build-script.js +30 -0
- package/dist/release/build-tagged.d.ts +26 -0
- package/dist/release/build-tagged.js +52 -0
- package/dist/release/bump.d.ts +24 -0
- package/dist/release/bump.js +58 -0
- package/dist/release/changelog.d.ts +25 -0
- package/dist/release/changelog.js +148 -0
- package/dist/release/check.d.ts +38 -0
- package/dist/release/check.js +150 -0
- package/dist/release/commands.d.ts +3 -0
- package/dist/release/commands.js +154 -0
- package/dist/release/committed-tree.d.ts +63 -0
- package/dist/release/committed-tree.js +118 -0
- package/dist/release/config.d.ts +3 -0
- package/dist/release/config.js +28 -0
- package/dist/release/gh-access.d.ts +14 -0
- package/dist/release/gh-access.js +45 -0
- package/dist/release/gh.d.ts +10 -0
- package/dist/release/gh.js +33 -0
- package/dist/release/git.d.ts +49 -0
- package/dist/release/git.js +111 -0
- package/dist/release/interrupt.d.ts +30 -0
- package/dist/release/interrupt.js +60 -0
- package/dist/release/json.d.ts +33 -0
- package/dist/release/json.js +81 -0
- package/dist/release/kit-check.d.ts +8 -0
- package/dist/release/kit-check.js +51 -0
- package/dist/release/manifest-rules.d.ts +23 -0
- package/dist/release/manifest-rules.js +255 -0
- package/dist/release/preflight.d.ts +10 -0
- package/dist/release/preflight.js +65 -0
- package/dist/release/problems.d.ts +24 -0
- package/dist/release/problems.js +11 -0
- package/dist/release/project-files.d.ts +14 -0
- package/dist/release/project-files.js +16 -0
- package/dist/release/publish-command.d.ts +21 -0
- package/dist/release/publish-command.js +83 -0
- package/dist/release/publish-git.d.ts +37 -0
- package/dist/release/publish-git.js +108 -0
- package/dist/release/publish-state.d.ts +29 -0
- package/dist/release/publish-state.js +70 -0
- package/dist/release/publish-verify.d.ts +41 -0
- package/dist/release/publish-verify.js +112 -0
- package/dist/release/publish-wait.d.ts +25 -0
- package/dist/release/publish-wait.js +56 -0
- package/dist/release/publish.d.ts +35 -0
- package/dist/release/publish.js +181 -0
- package/dist/release/release-assets.d.ts +39 -0
- package/dist/release/release-assets.js +68 -0
- package/dist/release/release.d.ts +37 -0
- package/dist/release/release.js +179 -0
- package/dist/release/remote.d.ts +12 -0
- package/dist/release/remote.js +15 -0
- package/dist/release/reproducible.d.ts +16 -0
- package/dist/release/reproducible.js +34 -0
- package/dist/release/run-tests.d.ts +5 -0
- package/dist/release/run-tests.js +17 -0
- package/dist/release/semver.d.ts +8 -0
- package/dist/release/semver.js +60 -0
- package/dist/release/version-rules.d.ts +28 -0
- package/dist/release/version-rules.js +63 -0
- package/dist/release/versions-json.d.ts +15 -0
- package/dist/release/versions-json.js +88 -0
- package/dist/release/workflow-run.d.ts +44 -0
- package/dist/release/workflow-run.js +71 -0
- package/dist/scanner/code-checks.d.ts +41 -0
- package/dist/scanner/code-checks.js +82 -0
- package/dist/scanner/commands.d.ts +3 -0
- package/dist/scanner/commands.js +43 -0
- package/dist/scanner/config.d.ts +7 -0
- package/dist/scanner/config.js +25 -0
- package/dist/scanner/css-check.d.ts +26 -0
- package/dist/scanner/css-check.js +63 -0
- package/dist/scanner/eslint-nesting.d.ts +16 -0
- package/dist/scanner/eslint-nesting.js +45 -0
- package/dist/scanner/findings.d.ts +23 -0
- package/dist/scanner/findings.js +21 -0
- package/dist/scanner/git-files.d.ts +16 -0
- package/dist/scanner/git-files.js +73 -0
- package/dist/scanner/lint.d.ts +47 -0
- package/dist/scanner/lint.js +163 -0
- package/dist/scanner/meta-checks.d.ts +8 -0
- package/dist/scanner/meta-checks.js +22 -0
- package/dist/scanner/outside-project.d.ts +19 -0
- package/dist/scanner/outside-project.js +20 -0
- package/dist/scanner/run.d.ts +19 -0
- package/dist/scanner/run.js +78 -0
- package/dist/scanner/scanner-config.d.ts +114 -0
- package/dist/scanner/scanner-config.js +207 -0
- package/dist/scanner/typescript-gate.d.ts +30 -0
- package/dist/scanner/typescript-gate.js +73 -0
- package/dist/vitest/build-config.d.ts +17 -0
- package/dist/vitest/build-config.js +106 -0
- package/dist/vitest/define-unit-config.d.ts +27 -0
- package/dist/vitest/define-unit-config.js +45 -0
- package/dist/vitest/dom/elements.d.ts +87 -0
- package/dist/vitest/dom/elements.js +291 -0
- package/dist/vitest/dom/index.d.ts +1 -0
- package/dist/vitest/dom/index.js +1 -0
- package/dist/vitest/dom/install.d.ts +8 -0
- package/dist/vitest/dom/install.js +261 -0
- package/dist/vitest/dom/setup.d.ts +1 -0
- package/dist/vitest/dom/setup.js +3 -0
- package/dist/vitest/index.d.ts +3 -0
- package/dist/vitest/index.js +2 -0
- package/dist/vitest/options.d.ts +92 -0
- package/dist/vitest/options.js +136 -0
- package/dist/vitest/peers.d.ts +10 -0
- package/dist/vitest/peers.js +51 -0
- package/dist/vitest/workspace-root.d.ts +7 -0
- package/dist/vitest/workspace-root.js +39 -0
- package/docs/build.md +232 -0
- package/docs/ci.md +182 -0
- package/docs/demo.md +364 -0
- package/docs/e2e.md +563 -0
- package/docs/release.md +374 -0
- package/docs/scanner.md +204 -0
- package/docs/unit.md +291 -0
- package/package.json +100 -4
- package/plugins/opdk/skills/audit/SKILL.md +81 -0
- package/plugins/opdk/skills/build/SKILL.md +79 -0
- package/plugins/opdk/skills/demo/SKILL.md +102 -0
- package/plugins/opdk/skills/e2e/SKILL.md +132 -0
- package/plugins/opdk/skills/release/SKILL.md +84 -0
- package/plugins/opdk/skills/scanner/SKILL.md +60 -0
- package/plugins/opdk/skills/unit/SKILL.md +90 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
function isListener(value) {
|
|
2
|
+
return typeof value === "function";
|
|
3
|
+
}
|
|
4
|
+
function normalize(info) {
|
|
5
|
+
return typeof info === "string" ? { cls: info } : (info ?? {});
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* `createEl()` of the app: a string is shorthand for `{ cls }`; `cls` (string or array), `text`, `attr` (a null value
|
|
9
|
+
* removes the attribute), `title`, `value` (input, select, option), `type` (input; a `<style>` gets it as an
|
|
10
|
+
* attribute), `placeholder` (input), `href` (a, link, base) are applied in that order; then the callback runs, then
|
|
11
|
+
* the element is inserted into `parent` (first, with `prepend`); then every `on*` function in the info is registered
|
|
12
|
+
* as an event listener (`addEventListener(key.substring(2), fn)`, the function itself, so `removeEventListener`
|
|
13
|
+
* with the same function removes it). The info is not mutated. The text and the attributes go through the
|
|
14
|
+
* installed `el.setText()` and `el.setAttrs()`, as in the app.
|
|
15
|
+
*/
|
|
16
|
+
export function createElement(tag, info, callback) {
|
|
17
|
+
const el = document.createElement(tag);
|
|
18
|
+
const { cls, text, attr, title, value, type, placeholder, href, parent, prepend } = normalize(info);
|
|
19
|
+
if (cls)
|
|
20
|
+
el.className = Array.isArray(cls) ? cls.join(" ") : cls;
|
|
21
|
+
if (text)
|
|
22
|
+
el.setText(text);
|
|
23
|
+
if (attr)
|
|
24
|
+
el.setAttrs(attr);
|
|
25
|
+
if (title !== undefined)
|
|
26
|
+
el.title = title;
|
|
27
|
+
if (value !== undefined && (el instanceof HTMLInputElement || el instanceof HTMLSelectElement || el instanceof HTMLOptionElement)) {
|
|
28
|
+
el.value = value;
|
|
29
|
+
}
|
|
30
|
+
if (type && el instanceof HTMLInputElement)
|
|
31
|
+
el.type = type;
|
|
32
|
+
if (type && el instanceof HTMLStyleElement)
|
|
33
|
+
el.setAttribute("type", type);
|
|
34
|
+
if (placeholder && el instanceof HTMLInputElement)
|
|
35
|
+
el.placeholder = placeholder;
|
|
36
|
+
if (href && (el instanceof HTMLAnchorElement || el instanceof HTMLLinkElement || el instanceof HTMLBaseElement))
|
|
37
|
+
el.href = href;
|
|
38
|
+
callback?.(el);
|
|
39
|
+
if (parent) {
|
|
40
|
+
if (prepend)
|
|
41
|
+
parent.insertBefore(el, parent.firstChild);
|
|
42
|
+
else
|
|
43
|
+
parent.appendChild(el);
|
|
44
|
+
}
|
|
45
|
+
for (const [key, handler] of Object.entries(normalize(info))) {
|
|
46
|
+
if (key.startsWith("on") && isListener(handler))
|
|
47
|
+
el.addEventListener(key.substring(2), handler);
|
|
48
|
+
}
|
|
49
|
+
return el;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* `Node#createEl`: the global `createEl` with `parent` set to the node. As in the app, `parent` is written into the
|
|
53
|
+
* info object the caller passed (a string becomes a new `{ cls }` object).
|
|
54
|
+
*/
|
|
55
|
+
export function createChild(parent, tag, info, callback) {
|
|
56
|
+
const given = normalize(info);
|
|
57
|
+
given.parent = parent;
|
|
58
|
+
return createEl(tag, given, callback);
|
|
59
|
+
}
|
|
60
|
+
/** `createFragment()` of the app: a detached fragment, passed to the callback first. */
|
|
61
|
+
export function createFragment(callback) {
|
|
62
|
+
const fragment = document.createDocumentFragment();
|
|
63
|
+
callback?.(fragment);
|
|
64
|
+
return fragment;
|
|
65
|
+
}
|
|
66
|
+
/** `Element#setText`: a Node or fragment replaces the children, anything else is stringified into textContent. */
|
|
67
|
+
export function setText(el, value) {
|
|
68
|
+
if (value instanceof Node) {
|
|
69
|
+
el.empty();
|
|
70
|
+
el.appendChild(value);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
el.textContent = String(value);
|
|
74
|
+
}
|
|
75
|
+
/** `Element#setAttr`: null removes the attribute, anything else is stringified. */
|
|
76
|
+
export function setAttr(el, name, value) {
|
|
77
|
+
if (value === null)
|
|
78
|
+
el.removeAttribute(name);
|
|
79
|
+
else
|
|
80
|
+
el.setAttribute(name, String(value));
|
|
81
|
+
}
|
|
82
|
+
/** `Element#setAttrs`: `setAttr` for every own key. */
|
|
83
|
+
export function setAttrs(el, attrs) {
|
|
84
|
+
for (const [name, value] of Object.entries(attrs))
|
|
85
|
+
setAttr(el, name, value);
|
|
86
|
+
}
|
|
87
|
+
/** The app's `getText()`: the text content, as the DOM defines it for elements, documents and fragments. */
|
|
88
|
+
export function getText(el) {
|
|
89
|
+
return el.textContent;
|
|
90
|
+
}
|
|
91
|
+
/** `Element#toggleClass`: the value is truthy to add and falsy to remove, so `undefined` removes (as in the app). */
|
|
92
|
+
export function toggleClass(el, classes, value) {
|
|
93
|
+
const list = Array.isArray(classes) ? classes : [classes];
|
|
94
|
+
if (value)
|
|
95
|
+
el.addClasses(list);
|
|
96
|
+
else
|
|
97
|
+
el.removeClasses(list);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* `HTMLElement#hide`: remember an inline display value in `data-display`, then set `display: none`. Does nothing
|
|
101
|
+
* when it is already hidden.
|
|
102
|
+
*/
|
|
103
|
+
export function hide(el) {
|
|
104
|
+
const previous = el.style.display;
|
|
105
|
+
if (previous === "none")
|
|
106
|
+
return;
|
|
107
|
+
el.style.display = "none";
|
|
108
|
+
if (previous)
|
|
109
|
+
el.setAttribute("data-display", previous);
|
|
110
|
+
else
|
|
111
|
+
el.removeAttribute("data-display");
|
|
112
|
+
}
|
|
113
|
+
/** `HTMLElement#show`: undo `hide()`, restoring the remembered display value. Does nothing unless hidden. */
|
|
114
|
+
export function show(el) {
|
|
115
|
+
if (el.style.display !== "none")
|
|
116
|
+
return;
|
|
117
|
+
el.style.display = el.getAttribute("data-display") ?? "";
|
|
118
|
+
el.removeAttribute("data-display");
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* `HTMLElement#isShown`. The app returns `!!offsetParent`, which jsdom (no layout) always reports as null, so a
|
|
122
|
+
* literal port would say false for everything. This one is true for an element that is in the document and has
|
|
123
|
+
* no inline `display: none` on itself or an ancestor.
|
|
124
|
+
*/
|
|
125
|
+
export function isShown(el) {
|
|
126
|
+
if (!el.isConnected)
|
|
127
|
+
return false;
|
|
128
|
+
for (let node = el; node; node = node.parentElement) {
|
|
129
|
+
if (node.style.display === "none")
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
/** `setCssStyles`: assign every own property to `style`. */
|
|
135
|
+
export function setCssStyles(el, styles) {
|
|
136
|
+
Object.assign(el.style, styles);
|
|
137
|
+
}
|
|
138
|
+
/** `setCssProps`: `style.setProperty` for every own key, so custom properties (`--x`) work. */
|
|
139
|
+
export function setCssProps(el, props) {
|
|
140
|
+
for (const [name, value] of Object.entries(props))
|
|
141
|
+
el.style.setProperty(name, value);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* `Node#instanceOf`: the plain operator, then the same class looked up by name on the node's own window and on the
|
|
145
|
+
* window the app's code runs in. With the one jsdom window of a test the `constructorWin` lookup finds what the
|
|
146
|
+
* `win` lookup found, so only the first two steps are reachable (and tested); the third is kept for parity.
|
|
147
|
+
*/
|
|
148
|
+
export function instanceOf(node, type) {
|
|
149
|
+
if (node instanceof type)
|
|
150
|
+
return true;
|
|
151
|
+
const lookup = (window) => {
|
|
152
|
+
const candidate = Reflect.get(window, type.name);
|
|
153
|
+
return typeof candidate === "function" && node instanceof candidate;
|
|
154
|
+
};
|
|
155
|
+
return lookup(node.win) || lookup(node.constructorWin);
|
|
156
|
+
}
|
|
157
|
+
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
158
|
+
/**
|
|
159
|
+
* `createSvg()` of the app: an SVG element in the SVG namespace. A string is shorthand for `{ cls }`; `cls` (string
|
|
160
|
+
* or array) goes to `classList`, then `attr`, then the callback, then insertion into `parent` (first, with `prepend`).
|
|
161
|
+
* Unlike `createEl`, there is no `text`, `title` or `on*` handling.
|
|
162
|
+
*/
|
|
163
|
+
export function createSvgElement(tag, info, callback) {
|
|
164
|
+
const el = document.createElementNS(SVG_NAMESPACE, tag);
|
|
165
|
+
const { cls, attr, parent, prepend } = typeof info === "string" ? { cls: info } : (info ?? {});
|
|
166
|
+
if (cls)
|
|
167
|
+
el.classList.add(...(Array.isArray(cls) ? cls : [cls]));
|
|
168
|
+
if (attr)
|
|
169
|
+
el.setAttrs(attr);
|
|
170
|
+
callback?.(el);
|
|
171
|
+
if (parent) {
|
|
172
|
+
if (prepend)
|
|
173
|
+
parent.insertBefore(el, parent.firstChild);
|
|
174
|
+
else
|
|
175
|
+
parent.appendChild(el);
|
|
176
|
+
}
|
|
177
|
+
return el;
|
|
178
|
+
}
|
|
179
|
+
/** `Node#createSvg`: the global `createSvg` with `parent` set to the node. As with `createChild`, `parent` is written into the info. */
|
|
180
|
+
export function createSvgChild(parent, tag, info, callback) {
|
|
181
|
+
const given = typeof info === "string" ? { cls: info } : (info ?? {});
|
|
182
|
+
given.parent = parent;
|
|
183
|
+
return createSvgElement(tag, given, callback);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* `Node#setChildrenInPlace`: makes the children of `node` exactly `children`, in order, moving as little as possible.
|
|
187
|
+
* A child already in the right place is not touched (it keeps focus and state); a child not in the list is removed.
|
|
188
|
+
*/
|
|
189
|
+
export function setChildrenInPlace(node, children) {
|
|
190
|
+
let current = node.firstChild;
|
|
191
|
+
const wanted = new Set(children);
|
|
192
|
+
for (const child of children) {
|
|
193
|
+
while (current && !wanted.has(current)) {
|
|
194
|
+
const stale = current;
|
|
195
|
+
current = current.nextSibling;
|
|
196
|
+
node.removeChild(stale);
|
|
197
|
+
}
|
|
198
|
+
if (child !== current)
|
|
199
|
+
node.insertBefore(child, current);
|
|
200
|
+
else
|
|
201
|
+
current = current.nextSibling;
|
|
202
|
+
}
|
|
203
|
+
while (current) {
|
|
204
|
+
const stale = current;
|
|
205
|
+
current = current.nextSibling;
|
|
206
|
+
node.removeChild(stale);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* `Element#matchParent`: the element itself or its closest ancestor that matches `selector`, not looking above
|
|
211
|
+
* `lastParent` (which is itself checked), or null.
|
|
212
|
+
*/
|
|
213
|
+
export function matchParent(el, selector, lastParent) {
|
|
214
|
+
for (let node = el; node; node = node.parentElement) {
|
|
215
|
+
if (node.matches(selector))
|
|
216
|
+
return node;
|
|
217
|
+
if (node === lastParent)
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* `Element#isActiveElement`: focused in its own document, and every frame up to the top window is focused in its
|
|
224
|
+
* parent document too, and that top window is the active one.
|
|
225
|
+
*/
|
|
226
|
+
export function isActiveElement(el) {
|
|
227
|
+
for (let node = el;;) {
|
|
228
|
+
if (node.doc.activeElement !== node)
|
|
229
|
+
return false;
|
|
230
|
+
const frame = node.win.frameElement;
|
|
231
|
+
if (!frame)
|
|
232
|
+
return node.win === activeWindow;
|
|
233
|
+
node = frame;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/** The delegated listeners per target, so `off` finds the wrapper `on` registered. The app keeps them in `_EVENTS`. */
|
|
237
|
+
const delegations = new WeakMap();
|
|
238
|
+
/**
|
|
239
|
+
* `on(type, selector, listener, options)`: a listener on the target that runs when the event's target (or an
|
|
240
|
+
* ancestor, up to the target) matches `selector`; `listener` gets the event and the matching element.
|
|
241
|
+
*/
|
|
242
|
+
export function onDelegated(target, type, selector, listener, options) {
|
|
243
|
+
let byType = delegations.get(target);
|
|
244
|
+
if (!byType) {
|
|
245
|
+
byType = new Map();
|
|
246
|
+
delegations.set(target, byType);
|
|
247
|
+
}
|
|
248
|
+
const list = byType.get(type) ?? [];
|
|
249
|
+
byType.set(type, list);
|
|
250
|
+
const callback = (event) => {
|
|
251
|
+
const origin = event.target;
|
|
252
|
+
if (origin instanceof Element) {
|
|
253
|
+
const match = origin.matchParent(selector, event.currentTarget instanceof Element ? event.currentTarget : undefined);
|
|
254
|
+
if (match)
|
|
255
|
+
listener.call(target, event, match);
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
list.push({ selector, listener, options, callback });
|
|
259
|
+
target.addEventListener(type, callback, options);
|
|
260
|
+
}
|
|
261
|
+
/** `off(type, selector, listener, options)`: removes what `onDelegated` registered for the same four arguments. */
|
|
262
|
+
export function offDelegated(target, type, selector, listener, options) {
|
|
263
|
+
const byType = delegations.get(target);
|
|
264
|
+
const list = byType?.get(type);
|
|
265
|
+
if (!byType || !list)
|
|
266
|
+
return;
|
|
267
|
+
byType.set(type, list.filter((entry) => {
|
|
268
|
+
if (entry.selector === selector && entry.listener === listener && entry.options === options) {
|
|
269
|
+
target.removeEventListener(type, entry.callback, options);
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
return true;
|
|
273
|
+
}));
|
|
274
|
+
}
|
|
275
|
+
/** `UIEvent#instanceOf`: the plain operator, then the same class by name on the window the event came from. */
|
|
276
|
+
export function eventInstanceOf(event, type) {
|
|
277
|
+
if (event instanceof type)
|
|
278
|
+
return true;
|
|
279
|
+
const view = event.view;
|
|
280
|
+
if (!view)
|
|
281
|
+
return false;
|
|
282
|
+
const candidate = Reflect.get(view, type.name);
|
|
283
|
+
return typeof candidate === "function" && candidate !== type && event instanceof candidate;
|
|
284
|
+
}
|
|
285
|
+
/** `addEventListeners`: `addEventListener(type, fn)` for every own key whose value is a function; other values are skipped. */
|
|
286
|
+
export function addEventListeners(el, listeners) {
|
|
287
|
+
for (const [type, listener] of Object.entries(listeners)) {
|
|
288
|
+
if (isListener(listener))
|
|
289
|
+
el.addEventListener(type, listener);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { installObsidianDom, uninstallObsidianDom } from "./install.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { installObsidianDom, uninstallObsidianDom } from "./install.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Installs Obsidian's DOM helpers (`createDiv`, `setText`, `addClass`, `activeDocument`, the global `createEl`, ...)
|
|
3
|
+
* on the current jsdom. The list and the behaviour follow the app's own implementation (see `elements.ts`).
|
|
4
|
+
* Calling it again is a no-op. Throws when there is no DOM: run the test file in the jsdom environment.
|
|
5
|
+
*/
|
|
6
|
+
export declare function installObsidianDom(): void;
|
|
7
|
+
/** Puts back what `installObsidianDom()` replaced and removes what it added. Safe to call when nothing is installed. */
|
|
8
|
+
export declare function uninstallObsidianDom(): void;
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { addEventListeners, createChild, createSvgChild, createSvgElement, eventInstanceOf, isActiveElement, matchParent, offDelegated, onDelegated, setChildrenInPlace, createElement, createFragment, getText, hide, instanceOf, isShown, setAttr, setAttrs, setCssProps, setCssStyles, setText, show, toggleClass, } from "./elements.js";
|
|
2
|
+
/** What was there before the install, per object and key, so `uninstallObsidianDom()` can put it back. */
|
|
3
|
+
const originals = new Map();
|
|
4
|
+
/** The prototype the helpers were installed on; a different one means vitest handed out a new DOM. */
|
|
5
|
+
let installedOn;
|
|
6
|
+
function define(target, key, descriptor, onlyIfMissing = false) {
|
|
7
|
+
if (onlyIfMissing && key in target)
|
|
8
|
+
return;
|
|
9
|
+
let saved = originals.get(target);
|
|
10
|
+
if (!saved) {
|
|
11
|
+
saved = new Map();
|
|
12
|
+
originals.set(target, saved);
|
|
13
|
+
}
|
|
14
|
+
if (!saved.has(key))
|
|
15
|
+
saved.set(key, Object.getOwnPropertyDescriptor(target, key));
|
|
16
|
+
Object.defineProperty(target, key, { configurable: true, enumerable: false, ...descriptor });
|
|
17
|
+
}
|
|
18
|
+
function method(target, key, value, onlyIfMissing = false) {
|
|
19
|
+
define(target, key, { value, writable: true }, onlyIfMissing);
|
|
20
|
+
}
|
|
21
|
+
function getter(target, key, get) {
|
|
22
|
+
define(target, key, { get });
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Installs Obsidian's DOM helpers (`createDiv`, `setText`, `addClass`, `activeDocument`, the global `createEl`, ...)
|
|
26
|
+
* on the current jsdom. The list and the behaviour follow the app's own implementation (see `elements.ts`).
|
|
27
|
+
* Calling it again is a no-op. Throws when there is no DOM: run the test file in the jsdom environment.
|
|
28
|
+
*/
|
|
29
|
+
export function installObsidianDom() {
|
|
30
|
+
if (typeof document === "undefined" || typeof Node === "undefined") {
|
|
31
|
+
throw new Error("installObsidianDom() needs a DOM and this test runs in the node environment. " +
|
|
32
|
+
"Add `// @vitest-environment jsdom` to the top of the test file, or list the file in the `dom` option of defineUnitConfig().");
|
|
33
|
+
}
|
|
34
|
+
if (installedOn === Node.prototype)
|
|
35
|
+
return;
|
|
36
|
+
// Within one module instance there is one jsdom window, so Node.prototype never changes and `installedOn` is
|
|
37
|
+
// only unset by uninstall(), which has already emptied `originals`. The clear() is a guard for a Vitest that would
|
|
38
|
+
// reuse this module with a fresh DOM; it cannot be reached from a test.
|
|
39
|
+
originals.clear();
|
|
40
|
+
installedOn = Node.prototype;
|
|
41
|
+
// Node
|
|
42
|
+
const node = Node.prototype;
|
|
43
|
+
getter(node, "doc", function () {
|
|
44
|
+
return this.ownerDocument ?? document;
|
|
45
|
+
});
|
|
46
|
+
getter(node, "win", function () {
|
|
47
|
+
return this.doc.defaultView ?? window;
|
|
48
|
+
});
|
|
49
|
+
define(node, "constructorWin", { value: window, writable: true });
|
|
50
|
+
method(node, "empty", function () {
|
|
51
|
+
while (this.lastChild)
|
|
52
|
+
this.removeChild(this.lastChild);
|
|
53
|
+
});
|
|
54
|
+
method(node, "appendText", function (text) {
|
|
55
|
+
this.appendChild(document.createTextNode(text));
|
|
56
|
+
});
|
|
57
|
+
method(node, "instanceOf", function (type) {
|
|
58
|
+
return instanceOf(this, type);
|
|
59
|
+
});
|
|
60
|
+
method(node, "createEl", function (tag, info, cb) {
|
|
61
|
+
return createChild(this, tag, info, cb);
|
|
62
|
+
});
|
|
63
|
+
method(node, "detach", function () {
|
|
64
|
+
this.parentNode?.removeChild(this);
|
|
65
|
+
});
|
|
66
|
+
method(node, "insertAfter", function (child, reference) {
|
|
67
|
+
this.insertBefore(child, reference ? reference.nextSibling : this.firstChild);
|
|
68
|
+
return child;
|
|
69
|
+
});
|
|
70
|
+
method(node, "indexOf", function (child) {
|
|
71
|
+
return Array.prototype.indexOf.call(this.childNodes, child);
|
|
72
|
+
});
|
|
73
|
+
method(node, "setChildrenInPlace", function (children) {
|
|
74
|
+
setChildrenInPlace(this, children);
|
|
75
|
+
});
|
|
76
|
+
method(node, "createSvg", function (tag, info, cb) {
|
|
77
|
+
return createSvgChild(this, tag, info, cb);
|
|
78
|
+
});
|
|
79
|
+
// createDiv and createSpan go through this.createEl(), and the globals through the global createEl, as in the app.
|
|
80
|
+
method(node, "createDiv", function (info, cb) {
|
|
81
|
+
return this.createEl("div", info, cb);
|
|
82
|
+
});
|
|
83
|
+
method(node, "createSpan", function (info, cb) {
|
|
84
|
+
return this.createEl("span", info, cb);
|
|
85
|
+
});
|
|
86
|
+
// Element
|
|
87
|
+
const element = Element.prototype;
|
|
88
|
+
method(element, "setText", function (value) {
|
|
89
|
+
setText(this, value);
|
|
90
|
+
});
|
|
91
|
+
method(element, "getText", function () {
|
|
92
|
+
return getText(this);
|
|
93
|
+
});
|
|
94
|
+
method(element, "setAttr", function (name, value) {
|
|
95
|
+
setAttr(this, name, value);
|
|
96
|
+
});
|
|
97
|
+
method(element, "setAttrs", function (attrs) {
|
|
98
|
+
setAttrs(this, attrs);
|
|
99
|
+
});
|
|
100
|
+
method(element, "getAttr", function (name) {
|
|
101
|
+
return this.getAttribute(name);
|
|
102
|
+
});
|
|
103
|
+
method(element, "addClass", function (...classes) {
|
|
104
|
+
this.addClasses(classes);
|
|
105
|
+
});
|
|
106
|
+
method(element, "addClasses", function (classes) {
|
|
107
|
+
for (const cls of classes)
|
|
108
|
+
this.classList.add(cls);
|
|
109
|
+
});
|
|
110
|
+
method(element, "removeClass", function (...classes) {
|
|
111
|
+
this.removeClasses(classes);
|
|
112
|
+
});
|
|
113
|
+
method(element, "removeClasses", function (classes) {
|
|
114
|
+
for (const cls of classes)
|
|
115
|
+
this.classList.remove(cls);
|
|
116
|
+
});
|
|
117
|
+
method(element, "toggleClass", function (classes, value) {
|
|
118
|
+
toggleClass(this, classes, value);
|
|
119
|
+
});
|
|
120
|
+
method(element, "hasClass", function (cls) {
|
|
121
|
+
return this.classList.contains(cls);
|
|
122
|
+
});
|
|
123
|
+
method(element, "find", function (selector) {
|
|
124
|
+
return this.querySelector(selector);
|
|
125
|
+
});
|
|
126
|
+
method(element, "findAll", function (selector) {
|
|
127
|
+
return Array.from(this.querySelectorAll(selector));
|
|
128
|
+
});
|
|
129
|
+
method(element, "findAllSelf", function (selector) {
|
|
130
|
+
const found = Array.from(this.querySelectorAll(selector));
|
|
131
|
+
if (this.matches(selector))
|
|
132
|
+
found.unshift(this);
|
|
133
|
+
return found;
|
|
134
|
+
});
|
|
135
|
+
method(element, "matchParent", function (selector, lastParent) {
|
|
136
|
+
return matchParent(this, selector, lastParent);
|
|
137
|
+
});
|
|
138
|
+
method(element, "getCssPropertyValue", function (property, pseudoElement) {
|
|
139
|
+
return getComputedStyle(this, pseudoElement).getPropertyValue(property).trim();
|
|
140
|
+
});
|
|
141
|
+
method(element, "isActiveElement", function () {
|
|
142
|
+
return isActiveElement(this);
|
|
143
|
+
});
|
|
144
|
+
// jsdom has no layout engine and no scrollIntoView; browsers (and the app) have it, tests need a no-op.
|
|
145
|
+
method(element, "scrollIntoView", function () {
|
|
146
|
+
/* nothing to scroll without layout */
|
|
147
|
+
}, true);
|
|
148
|
+
// DocumentFragment: find and findAll only (it is a Node, so the create helpers come from there).
|
|
149
|
+
const fragment = DocumentFragment.prototype;
|
|
150
|
+
method(fragment, "find", function (selector) {
|
|
151
|
+
return this.querySelector(selector);
|
|
152
|
+
});
|
|
153
|
+
method(fragment, "findAll", function (selector) {
|
|
154
|
+
return Array.from(this.querySelectorAll(selector));
|
|
155
|
+
});
|
|
156
|
+
// HTMLElement
|
|
157
|
+
const html = HTMLElement.prototype;
|
|
158
|
+
method(html, "show", function () {
|
|
159
|
+
show(this);
|
|
160
|
+
}, true);
|
|
161
|
+
method(html, "hide", function () {
|
|
162
|
+
hide(this);
|
|
163
|
+
}, true);
|
|
164
|
+
method(html, "toggle", function (visible) {
|
|
165
|
+
if (visible)
|
|
166
|
+
this.show();
|
|
167
|
+
else
|
|
168
|
+
this.hide();
|
|
169
|
+
}, true);
|
|
170
|
+
method(html, "toggleVisibility", function (visible) {
|
|
171
|
+
this.style.visibility = visible ? "" : "hidden";
|
|
172
|
+
}, true);
|
|
173
|
+
method(html, "addEventListeners", function (listeners) {
|
|
174
|
+
addEventListeners(this, listeners);
|
|
175
|
+
});
|
|
176
|
+
method(html, "trigger", function (eventType) {
|
|
177
|
+
this.dispatchEvent(new Event(eventType, { bubbles: true, cancelable: false }));
|
|
178
|
+
});
|
|
179
|
+
// The app's scrollWidth minus padding. jsdom reports scrollWidth 0 (no layout), so this is 0 minus the padding.
|
|
180
|
+
getter(html, "innerWidth", function () {
|
|
181
|
+
const style = getComputedStyle(this);
|
|
182
|
+
return this.scrollWidth - (parseFloat(style.paddingLeft) || 0) - (parseFloat(style.paddingRight) || 0);
|
|
183
|
+
});
|
|
184
|
+
getter(html, "innerHeight", function () {
|
|
185
|
+
const style = getComputedStyle(this);
|
|
186
|
+
return this.scrollHeight - (parseFloat(style.paddingTop) || 0) - (parseFloat(style.paddingBottom) || 0);
|
|
187
|
+
});
|
|
188
|
+
method(html, "isShown", function () {
|
|
189
|
+
return isShown(this);
|
|
190
|
+
});
|
|
191
|
+
method(html, "setCssStyles", function (styles) {
|
|
192
|
+
setCssStyles(this, styles);
|
|
193
|
+
});
|
|
194
|
+
method(html, "setCssProps", function (props) {
|
|
195
|
+
setCssProps(this, props);
|
|
196
|
+
});
|
|
197
|
+
method(html, "onClickEvent", function (listener, options) {
|
|
198
|
+
this.addEventListener("click", listener, options);
|
|
199
|
+
this.addEventListener("auxclick", listener, options);
|
|
200
|
+
});
|
|
201
|
+
for (const target of [html, Document.prototype]) {
|
|
202
|
+
method(target, "on", function (type, selector, listener, options) {
|
|
203
|
+
onDelegated(this, type, selector, listener, options);
|
|
204
|
+
});
|
|
205
|
+
method(target, "off", function (type, selector, listener, options) {
|
|
206
|
+
offDelegated(this, type, selector, listener, options);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
method(SVGElement.prototype, "setCssStyles", function (styles) {
|
|
210
|
+
setCssStyles(this, styles);
|
|
211
|
+
});
|
|
212
|
+
method(SVGElement.prototype, "setCssProps", function (props) {
|
|
213
|
+
setCssProps(this, props);
|
|
214
|
+
});
|
|
215
|
+
// UIEvent
|
|
216
|
+
const uiEvent = UIEvent.prototype;
|
|
217
|
+
getter(uiEvent, "targetNode", function () {
|
|
218
|
+
return this.target;
|
|
219
|
+
});
|
|
220
|
+
getter(uiEvent, "win", function () {
|
|
221
|
+
return this.view ?? window;
|
|
222
|
+
});
|
|
223
|
+
getter(uiEvent, "doc", function () {
|
|
224
|
+
return this.win.document;
|
|
225
|
+
});
|
|
226
|
+
method(uiEvent, "instanceOf", function (type) {
|
|
227
|
+
return eventInstanceOf(this, type);
|
|
228
|
+
});
|
|
229
|
+
// Globals the app puts on `window`. Here `window` is the global scope vitest builds from jsdom.
|
|
230
|
+
method(globalThis, "createEl", createElement);
|
|
231
|
+
method(globalThis, "createDiv", (info, cb) => createEl("div", info, cb));
|
|
232
|
+
method(globalThis, "createSpan", (info, cb) => createEl("span", info, cb));
|
|
233
|
+
method(globalThis, "createSvg", createSvgElement);
|
|
234
|
+
method(globalThis, "createFragment", createFragment);
|
|
235
|
+
method(globalThis, "fish", (selector) => document.querySelector(selector));
|
|
236
|
+
method(globalThis, "fishAll", (selector) => Array.from(document.querySelectorAll(selector)));
|
|
237
|
+
method(globalThis, "ready", (callback) => {
|
|
238
|
+
if (document.readyState !== "loading")
|
|
239
|
+
callback();
|
|
240
|
+
else
|
|
241
|
+
document.addEventListener("DOMContentLoaded", callback);
|
|
242
|
+
});
|
|
243
|
+
method(globalThis, "sleep", (ms) => new Promise((resolve) => void window.setTimeout(resolve, ms)));
|
|
244
|
+
method(globalThis, "nextFrame", () => new Promise((resolve) => void window.requestAnimationFrame(() => { resolve(); })));
|
|
245
|
+
// One window only, so the active window and document are the global ones.
|
|
246
|
+
define(globalThis, "activeWindow", { value: window, writable: true }, true);
|
|
247
|
+
define(globalThis, "activeDocument", { value: document, writable: true }, true);
|
|
248
|
+
}
|
|
249
|
+
/** Puts back what `installObsidianDom()` replaced and removes what it added. Safe to call when nothing is installed. */
|
|
250
|
+
export function uninstallObsidianDom() {
|
|
251
|
+
for (const [target, saved] of originals) {
|
|
252
|
+
for (const [key, original] of saved) {
|
|
253
|
+
if (original)
|
|
254
|
+
Object.defineProperty(target, key, original);
|
|
255
|
+
else
|
|
256
|
+
Reflect.deleteProperty(target, key);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
originals.clear();
|
|
260
|
+
installedOn = undefined;
|
|
261
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** Coverage thresholds in percent. */
|
|
2
|
+
export interface CoverageThresholds {
|
|
3
|
+
readonly lines: number;
|
|
4
|
+
readonly functions: number;
|
|
5
|
+
readonly statements: number;
|
|
6
|
+
readonly branches: number;
|
|
7
|
+
}
|
|
8
|
+
/** What three plugins gate on today. A floor, not a goal. */
|
|
9
|
+
export declare const DEFAULT_THRESHOLDS: CoverageThresholds;
|
|
10
|
+
/** Always left out of coverage: the tests themselves, the test helpers and type declarations. */
|
|
11
|
+
export declare const BASE_COVERAGE_EXCLUDE: readonly string[];
|
|
12
|
+
/** The `obsidian` value that selects the obsidian-test-mocks package instead of a plugin stub. */
|
|
13
|
+
export declare const OBSIDIAN_TEST_MOCKS = "obsidian-test-mocks";
|
|
14
|
+
export interface UnitCoverageOptions {
|
|
15
|
+
/** Required. Globs of the files the gate counts, for example `["src/**\/*.ts"]`. Unlisted files escape the gate. */
|
|
16
|
+
readonly include: readonly string[];
|
|
17
|
+
/** Added to {@link BASE_COVERAGE_EXCLUDE}. */
|
|
18
|
+
readonly exclude?: readonly string[];
|
|
19
|
+
/** Raise individual thresholds. A value below the default needs `allowLowerThresholds: true`. */
|
|
20
|
+
readonly thresholds?: Partial<CoverageThresholds>;
|
|
21
|
+
/** Lets `thresholds` go below {@link DEFAULT_THRESHOLDS}. Without it a lower value is an error. */
|
|
22
|
+
readonly allowLowerThresholds?: boolean;
|
|
23
|
+
/** Reports written next to `text` and `json-summary`. */
|
|
24
|
+
readonly extraReporters?: readonly ("lcov" | "html")[];
|
|
25
|
+
}
|
|
26
|
+
/** The `{ node, dom }` form of the `obsidian` option. */
|
|
27
|
+
export interface ObsidianSplitOption {
|
|
28
|
+
/** Stub path for the test files that run in node, relative to `rootDir`. */
|
|
29
|
+
readonly node: string;
|
|
30
|
+
/** `"obsidian-test-mocks"`, or a stub path relative to `rootDir`, for the test files that run in jsdom. */
|
|
31
|
+
readonly dom: string;
|
|
32
|
+
}
|
|
33
|
+
export interface UnitConfigOptions {
|
|
34
|
+
/** Required. See {@link UnitCoverageOptions}. */
|
|
35
|
+
readonly coverage: UnitCoverageOptions;
|
|
36
|
+
/**
|
|
37
|
+
* How `import "obsidian"` resolves in tests: a path to the plugin's own stub, relative to `rootDir`, or
|
|
38
|
+
* `"obsidian-test-mocks"` (the package's `vitest-setup` mocks the module and installs the DOM helpers; the
|
|
39
|
+
* package must be a dependency of the plugin, and it needs `dom`). An object `{ node, dom }` picks one per
|
|
40
|
+
* project: `node` is a stub path for the node tests, `dom` is `"obsidian-test-mocks"` or a stub path for the
|
|
41
|
+
* DOM tests (with `dom: "all"` there is no node project and `node` is not used). Omitted: no alias.
|
|
42
|
+
*/
|
|
43
|
+
readonly obsidian?: string | ObsidianSplitOption;
|
|
44
|
+
/**
|
|
45
|
+
* Test files that need a DOM, as globs relative to the Vitest root (the folder of `vitest.config.ts`). They run
|
|
46
|
+
* in jsdom with Obsidian's DOM helpers installed; every other test file runs in node. `"all"` puts every test file in jsdom. Omitted or `[]`: all node.
|
|
47
|
+
*/
|
|
48
|
+
readonly dom?: readonly string[] | "all";
|
|
49
|
+
/** Test file globs, relative to the Vitest root (the folder of `vitest.config.ts`). Default `["src/**\/*.test.ts"]`. */
|
|
50
|
+
readonly include?: readonly string[];
|
|
51
|
+
/** The folder the `obsidian` stub path is resolved against (nothing else uses it). Default `process.cwd()`; pass `import.meta.dirname`. */
|
|
52
|
+
readonly rootDir?: string;
|
|
53
|
+
}
|
|
54
|
+
export type ObsidianMode = {
|
|
55
|
+
readonly kind: "none";
|
|
56
|
+
} | {
|
|
57
|
+
readonly kind: "stub";
|
|
58
|
+
readonly path: string;
|
|
59
|
+
} | {
|
|
60
|
+
readonly kind: "mocks";
|
|
61
|
+
} | {
|
|
62
|
+
readonly kind: "split";
|
|
63
|
+
readonly node: string;
|
|
64
|
+
readonly dom: {
|
|
65
|
+
readonly kind: "mocks";
|
|
66
|
+
} | {
|
|
67
|
+
readonly kind: "stub";
|
|
68
|
+
readonly path: string;
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
export type DomMode = {
|
|
72
|
+
readonly kind: "none";
|
|
73
|
+
} | {
|
|
74
|
+
readonly kind: "all";
|
|
75
|
+
} | {
|
|
76
|
+
readonly kind: "split";
|
|
77
|
+
readonly globs: readonly string[];
|
|
78
|
+
};
|
|
79
|
+
export interface ResolvedUnitOptions {
|
|
80
|
+
readonly rootDir: string;
|
|
81
|
+
readonly include: readonly string[];
|
|
82
|
+
readonly obsidian: ObsidianMode;
|
|
83
|
+
readonly dom: DomMode;
|
|
84
|
+
readonly coverage: {
|
|
85
|
+
readonly include: readonly string[];
|
|
86
|
+
readonly exclude: readonly string[];
|
|
87
|
+
readonly thresholds: CoverageThresholds;
|
|
88
|
+
readonly reporters: readonly string[];
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** Applies the defaults and rejects values of the wrong type, naming the option and what to do. */
|
|
92
|
+
export declare function resolveUnitOptions(options: UnitConfigOptions, cwd: string): ResolvedUnitOptions;
|