Class: Roda::RodaPlugins::ErubisEscaping::UnsafeClassEscaper

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/plugins/_erubis_escaping.rb

Overview

Escaper which escapes by default, but does not escape instances of classes marked as safe.

Constant Summary collapse

Escaper =

Default escaper if the string needs to be escaped.

Erubis::XmlHelper

Instance Method Summary collapse

Constructor Details

#initialize(safe_classes) ⇒ UnsafeClassEscaper

Record the classes to consider safe.



18
19
20
21
# File 'lib/roda/plugins/_erubis_escaping.rb', line 18

def initialize(safe_classes)
  @safe_classes = Array(safe_classes).freeze
  freeze
end

Instance Method Details

#escape_xml(string) ⇒ Object

If the string given is not an instance of one of the safe classes, escape it, otherwise return it verbatim. If the given object is not already a string, convert it to a string first.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/roda/plugins/_erubis_escaping.rb', line 26

def escape_xml(string)
  unless string.is_a?(String)
    string = string.to_s
  end

  if @safe_classes.any?{|c| string.is_a?(c)}
    string
  else
    Escaper.escape_xml(string)
  end
end