Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Removed the check for email template text that would never show
Damian Legawiec041933456825
diff --git a/docs/user/settings/emails.mdx b/docs/user/settings/emails.mdxindex d74bdd472f..6ca79c2490 100644--- a/docs/user/settings/emails.mdx+++ b/docs/user/settings/emails.mdx@@ -69,8 +69,6 @@ Emails sent to your team and to sellers are not editable here.Templates are written in [MJML](https://mjml.io) with Liquid placeholders, like `{{ order.number }}`, which Spree fills in for each email. As you type, the editor suggests the placeholders the email can use, and the preview shows the email filled in with one of your store's real orders. Switch the preview between desktop and mobile, or to the plain-text version that some email apps show. To check a particular case, such as an order with a discount, enter that order's ID in **Preview with record**.-Text must sit inside a text block, `<mj-text>…</mj-text>` within a section and column, or email apps never show it. The editor marks text placed anywhere else on its line, and it cannot be published.-Changes are saved as a **draft**. Customers keep receiving the published version until you click **Publish**, and the editor always says whether you are looking at the draft or the published version. If someone else on your team saved the draft after you opened it, Spree tells you who, so you do not overwrite their work.Click **Send test email** to receive the email at your own address before publishing it.diff --git a/spree/api/spec/controllers/spree/api/v3/admin/email_templates/previews_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/email_templates/previews_controller_spec.rbindex 511f6a4c06..05c8190b88 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/email_templates/previews_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/email_templates/previews_controller_spec.rb@@ -58,13 +58,6 @@ RSpec.describe Spree::Api::V3::Admin::EmailTemplates::PreviewsController, type:endend- it 'marks text the email would not show on its line' do- post :create, params: { email_template_id: id, body: "<mj-section><mj-column><mj-text>Hi</mj-text></mj-column></mj-section>\ntesting preview" }, as: :json-- expect(response).to have_http_status(:unprocessable_content)- expect(json_response['error']['details']['problems'].first).to include('email' => id, 'line' => 2, 'message' => a_string_including('testing preview'))- end-it 'says what is wrong and where when the template does not render' dopost :create, params: { email_template_id: id, body: "<mj-section>\n{% if %}</mj-section>" }, as: :jsondiff --git a/spree/core/app/services/spree/email_templates/invisible_text.rb b/spree/core/app/services/spree/email_templates/invisible_text.rbdeleted file mode 100644index b4fad64445..0000000000--- a/spree/core/app/services/spree/email_templates/invisible_text.rb+++ /dev/null@@ -1,7 +0,0 @@-module Spree- module EmailTemplates- # A template's text sits outside any MJML content component, so the email- # would not show it. A Liquid error, so it is reported like one, with its line.- class InvisibleText < Liquid::Error; end- end-enddiff --git a/spree/core/app/services/spree/email_templates/preview.rb b/spree/core/app/services/spree/email_templates/preview.rbindex 36e9ac7437..a375c2859b 100644--- a/spree/core/app/services/spree/email_templates/preview.rb+++ b/spree/core/app/services/spree/email_templates/preview.rb@@ -39,7 +39,6 @@ module Spree# @return [Spree::Emails::RenderedEmail]# @raise [Spree::EmailTemplates::NoSampleRecord] when the store has no record to build the sample fromdef call- check_visible_text if @strict && [email protected]?sample = email.sample_class.new(store: @store, record_id: @record_id)in_locale do@@ -63,17 +62,6 @@ module Spreeprivate- # Text a merchant types between components would be rendered invisible;- # saying where is kinder than a preview in which nothing changes.- def check_visible_text- hidden = Spree::Emails::InvisibleTextCheck.call(@body, top_level_shown: definition.email?).first- return unless hidden-- error = Spree::EmailTemplates::InvisibleText.new(Spree.t('email_templates.invisible_text', text: hidden[:text]))- error.line_number = hidden[:line]- raise error- end-def definitionSpree.editable_email_templates[@key] || raise(ArgumentError, "#{@key} is not an editable email template")enddiff --git a/spree/core/app/services/spree/emails/invisible_text_check.rb b/spree/core/app/services/spree/emails/invisible_text_check.rbdeleted file mode 100644index c1bca99313..0000000000--- a/spree/core/app/services/spree/emails/invisible_text_check.rb+++ /dev/null@@ -1,65 +0,0 @@-module Spree- module Emails- # Finds text an email would never show: MJML keeps text only inside- # content components such as `<mj-text>` and `<mj-button>`, and renders- # text written straight into a section, column or wrapper in a cell with- # no font size. A template edited in the dashboard is checked, so the- # merchant is told where the text went instead of seeing nothing change.- class InvisibleTextCheck- # Components whose children are laid out, not printed.- CONTAINERS = %w[mj-body mj-wrapper mj-section mj-column mj-group mj-hero mj-accordion mj-accordion-element- mj-carousel mj-navbar mj-social].freeze-- TOKEN = %r{<(/?)(mj-[a-z-]+)\b[^>]*?(/?)>|<[^>]*>|[^<]+}m- # Liquid that prints nothing: tags, and blocks whose content is not output.- SILENT_BLOCKS = /\{%-?\s*(capture|comment|raw)\b.*?\{%-?\s*end\1\s*-?%\}/m- SILENT = /\{%.*?%\}|\{\{.*?\}\}/m-- # @param source [String] a template body- # @param top_level_shown [Boolean] whether text outside any component is- # inside the layout's wrapper (an email's body), rather than wherever a- # partial is rendered- # @return [Array<Hash>] `{ line:, text: }` per piece of text that would not show- def self.call(source, top_level_shown: true)- new(source, top_level_shown).call- end-- def initialize(source, top_level_shown)- @source = blank_out(blank_out(source.to_s, SILENT_BLOCKS), SILENT)- @top_level_shown = top_level_shown- end-- def call- stack = []- problems = []-- @source.to_enum(:scan, TOKEN).each do- match = Regexp.last_match- closing, component, self_closing = match[1], match[2], match[3]-- if component- next if self_closing.present?-- closing.present? ? stack.pop : stack.push(component)- elsif !match[0].start_with?('<') && match[0].strip.present? && hidden?(stack)- problems << { line: @source[0...match.begin(0)].count("\n") + 1 + match[0][/\A\s*/].count("\n"),- text: match[0].strip.truncate(40) }- end- end-- problems- end-- private-- def hidden?(stack)- stack.empty? ? @top_level_shown : CONTAINERS.include?(stack.last)- end-- # Replaces matches with spaces, keeping line breaks so line numbers hold.- def blank_out(text, pattern)- text.gsub(pattern) { |match| match.gsub(/[^\n]/, ' ') }- end- end- end-enddiff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex 6e775a9419..66f013f1d0 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -924,7 +924,6 @@ en:email_marketing: Email marketingemail_templates:another_admin: another admin- invisible_text: '"%{text}" is outside a text component, so the email would not show it. Put it inside <mj-section><mj-column><mj-text>…</mj-text></mj-column></mj-section>.'no_sample_record: There is no %{record} in this store to preview this email with yet.sample_needs_permission: Previewing this email shows your store's records, which needs the %{permissions} permission.stale_draft: "%{name} saved this draft after you opened it. Reload to see their changes before saving yours."diff --git a/spree/core/spec/services/spree/email_templates/preview_spec.rb b/spree/core/spec/services/spree/email_templates/preview_spec.rbindex c8c4d5addf..50054ab789 100644--- a/spree/core/spec/services/spree/email_templates/preview_spec.rb+++ b/spree/core/spec/services/spree/email_templates/preview_spec.rb@@ -23,14 +23,4 @@ describe Spree::EmailTemplates::Preview doexpect(email.html).to include('LAYOUT DRAFT')end-- it 'refuses text that the email would not show, naming its line, when rendering strictly' do- preview = described_class.new(store: store, key: editable_key, strict: true,- body: "<mj-section><mj-column><mj-text>Hi</mj-text></mj-column></mj-section>\ntesting preview")-- expect { preview.call }.to raise_error(Spree::EmailTemplates::InvisibleText) { |error|- expect(error.line_number).to eq(2)- expect(error.message).to include('testing preview')- }- endenddiff --git a/spree/core/spec/services/spree/emails/invisible_text_check_spec.rb b/spree/core/spec/services/spree/emails/invisible_text_check_spec.rbdeleted file mode 100644index c9349d97cb..0000000000--- a/spree/core/spec/services/spree/emails/invisible_text_check_spec.rb+++ /dev/null@@ -1,37 +0,0 @@-require 'spec_helper'--describe Spree::Emails::InvisibleTextCheck do- def check(source, top_level_shown: true)- described_class.call(source, top_level_shown: top_level_shown)- end-- it 'finds text written straight into a section, column or the email body, with its line' do- source = "<mj-section>\n <mj-column>\n lost in a column\n <mj-text>shown</mj-text>\n </mj-column>\n</mj-section>\nlost at the top"-- expect(check(source)).to eq([{ line: 3, text: 'lost in a column' }, { line: 7, text: 'lost at the top' }])- end-- it 'accepts text inside content components, including their HTML' do- source = "<mj-section><mj-column><mj-text><p>Hi <b>{{ name }}</b></p></mj-text>" \- "<mj-button href=\"#\">Go</mj-button><mj-table><tr><td>Row</td></tr></mj-table></mj-column></mj-section>"-- expect(check(source)).to eq([])- end-- it 'ignores Liquid that prints nothing or prints components' do- source = "{% capture heading %}Order {{ order.number }}{% endcapture %}\n{% if x %}{% endif %}\n{{ content_for_layout }}\n" \- "{% render 'spree/shared/order_summary', heading: heading %}"-- expect(check(source)).to eq([])- end-- it 'leaves text at the top of a partial alone, since the partial decides nothing about where it is rendered' do- expect(check('<tr><td>Subtotal</td></tr>', top_level_shown: false)).to eq([])- end-- it 'finds none in the templates Spree ships' do- Dir[Spree::Core::Engine.root.join('app/views/**/*.liquid')].each do |path|- expect(check(File.read(path), top_level_shown: false)).to eq([]), path- end- end-end