-
Notifications
You must be signed in to change notification settings - Fork 64
feat: adds directive and property to disable password managers #1346
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
22a5679
feat: adds directive that disables known password managers
iOvergaard 242eca3
test: adds test for new directive
iOvergaard 5ed7be9
feat: adds property to disable password managers on built-in form fie…
iOvergaard 6a09118
fix: makes disable-password-managers directive reversible and extendable
iOvergaard 8c09c9e
fix: address review issues in disable-password-managers implementation
iOvergaard 15bf3b0
Potential fix for pull request finding
iOvergaard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
packages/uui-base/lib/directives/disable-password-managers.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { noChange, nothing } from 'lit'; | ||
| import type { ElementPart } from 'lit'; | ||
| import { | ||
| directive, | ||
| Directive, | ||
| type DirectiveParameters, | ||
| PartInfo, | ||
| PartType, | ||
| } from 'lit/directive.js'; | ||
|
|
||
| /** | ||
| * The directive applies or removes attributes to disable known and commonly used password managers on the given element. | ||
| * | ||
| * The `attributes` field is `protected` and `readonly` to allow subclasses to extend the list of | ||
| * managed attributes without replacing the base implementation. Each entry must have a `name` and `value` | ||
| * that together signal to a password manager that it should ignore the element. | ||
| * | ||
| * To add support for additional password managers, subclass and redeclare `attributes` with the | ||
| * full list (base entries + any new ones): | ||
| * @example | ||
| * ```ts | ||
| * class MyDirective extends UUIDisablePasswordManagersDirective { | ||
| * protected override readonly attributes = [ | ||
| * { name: 'data-1p-ignore', value: '' }, | ||
| * { name: 'data-bwignore', value: '' }, | ||
| * { name: 'data-form-type', value: 'other' }, | ||
| * { name: 'data-lpignore', value: 'true' }, | ||
| * { name: 'data-custom-ignore', value: 'true' }, // additional manager | ||
| * ]; | ||
| * } | ||
| * export const myDisablePasswordManagers = directive(MyDirective); | ||
| * ``` | ||
| */ | ||
| export class UUIDisablePasswordManagersDirective extends Directive { | ||
| /** | ||
| * The list of attributes to apply or remove on the target element. | ||
| * Override this in a subclass to extend support for additional password managers. | ||
| */ | ||
| protected readonly attributes: ReadonlyArray<{ name: string; value: string }> = | ||
| Object.freeze([ | ||
| { name: 'data-1p-ignore', value: '' }, // 1Password | ||
| { name: 'data-bwignore', value: '' }, // Bitwarden | ||
| { name: 'data-form-type', value: 'other' }, // Dashlane | ||
| { name: 'data-lpignore', value: 'true' }, // LastPass | ||
| ]); | ||
|
|
||
| constructor(partInfo: PartInfo) { | ||
| super(partInfo); | ||
| if (partInfo.type !== PartType.ELEMENT) { | ||
| throw new Error( | ||
| 'The `uuiDisablePasswordManagers` directive can only be used in element parts', | ||
|
iOvergaard marked this conversation as resolved.
|
||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The directive does not render any content. | ||
| * @returns `nothing` to indicate that the directive does not render any content. | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| override render(_enabled: boolean) { | ||
| return nothing; | ||
| } | ||
|
|
||
| /** | ||
| * Applies or removes password manager ignore attributes on the element depending on `enabled`. | ||
| * Calling with `false` removes all managed attributes so toggling the property is fully reversible. | ||
| * @param part - The element part where the directive is applied. | ||
| * @param enabled - When `true`, sets the ignore attributes; when `false`, removes them. | ||
| * @returns `noChange` to indicate that no further updates are needed for this part. | ||
| */ | ||
| override update(part: ElementPart, [enabled]: DirectiveParameters<this>) { | ||
| this.attributes.forEach(attr => { | ||
| if (enabled) { | ||
| part.element.setAttribute(attr.name, attr.value); | ||
| } else { | ||
| part.element.removeAttribute(attr.name); | ||
| } | ||
| }); | ||
| return noChange; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A Lit directive which applies or removes attributes to disable known and commonly used password managers on an element. | ||
| * Currently supports 1Password, Bitwarden, Dashlane, and LastPass. | ||
| * | ||
| * Pass `true` to suppress password manager behaviour; pass `false` (or toggle back) to restore it. | ||
| * The directive is fully reversible: switching from `true` to `false` removes all managed attributes. | ||
| * | ||
| * @example html`<input ${uuiDisablePasswordManagers(this.disablePasswordManagers)} />` | ||
| */ | ||
| export const uuiDisablePasswordManagers = directive( | ||
| UUIDisablePasswordManagersDirective, | ||
| ); | ||
68 changes: 68 additions & 0 deletions
68
packages/uui-base/lib/directives/disable-password-managers.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { html, fixture, expect } from '@open-wc/testing'; | ||
| import { render } from 'lit'; | ||
| import { uuiDisablePasswordManagers } from './disable-password-managers.directive'; | ||
|
|
||
| const PASSWORD_MANAGER_ATTRS = [ | ||
| ['data-1p-ignore', ''], | ||
| ['data-bwignore', ''], | ||
| ['data-form-type', 'other'], | ||
| ['data-lpignore', 'true'], | ||
| ] as const; | ||
|
|
||
| function expectAttrsPresent(element: Element) { | ||
| PASSWORD_MANAGER_ATTRS.forEach(([name, value]) => { | ||
| expect(element.getAttribute(name)).to.equal(value); | ||
| }); | ||
| } | ||
|
|
||
| function expectAttrsAbsent(element: Element) { | ||
|
iOvergaard marked this conversation as resolved.
|
||
| PASSWORD_MANAGER_ATTRS.forEach(([name]) => { | ||
| expect(element.hasAttribute(name)).to.be.false; | ||
| }); | ||
| } | ||
|
|
||
|
iOvergaard marked this conversation as resolved.
|
||
| describe('UUIDisablePasswordManagersDirective', () => { | ||
| it('applies the correct attributes when enabled on a div', async () => { | ||
| const element = await fixture(html` | ||
| <div ${uuiDisablePasswordManagers(true)}>Div test element</div> | ||
| `); | ||
| expectAttrsPresent(element); | ||
| }); | ||
|
|
||
| it('applies the correct attributes when enabled on an input', async () => { | ||
| const element = await fixture(html` | ||
| <input | ||
| name="password" | ||
| autocomplete="current-password" | ||
| ${uuiDisablePasswordManagers(true)} /> | ||
| `); | ||
| expectAttrsPresent(element); | ||
| }); | ||
|
|
||
| it('does not apply attributes when disabled', async () => { | ||
| const element = await fixture(html` | ||
| <input ${uuiDisablePasswordManagers(false)} /> | ||
| `); | ||
| expectAttrsAbsent(element); | ||
| }); | ||
|
|
||
| it('removes attributes when toggled from true to false', async () => { | ||
| // Must use the same template function so Lit reuses the same directive | ||
| // instance across renders. Two separate html`` literals have different | ||
| // strings-array references and would cause Lit to replace the DOM entirely. | ||
| const template = (enabled: boolean) => | ||
| html`<input ${uuiDisablePasswordManagers(enabled)} />`; | ||
|
|
||
| const container = document.createElement('div'); | ||
| document.body.appendChild(container); | ||
|
|
||
| render(template(true), container); | ||
| const input = container.querySelector('input')!; | ||
| expectAttrsPresent(input); | ||
|
|
||
| render(template(false), container); | ||
| expectAttrsAbsent(input); | ||
|
|
||
| container.remove(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './disable-password-managers.directive.js'; | ||
|
iOvergaard marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.