Skip to content

Commit ee9cdc5

Browse files
committed
fix: use raw output for .to_json in cropper and uploader script tags
ERB's <%= %> HTML-escapes the output, turning JSON double quotes into &quot; entities which produces invalid JavaScript inside script tags. Using <%== %> outputs raw JSON, which is correct because script blocks do not need HTML escaping. Add minimal feature specs for both partials to catch this class of issue.
1 parent c374050 commit ee9cdc5

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

app/views/alchemy/admin/crop.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
const image = document.getElementById("imageToCrop")?.querySelector("img");
2424

25-
new ImageCropper(image, <%= @settings.merge(
25+
new ImageCropper(image, <%== @settings.merge(
2626
crop_from_form_field_id: params[:crop_from_form_field_id],
2727
crop_size_form_field_id: params[:crop_size_form_field_id],
2828
element_id: @element.id
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<script>
2-
Alchemy.uploader_defaults = <%= Alchemy.config.uploader.to_json %>
2+
Alchemy.uploader_defaults = <%== Alchemy.config.uploader.to_json %>
33
</script>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
RSpec.describe "Image Cropper", type: :system do
6+
before do
7+
authorize_user(:as_admin)
8+
end
9+
10+
let(:element) { create(:alchemy_element, name: "all_you_can_eat") }
11+
let(:picture) { create(:alchemy_picture) }
12+
let(:ingredient) do
13+
create(:alchemy_ingredient_picture, element: element, picture: picture)
14+
end
15+
16+
it "renders image cropper settings as valid JavaScript" do
17+
visit alchemy.crop_admin_ingredient_path(ingredient, picture_id: picture.id)
18+
expect(page).to have_content("new ImageCropper(image, {")
19+
end
20+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
RSpec.describe "Uploader setup", type: :system do
6+
before do
7+
authorize_user(:as_admin)
8+
end
9+
10+
it "renders uploader defaults as valid JavaScript" do
11+
visit admin_dashboard_path
12+
expect(page).to have_content("Alchemy.uploader_defaults = {")
13+
end
14+
end

0 commit comments

Comments
 (0)