Minify props string

This commit is contained in:
Akira Tempaku 2024-10-03 09:54:43 +09:00
commit b430b42699
5 changed files with 61 additions and 33 deletions

View file

@ -1,8 +1,9 @@
(defpackage #:hsx/element
(:use #:cl)
(:import-from #:hsx/escaper
(:import-from #:hsx/utils
#:escape-html-attribute
#:escape-html-text-content)
#:escape-html-text-content
#:minify)
(:import-from #:hsx/group
#:self-closing-tag-p
#:non-escaping-tag-p)
@ -118,18 +119,19 @@
(string-downcase (element-type element)))
(defmethod render-props ((element tag))
(with-output-to-string (stream)
(loop
:for (key value) :on (element-props element) :by #'cddr
:do (let ((key-str (string-downcase key)))
(if (typep value 'boolean)
(format stream
"~@[ ~a~]"
(and value key-str))
(format stream
" ~a=\"~a\""
key-str
(escape-html-attribute value)))))))
(minify
(with-output-to-string (stream)
(loop
:for (key value) :on (element-props element) :by #'cddr
:do (let ((key-str (string-downcase key)))
(if (typep value 'boolean)
(format stream
"~@[ ~a~]"
(and value key-str))
(format stream
" ~a=\"~a\""
key-str
(escape-html-attribute value))))))))
(defmethod render-children ((element tag))
(mapcar (lambda (child)

View file

@ -1,10 +1,11 @@
(defpackage #:hsx/escaper
(defpackage #:hsx/utils
(:use #:cl)
(:import-from #:alexandria
#:alist-hash-table)
(:export #:escape-html-attribute
#:escape-html-text-content))
(in-package #:hsx/escaper)
#:escape-html-text-content
#:minify))
(in-package #:hsx/utils)
(defparameter *text-content-escape-map*
(alist-hash-table
@ -22,7 +23,7 @@
'((#\" . """))))
(defun escape-char (char escape-map)
(or (gethash char escape-map)
(or (gethash char escape-map)
char))
(defun escape-string (string escape-map)
@ -38,3 +39,19 @@
(defun escape-html-attribute (text)
(escape-string text *attribute-escape-map*))
(defun minify (input-string)
(with-output-to-string (out)
(let ((previous-space-p nil))
(loop for char across input-string do
(cond
((whitespace-p char)
(unless previous-space-p
(write-char #\Space out))
(setf previous-space-p t))
(t
(write-char char out)
(setf previous-space-p nil)))))))
(defun whitespace-p (char)
(member char '(#\Space #\Newline #\Tab #\Return) :test #'char=))