fix core test; web/dom: provide rendering of self closing (empty) element

This commit is contained in:
Helmut Merz 2024-07-16 18:45:18 +02:00
parent a43de69210
commit 473616860f
2 changed files with 24 additions and 22 deletions

View file

@ -61,8 +61,6 @@
(t:show-result))))
(deftest test-util ()
(== (util:trunc "hello world" 5) "hello...")
(== (util:trunc "hello world" 11) "hello world")
(== (util:to-keyword "hello-kitty") :hello-kitty))
(deftest test-send ()

View file

@ -17,12 +17,11 @@
(defclass element ()
((tag :reader tag :initarg :tag)
(attrs :reader attrs :initarg :attrs)
(body :reader body :initarg :body)))
(attrs :reader attrs :initarg :attrs :initform nil)
(body :reader body :initarg :body :initform nil)))
(defun elem (tag attrs body)
(make-instance 'element :tag (string-downcase tag)
:attrs attrs :body body))
(defun elem (tag &optional attrs body)
(make-instance 'element :tag tag :attrs attrs :body body))
(defun element (tag attrs &rest body)
(elem tag attrs body))
@ -31,10 +30,13 @@
(format stream "<~a ~s>~s" (tag el) (attrs el) (body el)))
(defmethod put ((el element))
(start (tag el) (attrs el))
(dolist (c (body el))
(put c))
(end (tag el)))
(let ((tag (string-downcase (tag el)))
(body (body el)))
(start tag (attrs el) :close (not body))
(when body
(dolist (c body)
(put c))
(end tag))))
;;;; elements with specific functionality
@ -58,6 +60,19 @@
(put el))
(get-output-stream-string *output*)))
(defun start (tag attrs &key close)
(put-char #\<)
(put-string tag)
(put-attrs attrs)
(if close
(put-char #\/))
(put-char #\>))
(defun end (tag)
(put-string "</")
(put-string tag)
(put-char #\>))
(defun put-string (s)
(write-string s *output*))
@ -79,17 +94,6 @@
((:id :class) (util:to-string val :lower-case t))
(t (util:to-string val))))
(defun start (tag &optional attrs)
(put-char #\<)
(put-string tag)
(put-attrs attrs)
(put-char #\>))
(defun end (tag)
(put-string "</")
(put-string tag)
(put-char #\>))
(defun newline ()
(put-char #\Newline))