Add methods to render each slot

This commit is contained in:
paku 2024-06-09 21:28:18 +09:00
parent d203c703c4
commit b46aca0a13

View file

@ -77,45 +77,47 @@
(write element :stream stream :pretty pretty))) (write element :stream stream :pretty pretty)))
(defmethod print-object ((element tag) stream) (defmethod print-object ((element tag) stream)
(with-slots (type props children) element (let ((type (render-type element))
(let ((type-str (string-downcase type)) (props (render-props element))
(props-str (render-props props))) (children (render-children element)))
(if children (if children
(format stream (format stream
(if (or (rest children) (if (or (rest children)
(typep (first children) 'element)) (typep (first children) 'element))
"~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_</~a>~:>" "~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_</~a>~:>"
"~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_</~a>~:>") "~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_</~a>~:>")
type-str type
props-str props
(render-children element) children
type-str) type)
(format stream "<~a~a></~a>" type-str props-str type-str))))) (format stream "<~a~a></~a>" type props type))))
(defmethod print-object ((element self-closing-tag) stream) (defmethod print-object ((element self-closing-tag) stream)
(with-slots (type props) element (format stream "<~a~a>" (render-type element) (render-props element)))
(format stream "<~a~a>" (string-downcase type) (render-props props))))
(defmethod print-object ((element html-tag) stream) (defmethod print-object ((element html-tag) stream)
(format stream "<!DOCTYPE html>~%") (format stream "<!DOCTYPE html>~%")
(call-next-method)) (call-next-method))
(defmethod print-object ((element fragment) stream) (defmethod print-object ((element fragment) stream)
(with-slots (children) element (let ((children (render-children element)))
(if children (if children
(format stream (format stream
(if (rest children) (if (rest children)
"~<~@{~a~^~:@_~}~:>" "~<~@{~a~^~:@_~}~:>"
"~<~a~:>") "~<~a~:>")
(render-children element))))) children))))
(defmethod print-object ((element component) stream) (defmethod print-object ((element component) stream)
(print-object (expand-component element) stream)) (print-object (expand-component element) stream))
(defun render-props (props) (defmethod render-type ((element tag))
(string-downcase (element-type element)))
(defmethod render-props ((element tag))
(with-output-to-string (stream) (with-output-to-string (stream)
(loop (loop
:for (key value) :on props :by #'cddr :for (key value) :on (element-props element) :by #'cddr
:do (let ((key-str (string-downcase key))) :do (let ((key-str (string-downcase key)))
(if (typep value 'boolean) (if (typep value 'boolean)
(format stream (format stream