From f9bf53042fa87fc24b8f10df0cf0426e17f02eb2 Mon Sep 17 00:00:00 2001 From: paku Date: Thu, 30 May 2024 13:18:32 +0900 Subject: [PATCH] Improve --- src/defhsx.lisp | 14 ++++++------ tests/renderer.lisp | 52 ++++++++++++++++++++++----------------------- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/defhsx.lisp b/src/defhsx.lisp index 7a2cc82..ebc45de 100644 --- a/src/defhsx.lisp +++ b/src/defhsx.lisp @@ -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) diff --git a/tests/renderer.lisp b/tests/renderer.lisp index a14caa7..a3627e3 100644 --- a/tests/renderer.lisp +++ b/tests/renderer.lisp @@ -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= "
" - (render (hsx (div)))))) + (render (div))))) (test tag-with-props (is (string= "
" - (render (hsx (div :prop1 "value1" :prop2 t :prop3 nil)))))) + (render (div :prop1 "value1" :prop2 t :prop3 nil))))) (test tag-with-children (is (string= "

foo

" - (render (hsx (p "foo"))))) + (render (p "foo")))) (is (string= #M"

\ foo \

" - (render (hsx (p - (span "foo")))))) + (render (p + (span "foo"))))) (is (string= #M"

\ foo \ bar \

" - (render (hsx (p - "foo" - (span "bar"))))))) + (render (p + "foo" + (span "bar")))))) (test tag-with-props-and-children (is (string= "

foo

" - (render (hsx - (p :prop1 "value1" :prop2 t :prop3 nil - "foo"))))) + (render (p :prop1 "value1" :prop2 t :prop3 nil + "foo")))) (is (string= #M"

\ foo \ bar \

" - (render (hsx - (p :prop1 "value1" :prop2 t :prop3 nil - "foo" - (span "bar"))))))) + (render (p :prop1 "value1" :prop2 t :prop3 nil + "foo" + (span "bar")))))) (test fragment - (let ((frg (hsx - (<> - (li "bar") - (li "baz"))))) + (let ((frg (<> + (li "bar") + (li "baz")))) (is (string= #M"
  • bar
  • baz
  • " (render frg))) @@ -62,10 +61,9 @@ \
  • baz
  • \
  • brah
  • \" - (render (hsx - (ul - (li "foo") - frg - (li "brah")))))))) + (render (ul + (li "foo") + frg + (li "brah"))))))) ;; TODO: Add escaping test