Skip to content

Commit 8845d41

Browse files
committed
Bug: Fix issue with mutation observors on attributes
1 parent bce2d72 commit 8845d41

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

packages/query/src/behavior.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,24 @@ export class Behavior {
331331
this.mutationObservers = [];
332332
each(mutationConfigs, ({ observedElement, observerOptions, keyword, selector, handler }) => {
333333
const observer = new MutationObserver((mutations) => {
334+
if (keyword == 'attributes') {
335+
const attributeMutations = mutations.filter(mutation => mutation.type == 'attributes');
336+
each(attributeMutations, (mutation) => {
337+
const callbackArgs = this.getMutationCallbackArgs([mutation]);
338+
this.call(handler, { additionalParams: callbackArgs });
339+
});
340+
return;
341+
}
342+
if (keyword == 'text') {
343+
const textMutations = mutations.filter(mutation => mutation.type == 'characterData');
344+
each(textMutations, (mutation) => {
345+
const callbackArgs = this.getMutationCallbackArgs([mutation]);
346+
this.call(handler, { additionalParams: callbackArgs });
347+
});
348+
}
334349
// determine if it matches
335350
let $added = $();
336351
let $removed = $();
337-
338352
each(mutations, (mutation) => {
339353
const $matchingAdded = $(mutation.addedNodes).filter(selector);
340354
const $matchingRemoved = $(mutation.removedNodes).filter(selector);
@@ -346,9 +360,6 @@ export class Behavior {
346360
}
347361
});
348362

349-
// Check if we should trigger based on keyword
350-
const hasAdded = $added.exists();
351-
const hasRemoved = $removed.exists();
352363
const shouldTrigger = (keyword === 'add' && hasAdded)
353364
|| (keyword === 'remove' && hasRemoved)
354365
|| (keyword === 'standard' && (hasAdded || hasRemoved));
@@ -359,7 +370,6 @@ export class Behavior {
359370
this.call(handler, { additionalParams: callbackArgs });
360371
}
361372
});
362-
363373
observer.observe(observedElement, observerOptions);
364374
this.mutationObservers.push(observer);
365375
});
@@ -374,7 +384,7 @@ export class Behavior {
374384

375385
parseMutationString(mutationString) {
376386
const keywords = ['observe', 'add', 'remove', 'attributes', 'text'];
377-
const observerOptions = {
387+
let observerOptions = {
378388
childList: true,
379389
subtree: true,
380390
};
@@ -409,12 +419,16 @@ export class Behavior {
409419
}
410420
break;
411421
case 'attributes':
412-
observerOptions.attributes = true;
413-
observerOptions.attributeOldValue = true;
422+
observerOptions = {
423+
attributes: true,
424+
attributeOldValue: true,
425+
};
414426
break;
415427
case 'text':
416-
observerOptions.characterData = true;
417-
observerOptions.characterDataOldValue = true;
428+
observerOptions = {
429+
characterData: true,
430+
characterDataOldValue: true,
431+
};
418432
break;
419433
}
420434
return { observedElement, keyword, observerOptions, selector };
@@ -433,11 +447,11 @@ export class Behavior {
433447
switch (mutation.type) {
434448
case 'attributes':
435449
args.attributeName = mutation.attributeName;
436-
args.newValue = mutation.target.getAttribute(mutation.attributeName);
450+
args.attributeValue = mutation.target.getAttribute(mutation.attributeName);
437451
args.oldValue = mutation.oldValue;
438452
break;
439453
case 'characterData':
440-
args.newValue = mutation.target.textContent;
454+
args.textContent = mutation.target.textContent;
441455
args.oldValue = mutation.oldValue;
442456
break;
443457
}

0 commit comments

Comments
 (0)