Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
},
"dependencies": {
"@fontsource/noto-sans": "^5.2.10",
"@napi-rs/canvas": "^0.1.58",
"@napi-rs/canvas": "^0.1.84",
"jsdom": "^27.0.0",
"undici": "^6.22.0"
},
Expand Down
33 changes: 30 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,42 @@ function patchMapPrototype(
};

const mapInstance = originalInit.call(this, id, headlessOpts);
(this as any)._headlessSize = { ...options.mapSize };
// Initialize headless size with default or user-provided mapSize
const initialSize = (opts as any)?.mapSize || options.mapSize;
(this as any)._headlessSize = { ...initialSize };
return mapInstance;
};

// Override getSize since jsdom doesn't support clientWidth/clientHeight
L.Map.prototype.getSize = function (this: any): LeafletModule.Point {
if (!this._size || this._sizeChanged) {
const size = (this as any)._headlessSize ?? options.mapSize;
this._size = new L.Point(size.width, size.height);
let width: number | undefined;
let height: number | undefined;

// Try to get dimensions from the container first
// This allows tests to mock dimensions on the container element
const container = this.getContainer();
if (container) {
if (container.clientWidth > 0 && container.clientHeight > 0) {
width = container.clientWidth;
height = container.clientHeight;
} else if (container.getBoundingClientRect) {
const rect = container.getBoundingClientRect();
if (rect.width > 0 && rect.height > 0) {
width = rect.width;
height = rect.height;
}
}
}

// Fallback to headless size
if (width === undefined || height === undefined) {
const size = (this as any)._headlessSize ?? options.mapSize;
width = size.width;
height = size.height;
}

this._size = new L.Point(width as number, height as number);
this._sizeChanged = false;
}
return this._size.clone();
Expand Down
25 changes: 25 additions & 0 deletions src/polyfills/dom-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function patchDomEvent(L: typeof LeafletModule): void {
}

const originalOff = L.DomEvent.off;
const originalGetMousePosition = L.DomEvent.getMousePosition;

// Store original on the object to avoid closure issues in some environments (like Jest)
// This seems to prevent a silent crash during module import in specific test configurations
Expand All @@ -38,6 +39,30 @@ export function patchDomEvent(L: typeof LeafletModule): void {
}
} as typeof originalOff;

// Patch getMousePosition to support JSDOM environments where getBoundingClientRect works
// but offsetParent/clientLeft/clientTop layout properties might not be perfect
// Use 'any' for the event type to avoid TypeScript issues if Touch is not defined globally in strict environments
L.DomEvent.getMousePosition = function(e: any, container?: HTMLElement): LeafletModule.Point {
if (container && container.getBoundingClientRect) {
const rect = container.getBoundingClientRect();
const clientLeft = container.clientLeft || 0;
const clientTop = container.clientTop || 0;

return new L.Point(
e.clientX - rect.left - clientLeft,
e.clientY - rect.top - clientTop
);
}

// Fallback to original implementation if container doesn't have getBoundingClientRect
// or if it's not provided (though Leaflet usually provides it)
return originalGetMousePosition(e, container);
};

// Note: We don't need to patch addListener/removeListener because standard Leaflet
// already checks for addEventListener/removeEventListener on the object.
// Since we run in JSDOM (which has these), standard Leaflet works fine.

// Mark as patched
(L.DomEvent as any)._leafletNodePatched = true;

Expand Down
Loading