Minify props string
This commit is contained in:
parent
6f8df3e00d
commit
b430b42699
5 changed files with 61 additions and 33 deletions
|
@ -2,8 +2,8 @@
|
||||||
:class :package-inferred-system
|
:class :package-inferred-system
|
||||||
:pathname "tests"
|
:pathname "tests"
|
||||||
:depends-on ("rove"
|
:depends-on ("rove"
|
||||||
|
"hsx-test/utils"
|
||||||
"hsx-test/element"
|
"hsx-test/element"
|
||||||
"hsx-test/escaper"
|
|
||||||
"hsx-test/group"
|
"hsx-test/group"
|
||||||
"hsx-test/hsx")
|
"hsx-test/hsx")
|
||||||
:perform (test-op (o c) (symbol-call :rove :run c :style :dot)))
|
:perform (test-op (o c) (symbol-call :rove :run c :style :dot)))
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
(defpackage #:hsx/element
|
(defpackage #:hsx/element
|
||||||
(:use #:cl)
|
(:use #:cl)
|
||||||
(:import-from #:hsx/escaper
|
(:import-from #:hsx/utils
|
||||||
#:escape-html-attribute
|
#:escape-html-attribute
|
||||||
#:escape-html-text-content)
|
#:escape-html-text-content
|
||||||
|
#:minify)
|
||||||
(:import-from #:hsx/group
|
(:import-from #:hsx/group
|
||||||
#:self-closing-tag-p
|
#:self-closing-tag-p
|
||||||
#:non-escaping-tag-p)
|
#:non-escaping-tag-p)
|
||||||
|
@ -118,18 +119,19 @@
|
||||||
(string-downcase (element-type element)))
|
(string-downcase (element-type element)))
|
||||||
|
|
||||||
(defmethod render-props ((element tag))
|
(defmethod render-props ((element tag))
|
||||||
(with-output-to-string (stream)
|
(minify
|
||||||
(loop
|
(with-output-to-string (stream)
|
||||||
:for (key value) :on (element-props element) :by #'cddr
|
(loop
|
||||||
:do (let ((key-str (string-downcase key)))
|
:for (key value) :on (element-props element) :by #'cddr
|
||||||
(if (typep value 'boolean)
|
:do (let ((key-str (string-downcase key)))
|
||||||
(format stream
|
(if (typep value 'boolean)
|
||||||
"~@[ ~a~]"
|
(format stream
|
||||||
(and value key-str))
|
"~@[ ~a~]"
|
||||||
(format stream
|
(and value key-str))
|
||||||
" ~a=\"~a\""
|
(format stream
|
||||||
key-str
|
" ~a=\"~a\""
|
||||||
(escape-html-attribute value)))))))
|
key-str
|
||||||
|
(escape-html-attribute value))))))))
|
||||||
|
|
||||||
(defmethod render-children ((element tag))
|
(defmethod render-children ((element tag))
|
||||||
(mapcar (lambda (child)
|
(mapcar (lambda (child)
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
(defpackage #:hsx/escaper
|
(defpackage #:hsx/utils
|
||||||
(:use #:cl)
|
(:use #:cl)
|
||||||
(:import-from #:alexandria
|
(:import-from #:alexandria
|
||||||
#:alist-hash-table)
|
#:alist-hash-table)
|
||||||
(:export #:escape-html-attribute
|
(:export #:escape-html-attribute
|
||||||
#:escape-html-text-content))
|
#:escape-html-text-content
|
||||||
(in-package #:hsx/escaper)
|
#:minify))
|
||||||
|
(in-package #:hsx/utils)
|
||||||
|
|
||||||
(defparameter *text-content-escape-map*
|
(defparameter *text-content-escape-map*
|
||||||
(alist-hash-table
|
(alist-hash-table
|
||||||
|
@ -22,7 +23,7 @@
|
||||||
'((#\" . """))))
|
'((#\" . """))))
|
||||||
|
|
||||||
(defun escape-char (char escape-map)
|
(defun escape-char (char escape-map)
|
||||||
(or (gethash char escape-map)
|
(or (gethash char escape-map)
|
||||||
char))
|
char))
|
||||||
|
|
||||||
(defun escape-string (string escape-map)
|
(defun escape-string (string escape-map)
|
||||||
|
@ -38,3 +39,19 @@
|
||||||
|
|
||||||
(defun escape-html-attribute (text)
|
(defun escape-html-attribute (text)
|
||||||
(escape-string text *attribute-escape-map*))
|
(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=))
|
|
@ -1,14 +0,0 @@
|
||||||
(defpackage #:hsx-test/escaper
|
|
||||||
(:use #:cl
|
|
||||||
#:rove
|
|
||||||
#:hsx/escaper))
|
|
||||||
(in-package #:hsx-test/escaper)
|
|
||||||
|
|
||||||
(deftest escaper-test
|
|
||||||
(testing "escape-html-attribute"
|
|
||||||
(ok (string= ""foo""
|
|
||||||
(escape-html-attribute "\"foo\""))))
|
|
||||||
|
|
||||||
(testing "escape-html-text-content"
|
|
||||||
(ok (string= "&<>"'/`="
|
|
||||||
(escape-html-text-content "&<>\"'/`=")))))
|
|
23
tests/utils.lisp
Normal file
23
tests/utils.lisp
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
(defpackage #:hsx-test/utils
|
||||||
|
(:use #:cl
|
||||||
|
#:rove
|
||||||
|
#:hsx/utils))
|
||||||
|
(in-package #:hsx-test/utils)
|
||||||
|
|
||||||
|
(deftest text-util-test
|
||||||
|
(testing "escape-html-attribute"
|
||||||
|
(ok (string= ""foo""
|
||||||
|
(escape-html-attribute "\"foo\""))))
|
||||||
|
|
||||||
|
(testing "escape-html-text-content"
|
||||||
|
(ok (string= "&<>"'/`="
|
||||||
|
(escape-html-text-content "&<>\"'/`="))))
|
||||||
|
|
||||||
|
(testing "minify"
|
||||||
|
;; Test with Alpine.js
|
||||||
|
(ok (string= (minify "{
|
||||||
|
open: false,
|
||||||
|
get isOpen() { return this.open },
|
||||||
|
toggle() { this.open = ! this.open },
|
||||||
|
}")
|
||||||
|
"{ open: false, get isOpen() { return this.open }, toggle() { this.open = ! this.open }, }"))))
|
Loading…
Reference in a new issue