MCPcopy Create free account
hub / github.com/experdot/pointer / sanitizeColors

Function sanitizeColors

src/renderer/src/utils/screenshot.ts:11–42  ·  view source on GitHub ↗

* Sanitize CSS color values that html2canvas doesn't support

(element: HTMLElement)

Source from the content-addressed store, hash-verified

9 * Sanitize CSS color values that html2canvas doesn't support
10 */
11function sanitizeColors(element: HTMLElement): void {
12 const unsupportedColorFunctions = ['oklab', 'oklch', 'lab', 'lch', 'color-mix']
13
14 const walkDOM = (node: Element): void => {
15 if (node instanceof HTMLElement) {
16 const style = node.style
17 const computed = window.getComputedStyle(node)
18
19 // Check common color properties
20 const colorProps = ['color', 'backgroundColor', 'borderColor', 'outlineColor']
21 for (const prop of colorProps) {
22 const value = computed.getPropertyValue(prop.replace(/([A-Z])/g, '-$1').toLowerCase())
23 if (value && unsupportedColorFunctions.some((fn) => value.includes(fn))) {
24 // Replace with a fallback color
25 if (prop === 'backgroundColor') {
26 style.backgroundColor = '#ffffff'
27 } else if (prop === 'color') {
28 style.color = '#000000'
29 } else {
30 ;(style as unknown as Record<string, string>)[prop] = 'transparent'
31 }
32 }
33 }
34 }
35
36 for (const child of node.children) {
37 walkDOM(child)
38 }
39 }
40
41 walkDOM(element)
42}
43
44/**
45 * Take a screenshot of a DOM element

Callers 1

screenshotElementFunction · 0.85

Calls 1

walkDOMFunction · 0.85

Tested by

no test coverage detected