web/dom: render link (<a>) with attributes OK
This commit is contained in:
parent
84804f852a
commit
ead8f6eb80
3 changed files with 33 additions and 10 deletions
|
@ -26,6 +26,7 @@
|
||||||
(core:setup-services)
|
(core:setup-services)
|
||||||
(let ((server (core:find-service :server))
|
(let ((server (core:find-service :server))
|
||||||
(client (core:find-service :client)))
|
(client (core:find-service :client)))
|
||||||
|
(test-dom)
|
||||||
(test-server-config server)
|
(test-server-config server)
|
||||||
(sleep 0.1)
|
(sleep 0.1)
|
||||||
(test-fileserver client)
|
(test-fileserver client)
|
||||||
|
@ -37,7 +38,7 @@
|
||||||
(deftest test-dom ()
|
(deftest test-dom ()
|
||||||
(== (dom:render
|
(== (dom:render
|
||||||
(dom:link '(:href "https://example.com"
|
(dom:link '(:href "https://example.com"
|
||||||
:title "Demo" :class '(:demo-link :plain))
|
:title "Demo" :class (:demo-link :plain))
|
||||||
(dom:text "Link to example.com")))
|
(dom:text "Link to example.com")))
|
||||||
"<a href=\"https://example.com\" title=\"Demo\" class=\"demo-link plain\">Link to example.com</a>"))
|
"<a href=\"https://example.com\" title=\"Demo\" class=\"demo-link plain\">Link to example.com</a>"))
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
(defpackage :scopes/util
|
(defpackage :scopes/util
|
||||||
(:use :common-lisp)
|
(:use :common-lisp)
|
||||||
(:export #:flatten-str #:to-keyword
|
(:export #:flatten-str #:to-keyword #:to-string
|
||||||
#:absolute-dir #:check-dir #:ensure-dir #:home-path #:path-from-string
|
#:absolute-dir #:check-dir #:ensure-dir #:home-path #:path-from-string
|
||||||
#:relative-path #:runtime-path #:system-path))
|
#:relative-path #:runtime-path #:system-path))
|
||||||
|
|
||||||
|
@ -13,6 +13,11 @@
|
||||||
(defun flatten-str (s &key (with " "))
|
(defun flatten-str (s &key (with " "))
|
||||||
(str:join with (str:lines s)))
|
(str:join with (str:lines s)))
|
||||||
|
|
||||||
|
(defun to-string (k)
|
||||||
|
(if (atom k)
|
||||||
|
(string-downcase k)
|
||||||
|
(str:join " " (mapcar #'string-downcase k))))
|
||||||
|
|
||||||
(defun to-keyword (s)
|
(defun to-keyword (s)
|
||||||
(intern (string-upcase s) :keyword))
|
(intern (string-upcase s) :keyword))
|
||||||
|
|
||||||
|
|
31
web/dom.lisp
31
web/dom.lisp
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
(defpackage :scopes/web/dom
|
(defpackage :scopes/web/dom
|
||||||
(:use :common-lisp)
|
(:use :common-lisp)
|
||||||
(:local-nicknames (:alx :alexandria))
|
(:local-nicknames (:util :scopes/util)
|
||||||
|
(:alx :alexandria))
|
||||||
(:export #:render #:text
|
(:export #:render #:text
|
||||||
#:dl #:link))
|
#:dl #:link))
|
||||||
|
|
||||||
|
@ -30,20 +31,36 @@
|
||||||
`(progn
|
`(progn
|
||||||
(start ,tag ,attrs)
|
(start ,tag ,attrs)
|
||||||
,@body
|
,@body
|
||||||
(end ,tag t)))
|
(end ,tag)))
|
||||||
|
|
||||||
|
(defun put-attrs (plist)
|
||||||
|
(loop for (key val . r) on plist by #'cddr do
|
||||||
|
(put-char #\Space)
|
||||||
|
(when val
|
||||||
|
(put-string (string-downcase key))
|
||||||
|
(when (not (eq val t))
|
||||||
|
(put-string "=\"")
|
||||||
|
(put-string (attr-str key val))
|
||||||
|
(put-char #\")))))
|
||||||
|
|
||||||
|
(defun attr-str (key val)
|
||||||
|
(case key
|
||||||
|
((:id :class) (util:to-string val))
|
||||||
|
(t (string val))))
|
||||||
|
|
||||||
(defun start (tag &optional attrs)
|
(defun start (tag &optional attrs)
|
||||||
(put-char #\<)
|
(put-char #\<)
|
||||||
(put-string tag)
|
(put-string tag)
|
||||||
;(put-attrs attrs)
|
(put-attrs attrs)
|
||||||
(put-char #\>))
|
(put-char #\>))
|
||||||
|
|
||||||
(defun end (tag &optional newline)
|
(defun end (tag)
|
||||||
(put-string "</")
|
(put-string "</")
|
||||||
(put-string tag)
|
(put-string tag)
|
||||||
(put-char #\>)
|
(put-char #\>))
|
||||||
(if newline
|
|
||||||
(put-char #\Newline)))
|
(defun newline ()
|
||||||
|
(put-char #\Newline))
|
||||||
|
|
||||||
;;;; tag-specific renderers
|
;;;; tag-specific renderers
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue