When creating a <canvas> element via document.createElement('canvas'), the returned element does not inherit from HTMLElement. Specifically, it's missing the style property, which breaks any code that tries to set inline styles on the canvas element.
Reproduction
import { Window } from 'very-happy-dom'
const win = new Window()
const canvas = win.document.createElement('canvas')
// Check basic properties
console.log('canvas:', !!canvas) // true
console.log('canvas.style:', canvas.style) // undefined (BUG)
console.log('getContext:', typeof canvas.getContext) // 'function'
// This works
const ctx = canvas.getContext('2d')
console.log('ctx:', !!ctx) // true
// This throws TypeError: undefined is not an object
canvas.style.position = 'absolute'
canvas.style.width = '100px'Expected Behavior
Canvas elements should inherit from HTMLElement and have all standard element properties including style, classList, id, className, addEventListener, etc.
Actual Behavior
The canvas element returned by document.createElement('canvas') is missing:
style(CSSStyleDeclaration)classList(DOMTokenList)id,classNameaddEventListener/removeEventListener- All other HTMLElement properties
Environment
- very-happy-dom version: 0.1.21 (latest GitHub main)
- Bun version: 1.2.21
- OS: Linux x64
Notes
I checked the source code and saw that VirtualDocument.ts has special handling for canvas elements:
if (tagName.toLowerCase() === 'canvas') {
const { HTMLCanvasElement } = require('../apis/Canvas')
const el = new HTMLCanvasElement()
el.ownerDocument = this
return el
}The HTMLCanvasElement class in src/apis/Canvas.ts appears to be a standalone class that doesn't extend HTMLElement. It should extend the base element class to inherit DOM properties like style.
Impact
This breaks testing of any library that applies inline styles to canvas elements (e.g., positioning, sizing, transforms). Many canvas-based UI components (minimaps, charts, editors) rely on canvas.style for layout.
Reproduction
Included in description (unreproducible in online sandboxes).
System Info
System:
OS: Linux 6.17 EndeavourOS
CPU: (8) x64 Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
Memory: 3.32 GB / 15.29 GB
Container: Yes
Shell: 5.9 - /usr/bin/zsh
Binaries:
Node: 22.16.0 - /run/user/1000/fnm_multishells/696614_1776850704152/bin/node
npm: 11.6.4 - /run/user/1000/fnm_multishells/696614_1776850704152/bin/npm
bun: 1.2.21 - /run/user/1000/fnm_multishells/696614_1776850704152/bin/bun
Deno: 2.3.3 - /home/g/.deno/bin/deno
Browsers:
Brave Browser: 146.1.88.132
Firefox: 145.0.2Used Package Manager
bun
Validations
- Follow our Code of Conduct
- Read the Contributing Guide.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
- The provided reproduction is a minimal reproducible of the bug.