-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
28 lines (25 loc) · 1.05 KB
/
util.js
File metadata and controls
28 lines (25 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { htmlEscape } from 'escape-goat'
import stringifyAttrs from 'stringify-attributes'
export const voidTags = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr']
const isObject = v => v !== null && typeof v === 'object'
export const handleArgs = ([tag, attrsOrChildren, maybeChildren, opts]) => {
const isVoid = voidTags.includes(tag)
let attrs, children
if (isVoid) attrs = attrsOrChildren
else if (!maybeChildren) {
if (isObject(attrsOrChildren) && !Array.isArray(attrsOrChildren)) attrs = attrsOrChildren
else children = attrsOrChildren
} else {
attrs = attrsOrChildren
children = maybeChildren
}
attrs ||= ''
children ||= ''
if (Array.isArray(children)) children = children.flat(Infinity).filter(Boolean).join('')
if (opts?.escape) children = htmlEscape(children)
if (attrs) {
if (!isObject(attrs)) throw 'h() received non-object attributes - attrs must be an object'
attrs = stringifyAttrs(attrs)
}
return { attrs, children, isVoid }
}