fix(web): guard theme.js against missing browser globals (#9912)#10058
fix(web): guard theme.js against missing browser globals (#9912)#10058alokkumardalei-wq wants to merge 1 commit intoThe-OpenROAD-Project:masterfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a check for the existence of the document object in getThemeColors, providing fallback theme values for environments where it is undefined. The review feedback highlights a discrepancy between these hardcoded fallback values and the project's CSS definitions, and recommends using named constants instead of magic numbers to improve maintainability.
| if (typeof document === 'undefined') { | ||
| return { | ||
| canvasBg: '#1a1a1a', canvasText: '#ccc', canvasAxis: '#555', | ||
| canvasLabel: '#aaa', canvasGrid: '#333', canvasTitle: '#fff', | ||
| fgPrimary: '#e0e0e0', fgMuted: '#888', bgPanel: '#252525', | ||
| bgMap: '#1a1a1a', | ||
| }; | ||
| } |
There was a problem hiding this comment.
The hardcoded fallback values for the dark theme do not match the values defined in src/web/src/style.css. For consistency and to avoid potential discrepancies in tests, these values should be synchronized with the CSS source of truth.
Specifically:
canvasText:#cccshould be#666(matches--canvas-text)canvasTitle:#fffshould be#888(matches--canvas-title)fgPrimary:#e0e0e0should be#ccc(matches--fg-primary)bgMap:#1a1a1ashould be#111(matches--bg-map)
Additionally, per the general rules, these tunable parameters should ideally be defined as named constants rather than hardcoded magic numbers within the function body.
References
- Define tunable parameters as named constants instead of using hardcoded magic numbers.
|
clang-tidy review says "All clean, LGTM! 👍" |
990fb98 to
cacb52c
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
1 similar comment
|
clang-tidy review says "All clean, LGTM! 👍" |
cacb52c to
fe80920
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
fe80920 to
860d1f0
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
…D-Project#9912) Signed-off-by: alokkumardalei-wq <alokkumardalei2@gmail.com>
860d1f0 to
24cdda1
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
|
Hello @maliberty , @hzeller and @oharboe all checks are passing now , please have a look when you have time and would happy to address any feedback further. Thank you ! |
Problem
After ca12251 added the light/dark theme toggle, clock-tree-widget.js started importing getThemeColors() from theme.js. This function calls getComputedStyle(document.documentElement), which doesn't exist in Node.js. Any js_test that transitively imports clock-tree-widget.js now crashes with ReferenceError: document is not defined before a single test case runs.
What does this PR do?
Fixes #9912
Added a typeof document === 'undefined' guard at the top of getThemeColors(). In Node.js environments, it returns hardcoded dark-theme defaults instead of reading CSS custom properties. These values are only used by canvas rendering code that never runs in tests, so the fallback is safe.
Changes Made
src/web/src/theme.js — added typeof document check with dark-theme fallback values in getThemeColors()
Testing & Verification
bazelisk test //src/web/test:clock_tree_widget_testExpected output:
bazelisk test //src/web/test:charts_widget_testExpected output: