This commit is contained in:
paku 2024-05-30 13:18:32 +09:00
parent 37e9016b13
commit f9bf53042f
2 changed files with 31 additions and 35 deletions

View file

@ -9,22 +9,20 @@
#:defcomp))
(in-package #:hsx/defhsx)
(defmacro defhsx (name element-type)
`(defmacro ,name (&body body)
`(%create-element ,',element-type ,@body)))
(defun %create-element (type &rest body)
(multiple-value-bind (props children)
(parse-body body)
(create-element type props children)))
(defmacro defhsx (name element-type)
`(defmacro ,name (&body body)
`(%create-element ,',element-type ,@body)))
(defun parse-body (body)
(cond (; plist
(and (listp (first body))
(cond ((and (listp (first body))
(keywordp (first (first body))))
(values (first body) (rest body)))
(; inline-plist
(keywordp (first body))
((keywordp (first body))
(loop :for thing :on body :by #'cddr
:for (k v) := thing
:when (and (keywordp k) v)

View file

@ -1,9 +1,11 @@
(defpackage #:hsx-test/renderer
(:use #:cl
#:fiveam
#:hsx
#:named-readtables)
(:import-from #:mstrings))
#:named-readtables
#:hsx/builtin)
(:import-from #:mstrings)
(:import-from #:hsx/element
#:render))
(in-package :hsx-test/renderer)
(in-readtable mstrings:mstring-syntax)
@ -12,47 +14,44 @@
(test empty-tag
(is (string= "<div></div>"
(render (hsx (div))))))
(render (div)))))
(test tag-with-props
(is (string= "<div prop1=\"value1\" prop2></div>"
(render (hsx (div :prop1 "value1" :prop2 t :prop3 nil))))))
(render (div :prop1 "value1" :prop2 t :prop3 nil)))))
(test tag-with-children
(is (string= "<p>foo</p>"
(render (hsx (p "foo")))))
(render (p "foo"))))
(is (string= #M"<p>
\ <span>foo</span>
\</p>"
(render (hsx (p
(span "foo"))))))
(render (p
(span "foo")))))
(is (string= #M"<p>
\ foo
\ <span>bar</span>
\</p>"
(render (hsx (p
(render (p
"foo"
(span "bar")))))))
(span "bar"))))))
(test tag-with-props-and-children
(is (string= "<p prop1=\"value1\" prop2>foo</p>"
(render (hsx
(p :prop1 "value1" :prop2 t :prop3 nil
"foo")))))
(render (p :prop1 "value1" :prop2 t :prop3 nil
"foo"))))
(is (string= #M"<p prop1=\"value1\" prop2>
\ foo
\ <span>bar</span>
\</p>"
(render (hsx
(p :prop1 "value1" :prop2 t :prop3 nil
(render (p :prop1 "value1" :prop2 t :prop3 nil
"foo"
(span "bar")))))))
(span "bar"))))))
(test fragment
(let ((frg (hsx
(<>
(let ((frg (<>
(li "bar")
(li "baz")))))
(li "baz"))))
(is (string= #M"<li>bar</li>
<li>baz</li>"
(render frg)))
@ -62,10 +61,9 @@
\ <li>baz</li>
\ <li>brah</li>
\</ul>"
(render (hsx
(ul
(render (ul
(li "foo")
frg
(li "brah"))))))))
(li "brah")))))))
;; TODO: Add escaping test