HS: hyperscript:before:init / :after:init events (+2 tests)

integration.sx hs-activate! now wraps the activation block in a cancelable
hyperscript:before:init event (dispatched on the el via dom-dispatch which
returns the dispatchEvent boolean — true unless preventDefault was called).
On success it dispatches hyperscript:after:init at the end. Both events
bubble so listeners on a containing wa work-area receive them. Generator
gets two hand-rolled deftests that exercise the new dispatch via
hs-boot-subtree!: one captures both events into a list, the other
preventDefaults before:init and asserts data-hyperscript-powered is absent.

hs-upstream-core/bootstrap: 20/26 → 22/26. Smoke 0-195: 170 → 172.

Remaining 4 cluster-29 tests need stricter parser error-rejection
(hs-upstream-core/parser, parse-error event); larger than a single
cluster budget — leave as untranslated for now.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-25 11:58:19 +00:00
parent 484b55281b
commit e01a3baa5b
4 changed files with 69 additions and 12 deletions

View File

@@ -80,11 +80,14 @@
((src (dom-get-attr el "_")) (prev (dom-get-data el "hs-script"))) ((src (dom-get-attr el "_")) (prev (dom-get-data el "hs-script")))
(when (when
(and src (not (= src prev))) (and src (not (= src prev)))
(hs-log-event! "hyperscript:init") (when
(dom-set-data el "hs-script" src) (dom-dispatch el "hyperscript:before:init" nil)
(dom-set-data el "hs-active" true) (hs-log-event! "hyperscript:init")
(dom-set-attr el "data-hyperscript-powered" "true") (dom-set-data el "hs-script" src)
(let ((handler (hs-handler src))) (handler el)))))) (dom-set-data el "hs-active" true)
(dom-set-attr el "data-hyperscript-powered" "true")
(let ((handler (hs-handler src))) (handler el))
(dom-dispatch el "hyperscript:after:init" nil))))))
;; ── Boot: scan entire document ────────────────────────────────── ;; ── Boot: scan entire document ──────────────────────────────────
;; Called once at page load. Finds all elements with _ attribute, ;; Called once at page load. Finds all elements with _ attribute,

View File

@@ -80,11 +80,14 @@
((src (dom-get-attr el "_")) (prev (dom-get-data el "hs-script"))) ((src (dom-get-attr el "_")) (prev (dom-get-data el "hs-script")))
(when (when
(and src (not (= src prev))) (and src (not (= src prev)))
(hs-log-event! "hyperscript:init") (when
(dom-set-data el "hs-script" src) (dom-dispatch el "hyperscript:before:init" nil)
(dom-set-data el "hs-active" true) (hs-log-event! "hyperscript:init")
(dom-set-attr el "data-hyperscript-powered" "true") (dom-set-data el "hs-script" src)
(let ((handler (hs-handler src))) (handler el)))))) (dom-set-data el "hs-active" true)
(dom-set-attr el "data-hyperscript-powered" "true")
(let ((handler (hs-handler src))) (handler el))
(dom-dispatch el "hyperscript:after:init" nil))))))
;; ── Boot: scan entire document ────────────────────────────────── ;; ── Boot: scan entire document ──────────────────────────────────
;; Called once at page load. Finds all elements with _ attribute, ;; Called once at page load. Finds all elements with _ attribute,

View File

