From 7b4c6d6100f7829ec0250fec501d902cc2fa6986 Mon Sep 17 00:00:00 2001
From: Alex Riedler <alex@riedler.ca>
Date: Sun, 15 Dec 2024 19:42:49 -0500
Subject: [PATCH] fix: handle window case correctly due to this

---
 lib/handlebars_assets/handlebars_template.rb    | 4 ++--
 test/test_helper.rb                             | 7 +++----
 vendor/assets/javascripts/handlebars.js         | 2 +-
 vendor/assets/javascripts/handlebars.runtime.js | 2 +-
 4 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/lib/handlebars_assets/handlebars_template.rb b/lib/handlebars_assets/handlebars_template.rb
index 4fda1ac..0b72921 100644
--- a/lib/handlebars_assets/handlebars_template.rb
+++ b/lib/handlebars_assets/handlebars_template.rb
@@ -153,7 +153,7 @@ def compile_default(source)
         unindent <<-PARTIAL
             (function() {
               Handlebars.registerPartial(#{@template_path.name}, #{template});
-            }).call(this);
+            }).call(this || window);
         PARTIAL
       else
         unindent <<-TEMPLATE
@@ -161,7 +161,7 @@ def compile_default(source)
               this.#{template_namespace} || (this.#{template_namespace} = {});
               this.#{template_namespace}[#{@template_path.name}] = #{template};
               return this.#{template_namespace}[#{@template_path.name}];
-            }).call(this);
+            }).call(this || window);
         TEMPLATE
       end
     end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index dc2953b..9a39668 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -25,8 +25,7 @@ module CompilerSupport
 
   def compile_hbs(source)
     compiler_src = Pathname(HandlebarsAssets::Config.compiler_path).join(HandlebarsAssets::Config.compiler).read
-    ExecJS.compile("var window = {}; #{compiler_src}").call('Handlebars.precompile', source,
-                                                            HandlebarsAssets::Config.options)
+    ExecJS.compile("#{compiler_src}").call('Handlebars.precompile', source, HandlebarsAssets::Config.options)
   end
 
   def hbs_compiled(template_name, source)
@@ -38,7 +37,7 @@ def hbs_compiled(template_name, source)
         this.#{template_namespace} || (this.#{template_namespace} = {});
         this.#{template_namespace}[#{template_name.dump}] = Handlebars.template(#{compiled_hbs});
         return this.#{template_namespace}[#{template_name.dump}];
-      }).call(this);
+      }).call(this || window);
     END_EXPECTED
   end
 
@@ -48,7 +47,7 @@ def hbs_compiled_partial(partial_name, source)
     <<~END_EXPECTED
       (function() {
         Handlebars.registerPartial(#{partial_name.dump}, Handlebars.template(#{compiled_hbs}));
-      }).call(this);
+      }).call(this || window);
     END_EXPECTED
   end
 end
diff --git a/vendor/assets/javascripts/handlebars.js b/vendor/assets/javascripts/handlebars.js
index 746602a..1adba57 100644
--- a/vendor/assets/javascripts/handlebars.js
+++ b/vendor/assets/javascripts/handlebars.js
@@ -33,7 +33,7 @@ THE SOFTWARE.
 		exports["Handlebars"] = factory();
 	else
 		root["Handlebars"] = factory();
-})(this, function() {
+})(this || window, function() {
 return /******/ (function(modules) { // webpackBootstrap
 /******/ 	// The module cache
 /******/ 	var installedModules = {};
diff --git a/vendor/assets/javascripts/handlebars.runtime.js b/vendor/assets/javascripts/handlebars.runtime.js
index 42adcf9..17c45f9 100644
--- a/vendor/assets/javascripts/handlebars.runtime.js
+++ b/vendor/assets/javascripts/handlebars.runtime.js
@@ -33,7 +33,7 @@ THE SOFTWARE.
 		exports["Handlebars"] = factory();
 	else
 		root["Handlebars"] = factory();
-})(this, function() {
+})(this || window, function() {
 return /******/ (function(modules) { // webpackBootstrap
 /******/ 	// The module cache
 /******/ 	var installedModules = {};