-
Notifications
You must be signed in to change notification settings - Fork 12k
ensure getComputedStyle
always has a valid canvas provided
#11809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
412016b
ea5403d
e50c4c5
41d203f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,12 @@ function parseMaxStyle(styleValue: string | number, node: HTMLElement, parentPro | |
const getComputedStyle = (element: HTMLElement): CSSStyleDeclaration => | ||
element.ownerDocument.defaultView.getComputedStyle(element, null); | ||
|
||
export function getStyle(el: HTMLElement, property: string): string { | ||
// @ts-ignore | ||
export function getStyle(el?: HTMLElement, property: string): string { | ||
if (!el) { | ||
return ''; | ||
} | ||
|
||
return getComputedStyle(el).getPropertyValue(property); | ||
} | ||
|
||
|
@@ -105,20 +110,22 @@ function getCanvasPosition( | |
|
||
/** | ||
* Gets an event's x, y coordinates, relative to the chart area | ||
* @param event | ||
* @param chart | ||
Comment on lines
-108
to
-109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that this was not meant to be? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was removed since it didn't provide any additional information than the function signature already provided (not even variable types). If you still want this, I can revert this change. |
||
* @returns x and y coordinates of the event | ||
*/ | ||
|
||
export function getRelativePosition( | ||
event: Event | ChartEvent | TouchEvent | MouseEvent, | ||
chart: Chart | ||
chart?: Chart | ||
): { x: number; y: number } { | ||
if ('native' in event) { | ||
return event; | ||
} | ||
|
||
const {canvas, currentDevicePixelRatio} = chart; | ||
if (!canvas) { | ||
return {x: 0, y: 0}; | ||
} | ||
|
||
const style = getComputedStyle(canvas); | ||
const borderBox = style.boxSizing === 'border-box'; | ||
const paddings = getPositionedStyle(style, 'padding'); | ||
|
@@ -138,19 +145,21 @@ export function getRelativePosition( | |
}; | ||
} | ||
|
||
function getContainerSize(canvas: HTMLCanvasElement, width: number, height: number): Partial<Scale> { | ||
// @ts-ignore | ||
function getContainerSize(canvas?: HTMLCanvasElement, width: number, height: number): Partial<Scale> { | ||
let maxWidth: number, maxHeight: number; | ||
|
||
if (width === undefined || height === undefined) { | ||
const container = canvas && _getParentNode(canvas); | ||
if (!container) { | ||
width = canvas.clientWidth; | ||
height = canvas.clientHeight; | ||
width = canvas?.clientWidth ?? 0; | ||
height = canvas?.clientHeight ?? 0; | ||
} else { | ||
const rect = container.getBoundingClientRect(); // this is the border box of the container | ||
const containerStyle = getComputedStyle(container); | ||
const containerBorder = getPositionedStyle(containerStyle, 'border', 'width'); | ||
const containerPadding = getPositionedStyle(containerStyle, 'padding'); | ||
|
||
width = rect.width - containerPadding.width - containerBorder.width; | ||
height = rect.height - containerPadding.height - containerBorder.height; | ||
maxWidth = parseMaxStyle(containerStyle.maxWidth, container, 'clientWidth'); | ||
|
@@ -169,11 +178,15 @@ const round1 = (v: number) => Math.round(v * 10) / 10; | |
|
||
// eslint-disable-next-line complexity | ||
export function getMaximumSize( | ||
canvas: HTMLCanvasElement, | ||
canvas?: HTMLCanvasElement, | ||
bbWidth?: number, | ||
bbHeight?: number, | ||
aspectRatio?: number | ||
): { width: number; height: number } { | ||
if (!canvas) { | ||
return {width: 0, height: 0}; | ||
} | ||
|
||
const style = getComputedStyle(canvas); | ||
const margins = getPositionedStyle(style, 'margin'); | ||
const maxWidth = parseMaxStyle(style.maxWidth, canvas, 'clientWidth') || INFINITY; | ||
|
Uh oh!
There was an error while loading. Please reload this page.