/*{"parent": "binding-to-the-dom", "order": 5, "title": "Bindings in Markup", "description": "enableDeclarativeBinding(): opt-in, closed by default — bind HTML with data-tosi-* attributes and no script, against an explicit list of what markup may read, write and call."}*/
/*#
# Bindings in Markup (enableDeclarativeBinding)
Markup that cannot carry a script — a model's output, a saved view, a
server-rendered fragment, a doc page under a strict CSP — can still be live.
`enableDeclarativeBinding()` watches a root and turns `data-tosi-*`
attributes into ordinary `bind()` and `on()` calls. Nothing is evaluated: an
attribute's value is a state path, checked against what you granted.
It is **opt-in and closed by default.** With no grants nothing binds; it is
not in `tosijs/core` or the `')
)
await new Promise((resolve) => setTimeout(resolve, 0))
await updates()
expect(window.declTestRan).toBe(undefined)
expect(host.querySelector('script').textContent).toBe('')
binding.disable()
})
```
*/
import { bind, on, delegationRoot } from './bind'
import { bindings } from './bindings'
import { tosiPath } from './metadata'
import { extendsPath, overlapsPath } from './path-listener'
import { getByPath, isUnsafeKey } from './by-path'
import { registry } from './registry'
import { settings } from './settings'
import type { TosiBinding } from './xin-types'
export type DeclarativeGrant =
| Array
| ((path: string) => boolean)
export interface DeclarativeBindingOptions {
/** paths (or proxies) markup may show */
read?: DeclarativeGrant
/** paths a two-way binding may write */
write?: DeclarativeGrant
/** functions markup may call, and paths trusted to act (href, src, …) */
actions?: DeclarativeGrant
/** where to look; defaults to document.body */
root?: Element
}
export interface DeclarativeBinding {
/** bind the data-tosi-* attributes on an element and its descendants */
hydrate(element: Element): void
/** stop watching for inserted markup; bindings already made stay */
disable(): void
}
const PREFIX = 'data-tosi-'
const PATH = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\[\d+\])*$/
const INERT_ATTRS =
/^(class|title|hidden|disabled|checked|value|role|alt|placeholder|aria-.+)$/
const INERT_PROPS = new Set([
'textContent',
'value',
'checked',
'disabled',
'hidden',
'title',
'className',
])
const NEVER = /^(on.*|innerhtml|outerhtml|srcdoc)$/i
// their text is code: an empty parser-inserted