Improve render function

This commit is contained in:
paku 2024-05-30 09:22:18 +09:00
parent 5a63bf8154
commit 6a6e3231e7
2 changed files with 21 additions and 15 deletions

View file

@ -63,9 +63,10 @@
(let ((type-str (string-downcase type))) (let ((type-str (string-downcase type)))
(if children (if children
(format stream (format stream
(if (rest children) (if (and (null (rest children))
"~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_</~a>~:>" (typep (first children) 'string))
"~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_</~a>~:>") "~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_</~a>~:>"
"~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_</~a>~:>")
type-str type-str
(props->string props) (props->string props)
children children

View file

@ -19,29 +19,34 @@
(render (hsx (div :prop1 "value1" :prop2 t :prop3 nil)))))) (render (hsx (div :prop1 "value1" :prop2 t :prop3 nil))))))
(test tag-with-children (test tag-with-children
(is (string= "<p>Hello, World!</p>" (is (string= "<p>foo</p>"
(render (hsx (p "Hello, World!"))))) (render (hsx (p "foo")))))
(is (string= #M"<p> (is (string= #M"<p>
\ Hello, \ <span>foo</span>
\ <span>World!</span>
\</p>" \</p>"
(render (hsx (p (render (hsx (p
"Hello," (span "foo"))))))
(span "World!"))))))) (is (string= #M"<p>
\ foo
\ <span>bar</span>
\</p>"
(render (hsx (p
"foo"
(span "bar")))))))
(test tag-with-props-and-children (test tag-with-props-and-children
(is (string= "<p prop1=\"value1\" prop2>Hello, World!</p>" (is (string= "<p prop1=\"value1\" prop2>foo</p>"
(render (hsx (render (hsx
(p :prop1 "value1" :prop2 t :prop3 nil (p :prop1 "value1" :prop2 t :prop3 nil
"Hello, World!"))))) "foo")))))
(is (string= #M"<p prop1=\"value1\" prop2> (is (string= #M"<p prop1=\"value1\" prop2>
\ Hello, \ foo
\ <span>World!</span> \ <span>bar</span>
\</p>" \</p>"
(render (hsx (render (hsx
(p :prop1 "value1" :prop2 t :prop3 nil (p :prop1 "value1" :prop2 t :prop3 nil
"Hello," "foo"
(span "World!"))))))) (span "bar")))))))
(test fragment (test fragment
(let ((frg (hsx (let ((frg (hsx