Skip to content

Commit

Permalink
feat(runtime): allow to configure un-cloak (#1693)
Browse files Browse the repository at this point in the history
  • Loading branch information
chu121su12 committed Oct 8, 2022
1 parent a8d4db6 commit 88475a8
Showing 1 changed file with 13 additions and 7 deletions.
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

0 comments on commit 88475a8

Please sign in to comment.