A platform for community discussion. Free, open, simple.
FIX: only extract script tags with certain types (#6553)
Kyle Zhaof9b36820ef86
diff --git a/app/models/theme_field.rb b/app/models/theme_field.rbindex 84a6b9b561e..01b5c4c9fc2 100644--- a/app/models/theme_field.rb+++ b/app/models/theme_field.rb@@ -123,9 +123,10 @@ COMPILEDenddoc.css('script').each do |node|- next if node['src'].present?+ next unless inline_javascript?(node)- javascript_cache.content << "(function() { #{node.inner_html} })();"+ javascript_cache.content << node.inner_html+ javascript_cache.content << "\n"node.removeend@@ -253,6 +254,24 @@ COMPILEDMessageBus.publish "/header-change/#{theme.id}", self.value if theme && self.name == "header"MessageBus.publish "/footer-change/#{theme.id}", self.value if theme && self.name == "footer"end++ private++ JAVASCRIPT_TYPES = %w(+ text/javascript+ application/javascript+ application/ecmascript+ )++ def inline_javascript?(node)+ if node['src'].present?+ false+ elsif node['type'].present?+ JAVASCRIPT_TYPES.include?(node['type'].downcase)+ else+ true+ end+ endend# == Schema Informationdiff --git a/spec/models/theme_field_spec.rb b/spec/models/theme_field_spec.rbindex b129c3143cf..5052c883b60 100644--- a/spec/models/theme_field_spec.rb+++ b/spec/models/theme_field_spec.rb@@ -37,9 +37,18 @@ describe ThemeField do<script type="text/discourse-plugin" version="0.8">var a = "inline discourse plugin";</script>+ <script type="text/template" data-template="custom-template">+ <div>custom script type</div>+ </script><script>var b = "inline raw script";</script>+ <script type="texT/jAvasCripT">+ var c = "text/javascript";+ </script>+ <script type="application/javascript">+ var d = "application/javascript";+ </script><script src="/external-script.js"></script>HTML@@ -47,8 +56,27 @@ describe ThemeField doexpect(theme_field.value_baked).to include("<script src=\"#{theme_field.javascript_cache.url}\"></script>")expect(theme_field.value_baked).to include("external-script.js")- expect(theme_field.javascript_cache.content).to include('inline discourse plugin')- expect(theme_field.javascript_cache.content).to include('inline raw script')+ expect(theme_field.value_baked).to include('<script type="text/template"')+ expect(theme_field.javascript_cache.content).to include('a = "inline discourse plugin"')+ expect(theme_field.javascript_cache.content).to include('b = "inline raw script"')+ expect(theme_field.javascript_cache.content).to include('c = "text/javascript"')+ expect(theme_field.javascript_cache.content).to include('d = "application/javascript"')+ end++ it 'adds newlines between the extracted javascripts' do+ html = <<~HTML+ <script>var a = 10</script>+ <script>var b = 10</script>+ HTML++ extracted = <<~JavaScript+ var a = 10+ var b = 10+ JavaScript++ theme_field = ThemeField.create!(theme_id: 1, target_id: 0, name: "header", value: html)++ expect(theme_field.javascript_cache.content).to eq(extracted)endit "correctly extracts and generates errors for transpiled js" do