add dom:to-list for directly converting dom elements to nested list format
This commit is contained in:
parent
e507bf0c1f
commit
a4c87d4845
2 changed files with 12 additions and 3 deletions
|
@ -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 ()
|
||||
|
|
14
web/dom.lisp
14
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)))))
|
||||
|
|
Loading…
Add table
Reference in a new issue