From 1b0691056acacce31170a36f3c47205777e6ad56 Mon Sep 17 00:00:00 2001 From: Diego Scataglini Date: Mon, 29 Jul 2024 20:53:37 -0400 Subject: [PATCH 1/4] opal_tag should mirror the javascript_tag api --- app/helpers/opal_helper.rb | 12 ++++++++--- spec/helpers/opal_helper_spec.rb | 35 ++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/helpers/opal_helper.rb b/app/helpers/opal_helper.rb index 2a6e330..2905ed3 100644 --- a/app/helpers/opal_helper.rb +++ b/app/helpers/opal_helper.rb @@ -1,12 +1,18 @@ require 'opal/sprockets' module OpalHelper - def opal_tag(opal_code = nil, &block) - opal_code ||= capture(&block) + def opal_tag(opal_code = nil, html_options = {}, &block) + if block_given? + html_options = opal_code if opal_code.is_a?(Hash) + opal_code = capture(&block) + end + compiler_options = Opal::Config.compiler_options.merge(requirable: false) compiler = Opal::Compiler.new(opal_code, compiler_options) js_code = compiler.compile - javascript_tag js_code + javascript_tag html_options do + js_code + end end def javascript_include_tag(*sources) diff --git a/spec/helpers/opal_helper_spec.rb b/spec/helpers/opal_helper_spec.rb index 4853524..6863bb4 100644 --- a/spec/helpers/opal_helper_spec.rb +++ b/spec/helpers/opal_helper_spec.rb @@ -6,15 +6,38 @@ let(:helper) { view } describe '#opal_tag' do - it 'compiles to js' do - allow(helper).to receive(:javascript_tag) { |code| code } - ruby_code = 'puts 5' - - expect(Opal::Compiler).to receive(:new) + let(:ruby_code) { 'puts 5' } + let(:compiled_ruby_code) { 'self.$puts(5)' } + let(:html_options) { { async: true } } + before do + allow(helper).to receive(:javascript_tag).and_call_original + allow(Opal::Compiler).to receive(:new) .with(ruby_code, hash_including(requirable: false)) .and_call_original + end + + context 'when the ruby code is passed inline' do + it 'compiles the ruby code to js' do + expect(helper.opal_tag(ruby_code)).to include(compiled_ruby_code) + end + + it 'passes the html_options to the javascript_tag' do + helper.opal_tag(ruby_code, html_options) + expect(helper).to have_received(:javascript_tag).with(html_options) + end + end + + context 'when the ruby code is passed as a block' do + it 'compiles the block to js' do + expect(helper.opal_tag { ruby_code }).to include(compiled_ruby_code) + end - expect(helper.opal_tag(ruby_code)).to include('self.$puts(5)') + it 'uses the options as the first argument' do + aggregate_failures do + expect(helper.opal_tag(html_options) { ruby_code }).to include(compiled_ruby_code) + expect(helper).to have_received(:javascript_tag).with(html_options) + end + end end end From aad3806207fe750b79b6f60cad3225fc0e6229a3 Mon Sep 17 00:00:00 2001 From: Diego Scataglini Date: Mon, 29 Jul 2024 21:01:27 -0400 Subject: [PATCH 2/4] renaming variable --- app/helpers/opal_helper.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/helpers/opal_helper.rb b/app/helpers/opal_helper.rb index 2905ed3..8ec5b40 100644 --- a/app/helpers/opal_helper.rb +++ b/app/helpers/opal_helper.rb @@ -1,14 +1,14 @@ require 'opal/sprockets' module OpalHelper - def opal_tag(opal_code = nil, html_options = {}, &block) + def opal_tag(opal_code_or_options = nil, html_options = {}, &block) if block_given? - html_options = opal_code if opal_code.is_a?(Hash) - opal_code = capture(&block) + html_options = opal_code_or_options if opal_code_or_options.is_a?(Hash) + opal_code_or_options = capture(&block) end compiler_options = Opal::Config.compiler_options.merge(requirable: false) - compiler = Opal::Compiler.new(opal_code, compiler_options) + compiler = Opal::Compiler.new(opal_code_or_options, compiler_options) js_code = compiler.compile javascript_tag html_options do js_code From de72c30e4fbff6980a1fd461a5683b01506b62a6 Mon Sep 17 00:00:00 2001 From: Diego Scataglini Date: Tue, 30 Jul 2024 14:21:23 -0400 Subject: [PATCH 3/4] pass the options along in debug mode --- app/helpers/opal_helper.rb | 4 ++-- spec/helpers/opal_helper_spec.rb | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/helpers/opal_helper.rb b/app/helpers/opal_helper.rb index 8ec5b40..029c8d1 100644 --- a/app/helpers/opal_helper.rb +++ b/app/helpers/opal_helper.rb @@ -17,7 +17,7 @@ def opal_tag(opal_code_or_options = nil, html_options = {}, &block) def javascript_include_tag(*sources) options = sources.extract_options!.symbolize_keys - debug = options[:debug] != false + debug = options.delete(:debug) != false skip_loader = options.delete(:skip_opal_loader) force_opal_loader_tag = options.delete(:force_opal_loader_tag) || debug @@ -30,7 +30,7 @@ def javascript_include_tag(*sources) if force_opal_loader_tag script_tags << super(source, options) - script_tags << "\n".html_safe + javascript_tag(loading_code) + script_tags << "\n".html_safe + javascript_tag(loading_code, options) else script_tags << super(source, options.merge(onload: loading_code)) end diff --git a/spec/helpers/opal_helper_spec.rb b/spec/helpers/opal_helper_spec.rb index 6863bb4..45ea767 100644 --- a/spec/helpers/opal_helper_spec.rb +++ b/spec/helpers/opal_helper_spec.rb @@ -55,8 +55,13 @@ %(), ].join("\n") + loading_code_with_options_in_script_tag = [ + %(), + ].join("\n") + expect(helper.javascript_include_tag('application', debug: true)).to include(loading_code_in_script_tag) expect(helper.javascript_include_tag('application', debug: true)).not_to include(escaped_loading_code) + expect(helper.javascript_include_tag('application', debug: true, defer: true)).not_to include(escaped_loading_code) expect(helper.javascript_include_tag('application', debug: false)).to include(escaped_loading_code) expect(helper.javascript_include_tag('application', debug: false)).not_to include(loading_code_in_script_tag) From 744ed6bff8a3ad799f45f3aa70307905081370db Mon Sep 17 00:00:00 2001 From: Diego Scataglini Date: Tue, 30 Jul 2024 14:42:08 -0400 Subject: [PATCH 4/4] should mark js code as html_safe --- app/helpers/opal_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/opal_helper.rb b/app/helpers/opal_helper.rb index 029c8d1..0842780 100644 --- a/app/helpers/opal_helper.rb +++ b/app/helpers/opal_helper.rb @@ -11,7 +11,7 @@ def opal_tag(opal_code_or_options = nil, html_options = {}, &block) compiler = Opal::Compiler.new(opal_code_or_options, compiler_options) js_code = compiler.compile javascript_tag html_options do - js_code + js_code.html_safe end end