@stacksjs/stx 0.2.95 → 0.2.96
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/dist/a11y.js +598 -2
- package/dist/analytics.js +159 -2
- package/dist/analyzer.js +240 -2
- package/dist/animation.js +688 -2
- package/dist/app-shell.js +220 -2
- package/dist/app.js +79 -2
- package/dist/assets.js +155 -2
- package/dist/ast.js +630 -2
- package/dist/async-components.js +428 -2
- package/dist/auth.js +12 -2
- package/dist/broadcasting.js +102 -2
- package/dist/browser-composables.js +565 -2
- package/dist/build-assets.js +29 -2
- package/dist/build-mode-detector.js +50 -2
- package/dist/build-optimizer.js +433 -2
- package/dist/build-views.js +180 -2
- package/dist/build.js +33 -2
- package/dist/builtins/drawer.js +37 -2
- package/dist/builtins/escape.js +3 -2
- package/dist/builtins/icon.js +83 -2
- package/dist/builtins/index.js +34 -2
- package/dist/builtins/modal.js +38 -2
- package/dist/builtins/safe-image.js +51 -2
- package/dist/builtins/stx-image.js +200 -2
- package/dist/builtins/stx-link.js +41 -2
- package/dist/builtins/stx-loading-indicator.js +35 -2
- package/dist/builtins/suspense.js +24 -2
- package/dist/builtins/toast.js +42 -2
- package/dist/builtins/tooltip.js +106 -2
- package/dist/builtins/transition-group.js +22 -2
- package/dist/bundle-analyzer/collector.js +233 -2
- package/dist/bundle-analyzer/index.js +82 -2
- package/dist/bundle-analyzer/report.js +318 -2
- package/dist/bundle-analyzer/treemap.js +594 -2
- package/dist/caching.js +131 -2
- package/dist/cli.js +11317 -10982
- package/dist/client/directive.js +14 -2
- package/dist/client/index.js +2 -2
- package/dist/client-helpers.js +312 -2
- package/dist/client-script-bundler.js +216 -6
- package/dist/client-script.js +754 -2
- package/dist/client.js +73 -4
- package/dist/component-hmr.js +585 -2
- package/dist/component-library.d.ts +6 -0
- package/dist/component-library.js +683 -0
- package/dist/component-processing.js +613 -2
- package/dist/component-registry.js +90 -2
- package/dist/component-renderer.js +660 -2
- package/dist/components.js +213 -2
- package/dist/composables/index.js +220 -2
- package/dist/composables/use-battery.js +101 -2
- package/dist/composables/use-broadcast-channel.js +80 -2
- package/dist/composables/use-channel.js +124 -2
- package/dist/composables/use-clipboard.js +79 -2
- package/dist/composables/use-color-mode.js +131 -2
- package/dist/composables/use-cookie.js +86 -2
- package/dist/composables/use-device-orientation.js +181 -2
- package/dist/composables/use-event-listener.js +27 -2
- package/dist/composables/use-event-source.js +166 -2
- package/dist/composables/use-eye-dropper.js +142 -2
- package/dist/composables/use-fetch.js +223 -2
- package/dist/composables/use-fullscreen.js +110 -2
- package/dist/composables/use-geolocation.js +101 -2
- package/dist/composables/use-idle.js +182 -2
- package/dist/composables/use-intersection-observer.js +168 -2
- package/dist/composables/use-keyboard.js +224 -2
- package/dist/composables/use-media-query.js +62 -2
- package/dist/composables/use-meta.js +48 -2
- package/dist/composables/use-mouse.js +286 -2
- package/dist/composables/use-mutation-observer.js +129 -2
- package/dist/composables/use-network.js +85 -2
- package/dist/composables/use-notification.js +94 -2
- package/dist/composables/use-permissions.js +132 -2
- package/dist/composables/use-query.js +0 -1
- package/dist/composables/use-resize-observer.js +157 -2
- package/dist/composables/use-router.js +51 -2
- package/dist/composables/use-share.js +104 -2
- package/dist/composables/use-speech.js +294 -2
- package/dist/composables/use-storage.js +130 -2
- package/dist/composables/use-text-selection.js +205 -4
- package/dist/composables/use-timer.js +183 -2
- package/dist/composables/use-utilities.js +176 -2
- package/dist/composables/use-wake-lock.js +122 -2
- package/dist/composables/use-websocket.js +165 -2
- package/dist/composables/use-window.js +163 -2
- package/dist/composables.js +552 -2
- package/dist/composition-api.js +190 -2
- package/dist/computed.js +376 -8
- package/dist/conditionals.js +599 -2
- package/dist/config.js +751 -2
- package/dist/craft-bridge.js +432 -2
- package/dist/craft-compiler.js +810 -2
- package/dist/craft-components.js +945 -2
- package/dist/craft-entry.js +5 -2
- package/dist/craft-ssr.js +559 -2
- package/dist/csp.js +220 -2
- package/dist/csrf.js +38 -2
- package/dist/custom-directives.js +136 -2
- package/dist/defer.js +156 -2
- package/dist/deploy/config-generators.js +214 -2
- package/dist/deploy/index.js +203 -2
- package/dist/deploy/netlify.js +167 -2
- package/dist/dev-server/crosswind.js +379 -2
- package/dist/dev-server/index.js +7 -2
- package/dist/dev-server/keyboard-shortcuts.js +81 -2
- package/dist/dev-server/native-window.js +91 -2
- package/dist/dev-server/port-utils.js +40 -2
- package/dist/dev-server/serve-app.js +682 -2
- package/dist/dev-server/serve-file.js +439 -2
- package/dist/dev-server/serve-markdown.js +281 -2
- package/dist/dev-server/serve-multi.js +579 -2
- package/dist/dev-server/terminal-colors.js +45 -2
- package/dist/dev-server/theme-selector.js +111 -2
- package/dist/dev-server/types.js +43 -2
- package/dist/dev-server.js +4 -2
- package/dist/devtools.js +462 -18
- package/dist/directive-api.js +355 -2
- package/dist/docs.js +567 -2
- package/dist/doctor.js +123 -2
- package/dist/document-shell.js +105 -2
- package/dist/dynamic-components.js +78 -2
- package/dist/edge-runtime.js +539 -2
- package/dist/env.js +9 -2
- package/dist/error-boundaries.js +62 -12
- package/dist/error-handling.js +1 -2
- package/dist/errors/codes.js +97 -2
- package/dist/errors/formatter.js +196 -2
- package/dist/errors/index.js +44 -2
- package/dist/errors/logger.js +250 -2
- package/dist/errors/sanitizer.js +181 -2
- package/dist/errors/types.js +86 -2
- package/dist/events.js +382 -2
- package/dist/expressions.js +650 -2
- package/dist/formatter.js +265 -2
- package/dist/forms-validation.js +510 -4
- package/dist/forms.js +709 -2
- package/dist/head.js +224 -2
- package/dist/heatmap.js +566 -2
- package/dist/hot-reload.js +367 -2
- package/dist/html-masking.js +73 -2
- package/dist/hydration-runtime.js +195 -2
- package/dist/hydration.js +654 -2
- package/dist/i18n.js +633 -2
- package/dist/image-optimization/auto-sibling.js +88 -2
- package/dist/image-optimization/build-plugin.js +241 -2
- package/dist/image-optimization/component.js +244 -2
- package/dist/image-optimization/directive.js +203 -2
- package/dist/image-optimization/index.js +54 -2
- package/dist/image-optimization/processor.js +262 -2
- package/dist/image-optimization/serve.js +102 -2
- package/dist/includes.js +733 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +134 -2
- package/dist/init.js +505 -2
- package/dist/inline-assets.js +82 -2
- package/dist/interactive.js +383 -2
- package/dist/internal-markdown.js +141 -2
- package/dist/island-chunking.js +52 -2
- package/dist/js-ts.js +57 -2
- package/dist/jsx-runtime.js +289 -2
- package/dist/keep-alive.js +421 -2
- package/dist/lazy-loader.js +245 -2
- package/dist/loading-indicator.js +322 -2
- package/dist/loops.js +643 -2
- package/dist/manifest.js +31 -2
- package/dist/markdown.js +68 -2
- package/dist/media/client/blur-up.js +217 -2
- package/dist/media/client/index.js +53 -2
- package/dist/media/client/lazy-load.js +283 -2
- package/dist/media/client/upload-handler.js +354 -2
- package/dist/media/image/component.js +574 -2
- package/dist/media/image/directive.js +160 -2
- package/dist/media/image/editing.js +193 -2
- package/dist/media/image/index.js +80 -2
- package/dist/media/image/placeholder.js +188 -2
- package/dist/media/image/processor/cache.js +51 -2
- package/dist/media/image/processor/index.js +3 -2
- package/dist/media/image/processor/optimizer.js +202 -2
- package/dist/media/image/processor/responsive.js +162 -2
- package/dist/media/image/srcset.js +194 -2
- package/dist/media/index.js +198 -2
- package/dist/media/manager/embed.js +253 -2
- package/dist/media/protected/component.js +235 -2
- package/dist/media/protected/index.js +20 -2
- package/dist/media/protected/signature.js +205 -2
- package/dist/media/shared/cache.js +132 -2
- package/dist/media/shared/hash.js +35 -2
- package/dist/media/shared/index.js +2 -2
- package/dist/media/types.js +0 -1
- package/dist/media/upload/component.js +320 -2
- package/dist/media/upload/index.js +1 -2
- package/dist/media/video/directive.js +142 -2
- package/dist/media/video/index.js +583 -2
- package/dist/media/video/processor/cache.js +52 -2
- package/dist/media/video/processor/index.js +4 -2
- package/dist/media/video/processor/streaming.js +197 -2
- package/dist/media/video/processor/thumbnail.js +134 -2
- package/dist/media/video/processor/transcoder.js +283 -2
- package/dist/method-spoofing.js +17 -2
- package/dist/middleware.js +40 -2
- package/dist/misc-directives.js +222 -2
- package/dist/native-build.js +511 -2
- package/dist/parser/directive-parser.js +239 -2
- package/dist/parser/expression-parser.js +358 -2
- package/dist/parser/index.js +23 -2
- package/dist/parser/tokenizer.js +534 -2
- package/dist/partial-hydration.js +427 -2
- package/dist/performance-utils.js +430 -2
- package/dist/placeholder.js +35 -2
- package/dist/plugin-system.js +214 -2
- package/dist/plugin.js +173 -2
- package/dist/precompiler.js +660 -81
- package/dist/process.js +711 -2
- package/dist/production-build.js +596 -2
- package/dist/production-builder.js +132 -2
- package/dist/production-server.js +188 -2
- package/dist/props.js +224 -2
- package/dist/public-env.js +19 -2
- package/dist/pwa/audit.js +255 -2
- package/dist/pwa/directives.js +332 -2
- package/dist/pwa/icons.js +109 -2
- package/dist/pwa/index.js +47 -2
- package/dist/pwa/inject.js +102 -2
- package/dist/pwa/manifest.js +186 -2
- package/dist/pwa/offline.js +259 -2
- package/dist/pwa/precache.js +116 -2
- package/dist/pwa/service-worker.js +860 -2
- package/dist/pwa/workbox-strategies.js +167 -2
- package/dist/pwa/workbox.js +382 -2
- package/dist/pwa.js +427 -427
- package/dist/reactive-bindings.js +266 -2
- package/dist/reactive.js +569 -2
- package/dist/reactivity.js +559 -2
- package/dist/release.js +1 -2
- package/dist/render.js +165 -2
- package/dist/route-middleware.js +17 -2
- package/dist/router.js +9 -2
- package/dist/routes.js +14 -2
- package/dist/runtime-injection.js +120 -2
- package/dist/runtime.js +94 -2
- package/dist/safe-evaluator.js +444 -2
- package/dist/scaffolding.js +897 -2
- package/dist/screenshot.js +87 -2
- package/dist/script-classifier.js +32 -2
- package/dist/script-validation.js +158 -2
- package/dist/seo.js +457 -2
- package/dist/serve.js +237 -2
- package/dist/server-bindings.js +108 -2
- package/dist/server-components.js +338 -11
- package/dist/signal-processing.js +946 -2
- package/dist/signals-api.js +154 -2
- package/dist/signals.js +5287 -2
- package/dist/site-builder/build.js +224 -2
- package/dist/site-builder/i18n.js +140 -2
- package/dist/site-builder/index.js +15 -2
- package/dist/site-builder/robots.js +26 -2
- package/dist/site-builder/router.js +27 -2
- package/dist/site-builder/seo.js +36 -2
- package/dist/site-builder/sitemap.js +26 -2
- package/dist/site-builder/theme.js +33 -2
- package/dist/site-builder/types.js +0 -1
- package/dist/slots.js +201 -2
- package/dist/source-maps.js +350 -2
- package/dist/spa-shell.js +34 -2
- package/dist/ssg.js +389 -389
- package/dist/ssr.js +355 -2
- package/dist/state-management.js +533 -2
- package/dist/store-imports.js +13 -2
- package/dist/store-loader.js +72 -5
- package/dist/stores-client.js +132 -2
- package/dist/story/addons.js +261 -2
- package/dist/story/analytics.js +235 -2
- package/dist/story/auto-stories.js +158 -2
- package/dist/story/bookmarks.js +277 -2
- package/dist/story/bun-test.js +102 -2
- package/dist/story/cli.js +155 -2
- package/dist/story/collect/analyzer.js +305 -2
- package/dist/story/collect/index.js +27 -2
- package/dist/story/collect/parser.js +129 -2
- package/dist/story/collect/scanner.js +101 -2
- package/dist/story/collect/tree.js +110 -2
- package/dist/story/commands/build.js +172 -2
- package/dist/story/commands/dev.js +78 -2
- package/dist/story/commands/index.js +3 -2
- package/dist/story/commands/preview.js +56 -2
- package/dist/story/compiled-output.js +151 -2
- package/dist/story/composition.js +261 -2
- package/dist/story/config-watcher.js +67 -2
- package/dist/story/config.js +64 -2
- package/dist/story/context.js +27 -2
- package/dist/story/controls/index.js +320 -2
- package/dist/story/crosswind.js +189 -2
- package/dist/story/desktop-preview.js +105 -2
- package/dist/story/docs-generator.js +186 -2
- package/dist/story/errors.js +239 -2
- package/dist/story/figma-export.js +224 -2
- package/dist/story/generator.js +165 -2
- package/dist/story/hmr.js +131 -2
- package/dist/story/hot-swap.js +207 -2
- package/dist/story/index.js +49 -2
- package/dist/story/interactions.js +225 -2
- package/dist/story/keyboard-shortcuts.js +345 -2
- package/dist/story/output.js +153 -2
- package/dist/story/performance.js +252 -2
- package/dist/story/presets.js +289 -2
- package/dist/story/props-validation.js +215 -2
- package/dist/story/renderer.js +209 -2
- package/dist/story/search-index.js +106 -2
- package/dist/story/search.js +140 -2
- package/dist/story/server.js +965 -2
- package/dist/story/setup.js +78 -2
- package/dist/story/snapshots.js +170 -2
- package/dist/story/testing.js +163 -2
- package/dist/story/theme.js +170 -2
- package/dist/story/types.js +0 -2
- package/dist/story/ui/code-panel.js +175 -2
- package/dist/story/ui/controls-panel.js +168 -2
- package/dist/story/ui/index.js +4 -2
- package/dist/story/ui/navigation.js +250 -2
- package/dist/story/ui/preview.js +251 -2
- package/dist/story/visual-testing.js +247 -2
- package/dist/streaming.js +537 -2
- package/dist/strict-lint.js +288 -2
- package/dist/style-scoping.js +50 -2
- package/dist/suspense.js +398 -2
- package/dist/teleport.js +109 -2
- package/dist/template-compiler.js +63 -2
- package/dist/template-hydrator.js +82 -2
- package/dist/template-tag-balance.js +93 -2
- package/dist/testing.js +581 -2
- package/dist/transitions.js +443 -2
- package/dist/type-checker.js +581 -2
- package/dist/types/component-types.d.ts +54 -0
- package/dist/types/component-types.js +0 -1
- package/dist/types/config-types.js +0 -1
- package/dist/types/context-types.js +0 -1
- package/dist/types/csp-types.js +0 -1
- package/dist/types/directive-types.js +3 -2
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.js +1 -2
- package/dist/types/pwa-types.js +0 -1
- package/dist/types.js +1 -2
- package/dist/typescript-templates.js +386 -4
- package/dist/utils.js +942 -2
- package/dist/validator.js +171 -2
- package/dist/variable-extractor.js +1094 -2
- package/dist/view-composers.js +29 -2
- package/dist/virtual-scrolling.js +454 -2
- package/dist/visual-editor.js +688 -28
- package/dist/vue-template.js +475 -2
- package/dist/web-components/css-scoping.js +177 -2
- package/dist/web-components/index.js +13 -2
- package/dist/web-components/reactive-generator.js +121 -56
- package/dist/web-components.js +1447 -2
- package/dist/x-element.js +17 -5
- package/package.json +5 -6
- package/dist/chunk-04n64bnt.js +0 -5
- package/dist/chunk-06w68hyg.js +0 -7
- package/dist/chunk-07y6m5xx.js +0 -3
- package/dist/chunk-095s3972.js +0 -99
- package/dist/chunk-09k66d8a.js +0 -3
- package/dist/chunk-0a61y94n.js +0 -24
- package/dist/chunk-0b9w2w1y.js +0 -1
- package/dist/chunk-0dx4q6yq.js +0 -3
- package/dist/chunk-0dz85jrp.js +0 -3
- package/dist/chunk-0jd0pndq.js +0 -3
- package/dist/chunk-0p0nyq50.js +0 -6
- package/dist/chunk-0p1h3p3w.js +0 -3
- package/dist/chunk-0qakwen3.js +0 -1
- package/dist/chunk-0qnz93q2.js +0 -3
- package/dist/chunk-10debkj5.js +0 -71
- package/dist/chunk-15nesxgt.js +0 -5224
- package/dist/chunk-1sr54zp8.js +0 -3
- package/dist/chunk-1v49fwr5.js +0 -3
- package/dist/chunk-1xz0w746.js +0 -1539
- package/dist/chunk-1z5nhbjt.js +0 -90
- package/dist/chunk-1zgsgk4b.js +0 -1
- package/dist/chunk-20swzkx3.js +0 -216
- package/dist/chunk-27zsyhrd.js +0 -4
- package/dist/chunk-29y0hk5z.js +0 -3
- package/dist/chunk-2cdvw21b.js +0 -274
- package/dist/chunk-2j21d3ka.js +0 -161
- package/dist/chunk-2jxbwnkz.js +0 -2
- package/dist/chunk-31dr4bys.js +0 -6
- package/dist/chunk-35faj94w.js +0 -15
- package/dist/chunk-39gm08bn.js +0 -39
- package/dist/chunk-3a4qsygv.js +0 -51
- package/dist/chunk-3te20hqw.js +0 -3
- package/dist/chunk-43t7faps.js +0 -24
- package/dist/chunk-44hd3kab.js +0 -1
- package/dist/chunk-47kpewks.js +0 -71
- package/dist/chunk-4aj7ftqb.js +0 -1
- package/dist/chunk-4d2y57k7.js +0 -3
- package/dist/chunk-4deg33rw.js +0 -57
- package/dist/chunk-4k7d8p8d.js +0 -188
- package/dist/chunk-4qcy5tx6.js +0 -53
- package/dist/chunk-4x7kae4j.js +0 -3
- package/dist/chunk-55nwdnsd.js +0 -3
- package/dist/chunk-55yp0q8t.js +0 -201
- package/dist/chunk-5f43yz6z.js +0 -12
- package/dist/chunk-5shey2e4.js +0 -3
- package/dist/chunk-6b5k6w2q.js +0 -7
- package/dist/chunk-6djzdes9.js +0 -14
- package/dist/chunk-6e4c6g46.js +0 -3
- package/dist/chunk-6eqbxcrd.js +0 -8
- package/dist/chunk-7074g241.js +0 -3
- package/dist/chunk-7b6s3g65.js +0 -179
- package/dist/chunk-7cgmq685.js +0 -33
- package/dist/chunk-7dkz6z2s.js +0 -3
- package/dist/chunk-7ed3tean.js +0 -3
- package/dist/chunk-7gd339hd.js +0 -109
- package/dist/chunk-7j5e8sdj.js +0 -3
- package/dist/chunk-7n8g0nfn.js +0 -38
- package/dist/chunk-7sqdeqsw.js +0 -1
- package/dist/chunk-80g54ncj.js +0 -3
- package/dist/chunk-8a5bxece.js +0 -4
- package/dist/chunk-8ahwt6a4.js +0 -721
- package/dist/chunk-8b3z0zd5.js +0 -8
- package/dist/chunk-8dhdpa5h.js +0 -4
- package/dist/chunk-8n9nnqr6.js +0 -3
- package/dist/chunk-8qc1bsym.js +0 -3
- package/dist/chunk-8va64c9n.js +0 -3
- package/dist/chunk-8vtm0m84.js +0 -3
- package/dist/chunk-8xj7c3mf.js +0 -8
- package/dist/chunk-988dw6q3.js +0 -9
- package/dist/chunk-9b6cec0v.js +0 -321
- package/dist/chunk-9cf97f9p.js +0 -3
- package/dist/chunk-9ecv8aqj.js +0 -14
- package/dist/chunk-9eq3ht5f.js +0 -8
- package/dist/chunk-9jtj77jx.js +0 -3
- package/dist/chunk-9t14se2t.js +0 -83
- package/dist/chunk-9wmq2272.js +0 -3
- package/dist/chunk-9zbk5zx5.js +0 -57
- package/dist/chunk-a2g2qb2y.js +0 -386
- package/dist/chunk-a3hyn4we.js +0 -79
- package/dist/chunk-a87qqft6.js +0 -194
- package/dist/chunk-ah88wzbg.js +0 -10
- package/dist/chunk-akf1r75r.js +0 -102
- package/dist/chunk-amaa1bev.js +0 -2
- package/dist/chunk-avx8aydt.js +0 -3
- package/dist/chunk-ay9qf4zj.js +0 -6
- package/dist/chunk-aywxfqev.js +0 -3
- package/dist/chunk-azkegjd8.js +0 -61
- package/dist/chunk-b77wjyn7.js +0 -84
- package/dist/chunk-baf1hfwh.js +0 -231
- package/dist/chunk-bdk6g1vj.js +0 -3
- package/dist/chunk-bhqgmyqw.js +0 -36
- package/dist/chunk-bk3j0n82.js +0 -341
- package/dist/chunk-bndmdahk.js +0 -3
- package/dist/chunk-bpgg59wa.js +0 -3
- package/dist/chunk-bs3gmfxz.js +0 -5
- package/dist/chunk-bxc7kjsk.js +0 -32
- package/dist/chunk-c7jg8w13.js +0 -3
- package/dist/chunk-ckxjqqn9.js +0 -3
- package/dist/chunk-cn8az1b4.js +0 -1
- package/dist/chunk-d0zjtdw4.js +0 -29
- package/dist/chunk-d1qh1x2g.js +0 -3
- package/dist/chunk-djddhmcc.js +0 -60
- package/dist/chunk-dqn9gyhb.js +0 -21
- package/dist/chunk-drjq2zwg.js +0 -3
- package/dist/chunk-dvfs1h3c.js +0 -117
- package/dist/chunk-e2q9c5vq.js +0 -8
- package/dist/chunk-e98v1bc6.js +0 -12
- package/dist/chunk-ejayeex8.js +0 -2
- package/dist/chunk-ejh8fyak.js +0 -3
- package/dist/chunk-ek2w6zvy.js +0 -4
- package/dist/chunk-ewa88139.js +0 -6
- package/dist/chunk-fjk3x41y.js +0 -3
- package/dist/chunk-frg705zq.js +0 -302
- package/dist/chunk-fzm3y0r3.js +0 -267
- package/dist/chunk-g1vt329s.js +0 -21
- package/dist/chunk-g4ps1dj8.js +0 -140
- package/dist/chunk-gkzq8zw4.js +0 -23
- package/dist/chunk-gz5fmp3q.js +0 -3
- package/dist/chunk-h0hjy0kn.js +0 -292
- package/dist/chunk-h5jxydqq.js +0 -147
- package/dist/chunk-h5wa62ek.js +0 -145
- package/dist/chunk-h7aq56qj.js +0 -28
- package/dist/chunk-h82kjdr4.js +0 -83
- package/dist/chunk-hbehnm18.js +0 -3
- package/dist/chunk-hbgxbgam.js +0 -58
- package/dist/chunk-hc2mf5h6.js +0 -4
- package/dist/chunk-hjm2xab3.js +0 -3
- package/dist/chunk-hjv76kcw.js +0 -1
- package/dist/chunk-hrnnh4tq.js +0 -142
- package/dist/chunk-ht43ds42.js +0 -236
- package/dist/chunk-hxx3za24.js +0 -62
- package/dist/chunk-hy8820vc.js +0 -6
- package/dist/chunk-j447q024.js +0 -28
- package/dist/chunk-j58vh0w6.js +0 -314
- package/dist/chunk-j8tn8enq.js +0 -3
- package/dist/chunk-jc0fc4ht.js +0 -11
- package/dist/chunk-jcs0ymw7.js +0 -3
- package/dist/chunk-jjamhxpm.js +0 -139
- package/dist/chunk-jjb05d21.js +0 -3
- package/dist/chunk-jjdq01dv.js +0 -128
- package/dist/chunk-jnmgm2xw.js +0 -5
- package/dist/chunk-jpndqst5.js +0 -3
- package/dist/chunk-jt0ezawy.js +0 -351
- package/dist/chunk-jz73j3qs.js +0 -169
- package/dist/chunk-k21df0pg.js +0 -10
- package/dist/chunk-k848dzsa.js +0 -429
- package/dist/chunk-kdfe4t91.js +0 -3
- package/dist/chunk-keawsbpn.js +0 -5
- package/dist/chunk-kgym7ch9.js +0 -8
- package/dist/chunk-khdn7jwe.js +0 -3
- package/dist/chunk-km25cwz6.js +0 -3
- package/dist/chunk-knm8a8yx.js +0 -398
- package/dist/chunk-krqn0ddk.js +0 -3
- package/dist/chunk-ktkmvaa8.js +0 -1
- package/dist/chunk-kx6eb6ma.js +0 -4
- package/dist/chunk-kypas8vy.js +0 -4
- package/dist/chunk-m167kjhv.js +0 -4
- package/dist/chunk-m491r0rj.js +0 -358
- package/dist/chunk-m8448qby.js +0 -11
- package/dist/chunk-mgj88zeq.js +0 -64
- package/dist/chunk-mhgk632d.js +0 -3
- package/dist/chunk-mv4fb50w.js +0 -1
- package/dist/chunk-n5kg7fpx.js +0 -7
- package/dist/chunk-n98x35xd.js +0 -8
- package/dist/chunk-n9dhs1dw.js +0 -106
- package/dist/chunk-nd9rmddh.js +0 -6
- package/dist/chunk-ngxfwzq3.js +0 -3
- package/dist/chunk-nhv099jv.js +0 -19
- package/dist/chunk-nm15ccsg.js +0 -5
- package/dist/chunk-npfmdb6y.js +0 -71
- package/dist/chunk-nvwb4x04.js +0 -3
- package/dist/chunk-nw34xajc.js +0 -8
- package/dist/chunk-p077sw22.js +0 -3
- package/dist/chunk-p1h8065h.js +0 -3
- package/dist/chunk-p3e0cqtt.js +0 -34
- package/dist/chunk-p3fpb79v.js +0 -477
- package/dist/chunk-p52yzjq6.js +0 -498
- package/dist/chunk-p73meypx.js +0 -134
- package/dist/chunk-pa9zb7ty.js +0 -4
- package/dist/chunk-payfb24w.js +0 -207
- package/dist/chunk-pcm1vd8w.js +0 -3
- package/dist/chunk-pcntd9h4.js +0 -3
- package/dist/chunk-pg1fvqb9.js +0 -167
- package/dist/chunk-psc3wfcf.js +0 -1115
- package/dist/chunk-pznnww6k.js +0 -6
- package/dist/chunk-q6zhd9yf.js +0 -3
- package/dist/chunk-qa59zejf.js +0 -246
- package/dist/chunk-qvrbjpdc.js +0 -3
- package/dist/chunk-qygvvygy.js +0 -12
- package/dist/chunk-r0jcmpv6.js +0 -68
- package/dist/chunk-r5m9xtm7.js +0 -3
- package/dist/chunk-rh80q1nd.js +0 -1
- package/dist/chunk-rkffm9sy.js +0 -128
- package/dist/chunk-rpx5fa65.js +0 -3
- package/dist/chunk-rqxf5hns.js +0 -3
- package/dist/chunk-rtr974rc.js +0 -30
- package/dist/chunk-rvptcndz.js +0 -3
- package/dist/chunk-rw26rpek.js +0 -171
- package/dist/chunk-rw7937v4.js +0 -19
- package/dist/chunk-rwrv9wbf.js +0 -3
- package/dist/chunk-rz2ec6g7.js +0 -347
- package/dist/chunk-s6ja2bdg.js +0 -66
- package/dist/chunk-scpkdrdb.js +0 -4
- package/dist/chunk-shv88d2d.js +0 -10
- package/dist/chunk-t0c9wvpr.js +0 -3
- package/dist/chunk-tffh3b1e.js +0 -35
- package/dist/chunk-tg2f1mzq.js +0 -11
- package/dist/chunk-trfswy66.js +0 -77
- package/dist/chunk-tzwtg722.js +0 -3
- package/dist/chunk-v7f8jj15.js +0 -4
- package/dist/chunk-v8j49spx.js +0 -3
- package/dist/chunk-v97390pe.js +0 -1
- package/dist/chunk-vcr5pzpx.js +0 -233
- package/dist/chunk-veeerpwa.js +0 -3
- package/dist/chunk-vg5kt6az.js +0 -4
- package/dist/chunk-vgv7jvyz.js +0 -219
- package/dist/chunk-vjbefncc.js +0 -3
- package/dist/chunk-vmxjghsm.js +0 -3
- package/dist/chunk-vw7vqd76.js +0 -3
- package/dist/chunk-vx22he47.js +0 -1
- package/dist/chunk-w3b6m427.js +0 -45
- package/dist/chunk-wazkqv7n.js +0 -3
- package/dist/chunk-wf7zrjsw.js +0 -138
- package/dist/chunk-wfg2gm11.js +0 -3
- package/dist/chunk-wg9k6h8w.js +0 -12
- package/dist/chunk-wkz2vwkr.js +0 -81
- package/dist/chunk-wvef3ngh.js +0 -127
- package/dist/chunk-wvf31nzs.js +0 -3
- package/dist/chunk-x05nxhyv.js +0 -262
- package/dist/chunk-x0770gt9.js +0 -4
- package/dist/chunk-x24s4heg.js +0 -1166
- package/dist/chunk-x3r64r0c.js +0 -6
- package/dist/chunk-x5g9w4hh.js +0 -5
- package/dist/chunk-xgkv66q5.js +0 -2230
- package/dist/chunk-xqq1vxb6.js +0 -1
- package/dist/chunk-xs03kc1h.js +0 -196
- package/dist/chunk-xv34zbp4.js +0 -3
- package/dist/chunk-xw36p73h.js +0 -90
- package/dist/chunk-xyg1r5w0.js +0 -4
- package/dist/chunk-xys2sxh6.js +0 -16
- package/dist/chunk-y0qg6rrv.js +0 -3
- package/dist/chunk-y1k17c8b.js +0 -21
- package/dist/chunk-y5xz0jc4.js +0 -141
- package/dist/chunk-y6xsqe9a.js +0 -10
- package/dist/chunk-yb1t2h8h.js +0 -11
- package/dist/chunk-ygvyc6wv.js +0 -90
- package/dist/chunk-yj4x5ehw.js +0 -21
- package/dist/chunk-yn38c15w.js +0 -4
- package/dist/chunk-ynhngsdh.js +0 -3
- package/dist/chunk-yrsb6j3z.js +0 -3
- package/dist/chunk-yywfvfph.js +0 -2
- package/dist/chunk-zcxzdrn7.js +0 -5
- package/dist/chunk-zen6ax1y.js +0 -27
- package/dist/chunk-zf2pek72.js +0 -3
- package/dist/chunk-zk3d0jjf.js +0 -7
- package/dist/chunk-zy3rp1ab.js +0 -93
package/dist/forms.js
CHANGED
|
@@ -1,2 +1,709 @@
|
|
|
1
|
-
|
|
2
|
-
import{
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { isExpressionSafe, safeEvaluate } from "./safe-evaluator.js";
|
|
3
|
+
function escapeAttr(str) {
|
|
4
|
+
return String(str ?? "").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
5
|
+
}
|
|
6
|
+
export const defaultFormClasses = {
|
|
7
|
+
input: "form-control",
|
|
8
|
+
inputError: "is-invalid",
|
|
9
|
+
checkInput: "form-check-input",
|
|
10
|
+
label: "form-label",
|
|
11
|
+
errorFeedback: "invalid-feedback"
|
|
12
|
+
};
|
|
13
|
+
function getFormClasses(options) {
|
|
14
|
+
return {
|
|
15
|
+
...defaultFormClasses,
|
|
16
|
+
...options.forms?.classes
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function buildClassString(existingClass, defaultClass, hasError, errorClass) {
|
|
20
|
+
const baseClass = existingClass || defaultClass;
|
|
21
|
+
return hasError ? `${baseClass} ${errorClass}` : baseClass;
|
|
22
|
+
}
|
|
23
|
+
function genId() {
|
|
24
|
+
return crypto.randomUUID();
|
|
25
|
+
}
|
|
26
|
+
export function processForms(template, context, _filePath, options) {
|
|
27
|
+
const classes = getFormClasses(options);
|
|
28
|
+
let output = template;
|
|
29
|
+
output = processValidateDirective(output, context);
|
|
30
|
+
output = processBasicFormDirectives(output, context);
|
|
31
|
+
output = processFormInputDirectives(output, context, classes);
|
|
32
|
+
output = processErrorDirective(output, context, classes);
|
|
33
|
+
return output;
|
|
34
|
+
}
|
|
35
|
+
export function processBasicFormDirectives(template, context) {
|
|
36
|
+
let result = template;
|
|
37
|
+
result = result.replace(/@csrf/g, () => {
|
|
38
|
+
if (!context.csrf || typeof context.csrf !== "object") {
|
|
39
|
+
const token = genId();
|
|
40
|
+
context.csrf = { token };
|
|
41
|
+
}
|
|
42
|
+
if (context.csrf.field)
|
|
43
|
+
return context.csrf.field;
|
|
44
|
+
if (context.csrf.token)
|
|
45
|
+
return `<input type="hidden" name="_token" value="${escapeAttr(context.csrf.token)}">`;
|
|
46
|
+
return '<input type="hidden" name="_token" value="">';
|
|
47
|
+
});
|
|
48
|
+
result = result.replace(/@method\(['"]([^'"]+)['"]\)/g, (match, method) => {
|
|
49
|
+
const upperMethod = method.toUpperCase();
|
|
50
|
+
if (method && !["GET", "POST"].includes(upperMethod))
|
|
51
|
+
return `<input type="hidden" name="_method" value="${escapeAttr(upperMethod)}">`;
|
|
52
|
+
return match;
|
|
53
|
+
});
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
export function processFormInputDirectives(template, context, classes = defaultFormClasses) {
|
|
57
|
+
let result = template;
|
|
58
|
+
result = result.replace(/@form\(\s*(?:(?:'([^']+)'|"([^"]+)")\s*)?(?:,\s*(?:'([^']+)'|"([^"]+)")?)?\s*(?:,\s*\{([^}]+)\}\s*)?\)/g, (_match, singleQuoteMethod, doubleQuoteMethod, singleQuoteAction, doubleQuoteAction, attributes = "") => {
|
|
59
|
+
const method = singleQuoteMethod || doubleQuoteMethod || "POST", action = singleQuoteAction || doubleQuoteAction || "", attrs = parseAttributes(attributes), methodStr = method.toUpperCase(), htmlMethod = ["GET", "POST"].includes(methodStr) ? methodStr : "POST";
|
|
60
|
+
let formHtml = `<form method="${escapeAttr(htmlMethod)}" action="${escapeAttr(action)}"${attrs ? ` ${attrs}` : ""}>`;
|
|
61
|
+
formHtml += `
|
|
62
|
+
${processBasicFormDirectives("@csrf", context)}`;
|
|
63
|
+
if (!["GET", "POST"].includes(methodStr))
|
|
64
|
+
formHtml += `
|
|
65
|
+
${processBasicFormDirectives(`@method('${methodStr}')`, context)}`;
|
|
66
|
+
return formHtml;
|
|
67
|
+
});
|
|
68
|
+
result = result.replace(/@endform/g, "</form>");
|
|
69
|
+
result = result.replace(/@input\(\s*(?:'([^']+)'|"([^"]+)")\s*(?:,\s*(?:(?:'([^']*)'|"([^"]*)")\s*)?)?(?:,\s*\{([^}]+)\})?\s*\)/g, (_match, singleQuoteName, doubleQuoteName, singleQuoteValue, doubleQuoteValue, attributes = "") => {
|
|
70
|
+
const name = singleQuoteName || doubleQuoteName || "", value = singleQuoteValue || doubleQuoteValue || "", attrs = parseAttributes(attributes), rawOldValue = getOldValue(name, context), oldValue = rawOldValue !== void 0 && rawOldValue !== null ? rawOldValue : value || "", typeMatch = attrs.match(/type=['"]([^'"]+)['"]/i), type = typeMatch ? typeMatch[1] : "text", hasError = hasFieldError(name, context), classMatch = attrs.match(/class=['"]([^'"]+)['"]/i), className = buildClassString(classMatch ? classMatch[1] : "", classes.input, hasError, classes.inputError);
|
|
71
|
+
let attrsWithoutClassAndType = attrs.replace(/class=['"][^'"]+['"]/i, "");
|
|
72
|
+
attrsWithoutClassAndType = attrsWithoutClassAndType.replace(/type=['"][^'"]+['"]/i, "");
|
|
73
|
+
return `<input type="${escapeAttr(type)}" name="${escapeAttr(name)}" value="${escapeAttr(oldValue)}" class="${className}"${attrsWithoutClassAndType ? ` ${attrsWithoutClassAndType}` : ""}>`;
|
|
74
|
+
});
|
|
75
|
+
result = result.replace(/@textarea\(\s*['"]([^'"]+)['"]\s*(?:,\s*\{([^}]+)\})?\)([\s\S]*?)@endtextarea/g, (_match, name, attributes = "", content = "") => {
|
|
76
|
+
const attrs = parseAttributes(attributes), oldValue = getOldValue(name, context) || content.trim(), hasError = hasFieldError(name, context), classMatch = attrs.match(/class=['"]([^'"]+)['"]/i), className = buildClassString(classMatch ? classMatch[1] : "", classes.input, hasError, classes.inputError), attrsWithoutClass = attrs.replace(/class=['"][^'"]+['"]/i, "");
|
|
77
|
+
return `<textarea name="${escapeAttr(name)}" class="${className}"${attrsWithoutClass ? ` ${attrsWithoutClass}` : ""}>${escapeAttr(oldValue)}</textarea>`;
|
|
78
|
+
});
|
|
79
|
+
result = result.replace(/@select\(\s*['"]([^'"]+)['"]\s*(?:,\s*\{([^}]+)\})?\)([\s\S]*?)@endselect/g, (_match, name, attributes = "", content) => {
|
|
80
|
+
const attrs = parseAttributes(attributes), oldValue = getOldValue(name, context), hasError = hasFieldError(name, context), classMatch = attrs.match(/class=['"]([^'"]+)['"]/i), className = buildClassString(classMatch ? classMatch[1] : "", classes.input, hasError, classes.inputError), attrsWithoutClass = attrs.replace(/class=['"][^'"]+['"]/i, "");
|
|
81
|
+
let processedContent = content;
|
|
82
|
+
if (oldValue !== void 0)
|
|
83
|
+
processedContent = content.replace(/(<option[^>]*value=['"]([^'"]+)['"][^>]*>)/gi, (optionMatch, optionTag, optionValue) => {
|
|
84
|
+
if ((Array.isArray(oldValue) ? oldValue.some((v) => String(v) === optionValue) : String(oldValue) === optionValue) && !optionTag.includes("selected"))
|
|
85
|
+
return optionTag.replace(/(<option)/, "$1 selected");
|
|
86
|
+
return optionMatch;
|
|
87
|
+
});
|
|
88
|
+
return `<select name="${escapeAttr(name)}" class="${className}"${attrsWithoutClass ? ` ${attrsWithoutClass}` : ""}>${processedContent}</select>`;
|
|
89
|
+
});
|
|
90
|
+
result = result.replace(/@checkbox\(\s*['"]([^'"]+)['"]\s*(?:,\s*['"]([^'"]+)['"]\s*)?(?:,\s*\{([^}]+)\})?\)/g, (_match, name, value = "1", attributes = "") => {
|
|
91
|
+
const attrs = parseAttributes(attributes), oldValues = getOldValue(name, context), isChecked = oldValues !== void 0 && (Array.isArray(oldValues) ? oldValues.some((v) => String(v) === value) : oldValues === !0 || String(oldValues) === value), classMatch = attrs.match(/class=['"]([^'"]+)['"]/i), className = classMatch ? classMatch[1] : classes.checkInput, attrsWithoutClass = attrs.replace(/class=['"][^'"]+['"]/i, "");
|
|
92
|
+
return `<input type="checkbox" name="${escapeAttr(name)}" value="${escapeAttr(value)}" class="${className}"${isChecked ? " checked" : ""}${attrsWithoutClass ? ` ${attrsWithoutClass}` : ""}>`;
|
|
93
|
+
});
|
|
94
|
+
result = result.replace(/@radio\(\s*['"]([^'"]+)['"]\s*,\s*['"]([^'"]+)['"]\s*(?:,\s*\{([^}]+)\})?\)/g, (_match, name, value, attributes = "") => {
|
|
95
|
+
const attrs = parseAttributes(attributes), oldValue = getOldValue(name, context), isChecked = oldValue !== void 0 && oldValue === value, classMatch = attrs.match(/class=['"]([^'"]+)['"]/i), className = classMatch ? classMatch[1] : classes.checkInput, attrsWithoutClass = attrs.replace(/class=['"][^'"]+['"]/i, "");
|
|
96
|
+
return `<input type="radio" name="${escapeAttr(name)}" value="${escapeAttr(value)}" class="${className}"${isChecked ? " checked" : ""}${attrsWithoutClass ? ` ${attrsWithoutClass}` : ""}>`;
|
|
97
|
+
});
|
|
98
|
+
result = result.replace(/@file\(\s*['"]([^'"]+)['"]\s*(?:,\s*\{([^}]+)\})?\)/g, (_match, name, attributes = "") => {
|
|
99
|
+
const attrs = parseAttributes(attributes), acceptMatch = attrs.match(/accept=['"]([^'"]+)['"]/i), accept = acceptMatch ? acceptMatch[1] : "", isMultiple = attrs.includes("multiple"), hasError = hasFieldError(name, context), classMatch = attrs.match(/class=['"]([^'"]+)['"]/i), className = buildClassString(classMatch ? classMatch[1] : "", classes.input, hasError, classes.inputError), attrsWithoutHandled = attrs.replace(/class=['"][^'"]+['"]/gi, "").replace(/accept=['"][^'"]+['"]/gi, "").replace(/\bmultiple\b/gi, "").trim(), parts = [
|
|
100
|
+
`<input type="file" name="${escapeAttr(name)}"`,
|
|
101
|
+
`class="${className}"`
|
|
102
|
+
];
|
|
103
|
+
if (accept)
|
|
104
|
+
parts.push(`accept="${escapeAttr(accept)}"`);
|
|
105
|
+
if (isMultiple)
|
|
106
|
+
parts.push("multiple");
|
|
107
|
+
if (attrsWithoutHandled)
|
|
108
|
+
parts.push(attrsWithoutHandled);
|
|
109
|
+
return `${parts.join(" ")}>`;
|
|
110
|
+
});
|
|
111
|
+
result = result.replace(/@label\(\s*['"]([^'"]+)['"]\s*(?:,\s*\{([^}]+)\})?\)([\s\S]*?)@endlabel/g, (_match, forAttr, attributes = "", content) => {
|
|
112
|
+
const attrs = parseAttributes(attributes), classMatch = attrs.match(/class=['"]([^'"]+)['"]/i), className = classMatch ? classMatch[1] : classes.label, attrsWithoutClass = attrs.replace(/class=['"][^'"]+['"]/i, "");
|
|
113
|
+
return `<label for="${escapeAttr(forAttr)}" class="${className}"${attrsWithoutClass ? ` ${attrsWithoutClass}` : ""}>${content}</label>`;
|
|
114
|
+
});
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
export function processErrorDirective(template, context, _classes = defaultFormClasses) {
|
|
118
|
+
return template.replace(/@error\(['"]([^'"]+)['"]\)([\s\S]*?)@enderror/g, (match, field, content) => {
|
|
119
|
+
try {
|
|
120
|
+
if (hasFieldError(field, context))
|
|
121
|
+
return content.replace(/\{\{([^}]+)\}\}/g, (_, expr) => {
|
|
122
|
+
try {
|
|
123
|
+
if (expr.trim() === "$message" || expr.trim() === "message")
|
|
124
|
+
return getErrorMessage(field, context);
|
|
125
|
+
if (expr.trim().includes("errors.first") || expr.trim().includes("$errors.first")) {
|
|
126
|
+
if (typeof context.errors?.first === "function")
|
|
127
|
+
return context.errors.first(field);
|
|
128
|
+
return getErrorMessage(field, context);
|
|
129
|
+
}
|
|
130
|
+
const trimmedExpr = expr.trim();
|
|
131
|
+
if (isExpressionSafe(trimmedExpr)) {
|
|
132
|
+
const result = safeEvaluate(trimmedExpr, context);
|
|
133
|
+
return result !== void 0 ? String(result) : trimmedExpr;
|
|
134
|
+
}
|
|
135
|
+
return trimmedExpr;
|
|
136
|
+
} catch {
|
|
137
|
+
return expr;
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
return "";
|
|
141
|
+
} catch (error) {
|
|
142
|
+
console.error("Error processing @error directive:", error);
|
|
143
|
+
return match;
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
function hasFieldError(field, context) {
|
|
148
|
+
if (!context.errors)
|
|
149
|
+
return !1;
|
|
150
|
+
if (typeof context.errors.has === "function")
|
|
151
|
+
return context.errors.has(field);
|
|
152
|
+
if (typeof context.errors === "object")
|
|
153
|
+
return Object.prototype.hasOwnProperty.call(context.errors, field);
|
|
154
|
+
return !1;
|
|
155
|
+
}
|
|
156
|
+
function getErrorMessage(field, context) {
|
|
157
|
+
if (!context.errors)
|
|
158
|
+
return "";
|
|
159
|
+
if (typeof context.errors.get === "function")
|
|
160
|
+
return context.errors.get(field);
|
|
161
|
+
if (typeof context.errors === "object" && Object.prototype.hasOwnProperty.call(context.errors, field)) {
|
|
162
|
+
const error = context.errors[field];
|
|
163
|
+
return Array.isArray(error) ? error[0] : String(error);
|
|
164
|
+
}
|
|
165
|
+
return "";
|
|
166
|
+
}
|
|
167
|
+
function getOldValue(field, context) {
|
|
168
|
+
let value;
|
|
169
|
+
if (context.old && typeof context.old === "function")
|
|
170
|
+
return context.old(field);
|
|
171
|
+
if (context.old && typeof context.old === "object")
|
|
172
|
+
return context.old[field];
|
|
173
|
+
if (field.endsWith("[]")) {
|
|
174
|
+
const baseName = field.slice(0, -2);
|
|
175
|
+
value = context[baseName];
|
|
176
|
+
if (value !== void 0)
|
|
177
|
+
return value;
|
|
178
|
+
}
|
|
179
|
+
return context[field];
|
|
180
|
+
}
|
|
181
|
+
function parseAttributes(attributesStr) {
|
|
182
|
+
if (!attributesStr.trim())
|
|
183
|
+
return "";
|
|
184
|
+
const attrs = [], trimmed = attributesStr.trim(), attrRegex = /([\w-]+)\s*:\s*(['"]?)([^,'"]*)\2(?:\s*,\s*|\s*$)/g;
|
|
185
|
+
let match;
|
|
186
|
+
while ((match = attrRegex.exec(trimmed)) !== null) {
|
|
187
|
+
const [, name, , value] = match;
|
|
188
|
+
attrs.push(`${name}="${value.trim()}"`);
|
|
189
|
+
}
|
|
190
|
+
return attrs.join(" ");
|
|
191
|
+
}
|
|
192
|
+
const validationRules = {
|
|
193
|
+
required: (value, _params, field) => {
|
|
194
|
+
if (value === void 0 || value === null || value === "" || Array.isArray(value) && value.length === 0)
|
|
195
|
+
return `The ${field} field is required.`;
|
|
196
|
+
return null;
|
|
197
|
+
},
|
|
198
|
+
email: (value, _params, field) => {
|
|
199
|
+
if (!value)
|
|
200
|
+
return null;
|
|
201
|
+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(value)))
|
|
202
|
+
return `The ${field} field must be a valid email address.`;
|
|
203
|
+
return null;
|
|
204
|
+
},
|
|
205
|
+
min: (value, params, field) => {
|
|
206
|
+
if (!value)
|
|
207
|
+
return null;
|
|
208
|
+
const min = Number(params[0]);
|
|
209
|
+
if (typeof value === "string" && value.length < min)
|
|
210
|
+
return `The ${field} field must be at least ${min} characters.`;
|
|
211
|
+
if (typeof value === "number" && value < min)
|
|
212
|
+
return `The ${field} field must be at least ${min}.`;
|
|
213
|
+
if (Array.isArray(value) && value.length < min)
|
|
214
|
+
return `The ${field} field must have at least ${min} items.`;
|
|
215
|
+
return null;
|
|
216
|
+
},
|
|
217
|
+
max: (value, params, field) => {
|
|
218
|
+
if (!value)
|
|
219
|
+
return null;
|
|
220
|
+
const max = Number(params[0]);
|
|
221
|
+
if (typeof value === "string" && value.length > max)
|
|
222
|
+
return `The ${field} field must not exceed ${max} characters.`;
|
|
223
|
+
if (typeof value === "number" && value > max)
|
|
224
|
+
return `The ${field} field must not exceed ${max}.`;
|
|
225
|
+
if (Array.isArray(value) && value.length > max)
|
|
226
|
+
return `The ${field} field must not have more than ${max} items.`;
|
|
227
|
+
return null;
|
|
228
|
+
},
|
|
229
|
+
confirmed: (value, params, field) => {
|
|
230
|
+
if (params[0] !== void 0 && value !== params[0])
|
|
231
|
+
return `The ${field} confirmation does not match.`;
|
|
232
|
+
return null;
|
|
233
|
+
},
|
|
234
|
+
numeric: (value, _params, field) => {
|
|
235
|
+
if (!value)
|
|
236
|
+
return null;
|
|
237
|
+
if (Number.isNaN(Number(value)))
|
|
238
|
+
return `The ${field} field must be a number.`;
|
|
239
|
+
return null;
|
|
240
|
+
},
|
|
241
|
+
integer: (value, _params, field) => {
|
|
242
|
+
if (!value)
|
|
243
|
+
return null;
|
|
244
|
+
if (!Number.isInteger(Number(value)))
|
|
245
|
+
return `The ${field} field must be an integer.`;
|
|
246
|
+
return null;
|
|
247
|
+
},
|
|
248
|
+
alpha: (value, _params, field) => {
|
|
249
|
+
if (!value)
|
|
250
|
+
return null;
|
|
251
|
+
if (!/^[a-z]+$/i.test(String(value)))
|
|
252
|
+
return `The ${field} field must only contain letters.`;
|
|
253
|
+
return null;
|
|
254
|
+
},
|
|
255
|
+
alphanumeric: (value, _params, field) => {
|
|
256
|
+
if (!value)
|
|
257
|
+
return null;
|
|
258
|
+
if (!/^[a-z0-9]+$/i.test(String(value)))
|
|
259
|
+
return `The ${field} field must only contain letters and numbers.`;
|
|
260
|
+
return null;
|
|
261
|
+
},
|
|
262
|
+
url: (value, _params, field) => {
|
|
263
|
+
if (!value)
|
|
264
|
+
return null;
|
|
265
|
+
try {
|
|
266
|
+
return new URL(String(value)) ? null : null;
|
|
267
|
+
} catch {
|
|
268
|
+
return `The ${field} field must be a valid URL.`;
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
regex: (value, params, field) => {
|
|
272
|
+
if (!value)
|
|
273
|
+
return null;
|
|
274
|
+
try {
|
|
275
|
+
if (!new RegExp(params[0]).test(String(value)))
|
|
276
|
+
return `The ${field} field format is invalid.`;
|
|
277
|
+
return null;
|
|
278
|
+
} catch {
|
|
279
|
+
return `The ${field} field has an invalid validation pattern.`;
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
in: (value, params, field) => {
|
|
283
|
+
if (!value)
|
|
284
|
+
return null;
|
|
285
|
+
if (!params.includes(String(value)))
|
|
286
|
+
return `The ${field} field must be one of: ${params.join(", ")}.`;
|
|
287
|
+
return null;
|
|
288
|
+
},
|
|
289
|
+
notIn: (value, params, field) => {
|
|
290
|
+
if (!value)
|
|
291
|
+
return null;
|
|
292
|
+
if (params.includes(String(value)))
|
|
293
|
+
return `The ${field} field must not be: ${params.join(", ")}.`;
|
|
294
|
+
return null;
|
|
295
|
+
},
|
|
296
|
+
date: (value, _params, field) => {
|
|
297
|
+
if (!value)
|
|
298
|
+
return null;
|
|
299
|
+
const date = new Date(value);
|
|
300
|
+
if (Number.isNaN(date.getTime()))
|
|
301
|
+
return `The ${field} field must be a valid date.`;
|
|
302
|
+
return null;
|
|
303
|
+
},
|
|
304
|
+
before: (value, params, field) => {
|
|
305
|
+
if (!value)
|
|
306
|
+
return null;
|
|
307
|
+
const date = new Date(value), beforeDate = new Date(params[0]);
|
|
308
|
+
if (date >= beforeDate)
|
|
309
|
+
return `The ${field} field must be before ${params[0]}.`;
|
|
310
|
+
return null;
|
|
311
|
+
},
|
|
312
|
+
after: (value, params, field) => {
|
|
313
|
+
if (!value)
|
|
314
|
+
return null;
|
|
315
|
+
const date = new Date(value), afterDate = new Date(params[0]);
|
|
316
|
+
if (date <= afterDate)
|
|
317
|
+
return `The ${field} field must be after ${params[0]}.`;
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
export function registerValidationRule(name, validator) {
|
|
322
|
+
validationRules[name] = validator;
|
|
323
|
+
}
|
|
324
|
+
function parseValidationRule(ruleStr) {
|
|
325
|
+
const [name, ...paramParts] = ruleStr.split(":"), params = paramParts.length > 0 ? paramParts.join(":").split(",") : [];
|
|
326
|
+
return { name: name.trim(), params };
|
|
327
|
+
}
|
|
328
|
+
export function validateField(field, value, rules, context = {}) {
|
|
329
|
+
const errors = [], ruleList = rules.split("|").map((r) => r.trim()).filter(Boolean);
|
|
330
|
+
for (const ruleStr of ruleList) {
|
|
331
|
+
const rule = parseValidationRule(ruleStr), validator = validationRules[rule.name];
|
|
332
|
+
if (!validator) {
|
|
333
|
+
console.warn(`Unknown validation rule: ${rule.name}`);
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
if (rule.name === "confirmed") {
|
|
337
|
+
const confirmField = `${field}_confirmation`;
|
|
338
|
+
rule.params = [context[confirmField]];
|
|
339
|
+
}
|
|
340
|
+
const error = validator(value, rule.params || [], field);
|
|
341
|
+
if (error)
|
|
342
|
+
errors.push(rule.message || error);
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
field,
|
|
346
|
+
valid: errors.length === 0,
|
|
347
|
+
errors
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
export function validateFields(data, rules) {
|
|
351
|
+
const errors = {};
|
|
352
|
+
for (const [field, fieldRules] of Object.entries(rules)) {
|
|
353
|
+
const result = validateField(field, data[field], fieldRules, data);
|
|
354
|
+
if (!result.valid)
|
|
355
|
+
errors[field] = result.errors;
|
|
356
|
+
}
|
|
357
|
+
return errors;
|
|
358
|
+
}
|
|
359
|
+
export function processValidateDirective(template, _context) {
|
|
360
|
+
return template.replace(/@validate\(\s*['"]([^'"]+)['"]\s*,\s*['"]([^'"]+)['"]\s*(?:,\s*\{([^}]*)\})?\s*\)/g, (_match, field, rules, _options) => {
|
|
361
|
+
const ruleList = rules.split("|").map((r) => r.trim()), attrs = [];
|
|
362
|
+
for (const ruleStr of ruleList) {
|
|
363
|
+
const rule = parseValidationRule(ruleStr);
|
|
364
|
+
switch (rule.name) {
|
|
365
|
+
case "required":
|
|
366
|
+
attrs.push("required");
|
|
367
|
+
break;
|
|
368
|
+
case "email":
|
|
369
|
+
attrs.push('type="email"');
|
|
370
|
+
break;
|
|
371
|
+
case "url":
|
|
372
|
+
attrs.push('type="url"');
|
|
373
|
+
break;
|
|
374
|
+
case "numeric":
|
|
375
|
+
case "integer":
|
|
376
|
+
attrs.push('type="number"');
|
|
377
|
+
if (rule.name === "integer")
|
|
378
|
+
attrs.push('step="1"');
|
|
379
|
+
break;
|
|
380
|
+
case "min":
|
|
381
|
+
if (rule.params?.[0])
|
|
382
|
+
attrs.push(`minlength="${rule.params[0]}"`);
|
|
383
|
+
break;
|
|
384
|
+
case "max":
|
|
385
|
+
if (rule.params?.[0])
|
|
386
|
+
attrs.push(`maxlength="${rule.params[0]}"`);
|
|
387
|
+
break;
|
|
388
|
+
case "regex":
|
|
389
|
+
if (rule.params?.[0])
|
|
390
|
+
attrs.push(`pattern="${rule.params[0]}"`);
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return `<!-- @validate:${field}:${rules} -->${attrs.length > 0 ? `<!-- attrs:${attrs.join(" ")} -->` : ""}`;
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
export const enhancedValidationRules = {
|
|
398
|
+
required: {
|
|
399
|
+
name: "required",
|
|
400
|
+
validate: (value) => {
|
|
401
|
+
if (value === null || value === void 0 || value === "")
|
|
402
|
+
return "This field is required";
|
|
403
|
+
if (Array.isArray(value) && value.length === 0)
|
|
404
|
+
return "This field is required";
|
|
405
|
+
return !0;
|
|
406
|
+
},
|
|
407
|
+
toHtml5: () => ["required"],
|
|
408
|
+
message: "This field is required"
|
|
409
|
+
},
|
|
410
|
+
email: {
|
|
411
|
+
name: "email",
|
|
412
|
+
validate: (value) => {
|
|
413
|
+
if (!value)
|
|
414
|
+
return !0;
|
|
415
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(value)) || "Please enter a valid email address";
|
|
416
|
+
},
|
|
417
|
+
toHtml5: () => ['type="email"'],
|
|
418
|
+
message: "Please enter a valid email address"
|
|
419
|
+
},
|
|
420
|
+
url: {
|
|
421
|
+
name: "url",
|
|
422
|
+
validate: (value) => {
|
|
423
|
+
if (!value)
|
|
424
|
+
return !0;
|
|
425
|
+
try {
|
|
426
|
+
return new URL(String(value)).protocol.startsWith("http") || "Please enter a valid URL";
|
|
427
|
+
} catch {
|
|
428
|
+
return "Please enter a valid URL";
|
|
429
|
+
}
|
|
430
|
+
},
|
|
431
|
+
toHtml5: () => ['type="url"'],
|
|
432
|
+
message: "Please enter a valid URL"
|
|
433
|
+
},
|
|
434
|
+
numeric: {
|
|
435
|
+
name: "numeric",
|
|
436
|
+
validate: (value) => {
|
|
437
|
+
if (!value)
|
|
438
|
+
return !0;
|
|
439
|
+
return !Number.isNaN(Number(value)) || "Please enter a number";
|
|
440
|
+
},
|
|
441
|
+
toHtml5: () => ['type="number"'],
|
|
442
|
+
message: "Please enter a number"
|
|
443
|
+
},
|
|
444
|
+
integer: {
|
|
445
|
+
name: "integer",
|
|
446
|
+
validate: (value) => {
|
|
447
|
+
if (!value)
|
|
448
|
+
return !0;
|
|
449
|
+
const num = Number(value);
|
|
450
|
+
return Number.isInteger(num) || "Please enter a whole number";
|
|
451
|
+
},
|
|
452
|
+
toHtml5: () => ['type="number"', 'step="1"'],
|
|
453
|
+
message: "Please enter a whole number"
|
|
454
|
+
},
|
|
455
|
+
alpha: {
|
|
456
|
+
name: "alpha",
|
|
457
|
+
validate: (value) => {
|
|
458
|
+
if (!value)
|
|
459
|
+
return !0;
|
|
460
|
+
return /^[a-z]+$/i.test(String(value)) || "Please enter only letters";
|
|
461
|
+
},
|
|
462
|
+
toHtml5: () => ['pattern="[a-zA-Z]+"'],
|
|
463
|
+
message: "Please enter only letters"
|
|
464
|
+
},
|
|
465
|
+
alphanumeric: {
|
|
466
|
+
name: "alphanumeric",
|
|
467
|
+
validate: (value) => {
|
|
468
|
+
if (!value)
|
|
469
|
+
return !0;
|
|
470
|
+
return /^[a-z0-9]+$/i.test(String(value)) || "Please enter only letters and numbers";
|
|
471
|
+
},
|
|
472
|
+
toHtml5: () => ['pattern="[a-zA-Z0-9]+"'],
|
|
473
|
+
message: "Please enter only letters and numbers"
|
|
474
|
+
},
|
|
475
|
+
min: {
|
|
476
|
+
name: "min",
|
|
477
|
+
validate: (value, params) => {
|
|
478
|
+
if (!value)
|
|
479
|
+
return !0;
|
|
480
|
+
const min = Number(params[0]);
|
|
481
|
+
if (typeof value === "string")
|
|
482
|
+
return value.length >= min || `Must be at least ${min} characters`;
|
|
483
|
+
if (typeof value === "number")
|
|
484
|
+
return value >= min || `Must be at least ${min}`;
|
|
485
|
+
if (Array.isArray(value))
|
|
486
|
+
return value.length >= min || `Must have at least ${min} items`;
|
|
487
|
+
return !0;
|
|
488
|
+
},
|
|
489
|
+
toHtml5: (params) => [`minlength="${params[0]}"`, `min="${params[0]}"`],
|
|
490
|
+
message: "Must be at least {{param}} characters"
|
|
491
|
+
},
|
|
492
|
+
max: {
|
|
493
|
+
name: "max",
|
|
494
|
+
validate: (value, params) => {
|
|
495
|
+
if (!value)
|
|
496
|
+
return !0;
|
|
497
|
+
const max = Number(params[0]);
|
|
498
|
+
if (typeof value === "string")
|
|
499
|
+
return value.length <= max || `Must be no more than ${max} characters`;
|
|
500
|
+
if (typeof value === "number")
|
|
501
|
+
return value <= max || `Must be no more than ${max}`;
|
|
502
|
+
if (Array.isArray(value))
|
|
503
|
+
return value.length <= max || `Must have no more than ${max} items`;
|
|
504
|
+
return !0;
|
|
505
|
+
},
|
|
506
|
+
toHtml5: (params) => [`maxlength="${params[0]}"`, `max="${params[0]}"`],
|
|
507
|
+
message: "Must be no more than {{param}} characters"
|
|
508
|
+
},
|
|
509
|
+
between: {
|
|
510
|
+
name: "between",
|
|
511
|
+
validate: (value, params) => {
|
|
512
|
+
if (!value)
|
|
513
|
+
return !0;
|
|
514
|
+
const [min, max] = params.map(Number);
|
|
515
|
+
if (typeof value === "string")
|
|
516
|
+
return value.length >= min && value.length <= max || `Must be between ${min} and ${max} characters`;
|
|
517
|
+
if (typeof value === "number")
|
|
518
|
+
return value >= min && value <= max || `Must be between ${min} and ${max}`;
|
|
519
|
+
return !0;
|
|
520
|
+
},
|
|
521
|
+
toHtml5: (params) => [`minlength="${params[0]}"`, `maxlength="${params[1]}"`],
|
|
522
|
+
message: "Must be between {{min}} and {{max}}"
|
|
523
|
+
},
|
|
524
|
+
confirmed: {
|
|
525
|
+
name: "confirmed",
|
|
526
|
+
validate: (value, _params, allValues) => {
|
|
527
|
+
const fieldNames = Object.keys(allValues);
|
|
528
|
+
for (const key of fieldNames)
|
|
529
|
+
if (key.endsWith("_confirmation") && allValues[key] === value)
|
|
530
|
+
return !0;
|
|
531
|
+
return "Confirmation does not match";
|
|
532
|
+
},
|
|
533
|
+
message: "Confirmation does not match"
|
|
534
|
+
},
|
|
535
|
+
in: {
|
|
536
|
+
name: "in",
|
|
537
|
+
validate: (value, params) => {
|
|
538
|
+
if (!value)
|
|
539
|
+
return !0;
|
|
540
|
+
return params.includes(String(value)) || `Must be one of: ${params.join(", ")}`;
|
|
541
|
+
},
|
|
542
|
+
message: "Must be one of: {{values}}"
|
|
543
|
+
},
|
|
544
|
+
notIn: {
|
|
545
|
+
name: "notIn",
|
|
546
|
+
validate: (value, params) => {
|
|
547
|
+
if (!value)
|
|
548
|
+
return !0;
|
|
549
|
+
return !params.includes(String(value)) || `Must not be: ${params.join(", ")}`;
|
|
550
|
+
},
|
|
551
|
+
message: "Must not be: {{values}}"
|
|
552
|
+
},
|
|
553
|
+
regex: {
|
|
554
|
+
name: "regex",
|
|
555
|
+
validate: (value, params) => {
|
|
556
|
+
if (!value)
|
|
557
|
+
return !0;
|
|
558
|
+
try {
|
|
559
|
+
return new RegExp(params[0]).test(String(value)) || "Invalid format";
|
|
560
|
+
} catch {
|
|
561
|
+
return "Invalid format";
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
toHtml5: (params) => [`pattern="${params[0]}"`],
|
|
565
|
+
message: "Invalid format"
|
|
566
|
+
},
|
|
567
|
+
date: {
|
|
568
|
+
name: "date",
|
|
569
|
+
validate: (value) => {
|
|
570
|
+
if (!value)
|
|
571
|
+
return !0;
|
|
572
|
+
const date = new Date(String(value));
|
|
573
|
+
return !Number.isNaN(date.getTime()) || "Please enter a valid date";
|
|
574
|
+
},
|
|
575
|
+
toHtml5: () => ['type="date"'],
|
|
576
|
+
message: "Please enter a valid date"
|
|
577
|
+
},
|
|
578
|
+
before: {
|
|
579
|
+
name: "before",
|
|
580
|
+
validate: (value, params) => {
|
|
581
|
+
if (!value)
|
|
582
|
+
return !0;
|
|
583
|
+
const inputDate = new Date(String(value)), beforeDate = new Date(params[0]);
|
|
584
|
+
return inputDate < beforeDate || `Must be before ${params[0]}`;
|
|
585
|
+
},
|
|
586
|
+
toHtml5: (params) => [`max="${params[0]}"`],
|
|
587
|
+
message: "Must be before {{date}}"
|
|
588
|
+
},
|
|
589
|
+
after: {
|
|
590
|
+
name: "after",
|
|
591
|
+
validate: (value, params) => {
|
|
592
|
+
if (!value)
|
|
593
|
+
return !0;
|
|
594
|
+
const inputDate = new Date(String(value)), afterDate = new Date(params[0]);
|
|
595
|
+
return inputDate > afterDate || `Must be after ${params[0]}`;
|
|
596
|
+
},
|
|
597
|
+
toHtml5: (params) => [`min="${params[0]}"`],
|
|
598
|
+
message: "Must be after {{date}}"
|
|
599
|
+
},
|
|
600
|
+
size: {
|
|
601
|
+
name: "size",
|
|
602
|
+
validate: (value, params) => {
|
|
603
|
+
if (!value)
|
|
604
|
+
return !0;
|
|
605
|
+
const size = Number(params[0]);
|
|
606
|
+
if (typeof value === "string")
|
|
607
|
+
return value.length === size || `Must be exactly ${size} characters`;
|
|
608
|
+
if (Array.isArray(value))
|
|
609
|
+
return value.length === size || `Must have exactly ${size} items`;
|
|
610
|
+
return !0;
|
|
611
|
+
},
|
|
612
|
+
message: "Must be exactly {{size}}"
|
|
613
|
+
},
|
|
614
|
+
phone: {
|
|
615
|
+
name: "phone",
|
|
616
|
+
validate: (value) => {
|
|
617
|
+
if (!value)
|
|
618
|
+
return !0;
|
|
619
|
+
return /^\+?\(?\d{1,4}\)?[-\s./\d]*$/.test(String(value)) || "Please enter a valid phone number";
|
|
620
|
+
},
|
|
621
|
+
toHtml5: () => ['type="tel"', 'pattern="\\+?\\(?\\d{1,4}\\)?[-\\s./\\d]*"'],
|
|
622
|
+
message: "Please enter a valid phone number"
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
export function registerEnhancedValidationRule(rule) {
|
|
626
|
+
enhancedValidationRules[rule.name] = rule;
|
|
627
|
+
}
|
|
628
|
+
export function validateValueEnhanced(value, rules, allValues = {}) {
|
|
629
|
+
const errors = [], ruleList = rules.split("|").map((r) => r.trim());
|
|
630
|
+
for (const ruleStr of ruleList) {
|
|
631
|
+
const parsed = parseValidationRule(ruleStr), ruleDef = enhancedValidationRules[parsed.name];
|
|
632
|
+
if (!ruleDef) {
|
|
633
|
+
console.warn(`Unknown validation rule: ${parsed.name}`);
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
const result = ruleDef.validate(value, parsed.params || [], allValues);
|
|
637
|
+
if (result !== !0)
|
|
638
|
+
errors.push(result);
|
|
639
|
+
}
|
|
640
|
+
return { valid: errors.length === 0, errors };
|
|
641
|
+
}
|
|
642
|
+
export function validateFormEnhanced(values, rules) {
|
|
643
|
+
const errors = {};
|
|
644
|
+
let valid = !0;
|
|
645
|
+
for (const [field, fieldRules] of Object.entries(rules)) {
|
|
646
|
+
const result = validateValueEnhanced(values[field], fieldRules, values);
|
|
647
|
+
if (!result.valid) {
|
|
648
|
+
valid = !1;
|
|
649
|
+
errors[field] = result.errors;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
return { valid, errors };
|
|
653
|
+
}
|
|
654
|
+
export function generateValidationScript(formId, rules) {
|
|
655
|
+
const rulesJson = JSON.stringify(rules);
|
|
656
|
+
return `
|
|
657
|
+
<script>
|
|
658
|
+
(function() {
|
|
659
|
+
const form = document.getElementById('${formId}');
|
|
660
|
+
if (!form) return;
|
|
661
|
+
|
|
662
|
+
const rules = ${rulesJson};
|
|
663
|
+
const validators = {
|
|
664
|
+
required: (v) => v !== null && v !== undefined && v !== '' ? true : 'This field is required',
|
|
665
|
+
email: (v) => !v || /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(v) ? true : 'Please enter a valid email',
|
|
666
|
+
url: (v) => { if (!v) return true; try { new URL(v); return true; }
|
|
667
|
+
catch { return 'Please enter a valid URL'; } },
|
|
668
|
+
numeric: (v) => !v || !isNaN(Number(v)) ? true : 'Please enter a number',
|
|
669
|
+
integer: (v) => !v || Number.isInteger(Number(v)) ? true : 'Please enter a whole number',
|
|
670
|
+
min: (v, p) => !v || (typeof v === 'string' ? v.length >= p : v >= p) ? true : 'Too short/small',
|
|
671
|
+
max: (v, p) => !v || (typeof v === 'string' ? v.length <= p : v <= p) ? true : 'Too long/large',
|
|
672
|
+
regex: (v, p) => !v || new RegExp(p).test(v) ? true : 'Invalid format',
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
form.addEventListener('submit', function(e) {
|
|
676
|
+
let valid = true;
|
|
677
|
+
const errors = {};
|
|
678
|
+
|
|
679
|
+
for (const [field, fieldRules] of Object.entries(rules)) {
|
|
680
|
+
const input = form.querySelector('[name="' + field + '"]');
|
|
681
|
+
if (!input) continue;
|
|
682
|
+
|
|
683
|
+
const value = input.value;
|
|
684
|
+
const ruleList = fieldRules.split('|');
|
|
685
|
+
|
|
686
|
+
for (const rule of ruleList) {
|
|
687
|
+
const [name, param] = rule.split(':');
|
|
688
|
+
const validator = validators[name];
|
|
689
|
+
if (validator) {
|
|
690
|
+
const result = validator(value, param);
|
|
691
|
+
if (result !== true) {
|
|
692
|
+
valid = false;
|
|
693
|
+
errors[field] = errors[field] || [];
|
|
694
|
+
errors[field].push(result);
|
|
695
|
+
input.classList.add('is-invalid');
|
|
696
|
+
break;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
if (!valid) {
|
|
703
|
+
e.preventDefault();
|
|
704
|
+
console.log('Validation errors:', errors);
|
|
705
|
+
}
|
|
706
|
+
});
|
|
707
|
+
})();
|
|
708
|
+
</script>`.trim();
|
|
709
|
+
}
|