diff --git a/test/test-core.lisp b/test/test-core.lisp index 029af08..d202d55 100644 --- a/test/test-core.lisp +++ b/test/test-core.lisp @@ -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 () diff --git a/web/dom.lisp b/web/dom.lisp index a8c896e..b0c762e 100644 --- a/web/dom.lisp +++ b/web/dom.lisp @@ -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 ")) + (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 ")) - (defun newline () (put-char #\Newline))