HS: assignableElements — set vs put distinction (+8 tests)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 19s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 19s
Parser: parse-set-cmd now emits (set-el! target value) when target is a query node (e.g. #id, .class), keeping (set! ...) for all other targets. Compiler: add (set-el! ...) handler that calls hs-set-element!; revert emit-set for query targets back to hs-set-inner-html! so that put "x" into #target keeps setting innerHTML rather than replacing the element. Runtime: hs-set-element! new function — parses value as HTML into a temp div; if it contains element children, replaces the target element via replaceChild and boots hyperscript on the new element; otherwise falls through to hs-set-inner-html!. Removes the spurious host-to-list wrapper that was causing len() to always return 0. Result: all 8 assignableElements tests pass (set #id / set .class / set closest / swap, plus put-into-still-works-as-innerHTML). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1830,6 +1830,8 @@
|
|||||||
(emit-set
|
(emit-set
|
||||||
(nth ast 1)
|
(nth ast 1)
|
||||||
(hs-to-sx (nth ast 2))))
|
(hs-to-sx (nth ast 2))))
|
||||||
|
((= head (quote set-el!))
|
||||||
|
(list (quote hs-set-element!) (hs-to-sx (nth ast 1)) (hs-to-sx (nth ast 2))))
|
||||||
((= head (quote put!))
|
((= head (quote put!))
|
||||||
(let
|
(let
|
||||||
((val (hs-to-sx (nth ast 1)))
|
((val (hs-to-sx (nth ast 1)))
|
||||||
|
|||||||
@@ -1475,7 +1475,9 @@
|
|||||||
((match-kw "to")
|
((match-kw "to")
|
||||||
(let
|
(let
|
||||||
((value (parse-expr)))
|
((value (parse-expr)))
|
||||||
(list (quote set!) tgt value)))
|
(if (and (list? tgt) (= (first tgt) (quote query)))
|
||||||
|
(list (quote set-el!) tgt value)
|
||||||
|
(list (quote set!) tgt value))))
|
||||||
((match-kw "on")
|
((match-kw "on")
|
||||||
(let
|
(let
|
||||||
((target (parse-expr)))
|
((target (parse-expr)))
|
||||||
|
|||||||
@@ -314,6 +314,23 @@
|
|||||||
(let
|
(let
|
||||||
((str-val (if (list? value) (join "" (map (fn (x) (str x)) value)) value)))
|
((str-val (if (list? value) (join "" (map (fn (x) (str x)) value)) value)))
|
||||||
(do (dom-set-inner-html target str-val) (hs-boot-subtree! target)))))
|
(do (dom-set-inner-html target str-val) (hs-boot-subtree! target)))))
|
||||||
|
(define
|
||||||
|
hs-set-element!
|
||||||
|
(fn
|
||||||
|
(target value)
|
||||||
|
(let ((parent (dom-parent target)))
|
||||||
|
(when parent
|
||||||
|
(let ((tmp (dom-create-element "div"))
|
||||||
|
(str-val (if (list? value) (join "" (map (fn (x) (str x)) value)) value)))
|
||||||
|
(do
|
||||||
|
(dom-set-inner-html tmp str-val)
|
||||||
|
(let ((children (host-get tmp "children")))
|
||||||
|
(if (> (len children) 0)
|
||||||
|
(let ((new-el (first children)))
|
||||||
|
(do
|
||||||
|
(host-call parent "replaceChild" new-el target)
|
||||||
|
(hs-boot-subtree! new-el)))
|
||||||
|
(hs-set-inner-html! target str-val)))))))))
|
||||||
(define
|
(define
|
||||||
hs-put!
|
hs-put!
|
||||||
(fn
|
(fn
|
||||||
|
|||||||
@@ -1830,6 +1830,8 @@
|
|||||||
(emit-set
|
(emit-set
|
||||||
(nth ast 1)
|
(nth ast 1)
|
||||||
(hs-to-sx (nth ast 2))))
|
(hs-to-sx (nth ast 2))))
|
||||||
|
((= head (quote set-el!))
|
||||||
|
(list (quote hs-set-element!) (hs-to-sx (nth ast 1)) (hs-to-sx (nth ast 2))))
|
||||||
((= head (quote put!))
|
((= head (quote put!))
|
||||||
(let
|
(let
|
||||||
((val (hs-to-sx (nth ast 1)))
|
((val (hs-to-sx (nth ast 1)))
|
||||||
|
|||||||
@@ -1475,7 +1475,9 @@
|
|||||||
((match-kw "to")
|
((match-kw "to")
|
||||||
(let
|
(let
|
||||||
((value (parse-expr)))
|
((value (parse-expr)))
|
||||||
(list (quote set!) tgt value)))
|
(if (and (list? tgt) (= (first tgt) (quote query)))
|
||||||
|
(list (quote set-el!) tgt value)
|
||||||
|
(list (quote set!) tgt value))))
|
||||||
((match-kw "on")
|
((match-kw "on")
|
||||||
(let
|
(let
|
||||||
((target (parse-expr)))
|
((target (parse-expr)))
|
||||||
|
|||||||
@@ -314,6 +314,23 @@
|
|||||||
(let
|
(let
|
||||||
((str-val (if (list? value) (join "" (map (fn (x) (str x)) value)) value)))
|
((str-val (if (list? value) (join "" (map (fn (x) (str x)) value)) value)))
|
||||||
(do (dom-set-inner-html target str-val) (hs-boot-subtree! target)))))
|
(do (dom-set-inner-html target str-val) (hs-boot-subtree! target)))))
|
||||||
|
(define
|
||||||
|
hs-set-element!
|
||||||
|
(fn
|
||||||
|
(target value)
|
||||||
|
(let ((parent (dom-parent target)))
|
||||||
|
(when parent
|
||||||
|
(let ((tmp (dom-create-element "div"))
|
||||||
|
(str-val (if (list? value) (join "" (map (fn (x) (str x)) value)) value)))
|
||||||
|
(do
|
||||||
|
(dom-set-inner-html tmp str-val)
|
||||||
|
(let ((children (host-get tmp "children")))
|
||||||
|
(if (> (len children) 0)
|
||||||
|
(let ((new-el (first children)))
|
||||||
|
(do
|
||||||
|
(host-call parent "replaceChild" new-el target)
|
||||||
|
(hs-boot-subtree! new-el)))
|
||||||
|
(hs-set-inner-html! target str-val)))))))))
|
||||||
(define
|
(define
|
||||||
hs-put!
|
hs-put!
|
||||||
(fn
|
(fn
|
||||||
|
|||||||
@@ -3927,8 +3927,7 @@
|
|||||||
(dom-append (dom-body) _el-button)
|
(dom-append (dom-body) _el-button)
|
||||||
(hs-activate! _el-button)
|
(hs-activate! _el-button)
|
||||||
(dom-dispatch _el-button "click" nil)
|
(dom-dispatch _el-button "click" nil)
|
||||||
(assert= (dom-text-content (dom-query-by-id "target")) "new")
|
(assert= (dom-text-content (dom-query-by-id "target")) "new")))
|
||||||
))
|
|
||||||
(deftest "set #id replaces element with HTML string"
|
(deftest "set #id replaces element with HTML string"
|
||||||
(hs-cleanup!)
|
(hs-cleanup!)
|
||||||
(let ((_el-target (dom-create-element "div")) (_el-button (dom-create-element "button")))
|
(let ((_el-target (dom-create-element "div")) (_el-button (dom-create-element "button")))
|
||||||
|
|||||||
Reference in New Issue
Block a user