mirror of
				https://github.com/jaandrle/deka-dom-el
				synced 2025-11-03 22:59:16 +01:00 
			
		
		
		
	♻️ Refactor jsdom register function
This commit is contained in:
		
							
								
								
									
										1
									
								
								src/jsdom.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								src/jsdom.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -1,4 +1,5 @@
 | 
				
			|||||||
import { el, assign, on } from "../index.d";
 | 
					import { el, assign, on } from "../index.d";
 | 
				
			||||||
 | 
					export * from "../index.d";
 | 
				
			||||||
export function register(dom: typeof document): Promise<{
 | 
					export function register(dom: typeof document): Promise<{
 | 
				
			||||||
	el: typeof el,
 | 
						el: typeof el,
 | 
				
			||||||
	assign: typeof assign,
 | 
						assign: typeof assign,
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										39
									
								
								src/jsdom.js
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								src/jsdom.js
									
									
									
									
									
								
							@@ -1,11 +1,30 @@
 | 
				
			|||||||
let keys= [];
 | 
					const keys= [ "HTMLElement", "SVGElement", "DocumentFragment", "MutationObserver", "document" ];
 | 
				
			||||||
export function register(dom){
 | 
					let dom_last;
 | 
				
			||||||
	const window= dom.window;
 | 
					export let el;
 | 
				
			||||||
	if(!keys.length)
 | 
					export let assign;
 | 
				
			||||||
		keys= Object.getOwnPropertyNames(window).filter((k) => !k.startsWith('_') && !(k in globalThis));
 | 
					export let on;
 | 
				
			||||||
	keys.forEach(key=> globalThis[key]= window[key]);
 | 
					export async function register(dom, keys_aditional= []){
 | 
				
			||||||
	global.document= window.document
 | 
						if(dom_last===dom)
 | 
				
			||||||
	global.window= window
 | 
							return import("../index.js");
 | 
				
			||||||
	window.console= global.console
 | 
					
 | 
				
			||||||
	return import("../index.js");
 | 
						keys.push(...keys_aditional);
 | 
				
			||||||
 | 
						const w= dom.window;
 | 
				
			||||||
 | 
						keys.forEach(key=> globalThis[key]= w[key]);
 | 
				
			||||||
 | 
						globalThis.window= w;
 | 
				
			||||||
 | 
						w.console= globalThis.console;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const m= await import("../index.js");
 | 
				
			||||||
 | 
						el= m.el;
 | 
				
			||||||
 | 
						assign= m.assign;
 | 
				
			||||||
 | 
						on= m.on;
 | 
				
			||||||
 | 
						return m;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					export function unregister(){
 | 
				
			||||||
 | 
						if(!dom_last)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						keys.forEach(key=> Reflect.deleteProperty(globalThis, key));
 | 
				
			||||||
 | 
						Reflect.deleteProperty(globalThis, "window");
 | 
				
			||||||
 | 
						dom_last= undefined;
 | 
				
			||||||
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user