Skip to content

Commit

Permalink
capricorn86#1113@patch: Fixes second parameter missing from MutationO…
Browse files Browse the repository at this point in the history
…bserver callback.
  • Loading branch information
wojtekmaj committed Oct 4, 2023
1 parent 5394f83 commit 6a40d9e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
4 changes: 3 additions & 1 deletion packages/happy-dom/src/mutation-observer/MutationListener.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import IMutationObserverInit from './IMutationObserverInit.js';
import MutationObserver from './MutationObserver.js';
import MutationRecord from './MutationRecord.js';

/**
* MutationObserverListener is a model for what to listen for on a Node.
*/
export default class MutationListener {
public options: IMutationObserverInit = null;
public callback: (record: MutationRecord[]) => void = null;
public observer: MutationObserver = null;
public callback: (record: MutationRecord[], observer: MutationObserver) => void = null;
}
5 changes: 3 additions & 2 deletions packages/happy-dom/src/mutation-observer/MutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import MutationRecord from './MutationRecord.js';
* @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
*/
export default class MutationObserver {
private callback: (records: MutationRecord[]) => void;
private callback: (records: MutationRecord[], observer: MutationObserver) => void;
private target: INode = null;
private listener: MutationObserverListener = null;

Expand All @@ -20,7 +20,7 @@ export default class MutationObserver {
*
* @param callback Callback.
*/
constructor(callback: (records: MutationRecord[]) => void) {
constructor(callback: (records: MutationRecord[], observer: MutationObserver) => void) {
this.callback = callback;
}

Expand All @@ -47,6 +47,7 @@ export default class MutationObserver {
this.listener = new MutationObserverListener();
this.listener.options = options;
this.listener.callback = this.callback.bind(this);
this.listener.observer = this;

(<Node>target)._observe(this.listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default abstract class CharacterData extends Node implements ICharacterDa
record.target = this;
record.type = MutationTypeEnum.characterData;
record.oldValue = observer.options.characterDataOldValue ? oldValue : null;
observer.callback([record]);
observer.callback([record], observer.observer);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/happy-dom/src/nodes/element/ElementNamedNodeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class ElementNamedNodeMap extends NamedNodeMap {
record.type = MutationTypeEnum.attributes;
record.attributeName = item.name;
record.oldValue = observer.options.attributeOldValue ? oldValue : null;
observer.callback([record]);
observer.callback([record], observer.observer);
}
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ export default class ElementNamedNodeMap extends NamedNodeMap {
record.type = MutationTypeEnum.attributes;
record.attributeName = removedItem.name;
record.oldValue = observer.options.attributeOldValue ? removedItem.value : null;
observer.callback([record]);
observer.callback([record], observer.observer);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/happy-dom/src/nodes/node/NodeUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class NodeUtility {
(<Node>node)._observe(observer);
}
if (observer.options.childList) {
observer.callback([record]);
observer.callback([record], observer.observer);
}
}
}
Expand Down Expand Up @@ -121,7 +121,7 @@ export default class NodeUtility {
for (const observer of (<Node>ancestorNode)._observers) {
(<Node>node)._unobserve(observer);
if (observer.options.childList) {
observer.callback([record]);
observer.callback([record], observer.observer);
}
}
}
Expand Down Expand Up @@ -208,7 +208,7 @@ export default class NodeUtility {
(<Node>newNode)._observe(observer);
}
if (observer.options.childList) {
observer.callback([record]);
observer.callback([record], observer.observer);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ describe('MutationObserver', () => {
]
]);
});

it('Calls callback with the observer as second parameter.', () => {
const div = document.createElement('div');
const observer = new MutationObserver((mutationRecords, observer) => {
expect(observer).toBeInstanceOf(MutationObserver);
});
observer.observe(div, { attributes: true });
div.setAttribute('attr', 'value');
});
});

describe('disconnect()', () => {
Expand Down

0 comments on commit 6a40d9e

Please sign in to comment.