From 4b2984f4dd5074d0f1a4822ae1794dd58020c167 Mon Sep 17 00:00:00 2001 From: Keith Brink Date: Thu, 15 Jan 2026 12:49:15 -0500 Subject: [PATCH 1/2] fix: add null check to clipArea and unclipArea to prevent crash on destroyed context --- src/helpers/helpers.canvas.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/helpers/helpers.canvas.ts b/src/helpers/helpers.canvas.ts index f37504c0097..c3bfd0b8c7f 100644 --- a/src/helpers/helpers.canvas.ts +++ b/src/helpers/helpers.canvas.ts @@ -326,6 +326,7 @@ export function _isPointInArea( } export function clipArea(ctx: CanvasRenderingContext2D, area: TRBL) { + if (!ctx) return; ctx.save(); ctx.beginPath(); ctx.rect(area.left, area.top, area.right - area.left, area.bottom - area.top); @@ -333,6 +334,7 @@ export function clipArea(ctx: CanvasRenderingContext2D, area: TRBL) { } export function unclipArea(ctx: CanvasRenderingContext2D) { + if (!ctx) return; ctx.restore(); } From 94d8e3a414a19b9bafa9bbfafba71c3dfbe835fd Mon Sep 17 00:00:00 2001 From: Keith Brink Date: Thu, 15 Jan 2026 16:08:54 -0500 Subject: [PATCH 2/2] fix: add null check to getMaximumSize and getRelativePosition to prevent ownerDocument crash Adds null checks before calling getComputedStyle(canvas) to prevent crash when canvas element is null and code tries to access canvas.ownerDocument. Co-Authored-By: Claude Sonnet 4.5 --- src/helpers/helpers.dom.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/helpers/helpers.dom.ts b/src/helpers/helpers.dom.ts index b0c41eec0c0..cbb6341cc3b 100644 --- a/src/helpers/helpers.dom.ts +++ b/src/helpers/helpers.dom.ts @@ -111,6 +111,11 @@ export function getRelativePosition( } const {canvas, currentDevicePixelRatio} = chart; + // Guard against null canvas to prevent ownerDocument crash + if (!canvas) { + return {x: 0, y: 0}; + } + const style = getComputedStyle(canvas); const borderBox = style.boxSizing === 'border-box'; const paddings = getPositionedStyle(style, 'padding'); @@ -166,6 +171,11 @@ export function getMaximumSize( bbHeight?: number, aspectRatio?: number ): { width: number; height: number } { + // Guard against null canvas to prevent ownerDocument crash + if (!canvas) { + return {width: 0, height: 0}; + } + const style = getComputedStyle(canvas); const margins = getPositionedStyle(style, 'margin'); const maxWidth = parseMaxStyle(style.maxWidth, canvas, 'clientWidth') || INFINITY;