Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): allow to configure un-cloak #1693

Merged
merged 1 commit into from Oct 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/runtime/src/index.ts
Expand Up @@ -12,6 +12,11 @@ export interface RuntimeOptions {
* @default false
*/
autoPrefix?: boolean
/**
* Attribute to use as cloaking
* @default 'un-cloak'
*/
cloakAttribute?: string
/**
* Callback to modify config
*/
Expand Down Expand Up @@ -88,6 +93,7 @@ export default function init(inlineConfig: RuntimeOptions = {}) {
const config = window.__unocss || {}
const runtime = config?.runtime
const defaultConfig = Object.assign(inlineConfig.defaults || {}, runtime)
const cloakAttribute = defaultConfig.cloakAttribute ?? 'un-cloak'
if (runtime?.autoPrefix) {
let postprocess = defaultConfig.postprocess
if (!postprocess)
Expand Down Expand Up @@ -124,10 +130,10 @@ export default function init(inlineConfig: RuntimeOptions = {}) {
if (node.nodeType !== 1)
return
const el = node as Element
if (el.hasAttribute('un-cloak'))
el.removeAttribute('un-cloak')
el.querySelectorAll('[un-cloak]').forEach((n) => {
n.removeAttribute('un-cloak')
if (el.hasAttribute(cloakAttribute))
el.removeAttribute(cloakAttribute)
el.querySelectorAll(`[${cloakAttribute}]`).forEach((n) => {
n.removeAttribute(cloakAttribute)
})
}

Expand Down Expand Up @@ -179,15 +185,15 @@ export default function init(inlineConfig: RuntimeOptions = {}) {
else {
if (inspector && !inspector(target))
return
if (mutation.attributeName !== 'un-cloak') {
if (mutation.attributeName !== cloakAttribute) {
const attrs = Array.from(target.attributes)
.map(i => i.value ? `${i.name}="${i.value}"` : i.name)
.join(' ')
const tag = `<${target.tagName.toLowerCase()} ${attrs}>`
await extract(tag)
}
if (target.hasAttribute('un-cloak'))
target.removeAttribute('un-cloak')
if (target.hasAttribute(cloakAttribute))
target.removeAttribute(cloakAttribute)
}
})
})
Expand Down