From f0a9b9634523580c93d7916347bf18c0172b711b Mon Sep 17 00:00:00 2001 From: paku Date: Fri, 9 Feb 2024 19:10:41 +0900 Subject: [PATCH] Support boolean attributes --- src/elements.lisp | 14 +++++++++++++- t/piccolo.lisp | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/elements.lisp b/src/elements.lisp index c459b33..fe539ec 100644 --- a/src/elements.lisp +++ b/src/elements.lisp @@ -191,9 +191,21 @@ style sub summary sup svg table tbody td template textarea tfoot th thead |time| title tr track u ul var video wbr) +(defparameter *boolean-attrs* + '(allowfullscreen async autofocus autoplay checked controls default defer + disabled formnovalidate inert ismap itemscope loop multiple muted nomodule + novalidate open playsinline readonly required reversed selected)) + (defmethod print-object ((attrs attrs) stream) (if (attrs-alist attrs) - (format stream " ~{~a=~s~^ ~}" (util:alist-plist (attrs-alist attrs))) + (let ((alist (attrs-alist attrs))) + (dolist (pair alist) + (let ((key (car pair)) + (value (cdr pair))) + (if (member key *boolean-attrs* :test #'string=) + (when value + (format stream " ~a" (string-downcase key))) + (format stream " ~a=~s" (string-downcase key) value))))) (format stream ""))) (defparameter *self-closing-tags* diff --git a/t/piccolo.lisp b/t/piccolo.lisp index b59cdb3..86df663 100644 --- a/t/piccolo.lisp +++ b/t/piccolo.lisp @@ -148,6 +148,14 @@ (is (string= "
some text
some other text
" (elem-str div4))))) +(test boolean-attrs + (let ((script1 (script :defer t :data-domain "example.com" :src "example.com/script.js")) + (script2 (script :defer nil :data-domain "example.com" :src "example.com/script.js"))) + (is (string= "" + (element-string script1))) + (is (string= "" + (element-string script2))))) + (in-suite escape) (defparameter *a-attrs*