diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
commit | 6ed4ec3e0b1340f96b7c043ef51d1b33bbe85fde (patch) | |
tree | dc4d20fe6064752c0bd323187252c77e0a89144b /spec/support/shared_contexts/views/html_safe_render_shared_context.rb | |
parent | 9868dae7fc0655bd7ce4a6887d4e6d487690eeed (diff) |
Add latest changes from gitlab-org/gitlab@15-4-stable-eev15.4.0-rc42
Diffstat (limited to 'spec/support/shared_contexts/views/html_safe_render_shared_context.rb')
-rw-r--r-- | spec/support/shared_contexts/views/html_safe_render_shared_context.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/spec/support/shared_contexts/views/html_safe_render_shared_context.rb b/spec/support/shared_contexts/views/html_safe_render_shared_context.rb new file mode 100644 index 00000000000..3acca60c901 --- /dev/null +++ b/spec/support/shared_contexts/views/html_safe_render_shared_context.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +RSpec.shared_context 'when rendered view has no HTML escapes', type: :view do + # Check once per example if `rendered` contains HTML escapes. + let(:rendered) do |example| + super().tap do |rendered| + next if example.metadata[:skip_html_escaped_tags_check] + + ensure_no_html_escaped_tags!(rendered, example) + end + end + + def ensure_no_html_escaped_tags!(content, example) + match_data = HtmlEscapedHelpers.match_html_escaped_tags(content) + return unless match_data + + # Truncate + pre_match = match_data.pre_match.last(50) + match = match_data[0] + post_match = match_data.post_match.first(50) + + string = "#{pre_match}«#{match}»#{post_match}" + + raise <<~MESSAGE + The following string contains HTML escaped tags: + + #{string} + + Please consider using `.html_safe`. + + This check can be disabled via: + + it #{example.description.inspect}, :skip_html_escaped_tags_check do + ... + end + + MESSAGE + end +end |