Skip to content

Commit

Permalink
fix: don't use globalThis when using this
Browse files Browse the repository at this point in the history
Without "use strict", `this` will be set to `globalThis` which is
typically the window object. If certain properties are set on the
window object (e.g. "window.target"), this can cause problems for the
`css` and `styled` functions.

Instead, check that `this` has been set to something other than
`globalThis` before using it. This ensures that `this` will only have
a value if it is explicitly bound, and otherwise will be `undefined`.

Note: I tried to add "use strict"; directives where needed, but couldn't
get them to "survive" the build process -- they were removed. This fix
seems to work just fine.

Closes cristianbote#545
  • Loading branch information
jluxenberg committed Sep 6, 2023
1 parent a849b2d commit 7a26f07
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getSheet } from './core/get-sheet';
* @param {String|Object|Function} val
*/
function css(val) {
let ctx = this || {};
let ctx = (this !== globalThis ? this : undefined) || {};
let _val = val.call ? val(ctx.p) : val;

return hash(
Expand Down
2 changes: 1 addition & 1 deletion src/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function setup(pragma, prefix, theme, forwardProps) {
* @param {function} forwardRef
*/
function styled(tag, forwardRef) {
let _ctx = this || {};
let _ctx = (this !== globalThis ? this : undefined) || {};

return function wrapper() {
let _args = arguments;
Expand Down

0 comments on commit 7a26f07

Please sign in to comment.