From d4275d69676c8186d1d54daa1bfdd3964a3ee8f5 Mon Sep 17 00:00:00 2001 From: paku Date: Sat, 1 Jun 2024 20:14:17 +0900 Subject: [PATCH] Add group package --- hsx-test.asd | 8 +++++--- src/element.lisp | 6 +++++- src/group.lisp | 26 ++++++++++++++++++++++++++ tests/group.lisp | 16 ++++++++++++++++ tests/renderer.lisp | 4 ++++ 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/group.lisp create mode 100644 tests/group.lisp diff --git a/hsx-test.asd b/hsx-test.asd index 3498900..3840f58 100644 --- a/hsx-test.asd +++ b/hsx-test.asd @@ -7,10 +7,12 @@ "hsx-test/defhsx" "hsx-test/hsx" "hsx-test/escaper" - "hsx-test/renderer") + "hsx-test/renderer" + "hsx-test/group") :test-names ((#:element-test . #:hsx-test/element) (#:defhsx-test . #:hsx-test/defhsx) (#:hsx-test . #:hsx-test/hsx) (#:escaper-test . #:hsx-test/escaper) - (#:renderer-test . #:hsx-test/renderer)) - :num-checks 37) + (#:renderer-test . #:hsx-test/renderer) + (#:group-test . #:hsx-test/group)) + :num-checks 41) diff --git a/src/element.lisp b/src/element.lisp index 31accf3..bebe50a 100644 --- a/src/element.lisp +++ b/src/element.lisp @@ -3,6 +3,8 @@ (:import-from #:hsx/escaper #:escape-html-attribute #:escape-html-text-content) + (:import-from #:hsx/group + #:self-closing-tag-p) (:export #:element #:tag #:html-tag @@ -84,7 +86,9 @@ children) type-str) (format stream - "<~a~a>" + (if (self-closing-tag-p type) + "<~a~a>" + "<~a~a>") type-str (props->string props) type-str))))) diff --git a/src/group.lisp b/src/group.lisp new file mode 100644 index 0000000..7b058fe --- /dev/null +++ b/src/group.lisp @@ -0,0 +1,26 @@ +(defpackage #:hsx/group + (:use #:cl) + (:import-from #:alexandria + #:make-keyword + #:symbolicate) + (:export #:defgroup + #:self-closing-tag-p)) +(in-package #:hsx/group) + +(defun make-keyword-hash-table (symbols) + (let ((ht (make-hash-table))) + (mapcar (lambda (sym) + (setf (gethash (make-keyword sym) ht) t)) + symbols) + ht)) + +(defmacro defgroup (name &body symbols) + (let ((p-name (symbolicate '* name '*))) + `(progn + (defparameter ,p-name (make-keyword-hash-table ',symbols)) + (defun ,(symbolicate name '-p) (keyword) + (gethash keyword ,p-name))))) + +(defgroup self-closing-tag + area base br col embed hr img input keygen + link meta param source track wbr) diff --git a/tests/group.lisp b/tests/group.lisp new file mode 100644 index 0000000..da5b371 --- /dev/null +++ b/tests/group.lisp @@ -0,0 +1,16 @@ +(defpackage #:hsx-test/group + (:use #:cl + #:fiveam + #:hsx/group)) +(in-package #:hsx-test/group) + +(def-suite group-test) +(in-suite group-test) + +(defgroup fruit + apple banana orange) + +(test defgroup + (is (hash-table-p *fruit*)) + (is (fruit-p :apple)) + (is (not (fruit-p :tomato)))) diff --git a/tests/renderer.lisp b/tests/renderer.lisp index 68ccf9e..90353c6 100644 --- a/tests/renderer.lisp +++ b/tests/renderer.lisp @@ -48,6 +48,10 @@ "foo" (span "bar")))))) +(test self-closing-tag + (is (string= "" + (render (img :src "/background.png"))))) + (test fragment (let ((frg (<> (li "bar")