@@ -1396,7 +1396,17 @@
(hs-activate! _el-div) (hs-activate! _el-div)
)) ))
(deftest "fires hyperscript:before:init and hyperscript:after:init" (deftest "fires hyperscript:before:init and hyperscript:after:init"
(error "SKIP (untranslated): fires hyperscript:before:init and hyperscript:after:init")) (hs-cleanup!)
(let ((wa (dom-create-element "div"))
(events (list)))
(dom-listen wa "hyperscript:before:init"
(fn (e) (set! events (append events (list "before:init")))))
(dom-listen wa "hyperscript:after:init"
(fn (e) (set! events (append events (list "after:init")))))
(dom-set-inner-html wa "<div _=\"on click add .foo\"></div>")
(hs-boot-subtree! wa)
(assert= events (list "before:init" "after:init")))
)
(deftest "hyperscript can have more than one action" (deftest "hyperscript can have more than one action"
(hs-cleanup!) (hs-cleanup!)
(let ((_el-bar (dom-create-element "div")) (_el-div (dom-create-element "div"))) (let ((_el-bar (dom-create-element "div")) (_el-div (dom-create-element "div")))
@@ -1412,7 +1422,15 @@
(assert (dom-has-class? (dom-query "div:nth-of-type(2)") "blah")) (assert (dom-has-class? (dom-query "div:nth-of-type(2)") "blah"))
)) ))
(deftest "hyperscript:before:init can cancel initialization" (deftest "hyperscript:before:init can cancel initialization"
(error "SKIP (untranslated): hyperscript:before:init can cancel initialization")) (hs-cleanup!)
(let ((wa (dom-create-element "div")))
(dom-listen wa "hyperscript:before:init"
(fn (e) (host-call e "preventDefault")))
(dom-set-inner-html wa "<div _=\"on click add .foo\"></div>")
(hs-boot-subtree! wa)
(let ((d (host-call wa "querySelector" "div")))
(assert= (host-call d "hasAttribute" "data-hyperscript-powered") false)))
)
(deftest "logAll config logs events to console" (deftest "logAll config logs events to console"
(hs-cleanup!) (hs-cleanup!)
(hs-clear-log-captured!) (hs-clear-log-captured!)

View File

@@ -1881,6 +1881,39 @@ def generate_eval_only_test(test, idx):
f' (assert= (eval-hs "cookies.length") 0))' f' (assert= (eval-hs "cookies.length") 0))'
) )
# Special case: cluster-29 init events. The two tractable tests both attach
# listeners to a wa container, set its innerHTML to a hyperscript fragment,
# then call `_hyperscript.processNode(wa)`. Hand-roll deftests using
# hs-boot-subtree! which now dispatches hyperscript:before:init / :after:init.
if test.get('name') == 'fires hyperscript:before:init and hyperscript:after:init':
return (
f' (deftest "{safe_name}"\n'
f' (hs-cleanup!)\n'
f' (let ((wa (dom-create-element "div"))\n'
f' (events (list)))\n'
f' (dom-listen wa "hyperscript:before:init"\n'
f' (fn (e) (set! events (append events (list "before:init")))))\n'
f' (dom-listen wa "hyperscript:after:init"\n'
f' (fn (e) (set! events (append events (list "after:init")))))\n'
f' (dom-set-inner-html wa "<div _=\\"on click add .foo\\"></div>")\n'
f' (hs-boot-subtree! wa)\n'
f' (assert= events (list "before:init" "after:init")))\n'
f' )'
)
if test.get('name') == 'hyperscript:before:init can cancel initialization':
return (
f' (deftest "{safe_name}"\n'
f' (hs-cleanup!)\n'
f' (let ((wa (dom-create-element "div")))\n'
f' (dom-listen wa "hyperscript:before:init"\n'
f' (fn (e) (host-call e "preventDefault")))\n'
f' (dom-set-inner-html wa "<div _=\\"on click add .foo\\"></div>")\n'
f' (hs-boot-subtree! wa)\n'
f' (let ((d (host-call wa "querySelector" "div")))\n'
f' (assert= (host-call d "hasAttribute" "data-hyperscript-powered") false)))\n'
f' )'
)
# Special case: logAll config test. Body sets `_hyperscript.config.logAll = true`, # Special case: logAll config test. Body sets `_hyperscript.config.logAll = true`,
# then mutates an element's innerHTML and calls `_hyperscript.processNode`. # then mutates an element's innerHTML and calls `_hyperscript.processNode`.
# Our runtime exposes this via hs-set-log-all! + hs-log-captured; we reuse # Our runtime exposes this via hs-set-log-all! + hs-log-captured; we reuse