mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-21 10:53:33 +08:00
feat(forms): disable browser autocomplete by default
This commit is contained in:
parent
30820d3031
commit
15e4c3d427
29
__test__/app/autocomplete.spec.ts
Normal file
29
__test__/app/autocomplete.spec.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { installAutocompleteDefaults } from '@/app-components/provider/AppAutocompleteDefaults'
|
||||
|
||||
describe('autocomplete defaults', () => {
|
||||
it('handles present and newly mounted controls and respects explicit opt-ins', async () => {
|
||||
const root = document.createElement('div')
|
||||
|
||||
root.innerHTML =
|
||||
'<input><input autocomplete="email"><form autocomplete="on"><textarea></textarea></form>'
|
||||
const stop = installAutocompleteDefaults(root)
|
||||
|
||||
try {
|
||||
expect(root.querySelector('input')?.autocomplete).toBe('off')
|
||||
expect(root.querySelector('input[autocomplete=email]')).not.toBeNull()
|
||||
expect(root.querySelector('textarea')?.hasAttribute('autocomplete')).toBe(
|
||||
false,
|
||||
)
|
||||
|
||||
const portal = document.createElement('div')
|
||||
|
||||
portal.innerHTML = '<input type="search"><textarea></textarea>'
|
||||
root.append(portal)
|
||||
await new Promise((resolve) => setTimeout(resolve, 0))
|
||||
expect(portal.querySelector('input')?.autocomplete).toBe('off')
|
||||
expect(portal.querySelector('textarea')?.autocomplete).toBe('off')
|
||||
} finally {
|
||||
stop()
|
||||
}
|
||||
})
|
||||
})
|
||||
32
src/app-components/provider/AppAutocompleteDefaults/index.ts
Normal file
32
src/app-components/provider/AppAutocompleteDefaults/index.ts
Normal file
@ -0,0 +1,32 @@
|
||||
const controls =
|
||||
'input:not([type="hidden"],[type="checkbox"],[type="radio"],[type="button"],[type="submit"],[type="file"],[type="reset"]), textarea, select'
|
||||
|
||||
export function installAutocompleteDefaults(root: HTMLElement) {
|
||||
const apply = (element: Element) => {
|
||||
if (element.hasAttribute('autocomplete')) return
|
||||
|
||||
const form = (element as HTMLInputElement).form
|
||||
|
||||
if (form?.getAttribute('autocomplete') === 'on') return
|
||||
|
||||
element.setAttribute('autocomplete', 'off')
|
||||
}
|
||||
|
||||
const applyToTree = (node: Node) => {
|
||||
if (!(node instanceof Element)) return
|
||||
|
||||
if (node.matches(controls)) apply(node)
|
||||
|
||||
node.querySelectorAll(controls).forEach(apply)
|
||||
}
|
||||
|
||||
applyToTree(root)
|
||||
// 只检查新增子树,同时覆盖 Teleport 到 body 的搜索框、弹窗和下拉面板。
|
||||
const observer = new MutationObserver((mutations) => {
|
||||
for (const mutation of mutations) mutation.addedNodes.forEach(applyToTree)
|
||||
})
|
||||
|
||||
observer.observe(root, { childList: true, subtree: true })
|
||||
|
||||
return () => observer.disconnect()
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
import { call, unrefElement } from '@/utils'
|
||||
import { call } from '@/utils'
|
||||
import { useEventListener } from '@vueuse/core'
|
||||
import { NForm, NSpin } from 'naive-ui'
|
||||
import { useTemplateRef } from 'vue'
|
||||
@ -42,16 +42,6 @@ export default defineComponent({
|
||||
if (onRegister && formRef.value) {
|
||||
call(onRegister, formRef.value)
|
||||
}
|
||||
|
||||
if (formRef.value) {
|
||||
const formElement = unrefElement(
|
||||
formRef.value as unknown as HTMLFormElement,
|
||||
)
|
||||
|
||||
if (formElement) {
|
||||
formElement.autocomplete = props.autocomplete
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
expose()
|
||||
@ -62,7 +52,7 @@ export default defineComponent({
|
||||
},
|
||||
render() {
|
||||
const { $attrs, $props, $slots } = this
|
||||
const { loading, loadingDescription, ...restProps } = $props
|
||||
const { loading, loadingDescription, autocomplete, ...restProps } = $props
|
||||
|
||||
return (
|
||||
<NSpin
|
||||
@ -72,7 +62,7 @@ export default defineComponent({
|
||||
height: 'auto',
|
||||
}}
|
||||
>
|
||||
<NForm {...$attrs} {...restProps} ref="formRef">
|
||||
<NForm {...$attrs} {...restProps} {...{ autocomplete }} ref="formRef">
|
||||
{{
|
||||
...$slots,
|
||||
}}
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
import type { AnyFn, MaybeArray } from '@/types'
|
||||
import { omit } from 'lodash-es'
|
||||
import { formProps } from 'naive-ui'
|
||||
import type { RFormInst } from './types'
|
||||
|
||||
const props = {
|
||||
...omit(formProps, ['onSubmit']),
|
||||
...formProps,
|
||||
/**
|
||||
*
|
||||
* @description
|
||||
|
||||
@ -6,6 +6,7 @@ import 'vue3-next-qrcode/es/style.css' // vue3-next-qrcode 样式
|
||||
import 'virtual:uno.css'
|
||||
import type { App as AppType } from 'vue'
|
||||
import { setupRayTemplateCore } from './__ray-template'
|
||||
import { installAutocompleteDefaults } from './app-components/provider/AppAutocompleteDefaults'
|
||||
import { setupDayjs } from './dayjs'
|
||||
import { setupDirectives } from './directives'
|
||||
import { setupI18n } from './locales'
|
||||
@ -27,6 +28,7 @@ const setupPlugins = async (inst: AppType<Element>) => {
|
||||
await setupRouter(inst)
|
||||
setupDayjs()
|
||||
setupDirectives(inst)
|
||||
inst.onUnmount(installAutocompleteDefaults(document.body))
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user