Module: ZendeskAppsSupport::Validations::Svg

Defined in:
lib/zendesk_apps_support/validations/svg.rb

Constant Summary collapse

PLACEHOLDER_SVG_MARKUP =
File.read(File.expand_path('../../assets/default_app_logo.svg', __FILE__))

Class Method Summary collapse

Class Method Details

.call(package) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zendesk_apps_support/validations/svg.rb', line 55

def call(package)
  errors = []

  package.svg_files.each do |svg|
    if contains_embedded_bitmap?(svg.read)
      rewrite_svg(svg, PLACEHOLDER_SVG_MARKUP, package, errors)
    else
      markup = Loofah.xml_fragment(svg.read)
                     .scrub!(@strip_declaration)
                     .scrub!(@strip_spaces_between_css_attrs)
                     .to_xml.strip

      clean_markup = Loofah.xml_fragment(markup)
                           .scrub!(:prune)
                           .scrub!(@empty_malformed_markup)
                           .to_xml

      next if clean_markup == markup
      rewrite_svg(svg, clean_markup, package, errors)
    end
  end
  errors
end

.contains_embedded_bitmap?(markup) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/zendesk_apps_support/validations/svg.rb', line 37

def contains_embedded_bitmap?(markup)
  Nokogiri::XML(markup).css('//image').any?
end

.rewrite_svg(svg, new_markup, package, errors) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zendesk_apps_support/validations/svg.rb', line 41

def rewrite_svg(svg, new_markup, package, errors)
  warning_string = if contains_embedded_bitmap?(svg.read)
                     'txt.apps.admin.warning.app_build.bitmap_in_svg'
                   else
                     'txt.apps.admin.warning.app_build.sanitised_svg'
                   end

  package.warnings << I18n.t(warning_string, svg: svg.relative_path)
  compressed_new_markup = new_markup.tr("\n", '').squeeze(' ').gsub(/\>\s+\</, '><')
  IO.write(svg.absolute_path, compressed_new_markup)
rescue
  errors << ValidationError.new(:dirty_svg, svg: svg.relative_path)
end