diff --git a/test/test-web.lisp b/test/test-web.lisp index 5079da1..07f010c 100644 --- a/test/test-web.lisp +++ b/test/test-web.lisp @@ -49,6 +49,7 @@ (== parsed '(((:a :href "https://example.com") "Link"))) ;(== (dom:from-list parsed) ((dom:a '(:href "https://example.com") "Link"))) (== (dom:render (dom:from-list parsed)) inp) + (== (dom:to-list (dom:a '(:href "https://example.com") "Link")) (car parsed)) )) (deftest test-dom () diff --git a/web/dom.lisp b/web/dom.lisp index c8f5c08..def37fc 100644 --- a/web/dom.lisp +++ b/web/dom.lisp @@ -10,7 +10,7 @@ #:dlist #:a #:dd #:div #:dl #:dt #:label #:br #:input - #:parse #:from-list)) + #:parse #:from-list #:to-list)) (in-package :scopes/web/dom) (defvar *this* *package*) @@ -147,8 +147,6 @@ ;;;; conversions (for testing or manipulation of HTML using dom) -(defun to-list (&rest elems)) - (defun parse (html) (hp:parse-html html)) @@ -167,3 +165,13 @@ (setf attr (cdr tag) tag (car tag))) (apply (sym tag) attr (from-list (cdr part)))))))) +(defgeneric to-list (s) + (:method ((s string)) s) + (:method ((s symbol)) s) + (:method ((s cons)) (reverse (mapcar #'to-list s)))) + + (defmethod to-list ((el xml-element)) + (let* ((attrs (attrs el)) + (tag (util:to-keyword (tag el))) + (head (if attrs (cons tag attrs) tag))) + (cons head (to-list (body el)))))