Add defhsx macro

This commit is contained in:
paku 2024-05-27 11:10:11 +09:00
parent 9d0a425b49
commit a85fc32384
2 changed files with 40 additions and 19 deletions

View file

@ -5,27 +5,26 @@
#:make-keyword)
(:import-from #:hsx/element
#:create-element)
(:export #:defcomp
(:export #:defhsx
#:defcomp
#:hsx))
(in-package #:hsx/hsx)
;;;; hsx definitions
(defparameter *builtin-elements* (make-hash-table))
(defmacro define-builtin-element (name)
(defmacro defhsx (name element-type)
`(defmacro ,name (&body body)
(multiple-value-bind (props children)
(parse-body body)
`(create-element ,',(string-downcase name)
(list ,@props)
,@children))))
`(create-element ,',element-type (list ,@props) ,@children))))
(defparameter *builtin-elements* (make-hash-table))
(defmacro define-and-export-builtin-elements (&body names)
`(progn
,@(mapcan (lambda (name)
(list `(define-builtin-element ,name)
(list `(defhsx ,name ,(string-downcase name))
`(setf (gethash (make-keyword ',name) *builtin-elements*) t)
`(export ',name)))
names)))
@ -51,12 +50,7 @@
`(eval-when (:compile-toplevel :load-toplevel :execute)
(defun ,%name ,props
,@body)
(defmacro ,name (&body body)
(multiple-value-bind (props children)
(parse-body body)
`(create-element #',',%name
(list ,@props)
,@children))))))
(defhsx ,name (fdefinition ',%name)))))
(defun parse-body (body)
(if (keywordp (first body))

View file

@ -8,6 +8,7 @@
(def-suite hsx-test)
(in-suite hsx-test)
(test empty-hsx
(is (equal (macroexpand-1
'(div))
@ -44,16 +45,42 @@
"child1"
"child2"))))
(defcomp comp (&key prop1 prop2 children)
(declare (ignore prop1 prop2 children)))
(defhsx custom "custom")
(test component-hsx
(test hsx-for-custom-tag-element
(is (equal (macroexpand-1
'(comp :prop1 "value1" :prop2 "value2"
'(custom :prop1 "value1" :prop2 "value2"
"child1"
"child2"))
'(create-element
#'%comp
"custom"
(list :prop1 "value1" :prop2 "value2")
"child1"
"child2"))))
(defun %comp1 (&key prop1 prop2 children)
(declare (ignore prop1 prop2 children)))
(defhsx comp1 #'%comp1)
(defcomp comp2 (&key prop1 prop2 children)
(declare (ignore prop1 prop2 children)))
(test hsx-for-component-element
(is (equal (macroexpand-1
'(comp1 :prop1 "value1" :prop2 "value2"
"child1"
"child2"))
'(create-element
#'%comp1
(list :prop1 "value1" :prop2 "value2")
"child1"
"child2")))
(is (equal (macroexpand-1
'(comp2 :prop1 "value1" :prop2 "value2"
"child1"
"child2"))
'(create-element
(fdefinition '%comp2)
(list :prop1 "value1" :prop2 "value2")
"child1"
"child2"))))