Skip to content

Commit

Permalink
Run attribute updated callbacks when removing an attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Feb 27, 2024
1 parent 1badc94 commit 8d58362
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/morphlex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface Options {
element,
}: {
attributeName: string;
newValue: string;
newValue: string | null;
element: Element;
}) => boolean;
afterAttributeUpdated?: ({
Expand Down
7 changes: 6 additions & 1 deletion dist/morphlex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/morphlex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface Options {
element,
}: {
attributeName: string;
newValue: string;
newValue: string | null;
element: Element;
}) => boolean;

Expand Down Expand Up @@ -146,7 +146,12 @@ function morphNode(node: ChildNode, ref: ReadonlyNode<ChildNode>, context: Conte

function morphAttributes(element: Element, ref: ReadonlyNode<Element>, context: Context): void {
// Remove any excess attributes from the element that aren’t present in the reference.
for (const { name } of element.attributes) ref.hasAttribute(name) || element.removeAttribute(name);
for (const { name, value } of element.attributes) {
if (!ref.hasAttribute(name) && (context.beforeAttributeUpdated?.({ attributeName: name, newValue: null, element }) ?? true)) {
element.removeAttribute(name);
context.afterAttributeUpdated?.({ attributeName: name, previousValue: value, element });
}
}

// Copy attributes from the reference to the element, if they don’t already match.
for (const { name, value } of ref.attributes) {
Expand Down

0 comments on commit 8d58362

Please sign in to comment.