Welcome to mirror list, hosted at ThFree Co, Russian Federation.

html_safe_render_shared_context.rb « views « shared_contexts « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3acca60c901e7e0c52507631a680d6ce33f3b064 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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