forked from CoreBunch/Instatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlAttributes.ts
More file actions
43 lines (37 loc) · 1.36 KB
/
Copy pathhtmlAttributes.ts
File metadata and controls
43 lines (37 loc) · 1.36 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import type { PropertyControl } from '@core/module-engine'
import {
normalizeHtmlAttributeName,
sanitizeRenderableHtmlAttribute,
} from '@core/htmlAttributes'
import { escapeHtml } from '@modules/base/utils/escape'
export const HtmlAttributesPropSchemaOptions = { default: {} } as const
export function htmlAttributesControl(): PropertyControl {
return {
type: 'group',
label: 'HTML attributes',
hidden: true,
children: {},
}
}
function normalizeHtmlAttributes(value: unknown): Record<string, string> {
if (!value || typeof value !== 'object' || Array.isArray(value)) return {}
const attrs: Record<string, string> = {}
for (const [rawName, rawValue] of Object.entries(value as Record<string, unknown>)) {
if (typeof rawValue !== 'string') continue
const name = normalizeHtmlAttributeName(rawName)
const safeValue = sanitizeRenderableHtmlAttribute(name, rawValue)
if (safeValue === null) continue
attrs[name] = safeValue
}
return attrs
}
export function htmlAttributesAttr(value: unknown): string {
const attrs = normalizeHtmlAttributes(value)
return Object.entries(attrs)
.sort(([a], [b]) => a.localeCompare(b))
.map(([name, attrValue]) => ` ${name}="${escapeHtml(attrValue)}"`)
.join('')
}
export function htmlAttributesForReact(value: unknown): Record<string, string> {
return normalizeHtmlAttributes(value)
}