From 464bbfde65cde548d76316e7a730f5c58d678516 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sun, 21 Jul 2024 11:49:40 +0200 Subject: [PATCH] web/dom: more pre-defined elemnts; simplify dlist accordingly --- web/dom.lisp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/web/dom.lisp b/web/dom.lisp index f78db49..24ed61c 100644 --- a/web/dom.lisp +++ b/web/dom.lisp @@ -40,6 +40,8 @@ (put c)) (end tag)))) +;;;; void element (e.g. ): no body, no explicit closing of tag + (defclass void-element (element) ()) (defun void-element (tag attrs) @@ -48,27 +50,32 @@ (defmethod put ((el void-element)) (start (tag el) (attrs el))) +;;;; automatically define standard HTML elements + (defmacro make-elements (tags &optional (elem-fn 'elem)) `(progn ,@(mapcar (lambda (tag) ;`(make-element ,tag)) tags))) `(defun ,tag (attrs &rest body) - (funcall #',elem-fn ',tag attrs body))) + (funcall (function ,elem-fn) ',tag attrs body))) tags))) (eval-when (:compile-toplevel :load-toplevel :execute) - (make-elements (div label))) + (make-elements (a dd div dl dt label))) + +(eval-when (:compile-toplevel :load-toplevel :execute) + (make-elements (br input) :void-element)) ;;;; elements with specific functionality (defun dlist (attrs plist) - (elem :dl attrs + (dl attrs (util:loop-plist plist key val append - (cons (element :dt nil (string-downcase key)) (dds nil val))))) + (cons (dt nil (string-downcase key)) (dds nil val))))) (defun dds (attrs cont) (if (atom cont) - (list (element :dd attrs cont)) - (mapcar #'(lambda (x) (element :dd nil x)) cont))) + (list (dd attrs cont)) + (mapcar #'(lambda (x) (dd nil x)) cont))) ;;;; rendering @@ -84,8 +91,8 @@ (put-string tag) (put-attrs attrs) (if close - (put-char #\/)) - (put-char #\>)) + (put-string " />") + (put-char #\>))) (defun end (tag) (put-string "