Class: String

Inherits:
Object show all
Defined in:
lib/crystal/spec/xhtml.rb,
lib/crystal/support/string.rb

Direct Known Subclasses

Crystal::Format

Constant Summary collapse

JS_ESCAPE_MAP =
{
  '\\'    => '\\\\',
  '</'    => '<\/',
  "\r\n"  => '\n',
  "\n"    => '\n',
  "\r"    => '\n',
  '"'     => '\\"',
  "'"     => "\\'" 
}

Instance Method Summary collapse

Instance Method Details

#html_escapeObject



6
7
8
9
# File 'lib/crystal/support/string.rb', line 6

def html_escape
  # ERB::Util.html_escape self
  Rack::Utils.escape_html self
end

#js_escapeObject



20
21
22
# File 'lib/crystal/support/string.rb', line 20

def js_escape
  gsub(/(\\|<\/|\r\n|[\n\r"'])/){JS_ESCAPE_MAP[$1]}
end

#json_escapeObject



2
3
4
# File 'lib/crystal/support/string.rb', line 2

def json_escape
  ERB::Util.json_escape self
end

#marksObject

String marks, like :format, :safe



25
# File 'lib/crystal/support/string.rb', line 25

def marks; @marks ||= OpenObject.new end

#to_xhtml(css = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/crystal/spec/xhtml.rb', line 2

def to_xhtml css = nil    
  node = Nokogiri::HTML(self)
  unless css
    node
  else
    nodes = node.css(css)
    raise "Elements for '#{css}' CSS query not found!" if nodes.size < 1
    raise "Found more than one elment for '#{css}' CSS query!" if nodes.size > 1
    nodes.first
  end
end