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

View file

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