@shadow-js/web 0.0.0-stage → 1.0.0
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/CHANGELOG.md +12 -0
- package/LICENSE +21 -0
- package/README.md +190 -2
- package/dist/dev/attributes.d.ts +90 -0
- package/dist/dev/attributes.js +196 -0
- package/dist/dev/attributes.js.map +1 -0
- package/dist/dev/autofocus.d.ts +32 -0
- package/dist/dev/autofocus.js +45 -0
- package/dist/dev/autofocus.js.map +1 -0
- package/dist/dev/class-style.d.ts +89 -0
- package/dist/dev/class-style.js +197 -0
- package/dist/dev/class-style.js.map +1 -0
- package/dist/dev/component.d.ts +23 -0
- package/dist/dev/component.js +42 -0
- package/dist/dev/component.js.map +1 -0
- package/dist/dev/control-flow/activity.d.ts +58 -0
- package/dist/dev/control-flow/activity.js +136 -0
- package/dist/dev/control-flow/activity.js.map +1 -0
- package/dist/dev/control-flow/branch.js +104 -0
- package/dist/dev/control-flow/branch.js.map +1 -0
- package/dist/dev/control-flow/client-only.d.ts +48 -0
- package/dist/dev/control-flow/client-only.js +50 -0
- package/dist/dev/control-flow/client-only.js.map +1 -0
- package/dist/dev/control-flow/dynamic.d.ts +82 -0
- package/dist/dev/control-flow/dynamic.js +134 -0
- package/dist/dev/control-flow/dynamic.js.map +1 -0
- package/dist/dev/control-flow/error-boundary.d.ts +72 -0
- package/dist/dev/control-flow/error-boundary.js +115 -0
- package/dist/dev/control-flow/error-boundary.js.map +1 -0
- package/dist/dev/control-flow/for.d.ts +80 -0
- package/dist/dev/control-flow/for.js +23 -0
- package/dist/dev/control-flow/for.js.map +1 -0
- package/dist/dev/control-flow/fragment.d.ts +38 -0
- package/dist/dev/control-flow/fragment.js +31 -0
- package/dist/dev/control-flow/fragment.js.map +1 -0
- package/dist/dev/control-flow/hot-link.js +64 -0
- package/dist/dev/control-flow/hot-link.js.map +1 -0
- package/dist/dev/control-flow/index-list.d.ts +48 -0
- package/dist/dev/control-flow/index-list.js +31 -0
- package/dist/dev/control-flow/index-list.js.map +1 -0
- package/dist/dev/control-flow/index.d.ts +15 -0
- package/dist/dev/control-flow/lazy.d.ts +58 -0
- package/dist/dev/control-flow/lazy.js +64 -0
- package/dist/dev/control-flow/lazy.js.map +1 -0
- package/dist/dev/control-flow/no-hydration.d.ts +39 -0
- package/dist/dev/control-flow/no-hydration.js +58 -0
- package/dist/dev/control-flow/no-hydration.js.map +1 -0
- package/dist/dev/control-flow/portal.d.ts +69 -0
- package/dist/dev/control-flow/portal.js +134 -0
- package/dist/dev/control-flow/portal.js.map +1 -0
- package/dist/dev/control-flow/root-errors.d.ts +25 -0
- package/dist/dev/control-flow/root-errors.js +33 -0
- package/dist/dev/control-flow/root-errors.js.map +1 -0
- package/dist/dev/control-flow/show.d.ts +101 -0
- package/dist/dev/control-flow/show.js +18 -0
- package/dist/dev/control-flow/show.js.map +1 -0
- package/dist/dev/control-flow/suspense-list.d.ts +65 -0
- package/dist/dev/control-flow/suspense-list.js +105 -0
- package/dist/dev/control-flow/suspense-list.js.map +1 -0
- package/dist/dev/control-flow/suspense.d.ts +53 -0
- package/dist/dev/control-flow/suspense.js +102 -0
- package/dist/dev/control-flow/suspense.js.map +1 -0
- package/dist/dev/control-flow/switch.d.ts +85 -0
- package/dist/dev/control-flow/switch.js +90 -0
- package/dist/dev/control-flow/switch.js.map +1 -0
- package/dist/dev/derived.d.ts +52 -0
- package/dist/dev/derived.js +34 -0
- package/dist/dev/derived.js.map +1 -0
- package/dist/dev/element.d.ts +40 -0
- package/dist/dev/element.js +105 -0
- package/dist/dev/element.js.map +1 -0
- package/dist/dev/events.d.ts +87 -0
- package/dist/dev/events.js +265 -0
- package/dist/dev/events.js.map +1 -0
- package/dist/dev/extend.d.ts +12 -0
- package/dist/dev/extend.js +12 -0
- package/dist/dev/head/components.d.ts +137 -0
- package/dist/dev/head/components.js +170 -0
- package/dist/dev/head/components.js.map +1 -0
- package/dist/dev/head/registry.js +68 -0
- package/dist/dev/head/registry.js.map +1 -0
- package/dist/dev/head/server-head.d.ts +54 -0
- package/dist/dev/head/server-head.js +76 -0
- package/dist/dev/head/server-head.js.map +1 -0
- package/dist/dev/head/sink.js +96 -0
- package/dist/dev/head/sink.js.map +1 -0
- package/dist/dev/head/use-head.d.ts +35 -0
- package/dist/dev/head/use-head.js +84 -0
- package/dist/dev/head/use-head.js.map +1 -0
- package/dist/dev/head/validate.d.ts +85 -0
- package/dist/dev/head/validate.js +268 -0
- package/dist/dev/head/validate.js.map +1 -0
- package/dist/dev/hmr.d.ts +82 -0
- package/dist/dev/hmr.js +124 -0
- package/dist/dev/hmr.js.map +1 -0
- package/dist/dev/hydration.d.ts +61 -0
- package/dist/dev/hydration.js +120 -0
- package/dist/dev/hydration.js.map +1 -0
- package/dist/dev/index.d.ts +45 -0
- package/dist/dev/index.js +34 -0
- package/dist/dev/insert.d.ts +97 -0
- package/dist/dev/insert.js +357 -0
- package/dist/dev/insert.js.map +1 -0
- package/dist/dev/internal/claim.d.ts +106 -0
- package/dist/dev/internal/claim.js +357 -0
- package/dist/dev/internal/claim.js.map +1 -0
- package/dist/dev/internal/css-units.js +26 -0
- package/dist/dev/internal/css-units.js.map +1 -0
- package/dist/dev/internal/diagnostics.d.ts +30 -0
- package/dist/dev/internal/diagnostics.js +38 -0
- package/dist/dev/internal/diagnostics.js.map +1 -0
- package/dist/dev/internal/early-events.js +76 -0
- package/dist/dev/internal/early-events.js.map +1 -0
- package/dist/dev/internal/hydration-hooks.d.ts +43 -0
- package/dist/dev/internal/hydration-hooks.js +27 -0
- package/dist/dev/internal/hydration-hooks.js.map +1 -0
- package/dist/dev/internal/hydration-scope.d.ts +131 -0
- package/dist/dev/internal/hydration-scope.js +156 -0
- package/dist/dev/internal/hydration-scope.js.map +1 -0
- package/dist/dev/internal/names.d.ts +33 -0
- package/dist/dev/internal/names.js +35 -0
- package/dist/dev/internal/names.js.map +1 -0
- package/dist/dev/internal/portal-context.d.ts +39 -0
- package/dist/dev/internal/portal-context.js +28 -0
- package/dist/dev/internal/portal-context.js.map +1 -0
- package/dist/dev/internal/react-names.js +116 -0
- package/dist/dev/internal/react-names.js.map +1 -0
- package/dist/dev/internal/reconcile.d.ts +40 -0
- package/dist/dev/internal/reconcile.js +122 -0
- package/dist/dev/internal/reconcile.js.map +1 -0
- package/dist/dev/internal/stream-context.d.ts +47 -0
- package/dist/dev/internal/stream-context.js +23 -0
- package/dist/dev/internal/stream-context.js.map +1 -0
- package/dist/dev/jsx/aria.d.ts +72 -0
- package/dist/dev/jsx/component.d.ts +89 -0
- package/dist/dev/jsx/events.d.ts +364 -0
- package/dist/dev/jsx/global.d.ts +211 -0
- package/dist/dev/jsx/html.d.ts +879 -0
- package/dist/dev/jsx/intrinsic.d.ts +59 -0
- package/dist/dev/jsx/mathml.d.ts +186 -0
- package/dist/dev/jsx/svg.d.ts +607 -0
- package/dist/dev/jsx/types.d.ts +176 -0
- package/dist/dev/jsx-dev-runtime.d.ts +10 -0
- package/dist/dev/jsx-dev-runtime.js +0 -0
- package/dist/dev/jsx-runtime.d.ts +94 -0
- package/dist/dev/jsx-runtime.js +0 -0
- package/dist/dev/reactive.d.ts +110 -0
- package/dist/dev/reactive.js +85 -0
- package/dist/dev/reactive.js.map +1 -0
- package/dist/dev/render.d.ts +36 -0
- package/dist/dev/render.js +52 -0
- package/dist/dev/render.js.map +1 -0
- package/dist/dev/resource-hints.d.ts +145 -0
- package/dist/dev/resource-hints.js +200 -0
- package/dist/dev/resource-hints.js.map +1 -0
- package/dist/dev/select-value.d.ts +24 -0
- package/dist/dev/select-value.js +51 -0
- package/dist/dev/select-value.js.map +1 -0
- package/dist/dev/server/attributes.d.ts +33 -0
- package/dist/dev/server/attributes.js +155 -0
- package/dist/dev/server/attributes.js.map +1 -0
- package/dist/dev/server/class-style.d.ts +46 -0
- package/dist/dev/server/class-style.js +173 -0
- package/dist/dev/server/class-style.js.map +1 -0
- package/dist/dev/server/component.d.ts +42 -0
- package/dist/dev/server/component.js +72 -0
- package/dist/dev/server/component.js.map +1 -0
- package/dist/dev/server/derived.d.ts +41 -0
- package/dist/dev/server/derived.js +26 -0
- package/dist/dev/server/derived.js.map +1 -0
- package/dist/dev/server/element.d.ts +89 -0
- package/dist/dev/server/element.js +216 -0
- package/dist/dev/server/element.js.map +1 -0
- package/dist/dev/server/errors.js +26 -0
- package/dist/dev/server/errors.js.map +1 -0
- package/dist/dev/server/escape.d.ts +30 -0
- package/dist/dev/server/escape.js +119 -0
- package/dist/dev/server/escape.js.map +1 -0
- package/dist/dev/server/form-controls.d.ts +50 -0
- package/dist/dev/server/form-controls.js +193 -0
- package/dist/dev/server/form-controls.js.map +1 -0
- package/dist/dev/server/hydration-script.d.ts +65 -0
- package/dist/dev/server/hydration-script.js +59 -0
- package/dist/dev/server/hydration-script.js.map +1 -0
- package/dist/dev/server/index.d.ts +16 -0
- package/dist/dev/server/index.js +16 -0
- package/dist/dev/server/node.d.ts +13 -0
- package/dist/dev/server/node.js +21 -0
- package/dist/dev/server/node.js.map +1 -0
- package/dist/dev/server/portals.d.ts +46 -0
- package/dist/dev/server/portals.js +99 -0
- package/dist/dev/server/portals.js.map +1 -0
- package/dist/dev/server/render.d.ts +129 -0
- package/dist/dev/server/render.js +256 -0
- package/dist/dev/server/render.js.map +1 -0
- package/dist/dev/server/serialize.d.ts +45 -0
- package/dist/dev/server/serialize.js +315 -0
- package/dist/dev/server/serialize.js.map +1 -0
- package/dist/dev/server/stream.d.ts +94 -0
- package/dist/dev/server/stream.js +235 -0
- package/dist/dev/server/stream.js.map +1 -0
- package/dist/dev/server.d.ts +16 -0
- package/dist/dev/server.js +16 -0
- package/dist/dev/spread.d.ts +49 -0
- package/dist/dev/spread.js +296 -0
- package/dist/dev/spread.js.map +1 -0
- package/dist/dev/template.d.ts +26 -0
- package/dist/dev/template.js +120 -0
- package/dist/dev/template.js.map +1 -0
- package/dist/dev/view-transition.d.ts +97 -0
- package/dist/dev/view-transition.js +121 -0
- package/dist/dev/view-transition.js.map +1 -0
- package/dist/prod/attributes.js +2 -0
- package/dist/prod/attributes.js.map +1 -0
- package/dist/prod/autofocus.js +2 -0
- package/dist/prod/autofocus.js.map +1 -0
- package/dist/prod/class-style.js +2 -0
- package/dist/prod/class-style.js.map +1 -0
- package/dist/prod/component.js +2 -0
- package/dist/prod/component.js.map +1 -0
- package/dist/prod/control-flow/activity.js +2 -0
- package/dist/prod/control-flow/activity.js.map +1 -0
- package/dist/prod/control-flow/branch.js +2 -0
- package/dist/prod/control-flow/branch.js.map +1 -0
- package/dist/prod/control-flow/client-only.js +2 -0
- package/dist/prod/control-flow/client-only.js.map +1 -0
- package/dist/prod/control-flow/dynamic.js +2 -0
- package/dist/prod/control-flow/dynamic.js.map +1 -0
- package/dist/prod/control-flow/error-boundary.js +2 -0
- package/dist/prod/control-flow/error-boundary.js.map +1 -0
- package/dist/prod/control-flow/for.js +2 -0
- package/dist/prod/control-flow/for.js.map +1 -0
- package/dist/prod/control-flow/fragment.js +2 -0
- package/dist/prod/control-flow/fragment.js.map +1 -0
- package/dist/prod/control-flow/hot-link.js +2 -0
- package/dist/prod/control-flow/hot-link.js.map +1 -0
- package/dist/prod/control-flow/index-list.js +2 -0
- package/dist/prod/control-flow/index-list.js.map +1 -0
- package/dist/prod/control-flow/lazy.js +2 -0
- package/dist/prod/control-flow/lazy.js.map +1 -0
- package/dist/prod/control-flow/no-hydration.js +2 -0
- package/dist/prod/control-flow/no-hydration.js.map +1 -0
- package/dist/prod/control-flow/portal.js +2 -0
- package/dist/prod/control-flow/portal.js.map +1 -0
- package/dist/prod/control-flow/root-errors.js +2 -0
- package/dist/prod/control-flow/root-errors.js.map +1 -0
- package/dist/prod/control-flow/show.js +2 -0
- package/dist/prod/control-flow/show.js.map +1 -0
- package/dist/prod/control-flow/suspense-list.js +2 -0
- package/dist/prod/control-flow/suspense-list.js.map +1 -0
- package/dist/prod/control-flow/suspense.js +2 -0
- package/dist/prod/control-flow/suspense.js.map +1 -0
- package/dist/prod/control-flow/switch.js +2 -0
- package/dist/prod/control-flow/switch.js.map +1 -0
- package/dist/prod/derived.js +2 -0
- package/dist/prod/derived.js.map +1 -0
- package/dist/prod/element.js +2 -0
- package/dist/prod/element.js.map +1 -0
- package/dist/prod/events.js +2 -0
- package/dist/prod/events.js.map +1 -0
- package/dist/prod/extend.js +1 -0
- package/dist/prod/head/components.js +2 -0
- package/dist/prod/head/components.js.map +1 -0
- package/dist/prod/head/registry.js +2 -0
- package/dist/prod/head/registry.js.map +1 -0
- package/dist/prod/head/server-head.js +2 -0
- package/dist/prod/head/server-head.js.map +1 -0
- package/dist/prod/head/sink.js +2 -0
- package/dist/prod/head/sink.js.map +1 -0
- package/dist/prod/head/use-head.js +2 -0
- package/dist/prod/head/use-head.js.map +1 -0
- package/dist/prod/head/validate.js +2 -0
- package/dist/prod/head/validate.js.map +1 -0
- package/dist/prod/hmr.js +2 -0
- package/dist/prod/hmr.js.map +1 -0
- package/dist/prod/hydration.js +2 -0
- package/dist/prod/hydration.js.map +1 -0
- package/dist/prod/index.js +1 -0
- package/dist/prod/insert.js +2 -0
- package/dist/prod/insert.js.map +1 -0
- package/dist/prod/internal/claim.js +2 -0
- package/dist/prod/internal/claim.js.map +1 -0
- package/dist/prod/internal/css-units.js +2 -0
- package/dist/prod/internal/css-units.js.map +1 -0
- package/dist/prod/internal/diagnostics.js +2 -0
- package/dist/prod/internal/diagnostics.js.map +1 -0
- package/dist/prod/internal/early-events.js +2 -0
- package/dist/prod/internal/early-events.js.map +1 -0
- package/dist/prod/internal/hydration-hooks.js +2 -0
- package/dist/prod/internal/hydration-hooks.js.map +1 -0
- package/dist/prod/internal/hydration-scope.js +2 -0
- package/dist/prod/internal/hydration-scope.js.map +1 -0
- package/dist/prod/internal/names.js +2 -0
- package/dist/prod/internal/names.js.map +1 -0
- package/dist/prod/internal/portal-context.js +2 -0
- package/dist/prod/internal/portal-context.js.map +1 -0
- package/dist/prod/internal/react-names.js +2 -0
- package/dist/prod/internal/react-names.js.map +1 -0
- package/dist/prod/internal/reconcile.js +2 -0
- package/dist/prod/internal/reconcile.js.map +1 -0
- package/dist/prod/internal/stream-context.js +2 -0
- package/dist/prod/internal/stream-context.js.map +1 -0
- package/dist/prod/jsx-dev-runtime.js +0 -0
- package/dist/prod/jsx-runtime.js +0 -0
- package/dist/prod/reactive.js +2 -0
- package/dist/prod/reactive.js.map +1 -0
- package/dist/prod/render.js +2 -0
- package/dist/prod/render.js.map +1 -0
- package/dist/prod/resource-hints.js +2 -0
- package/dist/prod/resource-hints.js.map +1 -0
- package/dist/prod/select-value.js +2 -0
- package/dist/prod/select-value.js.map +1 -0
- package/dist/prod/server/attributes.js +2 -0
- package/dist/prod/server/attributes.js.map +1 -0
- package/dist/prod/server/class-style.js +2 -0
- package/dist/prod/server/class-style.js.map +1 -0
- package/dist/prod/server/component.js +2 -0
- package/dist/prod/server/component.js.map +1 -0
- package/dist/prod/server/derived.js +2 -0
- package/dist/prod/server/derived.js.map +1 -0
- package/dist/prod/server/element.js +2 -0
- package/dist/prod/server/element.js.map +1 -0
- package/dist/prod/server/errors.js +2 -0
- package/dist/prod/server/errors.js.map +1 -0
- package/dist/prod/server/escape.js +2 -0
- package/dist/prod/server/escape.js.map +1 -0
- package/dist/prod/server/form-controls.js +3 -0
- package/dist/prod/server/form-controls.js.map +1 -0
- package/dist/prod/server/hydration-script.js +2 -0
- package/dist/prod/server/hydration-script.js.map +1 -0
- package/dist/prod/server/index.js +1 -0
- package/dist/prod/server/node.js +2 -0
- package/dist/prod/server/node.js.map +1 -0
- package/dist/prod/server/portals.js +2 -0
- package/dist/prod/server/portals.js.map +1 -0
- package/dist/prod/server/render.js +2 -0
- package/dist/prod/server/render.js.map +1 -0
- package/dist/prod/server/serialize.js +2 -0
- package/dist/prod/server/serialize.js.map +1 -0
- package/dist/prod/server/stream.js +2 -0
- package/dist/prod/server/stream.js.map +1 -0
- package/dist/prod/server.js +1 -0
- package/dist/prod/spread.js +2 -0
- package/dist/prod/spread.js.map +1 -0
- package/dist/prod/template.js +2 -0
- package/dist/prod/template.js.map +1 -0
- package/dist/prod/view-transition.js +2 -0
- package/dist/prod/view-transition.js.map +1 -0
- package/dist/server/attributes.js +2 -0
- package/dist/server/attributes.js.map +1 -0
- package/dist/server/autofocus.js +2 -0
- package/dist/server/autofocus.js.map +1 -0
- package/dist/server/class-style.js +2 -0
- package/dist/server/class-style.js.map +1 -0
- package/dist/server/component.js +2 -0
- package/dist/server/component.js.map +1 -0
- package/dist/server/control-flow/activity.js +2 -0
- package/dist/server/control-flow/activity.js.map +1 -0
- package/dist/server/control-flow/branch.js +2 -0
- package/dist/server/control-flow/branch.js.map +1 -0
- package/dist/server/control-flow/client-only.js +2 -0
- package/dist/server/control-flow/client-only.js.map +1 -0
- package/dist/server/control-flow/dynamic.js +2 -0
- package/dist/server/control-flow/dynamic.js.map +1 -0
- package/dist/server/control-flow/error-boundary.js +2 -0
- package/dist/server/control-flow/error-boundary.js.map +1 -0
- package/dist/server/control-flow/for.js +2 -0
- package/dist/server/control-flow/for.js.map +1 -0
- package/dist/server/control-flow/fragment.js +2 -0
- package/dist/server/control-flow/fragment.js.map +1 -0
- package/dist/server/control-flow/index-list.js +2 -0
- package/dist/server/control-flow/index-list.js.map +1 -0
- package/dist/server/control-flow/lazy.js +2 -0
- package/dist/server/control-flow/lazy.js.map +1 -0
- package/dist/server/control-flow/no-hydration.js +2 -0
- package/dist/server/control-flow/no-hydration.js.map +1 -0
- package/dist/server/control-flow/portal.js +2 -0
- package/dist/server/control-flow/portal.js.map +1 -0
- package/dist/server/control-flow/root-errors.js +2 -0
- package/dist/server/control-flow/root-errors.js.map +1 -0
- package/dist/server/control-flow/show.js +2 -0
- package/dist/server/control-flow/show.js.map +1 -0
- package/dist/server/control-flow/suspense-list.js +2 -0
- package/dist/server/control-flow/suspense-list.js.map +1 -0
- package/dist/server/control-flow/suspense.js +2 -0
- package/dist/server/control-flow/suspense.js.map +1 -0
- package/dist/server/control-flow/switch.js +2 -0
- package/dist/server/control-flow/switch.js.map +1 -0
- package/dist/server/derived.js +2 -0
- package/dist/server/derived.js.map +1 -0
- package/dist/server/element.js +2 -0
- package/dist/server/element.js.map +1 -0
- package/dist/server/events.js +2 -0
- package/dist/server/events.js.map +1 -0
- package/dist/server/extend.js +1 -0
- package/dist/server/head/components.js +2 -0
- package/dist/server/head/components.js.map +1 -0
- package/dist/server/head/registry.js +2 -0
- package/dist/server/head/registry.js.map +1 -0
- package/dist/server/head/server-head.js +2 -0
- package/dist/server/head/server-head.js.map +1 -0
- package/dist/server/head/sink.js +2 -0
- package/dist/server/head/sink.js.map +1 -0
- package/dist/server/head/use-head.js +2 -0
- package/dist/server/head/use-head.js.map +1 -0
- package/dist/server/head/validate.js +2 -0
- package/dist/server/head/validate.js.map +1 -0
- package/dist/server/hydration.js +2 -0
- package/dist/server/hydration.js.map +1 -0
- package/dist/server/index.js +1 -0
- package/dist/server/insert.js +2 -0
- package/dist/server/insert.js.map +1 -0
- package/dist/server/internal/claim.js +2 -0
- package/dist/server/internal/claim.js.map +1 -0
- package/dist/server/internal/css-units.js +2 -0
- package/dist/server/internal/css-units.js.map +1 -0
- package/dist/server/internal/diagnostics.js +2 -0
- package/dist/server/internal/diagnostics.js.map +1 -0
- package/dist/server/internal/early-events.js +2 -0
- package/dist/server/internal/early-events.js.map +1 -0
- package/dist/server/internal/hydration-hooks.js +2 -0
- package/dist/server/internal/hydration-hooks.js.map +1 -0
- package/dist/server/internal/hydration-scope.js +2 -0
- package/dist/server/internal/hydration-scope.js.map +1 -0
- package/dist/server/internal/names.js +2 -0
- package/dist/server/internal/names.js.map +1 -0
- package/dist/server/internal/portal-context.js +2 -0
- package/dist/server/internal/portal-context.js.map +1 -0
- package/dist/server/internal/react-names.js +2 -0
- package/dist/server/internal/react-names.js.map +1 -0
- package/dist/server/internal/reconcile.js +2 -0
- package/dist/server/internal/reconcile.js.map +1 -0
- package/dist/server/internal/stream-context.js +2 -0
- package/dist/server/internal/stream-context.js.map +1 -0
- package/dist/server/reactive.js +2 -0
- package/dist/server/reactive.js.map +1 -0
- package/dist/server/render.js +2 -0
- package/dist/server/render.js.map +1 -0
- package/dist/server/resource-hints.js +2 -0
- package/dist/server/resource-hints.js.map +1 -0
- package/dist/server/select-value.js +2 -0
- package/dist/server/select-value.js.map +1 -0
- package/dist/server/server/attributes.js +2 -0
- package/dist/server/server/attributes.js.map +1 -0
- package/dist/server/server/class-style.js +2 -0
- package/dist/server/server/class-style.js.map +1 -0
- package/dist/server/server/component.js +2 -0
- package/dist/server/server/component.js.map +1 -0
- package/dist/server/server/derived.js +2 -0
- package/dist/server/server/derived.js.map +1 -0
- package/dist/server/server/element.js +2 -0
- package/dist/server/server/element.js.map +1 -0
- package/dist/server/server/errors.js +2 -0
- package/dist/server/server/errors.js.map +1 -0
- package/dist/server/server/escape.js +2 -0
- package/dist/server/server/escape.js.map +1 -0
- package/dist/server/server/form-controls.js +3 -0
- package/dist/server/server/form-controls.js.map +1 -0
- package/dist/server/server/hydration-script.js +2 -0
- package/dist/server/server/hydration-script.js.map +1 -0
- package/dist/server/server/index.js +1 -0
- package/dist/server/server/node.js +2 -0
- package/dist/server/server/node.js.map +1 -0
- package/dist/server/server/portals.js +2 -0
- package/dist/server/server/portals.js.map +1 -0
- package/dist/server/server/render.js +2 -0
- package/dist/server/server/render.js.map +1 -0
- package/dist/server/server/serialize.js +2 -0
- package/dist/server/server/serialize.js.map +1 -0
- package/dist/server/server/stream.js +2 -0
- package/dist/server/server/stream.js.map +1 -0
- package/dist/server/server.js +1 -0
- package/dist/server/spread.js +2 -0
- package/dist/server/spread.js.map +1 -0
- package/dist/server/template.js +2 -0
- package/dist/server/template.js.map +1 -0
- package/dist/server/view-transition.js +2 -0
- package/dist/server/view-transition.js.map +1 -0
- package/dist/server-dev/attributes.js +196 -0
- package/dist/server-dev/attributes.js.map +1 -0
- package/dist/server-dev/autofocus.js +45 -0
- package/dist/server-dev/autofocus.js.map +1 -0
- package/dist/server-dev/class-style.js +197 -0
- package/dist/server-dev/class-style.js.map +1 -0
- package/dist/server-dev/component.js +42 -0
- package/dist/server-dev/component.js.map +1 -0
- package/dist/server-dev/control-flow/activity.js +41 -0
- package/dist/server-dev/control-flow/activity.js.map +1 -0
- package/dist/server-dev/control-flow/branch.js +104 -0
- package/dist/server-dev/control-flow/branch.js.map +1 -0
- package/dist/server-dev/control-flow/client-only.js +50 -0
- package/dist/server-dev/control-flow/client-only.js.map +1 -0
- package/dist/server-dev/control-flow/dynamic.js +106 -0
- package/dist/server-dev/control-flow/dynamic.js.map +1 -0
- package/dist/server-dev/control-flow/error-boundary.js +115 -0
- package/dist/server-dev/control-flow/error-boundary.js.map +1 -0
- package/dist/server-dev/control-flow/for.js +23 -0
- package/dist/server-dev/control-flow/for.js.map +1 -0
- package/dist/server-dev/control-flow/fragment.js +31 -0
- package/dist/server-dev/control-flow/fragment.js.map +1 -0
- package/dist/server-dev/control-flow/hot-link.js +57 -0
- package/dist/server-dev/control-flow/hot-link.js.map +1 -0
- package/dist/server-dev/control-flow/index-list.js +31 -0
- package/dist/server-dev/control-flow/index-list.js.map +1 -0
- package/dist/server-dev/control-flow/lazy.js +64 -0
- package/dist/server-dev/control-flow/lazy.js.map +1 -0
- package/dist/server-dev/control-flow/no-hydration.js +40 -0
- package/dist/server-dev/control-flow/no-hydration.js.map +1 -0
- package/dist/server-dev/control-flow/portal.js +134 -0
- package/dist/server-dev/control-flow/portal.js.map +1 -0
- package/dist/server-dev/control-flow/root-errors.js +33 -0
- package/dist/server-dev/control-flow/root-errors.js.map +1 -0
- package/dist/server-dev/control-flow/show.js +18 -0
- package/dist/server-dev/control-flow/show.js.map +1 -0
- package/dist/server-dev/control-flow/suspense-list.js +100 -0
- package/dist/server-dev/control-flow/suspense-list.js.map +1 -0
- package/dist/server-dev/control-flow/suspense.js +119 -0
- package/dist/server-dev/control-flow/suspense.js.map +1 -0
- package/dist/server-dev/control-flow/switch.js +90 -0
- package/dist/server-dev/control-flow/switch.js.map +1 -0
- package/dist/server-dev/derived.js +34 -0
- package/dist/server-dev/derived.js.map +1 -0
- package/dist/server-dev/element.js +66 -0
- package/dist/server-dev/element.js.map +1 -0
- package/dist/server-dev/events.js +265 -0
- package/dist/server-dev/events.js.map +1 -0
- package/dist/server-dev/extend.js +12 -0
- package/dist/server-dev/head/components.js +170 -0
- package/dist/server-dev/head/components.js.map +1 -0
- package/dist/server-dev/head/registry.js +68 -0
- package/dist/server-dev/head/registry.js.map +1 -0
- package/dist/server-dev/head/server-head.js +76 -0
- package/dist/server-dev/head/server-head.js.map +1 -0
- package/dist/server-dev/head/sink.js +96 -0
- package/dist/server-dev/head/sink.js.map +1 -0
- package/dist/server-dev/head/use-head.js +84 -0
- package/dist/server-dev/head/use-head.js.map +1 -0
- package/dist/server-dev/head/validate.js +268 -0
- package/dist/server-dev/head/validate.js.map +1 -0
- package/dist/server-dev/hydration.js +120 -0
- package/dist/server-dev/hydration.js.map +1 -0
- package/dist/server-dev/index.js +34 -0
- package/dist/server-dev/insert.js +357 -0
- package/dist/server-dev/insert.js.map +1 -0
- package/dist/server-dev/internal/claim.js +357 -0
- package/dist/server-dev/internal/claim.js.map +1 -0
- package/dist/server-dev/internal/css-units.js +26 -0
- package/dist/server-dev/internal/css-units.js.map +1 -0
- package/dist/server-dev/internal/diagnostics.js +38 -0
- package/dist/server-dev/internal/diagnostics.js.map +1 -0
- package/dist/server-dev/internal/early-events.js +76 -0
- package/dist/server-dev/internal/early-events.js.map +1 -0
- package/dist/server-dev/internal/hydration-hooks.js +27 -0
- package/dist/server-dev/internal/hydration-hooks.js.map +1 -0
- package/dist/server-dev/internal/hydration-scope.js +156 -0
- package/dist/server-dev/internal/hydration-scope.js.map +1 -0
- package/dist/server-dev/internal/names.js +35 -0
- package/dist/server-dev/internal/names.js.map +1 -0
- package/dist/server-dev/internal/portal-context.js +28 -0
- package/dist/server-dev/internal/portal-context.js.map +1 -0
- package/dist/server-dev/internal/react-names.js +116 -0
- package/dist/server-dev/internal/react-names.js.map +1 -0
- package/dist/server-dev/internal/reconcile.js +122 -0
- package/dist/server-dev/internal/reconcile.js.map +1 -0
- package/dist/server-dev/internal/stream-context.js +23 -0
- package/dist/server-dev/internal/stream-context.js.map +1 -0
- package/dist/server-dev/reactive.js +85 -0
- package/dist/server-dev/reactive.js.map +1 -0
- package/dist/server-dev/render.js +52 -0
- package/dist/server-dev/render.js.map +1 -0
- package/dist/server-dev/resource-hints.js +200 -0
- package/dist/server-dev/resource-hints.js.map +1 -0
- package/dist/server-dev/select-value.js +51 -0
- package/dist/server-dev/select-value.js.map +1 -0
- package/dist/server-dev/server/attributes.js +155 -0
- package/dist/server-dev/server/attributes.js.map +1 -0
- package/dist/server-dev/server/class-style.js +173 -0
- package/dist/server-dev/server/class-style.js.map +1 -0
- package/dist/server-dev/server/component.js +72 -0
- package/dist/server-dev/server/component.js.map +1 -0
- package/dist/server-dev/server/derived.js +26 -0
- package/dist/server-dev/server/derived.js.map +1 -0
- package/dist/server-dev/server/element.js +216 -0
- package/dist/server-dev/server/element.js.map +1 -0
- package/dist/server-dev/server/errors.js +26 -0
- package/dist/server-dev/server/errors.js.map +1 -0
- package/dist/server-dev/server/escape.js +119 -0
- package/dist/server-dev/server/escape.js.map +1 -0
- package/dist/server-dev/server/form-controls.js +193 -0
- package/dist/server-dev/server/form-controls.js.map +1 -0
- package/dist/server-dev/server/hydration-script.js +59 -0
- package/dist/server-dev/server/hydration-script.js.map +1 -0
- package/dist/server-dev/server/index.js +16 -0
- package/dist/server-dev/server/node.js +21 -0
- package/dist/server-dev/server/node.js.map +1 -0
- package/dist/server-dev/server/portals.js +99 -0
- package/dist/server-dev/server/portals.js.map +1 -0
- package/dist/server-dev/server/render.js +256 -0
- package/dist/server-dev/server/render.js.map +1 -0
- package/dist/server-dev/server/serialize.js +315 -0
- package/dist/server-dev/server/serialize.js.map +1 -0
- package/dist/server-dev/server/stream.js +235 -0
- package/dist/server-dev/server/stream.js.map +1 -0
- package/dist/server-dev/server.js +16 -0
- package/dist/server-dev/spread.js +296 -0
- package/dist/server-dev/spread.js.map +1 -0
- package/dist/server-dev/template.js +120 -0
- package/dist/server-dev/template.js.map +1 -0
- package/dist/server-dev/view-transition.js +90 -0
- package/dist/server-dev/view-transition.js.map +1 -0
- package/package.json +136 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-head.js","names":[],"sources":["../../../src/head/use-head.ts"],"sourcesContent":["import {\n createRoot,\n getOwner,\n isServer,\n untrack,\n useContext,\n useEffect,\n useRenderEffect,\n useSignal,\n} from \"@shadow-js/core\";\nimport { createHeadRegistry, type HeadRegistry } from \"./registry\";\nimport { ServerHeadContext } from \"./server-head\";\nimport { createDomHeadSink } from \"./sink\";\nimport { type HeadInput, normalizeTags } from \"./validate\";\n\n/** One registry per document, shared by every `useHead` call on it. */\nconst registries = new WeakMap<Document, HeadRegistry>();\n\n/**\n * Creates the registry for a document and the deferred effect that writes it to the head. The\n * effect is detached: it lives as long as the document, not as long as the first caller. It runs\n * in the user-effect queue, so every registration made in one flush produces one DOM pass.\n */\nfunction createRegistryFor(doc: Document): HeadRegistry {\n return createRoot(\n (): HeadRegistry => {\n const [version, bump] = useSignal(0);\n const registry = createHeadRegistry(() => {\n bump((count) => count + 1);\n });\n const apply = createDomHeadSink(doc);\n useEffect(\n () => {\n version();\n return registry.winners();\n },\n (winners) => {\n apply(winners);\n },\n );\n return registry;\n },\n { detached: true },\n );\n}\n\n/** The registry of a document, created on first use. */\nfunction registryFor(doc: Document): HeadRegistry {\n const known = registries.get(doc);\n if (known !== undefined) return known;\n const registry = createRegistryFor(doc);\n registries.set(doc, registry);\n return registry;\n}\n\n/**\n * Registers head tags for the lifetime of the current owner. Each registration joins the stack of\n * its identity: `title` and `base` are singletons, `meta` is keyed by its name, property,\n * http-equiv or charset (and media), icons by rel, sizes and type, resource links by rel, href and\n * as, and styles by key. The last registration of an identity wins; disposing it restores the one\n * below. Append-only tags (no identity) all stay. Passing a function re-registers when its\n * dependencies change. On the client the registrations are written to `document.head` after the\n * render phase of a flush, in one pass per flush. On the server the tags go to the render's `head`\n * collector (see `createServerHead`), read once; without one, or outside an owner, nothing is\n * registered.\n *\n * @param input One tag, several tags, or a function returning them. `null`, `undefined` and `false`\n * register nothing.\n * @throws TypeError for an invalid tag description (see {@link normalizeTags}); on the client the\n * error is raised in the registration's render effect and goes to the owner's error handler.\n * In development builds, calling it on the client with no owner does not throw: it warns\n * `HEAD_OUTSIDE_OWNER` and registers nothing.\n * @example\n * import { useSignal } from \"@shadow-js/core\";\n * import { useHead } from \"@shadow-js/web\";\n *\n * function Post() {\n * const [post] = useSignal({ title: \"Hello\" });\n * useHead(() => ({ tag: \"title\", children: `${post().title} | Blog` }));\n * return null;\n * }\n * @see Title\n * @see Meta\n * @since 1.0.0\n */\nexport function useHead(input: HeadInput | (() => HeadInput)): void {\n if (isServer) {\n const collector = getOwner() === null ? null : useContext(ServerHeadContext);\n if (collector !== null) {\n collector.add(normalizeTags(untrack(() => (typeof input === \"function\" ? input() : input))));\n }\n return;\n }\n if (getOwner() === null) {\n if (__DEV__) {\n console.warn(\"[shadow HEAD_OUTSIDE_OWNER] useHead() needs an owner; nothing was registered.\");\n }\n return;\n }\n const registry = registryFor(document);\n useRenderEffect(\n () => normalizeTags(typeof input === \"function\" ? input() : input),\n (tags) => registry.add(tags),\n );\n}\n"],"mappings":";;;;;;;AAgBA,MAAM,6BAAa,IAAI,QAAgC;;;;;;AAOvD,SAAS,kBAAkB,KAA6B;CACtD,OAAO,iBACe;EAClB,MAAM,CAAC,SAAS,QAAQ,UAAU,CAAC;EACnC,MAAM,WAAW,yBAAyB;GACxC,MAAM,UAAU,QAAQ,CAAC;EAC3B,CAAC;EACD,MAAM,QAAQ,kBAAkB,GAAG;EACnC,gBACQ;GACJ,QAAQ;GACR,OAAO,SAAS,QAAQ;EAC1B,IACC,YAAY;GACX,MAAM,OAAO;EACf,CACF;EACA,OAAO;CACT,GACA,EAAE,UAAU,KAAK,CACnB;AACF;;AAGA,SAAS,YAAY,KAA6B;CAChD,MAAM,QAAQ,WAAW,IAAI,GAAG;CAChC,IAAI,UAAU,KAAA,GAAW,OAAO;CAChC,MAAM,WAAW,kBAAkB,GAAG;CACtC,WAAW,IAAI,KAAK,QAAQ;CAC5B,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,QAAQ,OAA4C;CAClE,IAAI,UAAU;EACZ,MAAM,YAAY,SAAS,MAAM,OAAO,OAAO,WAAW,iBAAiB;EAC3E,IAAI,cAAc,MAChB,UAAU,IAAI,cAAc,cAAe,OAAO,UAAU,aAAa,MAAM,IAAI,KAAM,CAAC,CAAC;EAE7F;CACF;CACA,IAAI,SAAS,MAAM,MAAM;EAErB,QAAQ,KAAK,+EAA+E;EAE9F;CACF;CACA,MAAM,WAAW,YAAY,QAAQ;CACrC,sBACQ,cAAc,OAAO,UAAU,aAAa,MAAM,IAAI,KAAK,IAChE,SAAS,SAAS,IAAI,IAAI,CAC7B;AACF"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
//#region src/head/validate.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Head elements the registry manages. `script` and `noscript` are refused: a head-registered
|
|
4
|
+
* script would bypass CSP nonces and Trusted Types, so scripts come only from the asset pipeline.
|
|
5
|
+
* @since 1.0.0
|
|
6
|
+
*/
|
|
7
|
+
export type HeadTagName = "title" | "meta" | "link" | "style" | "base";
|
|
8
|
+
/**
|
|
9
|
+
* One attribute value: a string is written as is, `true` as an empty attribute, `false` and
|
|
10
|
+
* `undefined` omit it, and a number is written as text.
|
|
11
|
+
* @since 1.0.0
|
|
12
|
+
*/
|
|
13
|
+
export type HeadAttributeValue = string | number | boolean | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Describes one head element. `key` identifies a `style` for replacement; other tags use their
|
|
16
|
+
* own identity (see {@link useHead}).
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* import type { HeadTag } from "@shadow-js/web";
|
|
20
|
+
*
|
|
21
|
+
* const description: HeadTag = { tag: "meta", props: { name: "description", content: "Docs" } };
|
|
22
|
+
* @since 1.0.0
|
|
23
|
+
*/
|
|
24
|
+
export interface HeadTag {
|
|
25
|
+
/** The element to register: `title`, `meta`, `link`, `style` or `base`. Any other name throws. */
|
|
26
|
+
readonly tag: HeadTagName;
|
|
27
|
+
/**
|
|
28
|
+
* The attributes of the element. Names are case-insensitive, must be valid attribute names, and
|
|
29
|
+
* may not start with `on`.
|
|
30
|
+
* @default undefined (the element has no attributes)
|
|
31
|
+
*/
|
|
32
|
+
readonly props?: Readonly<Record<string, HeadAttributeValue>>;
|
|
33
|
+
/**
|
|
34
|
+
* The text of a `title` or `style`. Other tags take no text and throw when it is given.
|
|
35
|
+
* @default undefined (no text)
|
|
36
|
+
*/
|
|
37
|
+
readonly children?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Identity of a `style`: a later style with the same key replaces an earlier one. Only `style`
|
|
40
|
+
* takes a key.
|
|
41
|
+
* @default undefined (the style has no identity and always stays)
|
|
42
|
+
*/
|
|
43
|
+
readonly key?: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* What {@link useHead} accepts: one tag, several tags, or nothing (which registers nothing).
|
|
47
|
+
* @since 1.0.0
|
|
48
|
+
*/
|
|
49
|
+
export type HeadInput = HeadTag | readonly HeadTag[] | null | undefined | false;
|
|
50
|
+
/**
|
|
51
|
+
* A validated tag: lower-case attribute names, the text it carries, and its identity. Produced
|
|
52
|
+
* only by {@link normalizeTag}, so the registry and the DOM sink never see unchecked input.
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
export interface NormalizedTag {
|
|
56
|
+
readonly tag: HeadTagName;
|
|
57
|
+
readonly attributes: ReadonlyArray<readonly [string, string]>;
|
|
58
|
+
readonly text: string | undefined;
|
|
59
|
+
readonly key: string | undefined;
|
|
60
|
+
/** Identity that replaces earlier registrations; undefined for append-only tags. */
|
|
61
|
+
readonly identity: string | undefined;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Normalizes what {@link useHead} receives: one description, a list, or nothing.
|
|
65
|
+
*
|
|
66
|
+
* @param input A description, a list of them, or null, undefined or false for none.
|
|
67
|
+
* @returns The normalized tags, in order.
|
|
68
|
+
* @throws TypeError when any description is invalid: an unknown tag (including `script`), an
|
|
69
|
+
* attribute name that is not valid or starts with `on`, a reserved marker attribute, an attribute
|
|
70
|
+
* value that is not a string, number or boolean, children on a tag other than `title` and `style`,
|
|
71
|
+
* or a key on anything but a `style`. The production messages start with `SHADOW_HEAD_`.
|
|
72
|
+
* @example
|
|
73
|
+
* import { normalizeTags } from "@shadow-js/web/server";
|
|
74
|
+
*
|
|
75
|
+
* const tags = normalizeTags([
|
|
76
|
+
* { tag: "title", children: "A" },
|
|
77
|
+
* { tag: "base", props: { href: "/" } },
|
|
78
|
+
* ]);
|
|
79
|
+
* console.log(tags.length); // 2
|
|
80
|
+
* @see useHead
|
|
81
|
+
* @since 1.0.0
|
|
82
|
+
*/
|
|
83
|
+
export declare function normalizeTags(input: unknown): NormalizedTag[];
|
|
84
|
+
//#endregion
|
|
85
|
+
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { warnOnce } from "../internal/diagnostics.js";
|
|
2
|
+
import { isUnsafeUrlAttribute } from "../attributes.js";
|
|
3
|
+
//#region src/head/validate.ts
|
|
4
|
+
/** Marks every element the sink created or adopted. Callers may not supply it. @internal */
|
|
5
|
+
const HEAD_MARKER = "data-shadow-head";
|
|
6
|
+
/** Carries a style's key so a server-rendered style can be adopted. @internal */
|
|
7
|
+
const HEAD_KEY = "data-shadow-key";
|
|
8
|
+
const TAG_NAMES = /* @__PURE__ */ new Set([
|
|
9
|
+
"title",
|
|
10
|
+
"meta",
|
|
11
|
+
"link",
|
|
12
|
+
"style",
|
|
13
|
+
"base"
|
|
14
|
+
]);
|
|
15
|
+
const ATTRIBUTE_NAME = /^[a-zA-Z_][a-zA-Z0-9_:.-]*$/;
|
|
16
|
+
const RESERVED_ATTRIBUTES = /* @__PURE__ */ new Set([HEAD_MARKER, HEAD_KEY]);
|
|
17
|
+
/** Names skipped silently, as spread does: they would reach a prototype if written as keys. */
|
|
18
|
+
const FORBIDDEN_NAMES = /* @__PURE__ */ new Set([
|
|
19
|
+
"__proto__",
|
|
20
|
+
"constructor",
|
|
21
|
+
"prototype"
|
|
22
|
+
]);
|
|
23
|
+
const META_IDENTITY_FIELDS = [
|
|
24
|
+
"name",
|
|
25
|
+
"property",
|
|
26
|
+
"http-equiv",
|
|
27
|
+
"charset"
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* Properties that repeat by design (one tag per image, per tag, per locale). Their identity includes the content,
|
|
31
|
+
* so every value stays; the others are last-wins. Sub-properties of an image (`og:image:width`) are single.
|
|
32
|
+
* @since 1.0.0
|
|
33
|
+
*/
|
|
34
|
+
const MULTI_VALUED_META = /* @__PURE__ */ new Set([
|
|
35
|
+
"og:image",
|
|
36
|
+
"og:video",
|
|
37
|
+
"og:audio",
|
|
38
|
+
"og:locale:alternate",
|
|
39
|
+
"og:email",
|
|
40
|
+
"og:phone_number",
|
|
41
|
+
"og:fax_number",
|
|
42
|
+
"article:tag",
|
|
43
|
+
"article:author",
|
|
44
|
+
"book:author",
|
|
45
|
+
"book:tag",
|
|
46
|
+
"music:album",
|
|
47
|
+
"music:song",
|
|
48
|
+
"music:musician",
|
|
49
|
+
"music:creator",
|
|
50
|
+
"video:actor",
|
|
51
|
+
"video:director",
|
|
52
|
+
"video:writer",
|
|
53
|
+
"video:tag",
|
|
54
|
+
"fb:admins",
|
|
55
|
+
"twitter:image"
|
|
56
|
+
]);
|
|
57
|
+
/** Links that repeat (alternates per language, authors, archives): their identity is every attribute. */
|
|
58
|
+
const MULTI_LINK_RELS = /* @__PURE__ */ new Set([
|
|
59
|
+
"alternate",
|
|
60
|
+
"author",
|
|
61
|
+
"archives",
|
|
62
|
+
"assets",
|
|
63
|
+
"bookmarks",
|
|
64
|
+
"me",
|
|
65
|
+
"apple-touch-startup-image"
|
|
66
|
+
]);
|
|
67
|
+
const RESOURCE_RELS = /* @__PURE__ */ new Set([
|
|
68
|
+
"preload",
|
|
69
|
+
"modulepreload",
|
|
70
|
+
"prefetch",
|
|
71
|
+
"preconnect",
|
|
72
|
+
"dns-prefetch",
|
|
73
|
+
"stylesheet"
|
|
74
|
+
]);
|
|
75
|
+
/** True for the tag names the registry manages. @internal */
|
|
76
|
+
function isHeadTagName(value) {
|
|
77
|
+
return TAG_NAMES.has(value);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Length-prefixed identity: `["a|b"]` and `["a", "b"]` can never collide because every part
|
|
81
|
+
* carries its own length.
|
|
82
|
+
*/
|
|
83
|
+
function frame(parts) {
|
|
84
|
+
return parts.map((part) => `${part.length}:${part}`).join("");
|
|
85
|
+
}
|
|
86
|
+
/** Text of an attribute value, or undefined when the attribute is omitted. */
|
|
87
|
+
function attributeText(value) {
|
|
88
|
+
if (value === void 0 || value === null || value === false) return void 0;
|
|
89
|
+
if (value === true) return "";
|
|
90
|
+
if (typeof value === "string") return value;
|
|
91
|
+
if (typeof value === "number") return String(value);
|
|
92
|
+
throw new TypeError("ShadowJS head: attribute values must be strings, numbers or booleans.");
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Validates the props of one tag and returns its lower-case, de-duplicated attributes, in order. The
|
|
96
|
+
* map is the lookup that the identity is built from, so it is built once per tag.
|
|
97
|
+
*/
|
|
98
|
+
function attributesFrom(tag, props) {
|
|
99
|
+
if (props === void 0 || props === null) return /* @__PURE__ */ new Map();
|
|
100
|
+
if (typeof props !== "object" || Array.isArray(props)) throw new TypeError("ShadowJS head: props must be an object.");
|
|
101
|
+
const values = /* @__PURE__ */ new Map();
|
|
102
|
+
for (const key of Object.keys(props)) {
|
|
103
|
+
if (!ATTRIBUTE_NAME.test(key)) throw new TypeError(`ShadowJS head: "${key}" is not a valid attribute name.`);
|
|
104
|
+
const name = key.toLowerCase();
|
|
105
|
+
if (name.startsWith("on")) throw new TypeError(`ShadowJS head: event attribute "${key}" is not allowed.`);
|
|
106
|
+
if (RESERVED_ATTRIBUTES.has(name)) throw new TypeError(`ShadowJS head: "${key}" is reserved for the runtime.`);
|
|
107
|
+
if (FORBIDDEN_NAMES.has(name)) continue;
|
|
108
|
+
const text = attributeText(Reflect.get(props, key));
|
|
109
|
+
if (text === void 0) continue;
|
|
110
|
+
if (isUnsafeUrlAttribute(name, text)) {
|
|
111
|
+
warnOnce("UNSAFE_URL_BLOCKED", `Dropped the unsafe "${name}" value of a <${tag}> head tag.`);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
values.set(name, text);
|
|
115
|
+
}
|
|
116
|
+
return values;
|
|
117
|
+
}
|
|
118
|
+
/** Children text: title and style only, and only as a string. */
|
|
119
|
+
function textFrom(tag, children) {
|
|
120
|
+
if (children === void 0 || children === null) return void 0;
|
|
121
|
+
if (tag !== "title" && tag !== "style") throw new TypeError(`ShadowJS head: <${tag}> takes no children.`);
|
|
122
|
+
if (typeof children !== "string") throw new TypeError("ShadowJS head: children must be text.");
|
|
123
|
+
return children;
|
|
124
|
+
}
|
|
125
|
+
/** The key of a style. Only style takes one. */
|
|
126
|
+
function keyFrom(tag, key) {
|
|
127
|
+
if (key === void 0 || key === null) return void 0;
|
|
128
|
+
if (tag !== "style") throw new TypeError("ShadowJS head: only <style> takes a key.");
|
|
129
|
+
if (typeof key !== "string" || key === "") throw new TypeError("ShadowJS head: a key must be a non-empty string.");
|
|
130
|
+
return key;
|
|
131
|
+
}
|
|
132
|
+
/** Rel tokens, lower-cased and sorted, so `"ICON apple"` and `"apple icon"` compare equal. */
|
|
133
|
+
function normalizeRel(raw) {
|
|
134
|
+
if (raw === void 0) return void 0;
|
|
135
|
+
const tokens = raw.toLowerCase().split(/\s+/).filter((token) => token !== "");
|
|
136
|
+
if (tokens.length === 0) return void 0;
|
|
137
|
+
return tokens.sort().join(" ");
|
|
138
|
+
}
|
|
139
|
+
/** Identity of a meta: its first identifying field and the media query. */
|
|
140
|
+
function metaIdentity(lookup) {
|
|
141
|
+
const media = lookup.get("media") ?? "";
|
|
142
|
+
for (const field of META_IDENTITY_FIELDS) {
|
|
143
|
+
const value = lookup.get(field);
|
|
144
|
+
if (value === void 0) continue;
|
|
145
|
+
if (field === "charset") return frame(["meta", field]);
|
|
146
|
+
if (MULTI_VALUED_META.has(value)) return frame([
|
|
147
|
+
"meta",
|
|
148
|
+
field,
|
|
149
|
+
value,
|
|
150
|
+
media,
|
|
151
|
+
lookup.get("content") ?? ""
|
|
152
|
+
]);
|
|
153
|
+
return frame([
|
|
154
|
+
"meta",
|
|
155
|
+
field,
|
|
156
|
+
value,
|
|
157
|
+
media
|
|
158
|
+
]);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** Identity of a link: icons by rel, sizes and type; preloads and stylesheets by rel, href and as. */
|
|
162
|
+
function linkIdentity(lookup) {
|
|
163
|
+
const rel = normalizeRel(lookup.get("rel"));
|
|
164
|
+
if (rel === void 0) return void 0;
|
|
165
|
+
if (rel.split(" ").some((token) => token === "icon" || token.endsWith("-icon"))) return frame([
|
|
166
|
+
"link",
|
|
167
|
+
"icon",
|
|
168
|
+
rel,
|
|
169
|
+
lookup.get("sizes") ?? "",
|
|
170
|
+
lookup.get("type") ?? ""
|
|
171
|
+
]);
|
|
172
|
+
if (rel === "canonical") return frame(["link", "canonical"]);
|
|
173
|
+
if (RESOURCE_RELS.has(rel)) return frame([
|
|
174
|
+
"link",
|
|
175
|
+
"resource",
|
|
176
|
+
rel,
|
|
177
|
+
lookup.get("href") ?? "",
|
|
178
|
+
lookup.get("as") ?? ""
|
|
179
|
+
]);
|
|
180
|
+
if (MULTI_LINK_RELS.has(rel)) return frame([
|
|
181
|
+
"link",
|
|
182
|
+
"multi",
|
|
183
|
+
rel,
|
|
184
|
+
lookup.get("href") ?? "",
|
|
185
|
+
lookup.get("hreflang") ?? "",
|
|
186
|
+
lookup.get("media") ?? "",
|
|
187
|
+
lookup.get("type") ?? ""
|
|
188
|
+
]);
|
|
189
|
+
}
|
|
190
|
+
/** Identity of a validated tag, or undefined when the tag is append-only. @internal */
|
|
191
|
+
function identityOf(tag, lookup, key) {
|
|
192
|
+
if (tag === "title") return frame(["title"]);
|
|
193
|
+
if (tag === "base") return frame(["base"]);
|
|
194
|
+
if (tag === "style") return key === void 0 ? void 0 : frame(["style", key]);
|
|
195
|
+
return tag === "meta" ? metaIdentity(lookup) : linkIdentity(lookup);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Validates one head tag description and computes its identity. Hostile input throws a
|
|
199
|
+
* TypeError: an unknown tag (including `script`), a bad attribute name, an event attribute, a
|
|
200
|
+
* reserved marker, children on a void tag, or a key on anything but a style. An unsafe URL
|
|
201
|
+
* attribute is dropped with a dev warning, the same way `setAttribute` treats it.
|
|
202
|
+
*
|
|
203
|
+
* @param input The description, usually a {@link HeadTag}; any value is checked.
|
|
204
|
+
* @returns The normalized tag.
|
|
205
|
+
* @throws TypeError for every invalid input listed above.
|
|
206
|
+
* @internal
|
|
207
|
+
* @since 1.0.0
|
|
208
|
+
*/
|
|
209
|
+
function normalizeTag(input) {
|
|
210
|
+
if (input === null || typeof input !== "object" || Array.isArray(input)) throw new TypeError("ShadowJS head: a head tag must be an object.");
|
|
211
|
+
const name = Reflect.get(input, "tag");
|
|
212
|
+
if (typeof name !== "string" || !isHeadTagName(name)) throw new TypeError(`ShadowJS head: unsupported tag "${typeof name === "string" ? name : typeof name}".`);
|
|
213
|
+
const lookup = attributesFrom(name, Reflect.get(input, "props"));
|
|
214
|
+
const text = textFrom(name, Reflect.get(input, "children"));
|
|
215
|
+
const key = keyFrom(name, Reflect.get(input, "key"));
|
|
216
|
+
return {
|
|
217
|
+
tag: name,
|
|
218
|
+
attributes: [...lookup],
|
|
219
|
+
text,
|
|
220
|
+
key,
|
|
221
|
+
identity: identityOf(name, lookup, key)
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Normalizes what {@link useHead} receives: one description, a list, or nothing.
|
|
226
|
+
*
|
|
227
|
+
* @param input A description, a list of them, or null, undefined or false for none.
|
|
228
|
+
* @returns The normalized tags, in order.
|
|
229
|
+
* @throws TypeError when any description is invalid: an unknown tag (including `script`), an
|
|
230
|
+
* attribute name that is not valid or starts with `on`, a reserved marker attribute, an attribute
|
|
231
|
+
* value that is not a string, number or boolean, children on a tag other than `title` and `style`,
|
|
232
|
+
* or a key on anything but a `style`. The production messages start with `SHADOW_HEAD_`.
|
|
233
|
+
* @example
|
|
234
|
+
* import { normalizeTags } from "@shadow-js/web/server";
|
|
235
|
+
*
|
|
236
|
+
* const tags = normalizeTags([
|
|
237
|
+
* { tag: "title", children: "A" },
|
|
238
|
+
* { tag: "base", props: { href: "/" } },
|
|
239
|
+
* ]);
|
|
240
|
+
* console.log(tags.length); // 2
|
|
241
|
+
* @see useHead
|
|
242
|
+
* @since 1.0.0
|
|
243
|
+
*/
|
|
244
|
+
function normalizeTags(input) {
|
|
245
|
+
if (input === null || input === void 0 || input === false) return [];
|
|
246
|
+
if (Array.isArray(input)) return input.map((item) => normalizeTag(item));
|
|
247
|
+
return [normalizeTag(input)];
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* The identity of an element already in the document, computed the same way as for a
|
|
251
|
+
* registration, so the sink can adopt a server-rendered element. Undefined for elements that are
|
|
252
|
+
* not head tags or that are append-only.
|
|
253
|
+
* @internal
|
|
254
|
+
*/
|
|
255
|
+
function identityOfElement(element) {
|
|
256
|
+
const name = element.localName;
|
|
257
|
+
if (!isHeadTagName(name)) return void 0;
|
|
258
|
+
const lookup = /* @__PURE__ */ new Map();
|
|
259
|
+
for (const attribute of Array.from(element.attributes)) {
|
|
260
|
+
if (attribute.name === "data-shadow-head" || attribute.name === "data-shadow-key") continue;
|
|
261
|
+
lookup.set(attribute.name.toLowerCase(), attribute.value);
|
|
262
|
+
}
|
|
263
|
+
return identityOf(name, lookup, element.getAttribute("data-shadow-key") ?? void 0);
|
|
264
|
+
}
|
|
265
|
+
//#endregion
|
|
266
|
+
export { HEAD_KEY, HEAD_MARKER, identityOf, identityOfElement, isHeadTagName, normalizeTag, normalizeTags };
|
|
267
|
+
|
|
268
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.js","names":[],"sources":["../../../src/head/validate.ts"],"sourcesContent":["import { isUnsafeUrlAttribute, warnOnce } from \"../extend\";\n\n/**\n * Head elements the registry manages. `script` and `noscript` are refused: a head-registered\n * script would bypass CSP nonces and Trusted Types, so scripts come only from the asset pipeline.\n * @since 1.0.0\n */\nexport type HeadTagName = \"title\" | \"meta\" | \"link\" | \"style\" | \"base\";\n\n/**\n * One attribute value: a string is written as is, `true` as an empty attribute, `false` and\n * `undefined` omit it, and a number is written as text.\n * @since 1.0.0\n */\nexport type HeadAttributeValue = string | number | boolean | undefined;\n\n/**\n * Describes one head element. `key` identifies a `style` for replacement; other tags use their\n * own identity (see {@link useHead}).\n *\n * @example\n * import type { HeadTag } from \"@shadow-js/web\";\n *\n * const description: HeadTag = { tag: \"meta\", props: { name: \"description\", content: \"Docs\" } };\n * @since 1.0.0\n */\nexport interface HeadTag {\n /** The element to register: `title`, `meta`, `link`, `style` or `base`. Any other name throws. */\n readonly tag: HeadTagName;\n /**\n * The attributes of the element. Names are case-insensitive, must be valid attribute names, and\n * may not start with `on`.\n * @default undefined (the element has no attributes)\n */\n readonly props?: Readonly<Record<string, HeadAttributeValue>>;\n /**\n * The text of a `title` or `style`. Other tags take no text and throw when it is given.\n * @default undefined (no text)\n */\n readonly children?: string;\n /**\n * Identity of a `style`: a later style with the same key replaces an earlier one. Only `style`\n * takes a key.\n * @default undefined (the style has no identity and always stays)\n */\n readonly key?: string;\n}\n\n/**\n * What {@link useHead} accepts: one tag, several tags, or nothing (which registers nothing).\n * @since 1.0.0\n */\nexport type HeadInput = HeadTag | readonly HeadTag[] | null | undefined | false;\n\n/**\n * A validated tag: lower-case attribute names, the text it carries, and its identity. Produced\n * only by {@link normalizeTag}, so the registry and the DOM sink never see unchecked input.\n * @internal\n */\nexport interface NormalizedTag {\n readonly tag: HeadTagName;\n readonly attributes: ReadonlyArray<readonly [string, string]>;\n readonly text: string | undefined;\n readonly key: string | undefined;\n /** Identity that replaces earlier registrations; undefined for append-only tags. */\n readonly identity: string | undefined;\n}\n\n/** Marks every element the sink created or adopted. Callers may not supply it. @internal */\nexport const HEAD_MARKER = \"data-shadow-head\";\n/** Carries a style's key so a server-rendered style can be adopted. @internal */\nexport const HEAD_KEY = \"data-shadow-key\";\n\nconst TAG_NAMES: ReadonlySet<string> = new Set([\"title\", \"meta\", \"link\", \"style\", \"base\"]);\nconst ATTRIBUTE_NAME = /^[a-zA-Z_][a-zA-Z0-9_:.-]*$/;\nconst RESERVED_ATTRIBUTES: ReadonlySet<string> = new Set([HEAD_MARKER, HEAD_KEY]);\n/** Names skipped silently, as spread does: they would reach a prototype if written as keys. */\nconst FORBIDDEN_NAMES: ReadonlySet<string> = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\nconst META_IDENTITY_FIELDS = [\"name\", \"property\", \"http-equiv\", \"charset\"] as const;\n/**\n * Properties that repeat by design (one tag per image, per tag, per locale). Their identity includes the content,\n * so every value stays; the others are last-wins. Sub-properties of an image (`og:image:width`) are single.\n * @since 1.0.0\n */\nconst MULTI_VALUED_META: ReadonlySet<string> = new Set([\n \"og:image\",\n \"og:video\",\n \"og:audio\",\n \"og:locale:alternate\",\n \"og:email\",\n \"og:phone_number\",\n \"og:fax_number\",\n \"article:tag\",\n \"article:author\",\n \"book:author\",\n \"book:tag\",\n \"music:album\",\n \"music:song\",\n \"music:musician\",\n \"music:creator\",\n \"video:actor\",\n \"video:director\",\n \"video:writer\",\n \"video:tag\",\n \"fb:admins\",\n \"twitter:image\",\n]);\n/** Links that repeat (alternates per language, authors, archives): their identity is every attribute. */\nconst MULTI_LINK_RELS: ReadonlySet<string> = new Set([\n \"alternate\",\n \"author\",\n \"archives\",\n \"assets\",\n \"bookmarks\",\n \"me\",\n \"apple-touch-startup-image\",\n]);\nconst RESOURCE_RELS: ReadonlySet<string> = new Set([\n \"preload\",\n \"modulepreload\",\n \"prefetch\",\n \"preconnect\",\n \"dns-prefetch\",\n \"stylesheet\",\n]);\n\n/** True for the tag names the registry manages. @internal */\nexport function isHeadTagName(value: string): value is HeadTagName {\n return TAG_NAMES.has(value);\n}\n\n/**\n * Length-prefixed identity: `[\"a|b\"]` and `[\"a\", \"b\"]` can never collide because every part\n * carries its own length.\n */\nfunction frame(parts: readonly string[]): string {\n return parts.map((part) => `${part.length}:${part}`).join(\"\");\n}\n\n/** Text of an attribute value, or undefined when the attribute is omitted. */\nfunction attributeText(value: unknown): string | undefined {\n if (value === undefined || value === null || value === false) return undefined;\n if (value === true) return \"\";\n if (typeof value === \"string\") return value;\n if (typeof value === \"number\") return String(value);\n throw new TypeError(\n __DEV__\n ? \"ShadowJS head: attribute values must be strings, numbers or booleans.\"\n : \"SHADOW_HEAD_ATTRIBUTE_VALUE\",\n );\n}\n\n/**\n * Validates the props of one tag and returns its lower-case, de-duplicated attributes, in order. The\n * map is the lookup that the identity is built from, so it is built once per tag.\n */\nfunction attributesFrom(tag: HeadTagName, props: unknown): ReadonlyMap<string, string> {\n if (props === undefined || props === null) return new Map();\n if (typeof props !== \"object\" || Array.isArray(props)) {\n throw new TypeError(__DEV__ ? \"ShadowJS head: props must be an object.\" : \"SHADOW_HEAD_PROPS\");\n }\n const values = new Map<string, string>();\n for (const key of Object.keys(props)) {\n if (!ATTRIBUTE_NAME.test(key)) {\n throw new TypeError(\n __DEV__\n ? `ShadowJS head: \"${key}\" is not a valid attribute name.`\n : \"SHADOW_HEAD_ATTRIBUTE_NAME\",\n );\n }\n const name = key.toLowerCase();\n if (name.startsWith(\"on\")) {\n throw new TypeError(\n __DEV__\n ? `ShadowJS head: event attribute \"${key}\" is not allowed.`\n : \"SHADOW_HEAD_EVENT_ATTRIBUTE\",\n );\n }\n if (RESERVED_ATTRIBUTES.has(name)) {\n throw new TypeError(\n __DEV__ ? `ShadowJS head: \"${key}\" is reserved for the runtime.` : \"SHADOW_HEAD_RESERVED\",\n );\n }\n if (FORBIDDEN_NAMES.has(name)) continue;\n const text = attributeText(Reflect.get(props, key));\n if (text === undefined) continue;\n if (isUnsafeUrlAttribute(name, text)) {\n warnOnce(\"UNSAFE_URL_BLOCKED\", `Dropped the unsafe \"${name}\" value of a <${tag}> head tag.`);\n continue;\n }\n values.set(name, text);\n }\n return values;\n}\n\n/** Children text: title and style only, and only as a string. */\nfunction textFrom(tag: HeadTagName, children: unknown): string | undefined {\n if (children === undefined || children === null) return undefined;\n if (tag !== \"title\" && tag !== \"style\") {\n throw new TypeError(\n __DEV__ ? `ShadowJS head: <${tag}> takes no children.` : \"SHADOW_HEAD_NO_CHILDREN\",\n );\n }\n if (typeof children !== \"string\")\n throw new TypeError(__DEV__ ? \"ShadowJS head: children must be text.\" : \"SHADOW_HEAD_TEXT\");\n return children;\n}\n\n/** The key of a style. Only style takes one. */\nfunction keyFrom(tag: HeadTagName, key: unknown): string | undefined {\n if (key === undefined || key === null) return undefined;\n if (tag !== \"style\")\n throw new TypeError(\n __DEV__ ? \"ShadowJS head: only <style> takes a key.\" : \"SHADOW_HEAD_STYLE_KEY\",\n );\n if (typeof key !== \"string\" || key === \"\") {\n throw new TypeError(\n __DEV__ ? \"ShadowJS head: a key must be a non-empty string.\" : \"SHADOW_HEAD_KEY\",\n );\n }\n return key;\n}\n\n/** Rel tokens, lower-cased and sorted, so `\"ICON apple\"` and `\"apple icon\"` compare equal. */\nfunction normalizeRel(raw: string | undefined): string | undefined {\n if (raw === undefined) return undefined;\n const tokens = raw\n .toLowerCase()\n .split(/\\s+/)\n .filter((token) => token !== \"\");\n if (tokens.length === 0) return undefined;\n return tokens.sort().join(\" \");\n}\n\n/** Identity of a meta: its first identifying field and the media query. */\nfunction metaIdentity(lookup: ReadonlyMap<string, string>): string | undefined {\n const media = lookup.get(\"media\") ?? \"\";\n for (const field of META_IDENTITY_FIELDS) {\n const value = lookup.get(field);\n if (value === undefined) continue;\n // A document has one charset, whatever its value: that meta is a singleton.\n if (field === \"charset\") return frame([\"meta\", field]);\n if (MULTI_VALUED_META.has(value))\n return frame([\"meta\", field, value, media, lookup.get(\"content\") ?? \"\"]);\n return frame([\"meta\", field, value, media]);\n }\n return undefined;\n}\n\n/** Identity of a link: icons by rel, sizes and type; preloads and stylesheets by rel, href and as. */\nfunction linkIdentity(lookup: ReadonlyMap<string, string>): string | undefined {\n const rel = normalizeRel(lookup.get(\"rel\"));\n if (rel === undefined) return undefined;\n const isIcon = rel.split(\" \").some((token) => token === \"icon\" || token.endsWith(\"-icon\"));\n if (isIcon) {\n return frame([\"link\", \"icon\", rel, lookup.get(\"sizes\") ?? \"\", lookup.get(\"type\") ?? \"\"]);\n }\n // A document has one canonical URL: a singleton, so a new value replaces the old element (and a server's\n // copy is adopted) instead of a second `<link rel=\"canonical\">` being appended.\n if (rel === \"canonical\") return frame([\"link\", \"canonical\"]);\n if (RESOURCE_RELS.has(rel)) {\n return frame([\"link\", \"resource\", rel, lookup.get(\"href\") ?? \"\", lookup.get(\"as\") ?? \"\"]);\n }\n if (MULTI_LINK_RELS.has(rel)) {\n return frame([\n \"link\",\n \"multi\",\n rel,\n lookup.get(\"href\") ?? \"\",\n lookup.get(\"hreflang\") ?? \"\",\n lookup.get(\"media\") ?? \"\",\n lookup.get(\"type\") ?? \"\",\n ]);\n }\n return undefined;\n}\n\n/** Identity of a validated tag, or undefined when the tag is append-only. @internal */\nexport function identityOf(\n tag: HeadTagName,\n lookup: ReadonlyMap<string, string>,\n key: string | undefined,\n): string | undefined {\n if (tag === \"title\") return frame([\"title\"]);\n if (tag === \"base\") return frame([\"base\"]);\n if (tag === \"style\") return key === undefined ? undefined : frame([\"style\", key]);\n return tag === \"meta\" ? metaIdentity(lookup) : linkIdentity(lookup);\n}\n\n/**\n * Validates one head tag description and computes its identity. Hostile input throws a\n * TypeError: an unknown tag (including `script`), a bad attribute name, an event attribute, a\n * reserved marker, children on a void tag, or a key on anything but a style. An unsafe URL\n * attribute is dropped with a dev warning, the same way `setAttribute` treats it.\n *\n * @param input The description, usually a {@link HeadTag}; any value is checked.\n * @returns The normalized tag.\n * @throws TypeError for every invalid input listed above.\n * @internal\n * @since 1.0.0\n */\nexport function normalizeTag(input: unknown): NormalizedTag {\n if (input === null || typeof input !== \"object\" || Array.isArray(input)) {\n throw new TypeError(\n __DEV__ ? \"ShadowJS head: a head tag must be an object.\" : \"SHADOW_HEAD_TAG\",\n );\n }\n const name = Reflect.get(input, \"tag\");\n if (typeof name !== \"string\" || !isHeadTagName(name)) {\n const shown = typeof name === \"string\" ? name : typeof name;\n throw new TypeError(\n __DEV__ ? `ShadowJS head: unsupported tag \"${shown}\".` : \"SHADOW_HEAD_TAG_NAME\",\n );\n }\n const lookup = attributesFrom(name, Reflect.get(input, \"props\"));\n const text = textFrom(name, Reflect.get(input, \"children\"));\n const key = keyFrom(name, Reflect.get(input, \"key\"));\n return {\n tag: name,\n attributes: [...lookup],\n text,\n key,\n identity: identityOf(name, lookup, key),\n };\n}\n\n/**\n * Normalizes what {@link useHead} receives: one description, a list, or nothing.\n *\n * @param input A description, a list of them, or null, undefined or false for none.\n * @returns The normalized tags, in order.\n * @throws TypeError when any description is invalid: an unknown tag (including `script`), an\n * attribute name that is not valid or starts with `on`, a reserved marker attribute, an attribute\n * value that is not a string, number or boolean, children on a tag other than `title` and `style`,\n * or a key on anything but a `style`. The production messages start with `SHADOW_HEAD_`.\n * @example\n * import { normalizeTags } from \"@shadow-js/web/server\";\n *\n * const tags = normalizeTags([\n * { tag: \"title\", children: \"A\" },\n * { tag: \"base\", props: { href: \"/\" } },\n * ]);\n * console.log(tags.length); // 2\n * @see useHead\n * @since 1.0.0\n */\nexport function normalizeTags(input: unknown): NormalizedTag[] {\n if (input === null || input === undefined || input === false) return [];\n if (Array.isArray(input)) return input.map((item: unknown) => normalizeTag(item));\n return [normalizeTag(input)];\n}\n\n/**\n * The identity of an element already in the document, computed the same way as for a\n * registration, so the sink can adopt a server-rendered element. Undefined for elements that are\n * not head tags or that are append-only.\n * @internal\n */\nexport function identityOfElement(element: Element): string | undefined {\n const name = element.localName;\n if (!isHeadTagName(name)) return undefined;\n const lookup = new Map<string, string>();\n for (const attribute of Array.from(element.attributes)) {\n if (attribute.name === HEAD_MARKER || attribute.name === HEAD_KEY) continue;\n lookup.set(attribute.name.toLowerCase(), attribute.value);\n }\n const key = element.getAttribute(HEAD_KEY) ?? undefined;\n return identityOf(name, lookup, key);\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
//#region src/hmr.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The subset of Vite's `import.meta.hot` the runtime uses. The compiled output passes
|
|
4
|
+
* `import.meta.hot` to `registry`, so any object with these three members works (a test runner can
|
|
5
|
+
* supply its own).
|
|
6
|
+
* @since 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
export interface HotContext {
|
|
9
|
+
/** Data kept across hot updates of the module: values stored in a `dispose` callback arrive here. */
|
|
10
|
+
readonly data: Record<string, unknown>;
|
|
11
|
+
/** Declares that the module accepts its own update, so the page is not reloaded. */
|
|
12
|
+
accept(): void;
|
|
13
|
+
/** Registers a callback that runs before the module is replaced and may store values in `data`. */
|
|
14
|
+
dispose(callback: (data: Record<string, unknown>) => void): void;
|
|
15
|
+
}
|
|
16
|
+
type ComponentFunction = (props: never) => unknown;
|
|
17
|
+
interface Entry {
|
|
18
|
+
implementation: ComponentFunction;
|
|
19
|
+
readonly version: () => number;
|
|
20
|
+
readonly bump: () => void;
|
|
21
|
+
readonly proxy: ComponentFunction;
|
|
22
|
+
dirty: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The components of one module, kept across hot updates. Instances are created through a stable
|
|
26
|
+
* proxy that follows the current implementation, so an edit re-renders only the edited component's
|
|
27
|
+
* instances; parents and siblings keep their state. Produced by `registry`; the compiled output
|
|
28
|
+
* of a development build is its only user.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* import { type HotContext, registry } from "@shadow-js/web/hmr";
|
|
32
|
+
*
|
|
33
|
+
* declare const hot: HotContext | undefined;
|
|
34
|
+
* const modules = registry(hot, "src/app.tsx");
|
|
35
|
+
* console.log(modules.managed); // false when there is no hot context (production, server)
|
|
36
|
+
* @see registry
|
|
37
|
+
* @since 1.0.0
|
|
38
|
+
*/
|
|
39
|
+
export declare class ModuleRegistry {
|
|
40
|
+
/** The components of the module by name, each with the proxy instances are created through. */
|
|
41
|
+
readonly entries: Map<string, Entry>;
|
|
42
|
+
/** True when the module can hot update (a hot context was given); false makes the registry inert. */
|
|
43
|
+
readonly managed: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Told after each update of this module, once its components are re-rendered. An error boundary
|
|
46
|
+
* that shows its fallback listens here, so the edit that fixes its error rebuilds its children.
|
|
47
|
+
*/
|
|
48
|
+
readonly listeners: Set<() => void>;
|
|
49
|
+
/**
|
|
50
|
+
* Creates an empty registry.
|
|
51
|
+
*
|
|
52
|
+
* @param managed Whether the module can hot update. When false, `component` returns each
|
|
53
|
+
* implementation unchanged.
|
|
54
|
+
*/
|
|
55
|
+
constructor(managed: boolean);
|
|
56
|
+
/**
|
|
57
|
+
* Re-renders every instance of the components replaced during the last module evaluation, then tells
|
|
58
|
+
* the listeners. The listeners are read before the re-render: a boundary that starts listening because
|
|
59
|
+
* this very update failed is not reset by it.
|
|
60
|
+
*/
|
|
61
|
+
commit(): void;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns the registry of the module identified by `hot`, reusing the one from the previous
|
|
65
|
+
* evaluation of the same module. Without `hot` (production, server) the registry is inert.
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
export declare function registry(hot: HotContext | undefined, _id: string): ModuleRegistry;
|
|
69
|
+
/**
|
|
70
|
+
* Registers a component implementation and returns the function to use in its place. On the first
|
|
71
|
+
* evaluation that is a proxy; on later evaluations the same proxy, now backed by the new code.
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
export declare function component<P>(reg: ModuleRegistry, name: string, implementation: (props: P) => unknown): (props: P) => unknown;
|
|
75
|
+
/**
|
|
76
|
+
* Marks the module as able to update itself and applies the replaced components.
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
export declare function accept(hot: HotContext | undefined, reg: ModuleRegistry): void;
|
|
80
|
+
//#endregion
|
|
81
|
+
export {};
|
|
82
|
+
//# sourceMappingURL=hmr.d.ts.map
|
package/dist/dev/hmr.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { hotModulesOf } from "./control-flow/hot-link.js";
|
|
2
|
+
import { batch, getOwner, untrack, useSignal } from "@shadow-js/core";
|
|
3
|
+
import { createComputation, readComputation } from "@shadow-js/core/extend";
|
|
4
|
+
//#region src/hmr.ts
|
|
5
|
+
const REGISTRY_KEY = "shadowjs:hmr-registry";
|
|
6
|
+
/**
|
|
7
|
+
* The components of one module, kept across hot updates. Instances are created through a stable
|
|
8
|
+
* proxy that follows the current implementation, so an edit re-renders only the edited component's
|
|
9
|
+
* instances; parents and siblings keep their state. Produced by `registry`; the compiled output
|
|
10
|
+
* of a development build is its only user.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* import { type HotContext, registry } from "@shadow-js/web/hmr";
|
|
14
|
+
*
|
|
15
|
+
* declare const hot: HotContext | undefined;
|
|
16
|
+
* const modules = registry(hot, "src/app.tsx");
|
|
17
|
+
* console.log(modules.managed); // false when there is no hot context (production, server)
|
|
18
|
+
* @see registry
|
|
19
|
+
* @since 1.0.0
|
|
20
|
+
*/
|
|
21
|
+
var ModuleRegistry = class {
|
|
22
|
+
/** The components of the module by name, each with the proxy instances are created through. */
|
|
23
|
+
entries = /* @__PURE__ */ new Map();
|
|
24
|
+
/** True when the module can hot update (a hot context was given); false makes the registry inert. */
|
|
25
|
+
managed;
|
|
26
|
+
/**
|
|
27
|
+
* Told after each update of this module, once its components are re-rendered. An error boundary
|
|
28
|
+
* that shows its fallback listens here, so the edit that fixes its error rebuilds its children.
|
|
29
|
+
*/
|
|
30
|
+
listeners = /* @__PURE__ */ new Set();
|
|
31
|
+
/**
|
|
32
|
+
* Creates an empty registry.
|
|
33
|
+
*
|
|
34
|
+
* @param managed Whether the module can hot update. When false, `component` returns each
|
|
35
|
+
* implementation unchanged.
|
|
36
|
+
*/
|
|
37
|
+
constructor(managed) {
|
|
38
|
+
this.managed = managed;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Re-renders every instance of the components replaced during the last module evaluation, then tells
|
|
42
|
+
* the listeners. The listeners are read before the re-render: a boundary that starts listening because
|
|
43
|
+
* this very update failed is not reset by it.
|
|
44
|
+
*/
|
|
45
|
+
commit() {
|
|
46
|
+
const listening = [...this.listeners];
|
|
47
|
+
batch(() => {
|
|
48
|
+
for (const entry of this.entries.values()) {
|
|
49
|
+
if (!entry.dirty) continue;
|
|
50
|
+
entry.dirty = false;
|
|
51
|
+
entry.bump();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
for (const listener of listening) listener();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Returns the registry of the module identified by `hot`, reusing the one from the previous
|
|
59
|
+
* evaluation of the same module. Without `hot` (production, server) the registry is inert.
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
function registry(hot, _id) {
|
|
63
|
+
if (hot === void 0 || typeof hot.data !== "object" || hot.data === null) return new ModuleRegistry(false);
|
|
64
|
+
const previous = hot.data[REGISTRY_KEY];
|
|
65
|
+
if (previous instanceof ModuleRegistry) return previous;
|
|
66
|
+
const created = new ModuleRegistry(true);
|
|
67
|
+
hot.dispose((data) => {
|
|
68
|
+
data[REGISTRY_KEY] = created;
|
|
69
|
+
});
|
|
70
|
+
return created;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Registers a component implementation and returns the function to use in its place. On the first
|
|
74
|
+
* evaluation that is a proxy; on later evaluations the same proxy, now backed by the new code.
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
function component(reg, name, implementation) {
|
|
78
|
+
if (!reg.managed) return implementation;
|
|
79
|
+
const existing = reg.entries.get(name);
|
|
80
|
+
if (existing !== void 0) {
|
|
81
|
+
existing.implementation = implementation;
|
|
82
|
+
existing.dirty = true;
|
|
83
|
+
return existing.proxy;
|
|
84
|
+
}
|
|
85
|
+
const [version, setVersion] = useSignal(0);
|
|
86
|
+
const entry = {
|
|
87
|
+
implementation,
|
|
88
|
+
version,
|
|
89
|
+
bump: () => {
|
|
90
|
+
setVersion((n) => n + 1);
|
|
91
|
+
},
|
|
92
|
+
proxy: void 0,
|
|
93
|
+
dirty: false
|
|
94
|
+
};
|
|
95
|
+
const proxy = (props) => {
|
|
96
|
+
hotModulesOf(getOwner())?.add(reg);
|
|
97
|
+
const node = createComputation({
|
|
98
|
+
kind: "memo",
|
|
99
|
+
allowWrites: true,
|
|
100
|
+
fn: () => {
|
|
101
|
+
entry.version();
|
|
102
|
+
return untrack(() => entry.implementation(props));
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return () => readComputation(node);
|
|
106
|
+
};
|
|
107
|
+
Object.defineProperty(proxy, "name", { value: name });
|
|
108
|
+
entry.proxy = proxy;
|
|
109
|
+
reg.entries.set(name, entry);
|
|
110
|
+
return proxy;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Marks the module as able to update itself and applies the replaced components.
|
|
114
|
+
* @internal
|
|
115
|
+
*/
|
|
116
|
+
function accept(hot, reg) {
|
|
117
|
+
if (hot === void 0 || !reg.managed) return;
|
|
118
|
+
hot.accept();
|
|
119
|
+
reg.commit();
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
export { ModuleRegistry, accept, component, registry };
|
|
123
|
+
|
|
124
|
+
//# sourceMappingURL=hmr.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hmr.js","names":[],"sources":["../../src/hmr.ts"],"sourcesContent":["import { batch, getOwner, untrack, useSignal } from \"@shadow-js/core\";\nimport { createComputation, readComputation } from \"@shadow-js/core/extend\";\nimport { hotModulesOf } from \"./control-flow/hot-link\";\n\n/**\n * The subset of Vite's `import.meta.hot` the runtime uses. The compiled output passes\n * `import.meta.hot` to `registry`, so any object with these three members works (a test runner can\n * supply its own).\n * @since 1.0.0\n */\nexport interface HotContext {\n /** Data kept across hot updates of the module: values stored in a `dispose` callback arrive here. */\n readonly data: Record<string, unknown>;\n /** Declares that the module accepts its own update, so the page is not reloaded. */\n accept(): void;\n /** Registers a callback that runs before the module is replaced and may store values in `data`. */\n dispose(callback: (data: Record<string, unknown>) => void): void;\n}\n\ntype ComponentFunction = (props: never) => unknown;\n\ninterface Entry {\n implementation: ComponentFunction;\n readonly version: () => number;\n readonly bump: () => void;\n readonly proxy: ComponentFunction;\n dirty: boolean;\n}\n\nconst REGISTRY_KEY = \"shadowjs:hmr-registry\";\n\n/**\n * The components of one module, kept across hot updates. Instances are created through a stable\n * proxy that follows the current implementation, so an edit re-renders only the edited component's\n * instances; parents and siblings keep their state. Produced by `registry`; the compiled output\n * of a development build is its only user.\n *\n * @example\n * import { type HotContext, registry } from \"@shadow-js/web/hmr\";\n *\n * declare const hot: HotContext | undefined;\n * const modules = registry(hot, \"src/app.tsx\");\n * console.log(modules.managed); // false when there is no hot context (production, server)\n * @see registry\n * @since 1.0.0\n */\nexport class ModuleRegistry {\n /** The components of the module by name, each with the proxy instances are created through. */\n readonly entries: Map<string, Entry> = new Map<string, Entry>();\n /** True when the module can hot update (a hot context was given); false makes the registry inert. */\n readonly managed: boolean;\n /**\n * Told after each update of this module, once its components are re-rendered. An error boundary\n * that shows its fallback listens here, so the edit that fixes its error rebuilds its children.\n */\n readonly listeners: Set<() => void> = new Set<() => void>();\n\n /**\n * Creates an empty registry.\n *\n * @param managed Whether the module can hot update. When false, `component` returns each\n * implementation unchanged.\n */\n constructor(managed: boolean) {\n this.managed = managed;\n }\n\n /**\n * Re-renders every instance of the components replaced during the last module evaluation, then tells\n * the listeners. The listeners are read before the re-render: a boundary that starts listening because\n * this very update failed is not reset by it.\n */\n commit(): void {\n const listening = [...this.listeners];\n batch(() => {\n for (const entry of this.entries.values()) {\n if (!entry.dirty) continue;\n entry.dirty = false;\n entry.bump();\n }\n });\n for (const listener of listening) listener();\n }\n}\n\n/**\n * Returns the registry of the module identified by `hot`, reusing the one from the previous\n * evaluation of the same module. Without `hot` (production, server) the registry is inert.\n * @internal\n */\nexport function registry(hot: HotContext | undefined, _id: string): ModuleRegistry {\n // A test runner may define `import.meta.hot` without a `data` bag: nothing can hot update there.\n if (hot === undefined || typeof hot.data !== \"object\" || hot.data === null) {\n return new ModuleRegistry(false);\n }\n const previous = hot.data[REGISTRY_KEY];\n if (previous instanceof ModuleRegistry) return previous;\n const created = new ModuleRegistry(true);\n hot.dispose((data) => {\n data[REGISTRY_KEY] = created;\n });\n return created;\n}\n\n/**\n * Registers a component implementation and returns the function to use in its place. On the first\n * evaluation that is a proxy; on later evaluations the same proxy, now backed by the new code.\n * @internal\n */\nexport function component<P>(\n reg: ModuleRegistry,\n name: string,\n implementation: (props: P) => unknown,\n): (props: P) => unknown {\n if (!reg.managed) return implementation;\n const existing = reg.entries.get(name);\n if (existing !== undefined) {\n existing.implementation = implementation as ComponentFunction;\n existing.dirty = true;\n return existing.proxy as (props: P) => unknown;\n }\n const [version, setVersion] = useSignal(0);\n const entry: Entry = {\n implementation: implementation as ComponentFunction,\n version,\n bump: () => {\n setVersion((n) => n + 1);\n },\n proxy: undefined as unknown as ComponentFunction,\n dirty: false,\n };\n const proxy = (props: P): (() => unknown) => {\n // The error boundary above this instance (if any) learns that its children come from this module.\n hotModulesOf(getOwner())?.add(reg);\n // A memo per instance: an update re-renders this component only, not the arrays or regions\n // that merely contain it. It runs component setup, which may write state it owns.\n const node = createComputation({\n kind: \"memo\",\n allowWrites: true,\n fn: () => {\n entry.version();\n return untrack(() => (entry.implementation as (props: P) => unknown)(props));\n },\n });\n return () => readComputation<unknown>(node);\n };\n Object.defineProperty(proxy, \"name\", { value: name });\n (entry as { proxy: ComponentFunction }).proxy = proxy as unknown as ComponentFunction;\n reg.entries.set(name, entry);\n return proxy;\n}\n\n/**\n * Marks the module as able to update itself and applies the replaced components.\n * @internal\n */\nexport function accept(hot: HotContext | undefined, reg: ModuleRegistry): void {\n if (hot === undefined || !reg.managed) return;\n hot.accept();\n reg.commit();\n}\n"],"mappings":";;;;AA6BA,MAAM,eAAe;;;;;;;;;;;;;;;;AAiBrB,IAAa,iBAAb,MAA4B;;CAE1B,0BAAuC,IAAI,IAAmB;;CAE9D;;;;;CAKA,4BAAsC,IAAI,IAAgB;;;;;;;CAQ1D,YAAY,SAAkB;EAC5B,KAAK,UAAU;CACjB;;;;;;CAOA,SAAe;EACb,MAAM,YAAY,CAAC,GAAG,KAAK,SAAS;EACpC,YAAY;GACV,KAAK,MAAM,SAAS,KAAK,QAAQ,OAAO,GAAG;IACzC,IAAI,CAAC,MAAM,OAAO;IAClB,MAAM,QAAQ;IACd,MAAM,KAAK;GACb;EACF,CAAC;EACD,KAAK,MAAM,YAAY,WAAW,SAAS;CAC7C;AACF;;;;;;AAOA,SAAgB,SAAS,KAA6B,KAA6B;CAEjF,IAAI,QAAQ,KAAA,KAAa,OAAO,IAAI,SAAS,YAAY,IAAI,SAAS,MACpE,OAAO,IAAI,eAAe,KAAK;CAEjC,MAAM,WAAW,IAAI,KAAK;CAC1B,IAAI,oBAAoB,gBAAgB,OAAO;CAC/C,MAAM,UAAU,IAAI,eAAe,IAAI;CACvC,IAAI,SAAS,SAAS;EACpB,KAAK,gBAAgB;CACvB,CAAC;CACD,OAAO;AACT;;;;;;AAOA,SAAgB,UACd,KACA,MACA,gBACuB;CACvB,IAAI,CAAC,IAAI,SAAS,OAAO;CACzB,MAAM,WAAW,IAAI,QAAQ,IAAI,IAAI;CACrC,IAAI,aAAa,KAAA,GAAW;EAC1B,SAAS,iBAAiB;EAC1B,SAAS,QAAQ;EACjB,OAAO,SAAS;CAClB;CACA,MAAM,CAAC,SAAS,cAAc,UAAU,CAAC;CACzC,MAAM,QAAe;EACH;EAChB;EACA,YAAY;GACV,YAAY,MAAM,IAAI,CAAC;EACzB;EACA,OAAO,KAAA;EACP,OAAO;CACT;CACA,MAAM,SAAS,UAA8B;EAE3C,aAAa,SAAS,CAAC,CAAC,EAAE,IAAI,GAAG;EAGjC,MAAM,OAAO,kBAAkB;GAC7B,MAAM;GACN,aAAa;GACb,UAAU;IACR,MAAM,QAAQ;IACd,OAAO,cAAe,MAAM,eAAyC,KAAK,CAAC;GAC7E;EACF,CAAC;EACD,aAAa,gBAAyB,IAAI;CAC5C;CACA,OAAO,eAAe,OAAO,QAAQ,EAAE,OAAO,KAAK,CAAC;CACpD,MAAwC,QAAQ;CAChD,IAAI,QAAQ,IAAI,MAAM,KAAK;CAC3B,OAAO;AACT;;;;;AAMA,SAAgB,OAAO,KAA6B,KAA2B;CAC7E,IAAI,QAAQ,KAAA,KAAa,CAAC,IAAI,SAAS;CACvC,IAAI,OAAO;CACX,IAAI,OAAO;AACb"}
|