Class: SpreeCmCommissioner::SeatLayouts::SanitizeSvg

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/seat_layouts/sanitize_svg.rb

Overview

Sanitizes an admin-uploaded background SVG once, before it's saved to the DB — so every downstream consumer (the dashboard builder and the app) can later extract paths from it or render it directly, without each having to sanitize or normalize it again itself.

Strips



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/spree_cm_commissioner/seat_layouts/sanitize_svg.rb', line 23

def call(svg:)
  document = Nokogiri::XML(svg.to_s) { |config| config.strict.noblanks }
  root = document.root

  return failure(nil, 'Not a valid SVG document') if root.nil? || root.name != 'svg'

  strip_disallowed_elements(document)
  strip_event_handlers(document)
  strip_external_references(document)

  width, height, view_box = extract_dimensions(root)
  full_bleed_shape_ids = detect_full_bleed_rects(document, width, height)
  normalize_shapes_to_paths(document)

  success(
    svg: document.to_xml,
    width: width,
    height: height,
    view_box: view_box,
    full_bleed_shape_ids: full_bleed_shape_ids
  )
rescue Nokogiri::XML::SyntaxError => e
  failure(nil, "Invalid SVG: #{e.message}")
end