From 6a6e3231e7719eedbed14255028f7d80121cd777 Mon Sep 17 00:00:00 2001 From: paku Date: Thu, 30 May 2024 09:22:18 +0900 Subject: [PATCH] Improve render function --- src/element.lisp | 7 ++++--- tests/renderer.lisp | 29 +++++++++++++++++------------ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/element.lisp b/src/element.lisp index f05e9bb..b563716 100644 --- a/src/element.lisp +++ b/src/element.lisp @@ -63,9 +63,10 @@ (let ((type-str (string-downcase type))) (if children (format stream - (if (rest children) - "~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_~:>" - "~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_~:>") + (if (and (null (rest children)) + (typep (first children) 'string)) + "~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_~:>" + "~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_~:>") type-str (props->string props) children diff --git a/tests/renderer.lisp b/tests/renderer.lisp index 93b7c7a..65c6cd2 100644 --- a/tests/renderer.lisp +++ b/tests/renderer.lisp @@ -19,29 +19,34 @@ (render (hsx (div :prop1 "value1" :prop2 t :prop3 nil)))))) (test tag-with-children - (is (string= "

Hello, World!

" - (render (hsx (p "Hello, World!"))))) + (is (string= "

foo

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

- \ Hello, - \ World! + \ foo \

" (render (hsx (p - "Hello," - (span "World!"))))))) + (span "foo")))))) + (is (string= #M"

+ \ foo + \ bar + \

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

Hello, World!

" + (is (string= "

foo

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

- \ Hello, - \ World! + \ foo + \ bar \

" (render (hsx (p :prop1 "value1" :prop2 t :prop3 nil - "Hello," - (span "World!"))))))) + "foo" + (span "bar"))))))) (test fragment (let ((frg (hsx